From 0501917756a4d965bcbd23d86dcbfa061f4fa7f2 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 29 Dec 2019 00:27:13 +0400 Subject: [PATCH] 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 --- core/src/com/me/pacman/entity/Ghost.java | 31 ++++++++++++++++----- core/src/com/me/pacman/entity/Pacman.java | 8 +++++- core/src/com/me/pacman/state/PlayState.java | 5 ++-- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/core/src/com/me/pacman/entity/Ghost.java b/core/src/com/me/pacman/entity/Ghost.java index 158b881..3c97947 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -15,8 +15,8 @@ import java.util.ArrayList; public class Ghost extends MovableEntity { - public static final float GHOST_SPEED = 7.03f; - public static final float GHOST_TUNNEL_SPEED = GHOST_SPEED / 2; + public static final float GHOST_SPEED = PlayState.FULL_SPEED * 0.75f; + public static final float GHOST_TUNNEL_SPEED = PlayState.FULL_SPEED * 0.4f; public static final float EYES_SPEED = 15f; public static final Direction[] GHOST_ORDER = { Direction.UP, Direction.LEFT, Direction.DOWN, Direction.RIGHT }; @@ -79,6 +79,18 @@ public class Ghost extends MovableEntity { @Override 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); if (age % 15 == 0) { @@ -92,14 +104,11 @@ public class Ghost extends MovableEntity { if (currentPath instanceof EnterGhostHousePath) { inHouse = true; - } else if (currentPath instanceof ExitGhostHousePath) { - speed = GHOST_SPEED; } currentPath = null; } if (inHouse) { - speed = GHOST_TUNNEL_SPEED; if (pos.y >= 17) { currDirection = Direction.DOWN; } else if (pos.y <= 16) { @@ -112,7 +121,7 @@ public class Ghost extends MovableEntity { if ((currentBehaviour == chaseBehaviour && !state.chase) || (currentBehaviour == scatterBehaviour && state.chase)) { setNextDirection(currDirection.getOpposite()); } - currentBehaviour = state.chase? chaseBehaviour : scatterBehaviour; + updateBehaviour(); } Direction nextDirection = getNextDirection(); @@ -182,7 +191,15 @@ public class Ghost extends MovableEntity { public void leaveHouse() { currentPath = new ExitGhostHousePath(pos); 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 { diff --git a/core/src/com/me/pacman/entity/Pacman.java b/core/src/com/me/pacman/entity/Pacman.java index 889ac57..d537458 100644 --- a/core/src/com/me/pacman/entity/Pacman.java +++ b/core/src/com/me/pacman/entity/Pacman.java @@ -18,7 +18,7 @@ public class Pacman extends MovableEntity { public int deathFrame = 0; 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; sprite = state.getGame().assets.pacman; death = state.getGame().assets.deathAnimation; @@ -44,6 +44,12 @@ public class Pacman extends MovableEntity { return; } + if (state.frightTimer > 0) { + speed = PlayState.FULL_SPEED * 0.9f; + } else { + speed = PlayState.FULL_SPEED * 0.8f; + } + super.update(dt); if (canMove && age % 4 == 0) { diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 6af6dad..fe6cbf4 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -18,6 +18,8 @@ import java.util.Random; public class PlayState extends LevelState { + public static final float FULL_SPEED = 9.375f; + public static final Vector2[] GHOST_SPAWN_POINTS = { new Vector2(14f, 19.5f), new Vector2(14f, 16.5f), @@ -177,7 +179,6 @@ public class PlayState extends LevelState { if (ghost.currentBehaviour instanceof ReturnToBase) continue; ghost.caught = false; ghost.currentBehaviour = ghost.chaseBehaviour; - ghost.speed = Ghost.GHOST_SPEED; } } } else { @@ -378,7 +379,6 @@ public class PlayState extends LevelState { ghost.caught = false; ghost.currentBehaviour = ghost.frightBehaviour; ghost.currDirection = ghost.currDirection.getOpposite(); - ghost.speed = Ghost.GHOST_TUNNEL_SPEED; } frightTimer = 6f; ghostsCaught = 0; @@ -397,7 +397,6 @@ public class PlayState extends LevelState { private void ghostCaught(Ghost ghost) { ghost.caught = true; ghost.currentBehaviour = new ReturnToBase(this); - ghost.speed = Ghost.EYES_SPEED; lastGhostCaptured = ghost; ghostsCaught++;