Skip to content

Instantly share code, notes, and snippets.

@chrislo27
Created December 29, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislo27/fd8eee43e917f5bc44b7 to your computer and use it in GitHub Desktop.
Save chrislo27/fd8eee43e917f5bc44b7 to your computer and use it in GitHub Desktop.
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