Implement PInky, Inky, and Clyde's chase behaviours

This commit is contained in:
Matt Low 2019-12-27 17:58:03 +04:00
parent 933f696fe8
commit e38c7a2dea
8 changed files with 96 additions and 24 deletions

View File

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

View File

@ -10,7 +10,7 @@ public class BlinkyChaseBehaviour extends Behaviour {
@Override
public Target getTarget() {
return new Target(state.pacman.pos);
return state.pacman.getAsTarget();
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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() {