Add chase/scatter alternation

This commit is contained in:
Matt Low 2019-12-28 16:51:09 +04:00
parent 0d42aac28d
commit dd9bdac33d
2 changed files with 41 additions and 5 deletions

View File

@ -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

View File

@ -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;
}
}