Fix Obstacle Collision

This commit is contained in:
orcook 2018-03-12 16:29:24 -05:00
parent 5457684753
commit 817cb8ef46
2 changed files with 15 additions and 27 deletions

View File

@ -25,6 +25,7 @@ public class MovingObstacle extends Obstacle {
}
public void move(short[][] board) {
if (++currentDelay < moveDelay) {
// don't move yet
hasMoved = false;
@ -76,35 +77,35 @@ public class MovingObstacle extends Obstacle {
}
private void moveRight(short[][] board) {
for (int i = 0; i <= height; i++) {
for (int i = 0; i < height; i++) {
// clear previous position
board[x][y + i] = GameBoard.SPOT_AVAILABLE;
// take new position
board[x + width + 1][y + i] = GameBoard.OBJECT_SPOT_TAKEN;
board[x + width][y + i] = GameBoard.OBJECT_SPOT_TAKEN;
}
x++;
}
private void moveLeft(short[][] board) {
for (int i = 0; i <= height; i++) {
for (int i = 0; i < height; i++) {
board[x - 1][y + i] = GameBoard.OBJECT_SPOT_TAKEN;
board[x + width][y + i] = GameBoard.SPOT_AVAILABLE;
board[x + width - 1][y + i] = GameBoard.SPOT_AVAILABLE;
}
x--;
}
private void moveUp(short[][] board) {
for (int i = 0; i <= width; i++) {
for (int i = 0; i < width; i++) {
board[x + i][y - 1] = GameBoard.OBJECT_SPOT_TAKEN;
board[x + i][y + height] = GameBoard.SPOT_AVAILABLE;
board[x + i][y + height - 1] = GameBoard.SPOT_AVAILABLE;
}
y--;
}
private void moveDown(short[][] board) {
for (int i = 0; i <= width; i++) {
for (int i = 0; i < width; i++) {
board[x + i][y] = GameBoard.SPOT_AVAILABLE;
board[x + i][y + height + 1] = GameBoard.OBJECT_SPOT_TAKEN;
board[x + i][y + height] = GameBoard.OBJECT_SPOT_TAKEN;
}
y++;
}
@ -114,26 +115,26 @@ public class MovingObstacle extends Obstacle {
private boolean hasCollision(short[][] board, DIRECTION dir) {
switch (dir) {
case UP:
for (int i = 0; i <= width; i++) {
for (int i = 0; i < width; i++) {
if (board[x + i][y - 1] == GameBoard.OBJECT_SPOT_TAKEN)
return true;
}
return false;
case DOWN:
for (int i = 0; i <= width; i++) {
if (board[x + i][y + height + 1] == GameBoard.OBJECT_SPOT_TAKEN)
for (int i = 0; i < width; i++) {
if (board[x + i][y + height] == GameBoard.OBJECT_SPOT_TAKEN)
return true;
}
return false;
case LEFT:
for (int i = 0; i <= height; i++) {
for (int i = 0; i < height; i++) {
if (board[x - 1][y + i] == GameBoard.OBJECT_SPOT_TAKEN)
return true;
}
return false;
default:
for (int i = 0; i <= height; i++) {
if (board[x + width + 1][y + i] == GameBoard.OBJECT_SPOT_TAKEN)
for (int i = 0; i < height; i++) {
if (board[x + width][y + i] == GameBoard.OBJECT_SPOT_TAKEN)
return true;
}
return false;

View File

@ -67,19 +67,6 @@ public class Player {
y = data.y;
}
public String toJson() {
// TODO: Use JSON-B to eliminate the need for this method
// {"color":"#FF0000","coords":{"x":251,"y":89}}
StringBuffer sb = new StringBuffer("{\"color\":\"");
sb.append(this.color);
sb.append("\",\"coords\":{\"x\":");
sb.append(this.x);
sb.append(",\"y\":");
sb.append(this.y);
sb.append("}}");
return sb.toString();
}
public void setDirection(DIRECTION newDirection) {
if (newDirection == direction)
return;