fix obstacle drawing, improve collisions

This commit is contained in:
orcook 2018-03-13 17:25:09 -05:00
parent 4d7d8dd4d4
commit 51cdce7f0e
4 changed files with 42 additions and 7 deletions

View File

@ -154,17 +154,17 @@ export class GameComponent implements OnInit {
drawObstacle(obstacle) {
this.context.fillStyle = '#808080'; // obstacles always grey
this.context.fillRect(GameComponent.BOX_SIZE * obstacle.x, GameComponent.BOX_SIZE * obstacle.y,
GameComponent.BOX_SIZE * obstacle.height, GameComponent.BOX_SIZE * obstacle.width);
GameComponent.BOX_SIZE * obstacle.width, GameComponent.BOX_SIZE * obstacle.height);
}
drawMovingObstacle(obstacle) {
this.context.fillStyle = '#808080'; // obstacles always grey
if (obstacle.hasMoved) {
this.context.clearRect(GameComponent.BOX_SIZE * obstacle.oldX, GameComponent.BOX_SIZE * obstacle.oldY,
GameComponent.BOX_SIZE * obstacle.height, GameComponent.BOX_SIZE * obstacle.width);
GameComponent.BOX_SIZE * obstacle.width, GameComponent.BOX_SIZE * obstacle.height);
}
this.context.fillRect(GameComponent.BOX_SIZE * obstacle.x, GameComponent.BOX_SIZE * obstacle.y,
GameComponent.BOX_SIZE * obstacle.height, GameComponent.BOX_SIZE * obstacle.width);
GameComponent.BOX_SIZE * obstacle.width, GameComponent.BOX_SIZE * obstacle.height);
}
writeToScreen(message: string) {

View File

@ -116,6 +116,10 @@ public class GameBoard {
return false;
}
for (MovingObstacle obstacle : movingObstacles) {
obstacle.checkCollision(board);
}
for (MovingObstacle obstacle : movingObstacles) {
obstacle.move(board);
}

View File

@ -68,6 +68,8 @@ public class GameRound implements Runnable {
public GameRound(String id) {
this.id = id;
nextRoundId = getRandomId();
board.addObstacle(new MovingObstacle(10, 5, 60, 60, 0, -1));
board.addObstacle(new MovingObstacle(10, 5, 60, 65, 0, 1));
board.addObstacle(new MovingObstacle(5, 5, GameBoard.BOARD_SIZE / 2, GameBoard.BOARD_SIZE / 3, -1, -1, 1));
board.addObstacle(new MovingObstacle(5, 5, GameBoard.BOARD_SIZE / 2, GameBoard.BOARD_SIZE / 3 * 2, 1, 1));
}

View File

@ -7,6 +7,7 @@ public class MovingObstacle extends Obstacle {
public int xDir, yDir, oldX, oldY, moveDelay, currentDelay = 0;
public boolean hasMoved = false;
private boolean willCollideX, willCollideY = false;
public MovingObstacle(int w, int h, int x, int y) {
super(w, h, x, y);
@ -24,6 +25,34 @@ public class MovingObstacle extends Obstacle {
this.moveDelay = moveDelay;
}
public void checkCollision(short[][] board) {
willCollideX = false;
willCollideY = false;
if (xDir != 0) {
if (xDir > 0) {
if (x + width + 1 >= GameBoard.BOARD_SIZE || hasCollision(board, DIRECTION.RIGHT)) {
willCollideX = true;
}
} else {
if (x - 1 < 0 || hasCollision(board, DIRECTION.LEFT)) {
willCollideX = true;
}
}
}
if (yDir != 0) {
if (yDir > 0) {
if (y + height + 1 >= GameBoard.BOARD_SIZE || hasCollision(board, DIRECTION.DOWN)) {
willCollideY = true;
}
} else {
if (y - 1 < 0 || hasCollision(board, DIRECTION.UP)) {
willCollideY = true;
}
}
}
}
public void move(short[][] board) {
if (++currentDelay < moveDelay) {
@ -40,7 +69,7 @@ public class MovingObstacle extends Obstacle {
if (xDir != 0) {
if (xDir > 0) {
// move right only if we can
if (x + width + 1 < GameBoard.BOARD_SIZE && !hasCollision(board, DIRECTION.RIGHT)) {
if (!willCollideX) {
moveRight(board);
// bounce left if we can't
} else if (!hasCollision(board, DIRECTION.LEFT)) {
@ -48,7 +77,7 @@ public class MovingObstacle extends Obstacle {
xDir = xDir * -1;
}
} else {
if (x - 1 >= 0 && !hasCollision(board, DIRECTION.LEFT)) {
if (!willCollideX) {
moveLeft(board);
} else if (!hasCollision(board, DIRECTION.RIGHT)) {
moveRight(board);
@ -59,14 +88,14 @@ public class MovingObstacle extends Obstacle {
if (yDir != 0) {
if (yDir > 0) {
if (y + height + 1 < GameBoard.BOARD_SIZE && !hasCollision(board, DIRECTION.DOWN)) {
if (!willCollideY) {
moveDown(board);
} else if (!hasCollision(board, DIRECTION.UP)) {
moveUp(board);
yDir = yDir * -1;
}
} else {
if (y - 1 >= 0 && !hasCollision(board, DIRECTION.UP)) {
if (!willCollideY) {
moveUp(board);
} else if (!hasCollision(board, DIRECTION.DOWN)) {
moveDown(board);