From dd9bdac33d658036c572d3cbbb76c9665ce01b9f Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 28 Dec 2019 16:51:09 +0400 Subject: [PATCH] Add chase/scatter alternation --- core/src/com/me/pacman/entity/Ghost.java | 11 ++++--- core/src/com/me/pacman/state/PlayState.java | 35 ++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/core/src/com/me/pacman/entity/Ghost.java b/core/src/com/me/pacman/entity/Ghost.java index dfc1ae5..d82d927 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -105,14 +105,17 @@ public class Ghost extends MovableEntity { } else if (pos.y <= 16) { currDirection = Direction.UP; } - } - - if (currentBehaviour == null) { return; } - Direction nextDirection = getNextDirection(); + if (!(currentBehaviour instanceof FrightenedBehaviour || currentBehaviour instanceof ReturnToBase)) { + if ((currentBehaviour == chaseBehaviour && !state.chase) || (currentBehaviour == scatterBehaviour && state.chase)) { + setNextDirection(currDirection.getOpposite()); + } + currentBehaviour = state.chase? chaseBehaviour : scatterBehaviour; + } + Direction nextDirection = getNextDirection(); if (!canMove) { // we're stuck somewhere, let's change directions to a new valid direction // check if we already have a nextDirection which will get us unstuck diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 3f22d3e..7cbd9c3 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -47,6 +47,12 @@ public class PlayState extends LevelState { private float pointsTimer; public float frightTimer; + private float chaseTimer; + private float scatterTimer; + private int scatterCount; + + public boolean chase; + private boolean pacmanCaught; private int ghostsCaught; private Ghost lastGhostCaptured; @@ -161,6 +167,24 @@ public class PlayState extends LevelState { ghost.speed = Ghost.GHOST_SPEED; } } + } else { + // pause chase/scatter timer when frightened + if (scatterTimer > 0) { + scatterTimer -= dt; + if (scatterTimer <= 0) { + chase = true; + if (scatterCount < 4) { + chaseTimer = 20f; + } + } + } else if (chaseTimer > 0) { + chaseTimer -= dt; + if (chaseTimer <= 0) { + chase = false; + scatterTimer = 7f; + scatterCount++; + } + } } if (!pacmanCaught) { @@ -208,6 +232,12 @@ public class PlayState extends LevelState { ghostsCaught = 0; lastGhostCaptured = null; + if (chase) { + chaseTimer = 20f; + } else { + scatterTimer = 7f; + } + random = new Random(897198256012865L); ghosts = new Ghost[4]; pacman = new Pacman(this, false); @@ -237,6 +267,9 @@ public class PlayState extends LevelState { spawnGhosts(); } + chase = false; + scatterCount = 1; + game.assets.siren.stop(sirenId); if (round == 1) { readyTimer = 4.2f; @@ -260,7 +293,7 @@ public class PlayState extends LevelState { ghosts[3] = new Ghost(this, 16f, 16.5f, Direction.UP, 3, new ClydeChaseBehaviour(this), new StaticTargetBehaviour(this, CLYDE_SCATTER_TARGET), true); for (int i = 0; i < 4; i++) { - ghosts[i].currentBehaviour = ghosts[i].chaseBehaviour; + ghosts[i].currentBehaviour = ghosts[i].scatterBehaviour; } }