diff --git a/core/src/com/me/pacman/entity/Entity.java b/core/src/com/me/pacman/entity/Entity.java index 4a7d1d5..5d58a33 100644 --- a/core/src/com/me/pacman/entity/Entity.java +++ b/core/src/com/me/pacman/entity/Entity.java @@ -18,7 +18,11 @@ public abstract class Entity { } public void render(SpriteBatch batch, int offsetX, int offsetY) { - batch.draw(getSprite(), (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8)); + TextureRegion texture = getSprite(); + if (texture == null) { + return; + } + batch.draw(texture, (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8)); } public boolean onSameTile(Entity other) { diff --git a/core/src/com/me/pacman/entity/Ghost.java b/core/src/com/me/pacman/entity/Ghost.java index e7963ea..e1f9b17 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Vector2; import com.me.pacman.entity.ai.Behaviour; +import com.me.pacman.entity.ai.ReturnToBase; import com.me.pacman.entity.ai.Target; import com.me.pacman.level.LevelTile; import com.me.pacman.state.PlayState; @@ -23,6 +24,8 @@ public class Ghost extends MovableEntity { public Behaviour frightBehaviour; public TextureRegion[][] sprite; + public boolean caught; + public boolean returned; private int spriteIndex; private int counter = 0; @@ -35,12 +38,20 @@ public class Ghost extends MovableEntity { this.chaseBehaviour = chaseBehaviour; this.scatterBehaviour = scatterBehaviour; this.frightBehaviour = new FrightenedBehaviour(state); + this.caught = false; + this.returned = true; sprite = state.getGame().assets.ghosts; } @Override public TextureRegion getSprite() { - return sprite[2 + (counter % 2)][spriteIndex]; + if (caught) { + return null; + } else if (state.frightTimer > 0 && !returned) { + return sprite[0][counter % 2]; + } else { + return sprite[2 + (counter % 2)][spriteIndex]; + } } @Override @@ -48,7 +59,8 @@ public class Ghost extends MovableEntity { super.render(batch, offsetX, offsetY); // draw eyes so the ghost can see - batch.draw(sprite[1][currDirection.ordinal()], (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8)); + if (state.frightTimer <= 0 || caught || returned) + batch.draw(sprite[1][currDirection.ordinal()], (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8)); } @Override @@ -73,6 +85,12 @@ public class Ghost extends MovableEntity { return; } + if (currentBehaviour instanceof ReturnToBase && target.targetReached(pos)) { + caught = false; + returned = true; + currentBehaviour = chaseBehaviour; + } + Vector2 ahead = new Vector2((int) pos.x, (int) pos.y).add(currDirection.getVector()); float shortest = Float.MAX_VALUE; Direction nextDirection = null; @@ -96,21 +114,6 @@ public class Ghost extends MovableEntity { setNextDirection(nextDirection); } - private class ReturnToBase extends Behaviour { - - final Target home = new Target(14, 16); - - public ReturnToBase(PlayState state) { - super(state); - } - - @Override - public Target getTarget() { - return home; - } - - } - private class FrightenedBehaviour extends Behaviour { private Target target; diff --git a/core/src/com/me/pacman/entity/ai/ReturnToBase.java b/core/src/com/me/pacman/entity/ai/ReturnToBase.java new file mode 100644 index 0000000..e2e1a6d --- /dev/null +++ b/core/src/com/me/pacman/entity/ai/ReturnToBase.java @@ -0,0 +1,19 @@ +package com.me.pacman.entity.ai; + +import com.me.pacman.state.PlayState; + +public class ReturnToBase extends Behaviour { + +// final Target home = new Target(14, 16); + final Target home = new Target(14, 19); + + public ReturnToBase(PlayState state) { + super(state); + } + + @Override + public Target getTarget() { + return home; + } + +} diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 3f77564..1b7ac45 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -13,6 +13,7 @@ import com.me.pacman.entity.Ghost; import com.me.pacman.entity.Pacman; import com.me.pacman.entity.ai.BlinkyChaseBehaviour; import com.me.pacman.entity.ai.BlinkyScatterBehaviour; +import com.me.pacman.entity.ai.ReturnToBase; import com.me.pacman.level.Level; import com.me.pacman.level.LevelTile; @@ -38,6 +39,7 @@ public class PlayState extends LevelState { private float gameOverTimer; private float newGameTimer; private float deathTimer; + public float frightTimer; private boolean pacmanCaught; @@ -131,7 +133,15 @@ public class PlayState extends LevelState { } } - dt = 1/60f; + if (frightTimer > 0) { + frightTimer -= dt; + if (frightTimer < 0) { + for (Ghost ghost : ghosts) { + if (ghost == null || ghost.caught) continue; + ghost.currentBehaviour = ghost.chaseBehaviour; + } + } + } if (!pacmanCaught) { for (Ghost ghost : ghosts) { @@ -141,8 +151,12 @@ public class PlayState extends LevelState { ghost.update(dt); if (ghost.onSameTile(pacman)) { - pacmanCaught(); - return; + if (frightTimer > 0) { + if (!ghost.caught) ghostCaught(ghost); + } else { + pacmanCaught(); + return; + } } } } else if (deathTimer <= 0) { @@ -220,7 +234,7 @@ public class PlayState extends LevelState { public void placeGhosts() { ghosts[0] = new Ghost(this, 14, 19.5f, Direction.LEFT, 0, new BlinkyChaseBehaviour(this), new BlinkyScatterBehaviour(this)); - ghosts[0].currentBehaviour = ghosts[0].scatterBehaviour; + ghosts[0].currentBehaviour = ghosts[0].chaseBehaviour; } public void startGame() { @@ -250,7 +264,12 @@ public class PlayState extends LevelState { public void eatPowerPellet(float x, float y) { pelletEaten(x, y); - // TODO: Enable ghost chase mode + for (Ghost ghost : ghosts) { + if (ghost == null || ghost.caught) continue; + ghost.currentBehaviour = ghost.frightBehaviour; + ghost.returned = false; + } + frightTimer = 6f; score += 50; } @@ -261,6 +280,11 @@ public class PlayState extends LevelState { pacman.moving = false; } + private void ghostCaught(Ghost ghost) { + ghost.caught = true; + ghost.returned = false; + ghost.currentBehaviour = new ReturnToBase(this); + } @Override public void dispose() {