Skip to content

Commit

Permalink
Added SimpleLocation. Finished board rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi committed Sep 23, 2012
1 parent a2b3b37 commit 733e1e2
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 49 deletions.
87 changes: 70 additions & 17 deletions Mazebuilder/src/com/mazebuilder/gameplay/DefaultBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import com.mazebuilder.renderer.BoardRenderer;

public final class DefaultBoard implements Board {

private final BoardRenderer renderer;
private final int tilesAcross, tilesDown;
private final Map<Player, Location> players = Maps.newIdentityHashMap();
private final WallContainer walls;

public DefaultBoard(BoardRenderer renderer, int tilesAcross, int tilesDown) {
this.renderer = renderer;
this.tilesAcross = tilesAcross;
Expand All @@ -24,56 +24,109 @@ public DefaultBoard(BoardRenderer renderer, int tilesAcross, int tilesDown) {

@Override
public void render(Graphics g, int xOffset, int yOffset) {
throw new RuntimeException("Not implemented.");
int x, y;
x = xOffset;
y = yOffset;

renderer.drawTile(g, x, y);
x += renderer.tileWidth();
for (int j = 1; j < tilesAcross; j++) {
if (walls.isWall(new SimpleLocation(0, j), Direction.LEFT)) {
renderer.drawWall(g, x, y, false);
} else {
renderer.drawNoWall(g, x, y, false);
}
x += renderer.wallShortSideLength();
renderer.drawTile(g, x, y);
x += renderer.tileWidth();
}
y += renderer.tileHeight();
x = xOffset;

for (int i = 1; i < tilesDown; i++) {
if (walls.isWall(new SimpleLocation(i - 1, 0), Direction.DOWN)) {
renderer.drawWall(g, x, y, true);
} else {
renderer.drawNoWall(g, x, y, true);
}
x += renderer.tileWidth();
for (int j = 1; j < tilesAcross; j++) {
renderer.drawCorner(g, x, y);
x += renderer.wallShortSideLength();
if (walls.isWall(new SimpleLocation(i - 1, j), Direction.DOWN)) {
renderer.drawWall(g, x, y, true);
} else {
renderer.drawNoWall(g, x, y, true);
}
x += renderer.tileWidth();
}
y += renderer.wallShortSideLength();
x = xOffset;

renderer.drawTile(g, x, y);
x += renderer.tileWidth();
for (int j = 1; j < tilesAcross; j++) {
if (walls.isWall(new SimpleLocation(i, j), Direction.LEFT)) {
renderer.drawWall(g, x, y, false);
} else {
renderer.drawNoWall(g, x, y, false);
}
x += renderer.wallShortSideLength();
renderer.drawTile(g, x, y);
x += renderer.tileWidth();
}
y += renderer.tileHeight();
x = xOffset;
}
}

@Override
public int tilesAcross() {
return tilesAcross;
}

@Override
public int tilesDown() {
return tilesDown;
}

@Override
public Location getPlayerLocation(Player p) {
return Preconditions.checkNotNull(players.get(p), "Player not found.");
return Preconditions.checkNotNull(players.get(p), "Player not found.");
}

@Override
public Location movePlayer(Player p, Direction d) {
Location l = getPlayerLocation(p);
if(!walls.isWall(l, d)) {
if (!walls.isWall(l, d)) {
Location moved = l.move(d);
if(isBoardLocation(moved)) {
if (isBoardLocation(moved)) {
l = moved;
players.put(p, l);
}
}
return l;
}

@Override
public Location jumpPlayer(Player p, Direction d, Direction... bonusesToSpend) {
throw new RuntimeException("Not implemented.");
}

@Override
public boolean putWall(Location l, Direction d) {
boolean alreadySet = walls.isWall(l, d);
if(!alreadySet) {
if (!alreadySet) {
walls.putWall(l, d);
}
return !alreadySet;
}

private boolean isBoardLocation(Location l) {
int r,c;
int r, c;
r = l.getRow();
c = l.getColumn();
return (r>=0 && c>=0 && r<tilesDown && c<tilesAcross);
return (r >= 0 && c >= 0 && r < tilesDown && c < tilesAcross);
}

}
38 changes: 38 additions & 0 deletions Mazebuilder/src/com/mazebuilder/gameplay/SimpleLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mazebuilder.gameplay;

public final class SimpleLocation implements Location {

private final int row, column;

public SimpleLocation(int row, int column) {
this.row = row;
this.column = column;
}

@Override
public int getRow() {
return row;
}

@Override
public int getColumn() {
return column;
}

@Override
public Location move(Direction d) {
switch (d) {
case DOWN:
return new SimpleLocation(row + 1, column);
case UP:
return new SimpleLocation(row - 1, column);
case LEFT:
return new SimpleLocation(row, column - 1);
case RIGHT:
return new SimpleLocation(row, column + 1);
default:
throw new RuntimeException("Unknown direction " + d.toString());
}
}

}
61 changes: 29 additions & 32 deletions Mazebuilder/src/com/mazebuilder/renderer/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@
import org.newdawn.slick.Graphics;

public interface BoardRenderer {

int tileHeight();

int tileWidth();

int wallHeight();

int wallWidth();

/**
* Draw the sprite for a tile.
*/
void drawTile(Graphics g, int x, int y);

/**

int tileHeight();

int tileWidth();

int wallShortSideLength();

/**
* Draw the sprite for a tile.
*/
void drawTile(Graphics g, int x, int y);

/**
* Draw the sprite for a wall.
*
* If horizontal is set to true, then width and height are specified by tileWidth() and wallHeight().
* Otherwise, width and height are specified by wallWidth() and tileHeight().
* If horizontal is set to true, then width and height are specified by tileWidth() and wallHeight(). Otherwise, width and height are specified by
* wallWidth() and tileHeight().
*/
void drawWall(Graphics g, int x, int y, boolean horizontal);

/**
* Draw the sprite for an empty (potential) wall location.
*
* If horizontal is set to true, then width and height are specified by tileWidth() and wallHeight(). Otherwise, width and height are specified by
* wallWidth() and tileHeight().
*/
void drawNoWall(Graphics g, int x, int y, boolean horizontal);

/**
* Draw the sprite for a corner (the intersection of walls). Width and height are specified by wallWidth() and wallHeight(), respectively.
*/
void drawWall(Graphics g, int x, int y, boolean horizontal);

/**
* Draw the sprite for an empty (potential) wall location.
*
* If horizontal is set to true, then width and height are specified by tileWidth() and wallHeight().
* Otherwise, width and height are specified by wallWidth() and tileHeight().
*/
void drawNoWall(Graphics g, int x, int y, boolean horizontal);

/**
* Draw the sprite for a corner (the intersection of walls).
* Width and height are specified by wallWidth() and wallHeight(), respectively.
*/
void drawCorner(Graphics g, int x, int y);
void drawCorner(Graphics g, int x, int y);
}

0 comments on commit 733e1e2

Please sign in to comment.