Skip to content

Commit

Permalink
General cleanup
Browse files Browse the repository at this point in the history
Additions:
+ Added "Summon Altar", for now it does not have an implemented use, but it is expected that it will be necessary to be able to summon bosses such as the Keeper or the Eye Queen in the future.

Changes:
* Updated spritesheets
* Reduced calls to Tiles::get() for fluids and other tiles to try to improve performance (memory consumption may increase a little)
* Removed unused OrangeTulip tile class
* Torches now take a few seconds to fully illuminate their area once they are placed
* Items such as Torches, Lanterns, Lava Cubes, etc. now emit light when thrown to the ground

- A very old bug related to the Tnt was corrected, now it can be picked up like any other furniture without it exploding by accident
  • Loading branch information
TheBigEye committed Jan 21, 2024
1 parent dd08267 commit f7c7aef
Show file tree
Hide file tree
Showing 22 changed files with 361 additions and 185 deletions.
18 changes: 16 additions & 2 deletions src/main/java/minicraft/entity/ItemEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import minicraft.graphic.Color;
import minicraft.graphic.Screen;
import minicraft.item.Item;
import minicraft.item.Items;
import minicraft.level.tile.Tiles;

public class ItemEntity extends Entity implements ClientTickable {
Expand Down Expand Up @@ -154,9 +155,9 @@ public void render(Screen screen) {
}

if (Settings.getBoolean("shadows")) {
item.sprite.render(screen, x - 4, y - 4, 4, -1, Color.BLACK); // item shadow uses black color
item.sprite.render(screen, x - 5, y - 8, 4, -1, Color.BLACK); // item shadow uses black color
}
item.sprite.render(screen, x - 4, y - 4 - (int)(zz));
item.sprite.render(screen, x - 5, y - 8 - (int)(zz));
}

@Override
Expand All @@ -180,4 +181,17 @@ protected List<String> getDataPrints() {
prints.add(0, item.toString());
return prints;
}

@Override
public int getLightRadius() {
return (
this.item.equals(Items.get("Torch")) ||
this.item.equals(Items.get("Lava Bucket")) ||
this.item.equals(Items.get("Lantern")) ||
this.item.equals(Items.get("Iron Lantern")) ||
this.item.equals(Items.get("Gold Lantern")) ||
this.item.equals(Items.get("Summon Altar"))
) ? 1 : 0;
}

}
28 changes: 18 additions & 10 deletions src/main/java/minicraft/entity/furniture/Tnt.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
import minicraft.graphic.Screen;
import minicraft.graphic.Sprite;
import minicraft.item.Item;
import minicraft.item.PowerGloveItem;
import minicraft.level.Level;
import minicraft.level.tile.Tile;
import minicraft.level.tile.Tiles;
import minicraft.screen.AchievementsDisplay;

