diff --git a/Mazebuilder/src/com/mazebuilder/gameplay/Location.java b/Mazebuilder/src/com/mazebuilder/gameplay/Location.java index 73fbd10..8da821d 100644 --- a/Mazebuilder/src/com/mazebuilder/gameplay/Location.java +++ b/Mazebuilder/src/com/mazebuilder/gameplay/Location.java @@ -7,5 +7,11 @@ public interface Location { int getColumn(); Location move(Direction d); + + /** + * Tests if the given location is orthogonally adjacent to this location. + * @return the direction that the other location is with respect to this one, null if not adjacent or is equal + */ + Direction isAdjacent(Location loc); } diff --git a/Mazebuilder/src/com/mazebuilder/gameplay/SimpleLocation.java b/Mazebuilder/src/com/mazebuilder/gameplay/SimpleLocation.java index ef4a10e..0036612 100644 --- a/Mazebuilder/src/com/mazebuilder/gameplay/SimpleLocation.java +++ b/Mazebuilder/src/com/mazebuilder/gameplay/SimpleLocation.java @@ -34,5 +34,52 @@ public Location move(Direction d) { throw new RuntimeException("Unknown direction " + d.toString()); } } + + @Override + public Direction isAdjacent(Location loc) { + if (loc.getRow() == this.getRow()) { + if (loc.getColumn() - 1 == this.getColumn()) { + return Direction.RIGHT; + } else if (loc.getColumn() + 1 == this.getColumn()) { + return Direction.LEFT; + } + } else if (loc.getColumn() == this.getColumn()) { + if (loc.getRow() - 1 == this.getRow()) { + return Direction.DOWN; + } else if (loc.getRow() + 1 == this.getRow()) { + return Direction.UP; + } + } + return null; + } + + @Override + public String toString() { + return "SimpleLocation [row=" + row + ", column=" + column + "]"; + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + column; + result = prime * result + row; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SimpleLocation other = (SimpleLocation) obj; + if (column != other.column) + return false; + if (row != other.row) + return false; + return true; + } } diff --git a/Mazebuilder/src/com/mazebuilder/gameplay/board/Board.java b/Mazebuilder/src/com/mazebuilder/gameplay/board/Board.java index 91f3596..d0390b0 100644 --- a/Mazebuilder/src/com/mazebuilder/gameplay/board/Board.java +++ b/Mazebuilder/src/com/mazebuilder/gameplay/board/Board.java @@ -43,4 +43,15 @@ public interface Board extends Renderable { */ boolean putWall(Location l, Direction d); + /** + * Return the location of the nearest tile to the pixel coordinates given + * @return null if the cursor position is out of bounds + */ + Location getTile(int x, int y); + + /** + * Returns either the wall direction for the location given by getTile, or null if pixel location is not on a wall tile. + */ + Direction getWallDirection(int x, int y); + } diff --git a/Mazebuilder/src/com/mazebuilder/gameplay/board/DefaultBoard.java b/Mazebuilder/src/com/mazebuilder/gameplay/board/DefaultBoard.java index 1e2dede..2d7f58d 100644 --- a/Mazebuilder/src/com/mazebuilder/gameplay/board/DefaultBoard.java +++ b/Mazebuilder/src/com/mazebuilder/gameplay/board/DefaultBoard.java @@ -26,6 +26,29 @@ public DefaultBoard(BoardRenderer renderer, int tilesAcross, int tilesDown) { this.tilesDown = tilesDown; walls = new BitmaskWallContainer(tilesDown, tilesAcross); } + + @Override + public Location getTile(int x, int y) { + x /= renderer.tileWidth() + renderer.wallShortSideLength(); + y /= renderer.tileHeight() + renderer.wallShortSideLength(); + if (x < 0 || y < 0 || x >= tilesAcross || y >= tilesDown) { + return null; + } + return new SimpleLocation(y, x); + } + + @Override + public Direction getWallDirection(int x, int y) { + x %= renderer.tileWidth() + renderer.wallShortSideLength(); + y %= renderer.tileHeight() + renderer.wallShortSideLength(); + if (x > renderer.tileWidth() && x > y) { + return Direction.RIGHT; + } + if (y > renderer.tileHeight() && y > x) { + return Direction.DOWN; + } + return null; + } private void renderTileRow(Graphics g, int x, int y, int row) { renderer.drawTile(g, x, y); diff --git a/Mazebuilder/src/com/mazebuilder/renderer/BoardRenderer.java b/Mazebuilder/src/com/mazebuilder/renderer/BoardRenderer.java index 39e6074..4d7b0c1 100644 --- a/Mazebuilder/src/com/mazebuilder/renderer/BoardRenderer.java +++ b/Mazebuilder/src/com/mazebuilder/renderer/BoardRenderer.java @@ -2,6 +2,8 @@ import org.newdawn.slick.Graphics; +import com.mazebuilder.gameplay.Location; + public interface BoardRenderer { int tileHeight(); diff --git a/Mazebuilder/src/com/mazebuilder/renderer/SimpleBoardRenderer.java b/Mazebuilder/src/com/mazebuilder/renderer/SimpleBoardRenderer.java index e2e52b7..813b804 100644 --- a/Mazebuilder/src/com/mazebuilder/renderer/SimpleBoardRenderer.java +++ b/Mazebuilder/src/com/mazebuilder/renderer/SimpleBoardRenderer.java @@ -3,6 +3,8 @@ import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; +import com.mazebuilder.gameplay.Location; + public final class SimpleBoardRenderer implements BoardRenderer { private static final int TILE_HEIGHT = 64; @@ -55,5 +57,4 @@ public void drawCorner(Graphics g, int x, int y) { g.setColor(Color.green); g.fillRoundRect(x, y, WALL_SHORT_SIDE, WALL_SHORT_SIDE, 8); } - } diff --git a/Mazebuilder/src/com/mazebuilder/root/GameplayState.java b/Mazebuilder/src/com/mazebuilder/root/GameplayState.java index 2a89de7..e35bb6f 100644 --- a/Mazebuilder/src/com/mazebuilder/root/GameplayState.java +++ b/Mazebuilder/src/com/mazebuilder/root/GameplayState.java @@ -7,6 +7,7 @@ import org.newdawn.slick.state.StateBasedGame; import com.mazebuilder.gameplay.Direction; +import com.mazebuilder.gameplay.Location; import com.mazebuilder.gameplay.SimpleLocation; import com.mazebuilder.gameplay.board.Board; import com.mazebuilder.gameplay.board.DefaultBoard; @@ -37,8 +38,8 @@ public class GameplayState extends BasicGameState { //private static final int CHASER_TURN_MILLIS = 5000; private final Board board = new DefaultBoard(new SimpleBoardRenderer(), BOARD_WIDTH, BOARD_HEIGHT); - private final Player runner = new RunnerPlayer(new RunnerPlayerRenderer(), "A"); - private final Player chaser = new ChaserPlayer(new ChaserPlayerRenderer(), "B"); + private final RunnerPlayer runner = new RunnerPlayer(new RunnerPlayerRenderer(), "A"); + private final ChaserPlayer chaser = new ChaserPlayer(new ChaserPlayerRenderer(), "B"); private int chaserTurnTimer; @@ -97,10 +98,31 @@ public void keyPressed(int key, char c) { } } else if (c == ' ') { runner.startTurn(); - } else if (c == '\n' || c == '\r') { - chaser.startTurn(); } - } + } + + @Override + public void mouseClicked(int button, int x, int y, int clickCount) { + if (runner.canMove()) { + Location loc = board.getTile(x-64, y-64); + if (loc != null) { + Direction dir = board.getPlayerLocation(runner).isAdjacent(loc); + if (dir != null) { + board.movePlayer(runner, dir); + } + } + } else if (runner.canWall()) { + Direction dir = board.getWallDirection(x-64, y-64); + Location loc = board.getTile(x-64, y-64); + if (dir != null && loc != null) { + if (board.putWall(loc, dir)) { + if (runner.spendWall() == 0) { + chaser.startTurn(); + } + } + } + } + } private void movePlayer(Player player, Direction dir) { if (board.movePlayer(player, dir)) {