Now the tunnel works

Also don't allow setting next direction to the current direction.
This commit is contained in:
Matt Low 2019-12-25 19:15:38 +04:00
parent f5ba40255a
commit f43840979b
2 changed files with 74 additions and 23 deletions

View File

@ -11,7 +11,7 @@ public abstract class MovableEntity extends Entity {
public Direction currDirection; public Direction currDirection;
private float tolerance; private float tolerance;
public Direction nextDirection = null; private Direction nextDirection = null;
public boolean canMove = true; public boolean canMove = true;
public MovableEntity(LevelState state, float x, float y, float speed, boolean moving, Direction currDirection, float turnTolerance) { public MovableEntity(LevelState state, float x, float y, float speed, boolean moving, Direction currDirection, float turnTolerance) {
@ -29,34 +29,51 @@ public abstract class MovableEntity extends Entity {
return; return;
} }
LevelTile nextTile = null;
if (nextDirection != null) { if (nextDirection != null) {
LevelTile nextComponent;
boolean turned = false; boolean turned = false;
switch (nextDirection) { switch (nextDirection) {
case NORTH: case NORTH:
nextComponent = state.level.getTile(x, y + 1f); nextTile = state.level.getTile(x, y + 1f);
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) { if (nextTile == null) {
if (currDirection == Direction.SOUTH) {
turned = true;
}
} else if (nextTile.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
x = ((int) x) + 0.5f; x = ((int) x) + 0.5f;
turned = true; turned = true;
} }
break; break;
case EAST: case EAST:
nextComponent = state.level.getTile(x + 1f, y); nextTile = state.level.getTile(x + 1f, y);
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) { if (nextTile == null) {
if (currDirection == Direction.WEST) {
turned = true;
}
} else if (nextTile.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
y = ((int) y) + 0.5f; y = ((int) y) + 0.5f;
turned = true; turned = true;
} }
break; break;
case SOUTH: case SOUTH:
nextComponent = state.level.getTile(x, y - 1f); nextTile = state.level.getTile(x, y - 1f);
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) { if (nextTile == null) {
if (currDirection == Direction.NORTH) {
turned = true;
}
} else if (nextTile.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
x = ((int) x) + 0.5f; x = ((int) x) + 0.5f;
turned = true; turned = true;
} }
break; break;
case WEST: case WEST:
nextComponent = state.level.getTile(x - 1f, y); nextTile = state.level.getTile(x - 1f, y);
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) { if (nextTile == null) {
if (currDirection == Direction.EAST) {
turned = true;
}
} else if (nextTile.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
y = ((int) y) + 0.5f; y = ((int) y) + 0.5f;
turned = true; turned = true;
} }
@ -69,34 +86,68 @@ public abstract class MovableEntity extends Entity {
} }
float dist = speed * dt; float dist = speed * dt;
LevelTile currentTile = state.level.getTile(x, y);
nextTile = null;
float new_y = y; float new_y = y;
float new_x = x; float new_x = x;
switch (currDirection) { switch (currDirection) {
case NORTH: case NORTH:
if (currentTile == null && y > state.level.height + 1) {
new_y = -1f;
canMove = true;
} else {
new_y += dist; new_y += dist;
canMove = state.level.getTile(new_x, new_y + 0.5f).isPassable(); nextTile = state.level.getTile(new_x, new_y + 0.5f);
canMove = nextTile == null || nextTile.isPassable();
}
break; break;
case EAST: case EAST:
if (currentTile == null && x > state.level.width + 1) {
new_x = -1f;
canMove = true;
} else {
new_x += dist; new_x += dist;
canMove = state.level.getTile(new_x + 0.5f, new_y).isPassable(); nextTile = state.level.getTile(new_x + 0.5f, new_y);
canMove = nextTile == null || nextTile.isPassable();
}
break; break;
case SOUTH: case SOUTH:
if (currentTile == null && y < 0) {
new_y = state.level.height + 1;
canMove = true;
} else {
new_y -= dist; new_y -= dist;
canMove = state.level.getTile(new_x, new_y - 0.5f).isPassable(); nextTile = state.level.getTile(new_x, new_y - 0.5f);
canMove = nextTile == null || nextTile.isPassable();
}
break; break;
case WEST: case WEST:
if (currentTile == null && x < 0) {
new_x = state.level.width + 1;
canMove = true;
} else {
new_x -= dist; new_x -= dist;
canMove = state.level.getTile(new_x - 0.5f, new_y).isPassable(); nextTile = state.level.getTile(new_x - 0.5f, new_y);
canMove = nextTile == null || nextTile.isPassable();
}
break; break;
} }
// if move wasn't going to collide with wall, move normally. // if move isn't going to collide with wall, move normally.
// otherwise, trim would-be decimal and center entity on tile // otherwise, trim would-be decimal and center entity on tile
x = canMove? new_x : ((int) new_x) + 0.5f; x = canMove? new_x : ((int) new_x) + 0.5f;
y = canMove? new_y : ((int) new_y) + 0.5f; y = canMove? new_y : ((int) new_y) + 0.5f;
} }
public void setNextDirection(Direction direction) {
if (direction != currDirection) {
nextDirection = direction;
}
}
public enum Direction { public enum Direction {
NORTH, NORTH,
EAST, EAST,

View File

@ -157,16 +157,16 @@ public class PlayState extends LevelState {
public boolean keyDown(int keycode) { public boolean keyDown(int keycode) {
switch (keycode) { switch (keycode) {
case Input.Keys.UP: case Input.Keys.UP:
pacman.nextDirection = MovableEntity.Direction.NORTH; pacman.setNextDirection(MovableEntity.Direction.NORTH);
break; break;
case Input.Keys.DOWN: case Input.Keys.DOWN:
pacman.nextDirection = MovableEntity.Direction.SOUTH; pacman.setNextDirection(MovableEntity.Direction.SOUTH);
break; break;
case Input.Keys.LEFT: case Input.Keys.LEFT:
pacman.nextDirection = MovableEntity.Direction.WEST; pacman.setNextDirection(MovableEntity.Direction.WEST);
break; break;
case Input.Keys.RIGHT: case Input.Keys.RIGHT:
pacman.nextDirection = MovableEntity.Direction.EAST; pacman.setNextDirection(MovableEntity.Direction.EAST);
break; break;
case Input.Keys.P: case Input.Keys.P:
paused = !paused; paused = !paused;