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
This commit is contained in:
Matt Low 2020-01-05 14:02:37 +04:00
parent fb3e26da35
commit b770c2e40d
2 changed files with 24 additions and 14 deletions

View File

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

View File

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