Skip to content

Commit

Permalink
Player A can now move!
Browse files Browse the repository at this point in the history
  • Loading branch information
dxiao committed Sep 25, 2012
1 parent 4dd2a9d commit de019ce
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
47 changes: 47 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/SimpleLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 11 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
23 changes: 23 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/board/DefaultBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions Mazebuilder/src/com/mazebuilder/renderer/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.newdawn.slick.Graphics;

import com.mazebuilder.gameplay.Location;

public interface BoardRenderer {

int tileHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
32 changes: 27 additions & 5 deletions Mazebuilder/src/com/mazebuilder/root/GameplayState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit de019ce

Please sign in to comment.