Set speed based on current state before each update. Fixes #2

Ghosts now slow down in side tunnels
All speeds now ratios of PlayState.FULL_SPEED
This commit is contained in:
Matt Low 2019-12-29 00:27:13 +04:00
parent 20719621e2
commit 0501917756
3 changed files with 33 additions and 11 deletions

View File

@ -15,8 +15,8 @@ import java.util.ArrayList;
public class Ghost extends MovableEntity { public class Ghost extends MovableEntity {
public static final float GHOST_SPEED = 7.03f; public static final float GHOST_SPEED = PlayState.FULL_SPEED * 0.75f;
public static final float GHOST_TUNNEL_SPEED = GHOST_SPEED / 2; public static final float GHOST_TUNNEL_SPEED = PlayState.FULL_SPEED * 0.4f;
public static final float EYES_SPEED = 15f; public static final float EYES_SPEED = 15f;
public static final Direction[] GHOST_ORDER = { Direction.UP, Direction.LEFT, Direction.DOWN, Direction.RIGHT }; public static final Direction[] GHOST_ORDER = { Direction.UP, Direction.LEFT, Direction.DOWN, Direction.RIGHT };
@ -79,6 +79,18 @@ public class Ghost extends MovableEntity {
@Override @Override
public void update(float dt) { public void update(float dt) {
LevelTile currentTile = state.level.getTile(pos);
if (currentBehaviour instanceof FrightenedBehaviour) {
speed = PlayState.FULL_SPEED * 0.5f;
} else if (currentBehaviour instanceof ReturnToBase) {
speed = EYES_SPEED;
} else if (currentTile == LevelTile.TUNNEL || inHouse) {
speed = GHOST_TUNNEL_SPEED;
} else {
speed = GHOST_SPEED;
}
super.update(dt); super.update(dt);
if (age % 15 == 0) { if (age % 15 == 0) {
@ -92,14 +104,11 @@ public class Ghost extends MovableEntity {
if (currentPath instanceof EnterGhostHousePath) { if (currentPath instanceof EnterGhostHousePath) {
inHouse = true; inHouse = true;
} else if (currentPath instanceof ExitGhostHousePath) {
speed = GHOST_SPEED;
} }
currentPath = null; currentPath = null;
} }
if (inHouse) { if (inHouse) {
speed = GHOST_TUNNEL_SPEED;
if (pos.y >= 17) { if (pos.y >= 17) {
currDirection = Direction.DOWN; currDirection = Direction.DOWN;
} else if (pos.y <= 16) { } else if (pos.y <= 16) {
@ -112,7 +121,7 @@ public class Ghost extends MovableEntity {
if ((currentBehaviour == chaseBehaviour && !state.chase) || (currentBehaviour == scatterBehaviour && state.chase)) { if ((currentBehaviour == chaseBehaviour && !state.chase) || (currentBehaviour == scatterBehaviour && state.chase)) {
setNextDirection(currDirection.getOpposite()); setNextDirection(currDirection.getOpposite());
} }
currentBehaviour = state.chase? chaseBehaviour : scatterBehaviour; updateBehaviour();
} }
Direction nextDirection = getNextDirection(); Direction nextDirection = getNextDirection();
@ -182,7 +191,15 @@ public class Ghost extends MovableEntity {
public void leaveHouse() { public void leaveHouse() {
currentPath = new ExitGhostHousePath(pos); currentPath = new ExitGhostHousePath(pos);
inHouse = false; inHouse = false;
currentBehaviour = state.chase? chaseBehaviour : scatterBehaviour; updateBehaviour();
}
public void updateBehaviour() {
if (state.frightTimer > 0 && !caught) {
currentBehaviour = frightBehaviour;
} else {
currentBehaviour = state.chase ? chaseBehaviour : scatterBehaviour;
}
} }
private class FrightenedBehaviour extends Behaviour { private class FrightenedBehaviour extends Behaviour {

View File

@ -18,7 +18,7 @@ public class Pacman extends MovableEntity {
public int deathFrame = 0; public int deathFrame = 0;
public Pacman(PlayState state, boolean moving) { public Pacman(PlayState state, boolean moving) {
super(state, 14, 7.5f, 7.5f, moving, Direction.LEFT, 0.3f); super(state, 14, 7.5f, PlayState.FULL_SPEED * 0.8f, moving, Direction.LEFT, 0.3f);
this.state = state; this.state = state;
sprite = state.getGame().assets.pacman; sprite = state.getGame().assets.pacman;
death = state.getGame().assets.deathAnimation; death = state.getGame().assets.deathAnimation;
@ -44,6 +44,12 @@ public class Pacman extends MovableEntity {
return; return;
} }
if (state.frightTimer > 0) {
speed = PlayState.FULL_SPEED * 0.9f;
} else {
speed = PlayState.FULL_SPEED * 0.8f;
}
super.update(dt); super.update(dt);
if (canMove && age % 4 == 0) { if (canMove && age % 4 == 0) {

View File

@ -18,6 +18,8 @@ import java.util.Random;
public class PlayState extends LevelState { public class PlayState extends LevelState {
public static final float FULL_SPEED = 9.375f;
public static final Vector2[] GHOST_SPAWN_POINTS = { public static final Vector2[] GHOST_SPAWN_POINTS = {
new Vector2(14f, 19.5f), new Vector2(14f, 19.5f),
new Vector2(14f, 16.5f), new Vector2(14f, 16.5f),
@ -177,7 +179,6 @@ public class PlayState extends LevelState {
if (ghost.currentBehaviour instanceof ReturnToBase) continue; if (ghost.currentBehaviour instanceof ReturnToBase) continue;
ghost.caught = false; ghost.caught = false;
ghost.currentBehaviour = ghost.chaseBehaviour; ghost.currentBehaviour = ghost.chaseBehaviour;
ghost.speed = Ghost.GHOST_SPEED;
} }
} }
} else { } else {
@ -378,7 +379,6 @@ public class PlayState extends LevelState {
ghost.caught = false; ghost.caught = false;
ghost.currentBehaviour = ghost.frightBehaviour; ghost.currentBehaviour = ghost.frightBehaviour;
ghost.currDirection = ghost.currDirection.getOpposite(); ghost.currDirection = ghost.currDirection.getOpposite();
ghost.speed = Ghost.GHOST_TUNNEL_SPEED;
} }
frightTimer = 6f; frightTimer = 6f;
ghostsCaught = 0; ghostsCaught = 0;
@ -397,7 +397,6 @@ public class PlayState extends LevelState {
private void ghostCaught(Ghost ghost) { private void ghostCaught(Ghost ghost) {
ghost.caught = true; ghost.caught = true;
ghost.currentBehaviour = new ReturnToBase(this); ghost.currentBehaviour = new ReturnToBase(this);
ghost.speed = Ghost.EYES_SPEED;
lastGhostCaptured = ghost; lastGhostCaptured = ghost;
ghostsCaught++; ghostsCaught++;