Skip to content

Commit

Permalink
Move torch variations into tile data
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCheung0422 committed Aug 9, 2023
1 parent c4688ad commit a097072
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/client/java/minicraft/item/TileItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected TileItem(String name, LinkedSprite sprite, int count, String model, Li
public boolean interactOn(Tile tile, Level level, int xt, int yt, Player player, Direction attackDir) {
for (String tilename : validTiles) {
if (tile.matches(level.getData(xt, yt), tilename)) {
level.setTile(xt, yt, model); // TODO maybe data should be part of the saved tile..?
level.setTile(xt, yt, Tiles.get(model)); // TODO maybe data should be part of the saved tile..?
AdvancementElement.AdvancementTrigger.PlacedTileTrigger.INSTANCE.trigger(
new AdvancementElement.AdvancementTrigger.PlacedTileTrigger.PlacedTileTriggerConditionHandler.PlacedTileTriggerConditions(
this, level.getTile(xt, yt), level.getData(xt, yt), xt, yt, level.depth
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/item/TorchItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private TorchItem(int count) {

public boolean interactOn(Tile tile, Level level, int xt, int yt, Player player, Direction attackDir) {
if (validTiles.contains(tile.name)) {
level.setTile(xt, yt, TorchTile.getTorchTile(tile));
level.setTile(xt, yt, TorchTile.DELEGATE, tile.id);
return super.interactOn(true);
}
return super.interactOn(false);
Expand Down
10 changes: 7 additions & 3 deletions src/client/java/minicraft/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,15 @@ private void sortAndRender(Screen screen, List<Entity> list) {

public Tile getTile(int x, int y) {
if (x < 0 || y < 0 || x >= w || y >= h /* || (x + y * w) >= tiles.length*/ ) return Tiles.get("connector tile");
int id = tiles[x + y * w];
if(id < 0) id += 256;
return Tiles.get(id);
short id = tiles[x + y * w];
Tile tile;
return (tile = Tiles.get(id)) == TorchTile.DELEGATE ? TorchTile.getTorchTile(Tiles.get((short) getData(x, y))) : tile;
}

/**
* @deprecated Currently unused, but this should be prevented being used.
*/
@Deprecated
public void setTile(int x, int y, String tilewithdata) {
if (!tilewithdata.contains("_")) {
setTile(x, y, Tiles.get(tilewithdata));
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/level/tile/DoorTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
if (tool.type == type.getRequiredTool()) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
int data = level.getData(xt, yt);
level.setTile(xt, yt, Tiles.get(id + 3)); // Will get the corresponding floor tile.
level.setTile(xt, yt, Tiles.get((short) (id + 3))); // Will get the corresponding floor tile.
Sound.play("monsterhurt");
level.dropItem(xt * 16 + 8, yt * 16 + 8, Items.get(type.name() + " Door"));
AdvancementElement.AdvancementTrigger.ItemUsedOnTileTrigger.INSTANCE.trigger(
Expand Down
10 changes: 10 additions & 0 deletions src/client/java/minicraft/level/tile/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public boolean onExplode(Level level, int xt, int yt) {
/** Sees if the tile connects to a fluid. */
public boolean connectsToLiquid() { return connectsToFluid; }

/**
* @deprecated This should be planned to be removed as this method is not ideally used.
* The current only usage is in {@link Level#setTile(int, int, String)}.
*/
@Deprecated
public int getData(String data) {
try {
return Integer.parseInt(data);
Expand All @@ -146,6 +151,11 @@ public int getData(String data) {
}
}

/**
* @deprecated Similar to {@link #getData(String)}. Also, param {@code thisData} is unused.
* The current only usage is in {@link minicraft.item.TileItem#interactOn(Tile, Level, int, int, Player, Direction)}.
*/
@Deprecated
public boolean matches(int thisData, String tileInfo) {
return name.equals(tileInfo.split("_")[0]);
}
Expand Down
25 changes: 8 additions & 17 deletions src/client/java/minicraft/level/tile/Tiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static void initTileList() {
tiles.put((short)47, new BossWallTile());
tiles.put((short)48, new BossFloorTile());
tiles.put((short)49, new BossDoorTile());
tiles.put((short)50, TorchTile.DELEGATE);

// WARNING: don't use this tile for anything!
tiles.put((short)255, new ConnectTile());
Expand Down Expand Up @@ -199,7 +200,7 @@ public static Tile get(String name) {
Tile getting = null;

boolean isTorch = false;
if(name.startsWith("TORCH")) {
if(name.startsWith("TORCH") && name.length() > 5) {
isTorch = true;
name = name.substring(6); // Cuts off torch prefix.
}
Expand All @@ -221,34 +222,24 @@ public static Tile get(String name) {
getting = tiles.get((short)0);
}

if(isTorch) {
if (isTorch) {
getting = TorchTile.getTorchTile(getting);
}

overflowCheck = 0;
return getting;
}

public static Tile get(int id) {
public static Tile get(short id) {
//System.out.println("Requesting tile by id: " + id);
if(id < 0) id += 32768;

if(tiles.get((short)id) != null) {
return tiles.get((short)id);
}
else if(id >= 32767) {
return TorchTile.getTorchTile(get(id - 32767));
}
else {
if (tiles.get(id) != null) {
return tiles.get(id);
} else {
Logging.TILES.info("Unknown tile id requested: " + id);
return tiles.get((short)0);
return tiles.get((short) 0);
}
}

public static boolean containsTile(int id) {
return tiles.get((short)id) != null;
}

public static String getName(String descriptName) {
if(!descriptName.contains("_")) return descriptName;
int data;
Expand Down
28 changes: 15 additions & 13 deletions src/client/java/minicraft/level/tile/TorchTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@
import minicraft.util.AdvancementElement;
import org.tinylog.Logger;

import java.util.HashMap;

public class TorchTile extends Tile {
private Tile onType;
public static final TorchTile DELEGATE = new TorchTile(new ConnectTile()); // ConnectTile is used for placeholder.

private static final HashMap<Integer, TorchTile> instances = new HashMap<>();

private final Tile onType;

/** @param onTile The tile identified by tile data. */
public static TorchTile getTorchTile(Tile onTile) {
if (onTile instanceof TorchTile) return (TorchTile) onTile;
int id = onTile.id & 0xFFFF;
if(id < 16384) id += 16384;
else Logger.tag("TorchTile").info("Tried to place torch on torch tile...");

if(Tiles.containsTile(id))
return (TorchTile)Tiles.get(id);
else {
TorchTile tile = new TorchTile(onTile);
Tiles.add(id, tile);
return tile;
}
if (instances.containsKey(id)) return instances.get(id);
TorchTile tile = new TorchTile(onTile);
instances.put(id, tile);
return tile;
}

private TorchTile(Tile onType) {
super("Torch "+ onType.name, new SpriteAnimation(SpriteType.Tile, "torch"));
super("Torch", new SpriteAnimation(SpriteType.Tile, "torch"));
this.onType = onType;
this.connectsToSand = onType.connectsToSand;
this.connectsToGrass = onType.connectsToGrass;
Expand All @@ -50,7 +52,7 @@ public int getLightRadius(Level level, int x, int y) {
public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
if(item instanceof PowerGloveItem) {
int data = level.getData(xt, yt);
level.setTile(xt, yt, this.onType);
level.setTile(xt, yt, onType);
Sound.play("monsterhurt");
level.dropItem(xt*16+8, yt*16+8, Items.get("Torch"));
AdvancementElement.AdvancementTrigger.ItemUsedOnTileTrigger.INSTANCE.trigger(
Expand Down

0 comments on commit a097072

Please sign in to comment.