From b770c2e40d6e34513e0ef63544c69d9591db6d17 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 5 Jan 2020 14:02:37 +0400 Subject: [PATCH] Don't reverse ghosts immediately, wait until they've reached the next tile Also fix index out of bounds in retrieving next scatterChaseTimer value, and retrieving an offset-by-1 value (due to increasing scatterChaseTransition after retrieving the next timer value). Closes #9 --- core/src/com/me/pacman/entity/Ghost.java | 31 ++++++++++++++------- core/src/com/me/pacman/state/PlayState.java | 7 ++--- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/core/src/com/me/pacman/entity/Ghost.java b/core/src/com/me/pacman/entity/Ghost.java index 47b56a1..c63cb70 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -38,6 +38,10 @@ public class Ghost extends MovableEntity { public int pelletCounter; + public boolean reverse; + private int prevTileX; + private int prevTileY; + public Ghost(PlayState state, Vector2 pos, Direction direction, int spriteIndex, Behaviour chaseBehaviour, Behaviour scatterBehaviour, boolean inHouse) { super(state, pos.x, pos.y, Modifiers.getGhostSpeed(0), true, direction, 0.1f); @@ -48,6 +52,7 @@ public class Ghost extends MovableEntity { this.frightBehaviour = new FrightenedBehaviour(state); this.inHouse = inHouse; this.caught = false; + this.reverse = false; sprite = state.getGame().assets.ghosts; } @@ -83,8 +88,11 @@ public class Ghost extends MovableEntity { @Override public void update(float dt) { - LevelTile currentTile = state.level.getTile(pos); + if (age % 15 == 0) { + counter++; + } + LevelTile currentTile = state.level.getTile(pos); if (currentTile == null || currentTile == LevelTile.TUNNEL) { speed = Modifiers.getGhostTunnelSpeed(state.round); } else if (currentBehaviour instanceof FrightenedBehaviour) { @@ -97,11 +105,16 @@ public class Ghost extends MovableEntity { speed = getNormalSpeed(); } - super.update(dt); - - if (age % 15 == 0) { - counter++; + int tileX = (int) pos.x; + int tileY = (int) pos.y; + if (reverse && (tileX != prevTileX || tileY != prevTileY)) { + setNextDirection(currDirection.getOpposite()); + reverse = false; } + prevTileX = tileX; + prevTileY = tileY; + + super.update(dt); if (currentPath != null) { if (!currentPath.finished()) { @@ -123,11 +136,9 @@ public class Ghost extends MovableEntity { return; } - if (currentBehaviour == chaseBehaviour && state.scatter) { - setNextDirection(currDirection.getOpposite()); - updateBehaviour(); - } else if (currentBehaviour == scatterBehaviour && !state.scatter) { - setNextDirection(currDirection.getOpposite()); + if ((currentBehaviour == chaseBehaviour && state.scatter) + || (currentBehaviour == scatterBehaviour && !state.scatter)) { + reverse = true; updateBehaviour(); } diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 083f516..ab5348e 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -277,7 +277,7 @@ public class PlayState extends LevelState { if (ghost.currentBehaviour instanceof ReturnToBase) continue; ghost.caught = false; ghost.currentBehaviour = ghost.frightBehaviour; - ghost.currDirection = ghost.currDirection.getOpposite(); + ghost.reverse = true; } frightTimer = Modifiers.getFrightTime(round); ghostsCaught = 0; @@ -365,13 +365,12 @@ public class PlayState extends LevelState { } private void updateScatterTimer(float dt) { - if (scatterChaseTransition < 7) { - // only update scatter timer if we're currently in scatter or if we've yet to enter scatter 4 times + if (scatterChaseTransition < 6) { scatterChaseTimer -= dt; if (scatterChaseTimer <= 0) { scatter = !scatter; - scatterChaseTimer = Modifiers.getScatterChaseTimer(round, scatterChaseTransition); scatterChaseTransition++; + scatterChaseTimer = Modifiers.getScatterChaseTimer(round, scatterChaseTransition); } } }