From e38c7a2deaeaf7822b89715df756c2641843bad5 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Fri, 27 Dec 2019 17:58:03 +0400 Subject: [PATCH] Implement PInky, Inky, and Clyde's chase behaviours --- core/src/com/me/pacman/entity/Entity.java | 9 +++++++++ .../entity/ai/BlinkyChaseBehaviour.java | 2 +- .../entity/ai/BlinkyScatterBehaviour.java | 18 ----------------- .../pacman/entity/ai/ClydeChaseBehaviour.java | 18 +++++++++++++++++ .../pacman/entity/ai/InkyChaseBehaviour.java | 20 +++++++++++++++++++ .../pacman/entity/ai/PinkyChaseBehaviour.java | 16 +++++++++++++++ .../entity/ai/StaticTargetBehaviour.java | 18 +++++++++++++++++ core/src/com/me/pacman/state/PlayState.java | 19 +++++++++++++----- 8 files changed, 96 insertions(+), 24 deletions(-) delete mode 100644 core/src/com/me/pacman/entity/ai/BlinkyScatterBehaviour.java create mode 100644 core/src/com/me/pacman/entity/ai/ClydeChaseBehaviour.java create mode 100644 core/src/com/me/pacman/entity/ai/InkyChaseBehaviour.java create mode 100644 core/src/com/me/pacman/entity/ai/PinkyChaseBehaviour.java create mode 100644 core/src/com/me/pacman/entity/ai/StaticTargetBehaviour.java diff --git a/core/src/com/me/pacman/entity/Entity.java b/core/src/com/me/pacman/entity/Entity.java index 5d58a33..dc5e774 100644 --- a/core/src/com/me/pacman/entity/Entity.java +++ b/core/src/com/me/pacman/entity/Entity.java @@ -3,6 +3,7 @@ package com.me.pacman.entity; 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.Target; import com.me.pacman.state.LevelState; public abstract class Entity { @@ -29,6 +30,14 @@ public abstract class Entity { return (int) pos.x == (int) other.pos.x && (int) pos.y == (int) other.pos.y; } + public Vector2 getTileVector() { + return new Vector2((int) pos.x, (int) pos.y); + } + + public Target getAsTarget() { + return new Target(pos.x, pos.y); + } + public abstract TextureRegion getSprite(); public void update(float dt) { diff --git a/core/src/com/me/pacman/entity/ai/BlinkyChaseBehaviour.java b/core/src/com/me/pacman/entity/ai/BlinkyChaseBehaviour.java index f4eab87..d6c01c7 100644 --- a/core/src/com/me/pacman/entity/ai/BlinkyChaseBehaviour.java +++ b/core/src/com/me/pacman/entity/ai/BlinkyChaseBehaviour.java @@ -10,7 +10,7 @@ public class BlinkyChaseBehaviour extends Behaviour { @Override public Target getTarget() { - return new Target(state.pacman.pos); + return state.pacman.getAsTarget(); } } diff --git a/core/src/com/me/pacman/entity/ai/BlinkyScatterBehaviour.java b/core/src/com/me/pacman/entity/ai/BlinkyScatterBehaviour.java deleted file mode 100644 index c6705dd..0000000 --- a/core/src/com/me/pacman/entity/ai/BlinkyScatterBehaviour.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.me.pacman.entity.ai; - -import com.me.pacman.PacDude; -import com.me.pacman.state.PlayState; - -public class BlinkyScatterBehaviour extends Behaviour { - - private static final Target target = new Target((PacDude.LEVEL_WIDTH / 8) - 2, PacDude.LEVEL_HEIGHT / 8); - - public BlinkyScatterBehaviour(PlayState state) { - super(state); - } - - @Override - public Target getTarget() { - return target; - } -} diff --git a/core/src/com/me/pacman/entity/ai/ClydeChaseBehaviour.java b/core/src/com/me/pacman/entity/ai/ClydeChaseBehaviour.java new file mode 100644 index 0000000..e2ee587 --- /dev/null +++ b/core/src/com/me/pacman/entity/ai/ClydeChaseBehaviour.java @@ -0,0 +1,18 @@ +package com.me.pacman.entity.ai; + +import com.badlogic.gdx.math.Vector2; +import com.me.pacman.state.PlayState; + +public class ClydeChaseBehaviour extends Behaviour { + + public ClydeChaseBehaviour(PlayState state) { + super(state); + } + + @Override + public Target getTarget() { + Vector2 pacmanTile = state.pacman.getTileVector(); + Vector2 clydeTile = state.ghosts[2].getTileVector(); + return pacmanTile.dst2(clydeTile) >= 8*8? new Target(pacmanTile) : PlayState.CLYDE_SCATTER_TARGET; + } +} diff --git a/core/src/com/me/pacman/entity/ai/InkyChaseBehaviour.java b/core/src/com/me/pacman/entity/ai/InkyChaseBehaviour.java new file mode 100644 index 0000000..73f75f2 --- /dev/null +++ b/core/src/com/me/pacman/entity/ai/InkyChaseBehaviour.java @@ -0,0 +1,20 @@ +package com.me.pacman.entity.ai; + +import com.badlogic.gdx.math.Vector2; +import com.me.pacman.state.PlayState; + +public class InkyChaseBehaviour extends Behaviour { + + public InkyChaseBehaviour(PlayState state) { + super(state); + } + + @Override + public Target getTarget() { + Vector2 twoAheadPacman = state.pacman.currDirection.getVector(2).add(state.pacman.getTileVector()); + Vector2 blinkyTile = state.ghosts[0].getTileVector(); + float dst = twoAheadPacman.dst(blinkyTile); // distance between Blinky + pacman + Vector2 dir = twoAheadPacman.sub(blinkyTile); // direction from blink to pacman tile + return new Target(blinkyTile.add(dir.scl(dst * 2))); + } +} diff --git a/core/src/com/me/pacman/entity/ai/PinkyChaseBehaviour.java b/core/src/com/me/pacman/entity/ai/PinkyChaseBehaviour.java new file mode 100644 index 0000000..5c82828 --- /dev/null +++ b/core/src/com/me/pacman/entity/ai/PinkyChaseBehaviour.java @@ -0,0 +1,16 @@ +package com.me.pacman.entity.ai; + +import com.me.pacman.state.PlayState; + +public class PinkyChaseBehaviour extends Behaviour { + + public PinkyChaseBehaviour(PlayState state) { + super(state); + } + + @Override + public Target getTarget() { + return new Target(state.pacman.currDirection.getVector(4).add(state.pacman.getTileVector())); + } + +} diff --git a/core/src/com/me/pacman/entity/ai/StaticTargetBehaviour.java b/core/src/com/me/pacman/entity/ai/StaticTargetBehaviour.java new file mode 100644 index 0000000..b45a263 --- /dev/null +++ b/core/src/com/me/pacman/entity/ai/StaticTargetBehaviour.java @@ -0,0 +1,18 @@ +package com.me.pacman.entity.ai; + +import com.me.pacman.state.PlayState; + +public class StaticTargetBehaviour extends Behaviour { + + private final Target target; + + public StaticTargetBehaviour(PlayState state, Target target) { + super(state); + this.target = target; + } + + @Override + public Target getTarget() { + return target; + } +} diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 15e3a30..e828d08 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -11,9 +11,7 @@ import com.me.pacman.PacDude; import com.me.pacman.entity.Direction; 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.entity.ai.*; import com.me.pacman.level.Level; import com.me.pacman.level.LevelTile; @@ -21,6 +19,11 @@ import java.util.Random; public class PlayState extends LevelState { + public static final Target BLINKY_SCATTER_TARGET = new Target((PacDude.LEVEL_WIDTH / 8) - 2, PacDude.LEVEL_HEIGHT / 8); + public static final Target PINKY_SCATTER_TARGET = new Target(2, PacDude.LEVEL_HEIGHT / 8); + public static final Target INKY_SCATTER_TARGET = new Target(PacDude.LEVEL_WIDTH / 8, -1); + public static final Target CLYDE_SCATTER_TARGET = new Target(1, -1); + private Texture levelBackground; private BitmapFont font; @@ -239,8 +242,14 @@ 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].chaseBehaviour; + 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)); + + for (int i = 0; i < 4; i++) { + ghosts[i].currentBehaviour = ghosts[i].chaseBehaviour; + } } public void startGame() {