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;
private float tolerance;
public Direction nextDirection = null;
private Direction nextDirection = null;
public boolean canMove = true;
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;
}
LevelTile nextTile = null;
if (nextDirection != null) {
LevelTile nextComponent;
boolean turned = false;
switch (nextDirection) {
case NORTH:
nextComponent = state.level.getTile(x, y + 1f);
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
nextTile = state.level.getTile(x, y + 1f);
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;
turned = true;
}
break;
case EAST:
nextComponent = state.level.getTile(x + 1f, y);
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
nextTile = state.level.getTile(x + 1f, y);
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;
turned = true;
}
break;
case SOUTH:
nextComponent = state.level.getTile(x, y - 1f);
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
nextTile = state.level.getTile(x, y - 1f);
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;
turned = true;
}
break;
case WEST:
nextComponent = state.level.getTile(x - 1f, y);
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
nextTile = state.level.getTile(x - 1f, y);
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;
turned = true;
}
@ -69,34 +86,68 @@ public abstract class MovableEntity extends Entity {
}
float dist = speed * dt;
LevelTile currentTile = state.level.getTile(x, y);
nextTile = null;
float new_y = y;
float new_x = x;
switch (currDirection) {
case NORTH:
if (currentTile == null && y > state.level.height + 1) {
new_y = -1f;
canMove = true;
} else {
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;
case EAST:
if (currentTile == null && x > state.level.width + 1) {
new_x = -1f;
canMove = true;
} else {
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;
case SOUTH:
if (currentTile == null && y < 0) {
new_y = state.level.height + 1;
canMove = true;
} else {
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;
case WEST:
if (currentTile == null && x < 0) {
new_x = state.level.width + 1;
canMove = true;
} else {
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;
}
// 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
x = canMove? new_x : ((int) new_x) + 0.5f;
y = canMove? new_y : ((int) new_y) + 0.5f;
}
public void setNextDirection(Direction direction) {
if (direction != currDirection) {
nextDirection = direction;
}
}
public enum Direction {
NORTH,
EAST,

View File

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