public class Tnt extends Furniture implements ActionListener {
private static int FUSE_TIME = 90;
private static int BLAST_RADIUS = 32;
private static int BLAST_DAMAGE = 75;
private static final int FUSE_TIME = 90;
private static final int BLAST_RADIUS = 32;
private static final int BLAST_DAMAGE = 75;

private int damage = 0;
private int light = 0;
Expand All @@ -35,8 +36,8 @@ public class Tnt extends Furniture implements ActionListener {
private Timer explodeTimer;
private Level levelSave;

private final String[] explosionBlacklist = new String[] {
"hard rock", "obsidian wall", "raw obsidian", "stairs up", "stairs down", "infinite fall"
private static final String[] explosionBlacklist = new String[] {
"hard rock", "obsidian wall", "raw obsidian", "stairs up", "stairs down", "infinite fall", "summon altar"
};

/**
Expand Down Expand Up @@ -150,11 +151,18 @@ public void actionPerformed(ActionEvent e) {

@Override
public boolean interact(Player player, Item heldItem, Direction attackDir) {
if (!fuseLit) {
fuseLit = true;
Sound.genericFuse.playOnLevel(this.x, this.y);
return true;
}
if (heldItem instanceof PowerGloveItem) {
if (!fuseLit) {
return super.interact(player, heldItem, attackDir);
}
return true;
} else {
if (!fuseLit) {
fuseLit = true;
Sound.genericFuse.playOnLevel(this.x, this.y);
return true;
}
}
return false;
}

Expand Down
25 changes: 12 additions & 13 deletions src/main/java/minicraft/entity/mob/AirWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,18 @@ else if (attackTime > 0 && currentPhase == 3) {
if (tickTime % 4 != 0 && currentPhase != 1) {
speed = 1;
}


if (player != null && randomWalkTime == 0) {
int xd = player.x - x; // x dist to player
int yd = player.y - y; // y dist to player
if (random.nextInt(4) == 0 && xd * xd + yd * yd < 50 * 50 && attackDelay == 0 && attackTime == 0) {
if (currentPhase == 1) {
attackDelay = 60 * 4; // ...then set attackDelay to 240 (4 seconds at default 60 ticks/sec)
} else {
attackDelay = 60 * 2; // ...then set attackDelay to 120 (2 seconds at default 60 ticks/sec)
}
}
}

if (player != null && randomWalkTime == 0) {
int xd = player.x - x; // x dist to player
int yd = player.y - y; // y dist to player
if (random.nextInt(4) == 0 && xd * xd + yd * yd < 50 * 50 && attackDelay == 0 && attackTime == 0) {
if (currentPhase == 1) {
attackDelay = 60 * 4; // ...then set attackDelay to 240 (4 seconds at default 60 ticks/sec)
} else {
attackDelay = 60 * 2; // ...then set attackDelay to 120 (2 seconds at default 60 ticks/sec)
}
}
}


}
Expand Down
26 changes: 11 additions & 15 deletions src/main/java/minicraft/entity/mob/Goat.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public void tick() {

// follows to the player if holds wheat
followOnHold(Items.get("Wheat"), 3);

Tile tile = level.getTile(x >> 4, y >> 4);
if (tile == Tiles.get("Grass") || tile == Tiles.get("Sand") || tile == Tiles.get("Lawn") || tile == Tiles.get("Rose") || tile == Tiles.get("Daisy") || tile == Tiles.get("Dandelion") || tile == Tiles.get("Poppy")) {
this.remove();
level.add(new Sheep(), x, y);

}

if (tickTime /16 %24 == 0) {
Tile tile = level.getTile(x >> 4, y >> 4);
if (tile == Tiles.get("Grass") || tile == Tiles.get("Sand") || tile == Tiles.get("Lawn") || tile == Tiles.get("Rose") || tile == Tiles.get("Daisy") || tile == Tiles.get("Dandelion") || tile == Tiles.get("Poppy")) {
this.remove();
level.add(new Sheep(), x, y);

}
}
}

public void die() {
Expand All @@ -37,16 +39,10 @@ public void die() {
if (Settings.get("diff").equals("Peaceful")) {
min = 1;
max = 3;
}
if (Settings.get("diff").equals("Easy")) {
} else if (Settings.get("diff").equals("Easy") || Settings.get("diff").equals("Normal")) {
min = 1;
max = 2;
}
if (Settings.get("diff").equals("Normal")) {
min = 1;
max = 2;
}
if (Settings.get("diff").equals("Hard")) {
} else if (Settings.get("diff").equals("Hard")) {
min = 0;
max = 1;
}
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/minicraft/entity/mob/Sheep.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,25 @@ public void tick() {

// follows to the player if holds wheat
followOnHold(Items.get("Wheat"), 3);

Tile tile = level.getTile(x >> 4, y >> 4);
if (tile == Tiles.get("Snow")) {
remove();
level.add(new Goat(), x, y);
}

// Sheep sounds
if (tickTime /16 %24 == 0 && random.nextInt(8) == 4) {
if (random.nextBoolean()) {
if (!random.nextBoolean()) {
Sound.sheepSay1.playOnLevel(this.x, this.y);
if (tickTime /16 %24 == 0) {
Tile tile = level.getTile(x >> 4, y >> 4);
if (tile == Tiles.get("Snow")) {
remove();
level.add(new Goat(), x, y);
}

// Sheep sounds
if (random.nextInt(8) == 4) {
if (random.nextBoolean()) {
if (!random.nextBoolean()) {
Sound.sheepSay1.playOnLevel(this.x, this.y);
} else {
Sound.sheepSay2.playOnLevel(this.x, this.y);
}
} else {
Sound.sheepSay2.playOnLevel(this.x, this.y);
Sound.sheepSay3.playOnLevel(this.x, this.y);
}
} else {
Sound.sheepSay3.playOnLevel(this.x, this.y);
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/minicraft/graphic/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,42 @@ public void renderLight(int x, int y, int lightRadius) {
}
}
}

public void renderLight(int x, int y, int lightRadius, int intensReduce) {
// Apply the x and y offsets to the light's position (by screen scrolling)
x -= xOffset;
y -= yOffset;

// Calculate the starting and ending positions of the light circle on the x and y axis, ensuring the light does not render outside the screen
int x0 = Math.max(x - lightRadius, 0); // start x
int y0 = Math.max(y - lightRadius, 0); // start y
int x1 = Math.min(x + lightRadius, w); // end x
int y1 = Math.min(y + lightRadius, h); // end y

// Declare the distance from the center of the light circle along the y axis
int yd = 0;

// Loop through each y position
for (int yy = y0; yy < y1; yy++) {
// Calculate distance between current y position and previous y position
yd = yy - y;
yd *= yd;
// Loop through each x position
for (int xx = x0; xx < x1; xx++) {
int xd = xx - x; // Calculate distance between current x position and previous x position
int dist = xd * xd + yd; // Calculate total distance between current position and center of the circle

// If the current position is within the circle of light
if (dist <= lightRadius * lightRadius) {
// Calculate the brightness of the light at the current position
int br = 255 - dist * 255 / (lightRadius * lightRadius);
br -= intensReduce;
// Set the pixel value to the maximum between its current value and the calculated brightness
pixels[xx + yy * w] = Math.max(pixels[xx + yy * w], br);
}
}
}
}

public void setPixel(int xp, int yp, int color) {
// If the pixel is out of bounds, then skip the rest of the loop.
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/minicraft/item/TileItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ protected static ArrayList<Item> getAllInstances() {
items.add(new TileItem("Ice", (new Sprite(15, 21, 0)), "Ice", "Hole", "Water"));
items.add(new TileItem("Brown Mushroom", (new Sprite(25, 0, 0)), "Brown Mushroom", "Mycelium"));
items.add(new TileItem("Red Mushroom", (new Sprite(24, 0, 0)), "Red Mushroom", "Mycelium"));

items.add(new TileItem("Summon Altar", (new Sprite(32, 0, 0)), "Summon Altar", "Grass", "Dirt", "Sand"));

// Creative mode available tiles:
items.add(new TileItem("Natural Rock", (new Sprite(2, 0, 0)), "Rock", "Hole", "Dirt", "Sand", "Path", "Grass", "Snow"));
Expand Down Expand Up @@ -140,15 +142,24 @@ public boolean interactOn(Tile tile, Level level, int xt, int yt, Player player,
}
}
}

for (int i = 0; i < 3; i++) {
int randX = (int) Math.ceil(Math.random() * 12) - 4;
int randY = (int) Math.ceil(Math.random() * 12) - 4;
level.add(new BrightParticle(xt * 16 + randX, yt * 16 + randY));
}
}
}

for (int i = 0; i < 3; i++) {
int randX = (int) Math.ceil(Math.random() * 12) - 4;
int randY = (int) Math.ceil(Math.random() * 12) - 4;
level.add(new BrightParticle(xt * 16 + randX, yt * 16 + randY));
}


} else if (getName() == "Summon Altar") {
for (int y = yt - 1; y <= yt + 1; y++) {
for (int x = xt - 1; x <= xt + 1; x++) {
if (level.getTile(x, y) != Tiles.get("Summon Altar")) {
level.setTile(x, y, Tiles.get("Summon Altar"));
}
}
}

} else {
level.setTile(xt, yt, model); // TODO maybe data should be part of the saved tile..?
//Sound.playerPlace.playOnDisplay();
Expand Down
58 changes: 46 additions & 12 deletions src/main/java/minicraft/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.Predicate;
Expand Down Expand Up @@ -630,6 +631,42 @@ public void renderLight(Screen screen, int xScroll, int yScroll, int brightness)
screen.setOffset(0, 0);
}

public void renderLight(Screen screen, int xScroll, int yScroll, int brightness, int additionalRadius) {
int xo = xScroll >> 4;
int yo = yScroll >> 4;

int w = (Screen.w + 15) >> 4;
int h = (Screen.h + 15) >> 4;

screen.setOffset(xScroll, yScroll);

int r = 8;

List <Entity> entities = getEntitiesInTiles(xo - r, yo - r, w + xo + r, h + yo + r);
for (Entity entity: entities) {
int lightRadius = entity.getLightRadius();
if (lightRadius > 0) {
lightRadius += additionalRadius;
screen.renderLight(entity.x - 1, entity.y - 4, lightRadius * brightness, 51);
}
}

for (int y = yo - r; y <= h + yo + r; y++) {
for (int x = xo - r; x <= w + xo + r; x++) {
if (x < 0 || y < 0 || x >= this.w || y >= this.h) {
continue;
}

int lightRadius = getTile(x, y).getLightRadius(this, x, y);
if (lightRadius > 0) {
lightRadius += additionalRadius;
screen.renderLight((x << 4) + 8, (y << 4) + 8, lightRadius * brightness, 0);
}
}
}
screen.setOffset(0, 0);
}

private void sortAndRender(Screen screen, List <Entity> list) {
list.sort(spriteSorter);
for (Entity entity : list) {
Expand Down Expand Up @@ -1284,18 +1321,15 @@ private void generateVillages() {


private void playRandomMusic(int depth) {
int randomNum = random.nextInt(8);
if (depth == 0) {
Sound[] sounds = {Sound.Theme_Surface, Sound.Theme_Cave, Sound.Theme_Peaceful, Sound.Theme_Peaceful};
sounds[randomNum].playOnDisplay();
} else if (depth == -1) {
Sound[] sounds = {Sound.Ambience1, Sound.Ambience2, Sound.Ambience3, Sound.Ambience4};
sounds[randomNum].playOnDisplay();
} else if (depth == -2) {
Sound[] sounds = {Sound.Theme_Cavern, Sound.Theme_Cavern_drip};
sounds[randomNum].playOnDisplay();
} else if (depth == 1) {
Sound[] sounds = {Sound.Theme_Surface, Sound.Theme_Fall};
Map<Integer, Sound[]> soundMap = new HashMap<>();
soundMap.put(0, new Sound[]{Sound.Theme_Surface, Sound.Theme_Cave, Sound.Theme_Peaceful, Sound.Theme_Peaceful});
soundMap.put(-1, new Sound[]{Sound.Ambience1, Sound.Ambience2, Sound.Ambience3, Sound.Ambience4});
soundMap.put(-2, new Sound[]{Sound.Theme_Cavern, Sound.Theme_Cavern_drip});
soundMap.put(1, new Sound[]{Sound.Theme_Surface, Sound.Theme_Fall});

if (soundMap.containsKey(depth)) {
Sound[] sounds = soundMap.get(depth);
int randomNum = random.nextInt(sounds.length);
sounds[randomNum].playOnDisplay();
}
}
Expand Down
Loading

0 comments on commit f7c7aef

Please sign in to comment.