Fix (again) and simplify ghost capture logic
Spawn ghosts inside their house Start a new game by pressing 'n'
This commit is contained in:
parent
e38c7a2dea
commit
a8e4d2526e
@ -21,6 +21,10 @@ public class Ghost extends MovableEntity {
|
||||
|
||||
public static final Direction[] GHOST_ORDER = { Direction.UP, Direction.LEFT, Direction.DOWN, Direction.RIGHT };
|
||||
|
||||
private TextureRegion[][] sprite;
|
||||
private int spriteIndex;
|
||||
private int counter = 0;
|
||||
|
||||
private PlayState state;
|
||||
|
||||
public Behaviour currentBehaviour;
|
||||
@ -29,12 +33,7 @@ public class Ghost extends MovableEntity {
|
||||
public Behaviour scatterBehaviour;
|
||||
public Behaviour frightBehaviour;
|
||||
|
||||
public TextureRegion[][] sprite;
|
||||
public boolean caught;
|
||||
public boolean returned;
|
||||
|
||||
private int spriteIndex;
|
||||
private int counter = 0;
|
||||
|
||||
public Ghost(PlayState state, float x, float y, Direction direction, int spriteIndex,
|
||||
Behaviour chaseBehaviour, Behaviour scatterBehaviour) {
|
||||
@ -45,15 +44,14 @@ public class Ghost extends MovableEntity {
|
||||
this.scatterBehaviour = scatterBehaviour;
|
||||
this.frightBehaviour = new FrightenedBehaviour(state);
|
||||
this.caught = false;
|
||||
this.returned = true;
|
||||
sprite = state.getGame().assets.ghosts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextureRegion getSprite() {
|
||||
if (caught && !returned) {
|
||||
if (currentBehaviour instanceof ReturnToBase) {
|
||||
return null;
|
||||
} else if (state.frightTimer > 0 && !returned) {
|
||||
} else if (state.frightTimer > 0 && !caught) {
|
||||
if (state.frightTimer > 2) {
|
||||
// Render the blue scared ghost
|
||||
return sprite[0][counter % 2];
|
||||
@ -71,7 +69,7 @@ public class Ghost extends MovableEntity {
|
||||
super.render(batch, offsetX, offsetY);
|
||||
|
||||
// draw eyes so the ghost can see
|
||||
if (state.frightTimer <= 0 || caught || returned)
|
||||
if (state.frightTimer <= 0 || caught)
|
||||
batch.draw(sprite[1][currDirection.ordinal()], (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8));
|
||||
}
|
||||
|
||||
@ -90,9 +88,9 @@ public class Ghost extends MovableEntity {
|
||||
|
||||
if (currentPath instanceof EnterGhostHousePath) {
|
||||
currentPath = new ExitGhostHousePath(pos);
|
||||
returned = true;
|
||||
} else if (currentPath instanceof ExitGhostHousePath) {
|
||||
currentBehaviour = null;
|
||||
speed = GHOST_SPEED;
|
||||
} else if (currentPath instanceof ExitGhostHousePath) {
|
||||
currentBehaviour = chaseBehaviour;
|
||||
currentPath = null;
|
||||
}
|
||||
@ -106,7 +104,6 @@ public class Ghost extends MovableEntity {
|
||||
|
||||
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
|
||||
if (nextDirection != null) {
|
||||
Vector2 adjacent = nextDirection.getVector().add(pos);
|
||||
@ -115,7 +112,7 @@ public class Ghost extends MovableEntity {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// we didn't so check other directions
|
||||
for (Direction dir : GHOST_ORDER) {
|
||||
if (dir.isOpposite(currDirection) || dir == currDirection) {
|
||||
// don't just turn around or keep going our current direction (since we'll continue being stuck)
|
||||
@ -145,10 +142,11 @@ public class Ghost extends MovableEntity {
|
||||
return;
|
||||
}
|
||||
|
||||
// Rudimentary path finding
|
||||
// Check all possible turns of the tile immediately in front of us and turn the direction that is closest
|
||||
// to our target.
|
||||
Vector2 ahead = new Vector2((int) pos.x, (int) pos.y).add(currDirection.getVector());
|
||||
|
||||
float shortest = Float.MAX_VALUE;
|
||||
|
||||
for (Direction dir : GHOST_ORDER) {
|
||||
if (dir.isOpposite(currDirection)) {
|
||||
continue;
|
||||
@ -164,7 +162,6 @@ public class Ghost extends MovableEntity {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setNextDirection(nextDirection);
|
||||
}
|
||||
|
||||
|
@ -143,9 +143,9 @@ public class PlayState extends LevelState {
|
||||
|
||||
if (frightTimer > 0) {
|
||||
frightTimer -= dt;
|
||||
if (frightTimer < 0) {
|
||||
if (frightTimer <= 0) {
|
||||
for (Ghost ghost : ghosts) {
|
||||
if (ghost == null || (ghost.caught && !ghost.returned)) continue;
|
||||
if (ghost == null || ghost.currentBehaviour instanceof ReturnToBase) continue;
|
||||
ghost.caught = false;
|
||||
ghost.currentBehaviour = ghost.chaseBehaviour;
|
||||
}
|
||||
@ -160,9 +160,9 @@ public class PlayState extends LevelState {
|
||||
ghost.update(dt);
|
||||
|
||||
if (ghost.onSameTile(pacman)) {
|
||||
if (frightTimer > 0 && !ghost.returned) {
|
||||
if (!ghost.caught) ghostCaught(ghost);
|
||||
} else {
|
||||
if (frightTimer > 0 && !ghost.caught) {
|
||||
ghostCaught(ghost);
|
||||
} else if (frightTimer <= 0 || !(ghost.currentBehaviour instanceof ReturnToBase)) {
|
||||
pacmanCaught();
|
||||
return;
|
||||
}
|
||||
@ -235,6 +235,8 @@ public class PlayState extends LevelState {
|
||||
lives--;
|
||||
pacmanCaught = false;
|
||||
|
||||
frightTimer = 0f;
|
||||
|
||||
random = new Random(897198256012865L);
|
||||
|
||||
pacman = new Pacman(this, false);
|
||||
@ -243,9 +245,9 @@ public class PlayState extends LevelState {
|
||||
|
||||
public void placeGhosts() {
|
||||
ghosts[0] = new Ghost(this, 14, 19.5f, Direction.LEFT, 0, new BlinkyChaseBehaviour(this), new StaticTargetBehaviour(this, BLINKY_SCATTER_TARGET));
|
||||
ghosts[1] = new Ghost(this, 14, 19.5f, Direction.LEFT, 1, new PinkyChaseBehaviour(this), new StaticTargetBehaviour(this, PINKY_SCATTER_TARGET));
|
||||
ghosts[2] = new Ghost(this, 14, 19.5f, Direction.RIGHT, 2, new InkyChaseBehaviour(this), new StaticTargetBehaviour(this, INKY_SCATTER_TARGET));
|
||||
ghosts[3] = new Ghost(this, 14, 19.5f, Direction.RIGHT, 3, new ClydeChaseBehaviour(this), new StaticTargetBehaviour(this, CLYDE_SCATTER_TARGET));
|
||||
ghosts[1] = new Ghost(this, 14f, 16.5f, Direction.DOWN, 1, new PinkyChaseBehaviour(this), new StaticTargetBehaviour(this, PINKY_SCATTER_TARGET));
|
||||
ghosts[2] = new Ghost(this, 12f, 16.5f, Direction.UP, 2, new InkyChaseBehaviour(this), new StaticTargetBehaviour(this, INKY_SCATTER_TARGET));
|
||||
ghosts[3] = new Ghost(this, 16f, 16.5f, Direction.UP, 3, new ClydeChaseBehaviour(this), new StaticTargetBehaviour(this, CLYDE_SCATTER_TARGET));
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
ghosts[i].currentBehaviour = ghosts[i].chaseBehaviour;
|
||||
@ -280,9 +282,9 @@ public class PlayState extends LevelState {
|
||||
public void eatPowerPellet(float x, float y) {
|
||||
pelletEaten(x, y);
|
||||
for (Ghost ghost : ghosts) {
|
||||
if (ghost == null || ghost.caught) continue;
|
||||
if (ghost == null || ghost.currentBehaviour instanceof ReturnToBase) continue;
|
||||
ghost.caught = false;
|
||||
ghost.currentBehaviour = ghost.frightBehaviour;
|
||||
ghost.returned = false;
|
||||
ghost.currDirection = ghost.currDirection.getOpposite();
|
||||
}
|
||||
frightTimer = 6f;
|
||||
@ -298,7 +300,6 @@ public class PlayState extends LevelState {
|
||||
|
||||
private void ghostCaught(Ghost ghost) {
|
||||
ghost.caught = true;
|
||||
ghost.returned = false;
|
||||
ghost.currentBehaviour = new ReturnToBase(this);
|
||||
ghost.speed = Ghost.EYES_SPEED;
|
||||
}
|
||||
@ -328,6 +329,9 @@ public class PlayState extends LevelState {
|
||||
case Input.Keys.P:
|
||||
paused = !paused;
|
||||
break;
|
||||
case Input.Keys.N:
|
||||
newGame();
|
||||
break;
|
||||
}
|
||||
return super.keyDown(keycode);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user