Fix bug when two moves occur in a single game tick

This commit is contained in:
Andrew Guibert 2018-02-09 13:54:50 -06:00
parent 6adfb407b0
commit 9ef789acba
2 changed files with 15 additions and 8 deletions

View File

@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>Frontend</title>
<title>Liberty Bikes</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">

View File

@ -31,6 +31,7 @@ public class Player {
public final String color;
public DIRECTION direction = DIRECTION.RIGHT;
private DIRECTION lastDirection = null;
private DIRECTION desiredNextDirection = null;
public int x;
public int y;
public String playerName;
@ -64,14 +65,13 @@ public class Player {
public void setDirection(DIRECTION newDirection) {
// Make sure the player doesn't move backwards on themselves
if (lastDirection != null) {
if (newDirection == DIRECTION.UP && lastDirection == DIRECTION.DOWN)
return;
else if (newDirection == DIRECTION.DOWN && lastDirection == DIRECTION.UP)
return;
else if (newDirection == DIRECTION.LEFT && lastDirection == DIRECTION.RIGHT)
return;
else if (newDirection == DIRECTION.RIGHT && lastDirection == DIRECTION.LEFT)
if ((newDirection == DIRECTION.UP && lastDirection == DIRECTION.DOWN) ||
(newDirection == DIRECTION.DOWN && lastDirection == DIRECTION.UP) ||
(newDirection == DIRECTION.LEFT && lastDirection == DIRECTION.RIGHT) ||
(newDirection == DIRECTION.RIGHT && lastDirection == DIRECTION.LEFT)) {
desiredNextDirection = newDirection;
return;
}
}
direction = newDirection;
@ -90,6 +90,13 @@ public class Player {
// Consume the space the player was in before the move
board[x][y] = false;
// If a player issues two moves in the same game tick and the second direction is illegal,
// spread out the moves across two ticks rather than ignoring the second move entirely
if (desiredNextDirection != null && lastDirection == direction) {
setDirection(desiredNextDirection);
desiredNextDirection = null;
}
switch (direction) {
case UP:
if (y - 1 >= 0)