-
-
Save chrislo27/fd8eee43e917f5bc44b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Level { | |
// your other stuff here | |
public Tile getTile(int x, int y) { | |
if (x < 0 || y < 0 || x >= world_width || y >= world_height) return null; | |
return tiles[x][y]; | |
} | |
public void setTile(int x, int y, Tile tile) { | |
if (x < 0 || y < 0 || x >= world_width || y >= world_height) return; | |
tiles[x][y] = tile; | |
} | |
} | |
public class Tile { | |
// ... stuff here | |
public void onUpdate(Level level, int x, int y) { | |
// nothing here, base tile does nothing | |
} | |
} | |
public abstract class LampTile extends Tile { | |
@Override | |
public void onUpdate(Level level, int x, int y) { | |
if (this instanceof LampTileOff) { | |
// this is an off lamp, check for any power blocks | |
// I actually ignore having an out of bounds check since getTile/setTile should have one | |
if (level.getTile(x - 1, y) instanceof PowerTile || | |
level.getTile(x + 1, y) instanceof PowerTile || | |
level.getTile(x, y - 1) instanceof PowerTile || | |
level.getTile(x, y + 1) instanceof PowerTile) { | |
level.setTile(x, y, Tiles.lampTileOn); | |
} | |
} else if (this instanceof LampTileOn) { | |
// this is an on lamp, none of the tiles should be power tiles | |
if (!(level.getTile(x - 1, y) instanceof PowerTile) && | |
!(level.getTile(x + 1, y) instanceof PowerTile) && | |
!(level.getTile(x, y - 1) instanceof PowerTile) && | |
!(level.getTile(x, y + 1) instanceof PowerTile)) { | |
level.setTile(x, y, Tiles.lampTileOff); | |
} | |
} | |
} | |
} | |
public class LampTileOn extends LampTile { | |
// nothing needed since LampTile's onUpdate handles things for us | |
} | |
public class LampTileOff extends LampTile { | |
// same deal here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment