Rudimentary ghost capturing
This commit is contained in:
parent
0bd4d7f042
commit
2a65084363
@ -18,7 +18,11 @@ public abstract class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void render(SpriteBatch batch, int offsetX, int offsetY) {
|
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) {
|
public boolean onSameTile(Entity other) {
|
||||||
|
@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
|||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||||
import com.badlogic.gdx.math.Vector2;
|
import com.badlogic.gdx.math.Vector2;
|
||||||
import com.me.pacman.entity.ai.Behaviour;
|
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.entity.ai.Target;
|
||||||
import com.me.pacman.level.LevelTile;
|
import com.me.pacman.level.LevelTile;
|
||||||
import com.me.pacman.state.PlayState;
|
import com.me.pacman.state.PlayState;
|
||||||
@ -23,6 +24,8 @@ public class Ghost extends MovableEntity {
|
|||||||
public Behaviour frightBehaviour;
|
public Behaviour frightBehaviour;
|
||||||
|
|
||||||
public TextureRegion[][] sprite;
|
public TextureRegion[][] sprite;
|
||||||
|
public boolean caught;
|
||||||
|
public boolean returned;
|
||||||
|
|
||||||
private int spriteIndex;
|
private int spriteIndex;
|
||||||
private int counter = 0;
|
private int counter = 0;
|
||||||
@ -35,19 +38,28 @@ public class Ghost extends MovableEntity {
|
|||||||
this.chaseBehaviour = chaseBehaviour;
|
this.chaseBehaviour = chaseBehaviour;
|
||||||
this.scatterBehaviour = scatterBehaviour;
|
this.scatterBehaviour = scatterBehaviour;
|
||||||
this.frightBehaviour = new FrightenedBehaviour(state);
|
this.frightBehaviour = new FrightenedBehaviour(state);
|
||||||
|
this.caught = false;
|
||||||
|
this.returned = true;
|
||||||
sprite = state.getGame().assets.ghosts;
|
sprite = state.getGame().assets.ghosts;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TextureRegion getSprite() {
|
public TextureRegion getSprite() {
|
||||||
|
if (caught) {
|
||||||
|
return null;
|
||||||
|
} else if (state.frightTimer > 0 && !returned) {
|
||||||
|
return sprite[0][counter % 2];
|
||||||
|
} else {
|
||||||
return sprite[2 + (counter % 2)][spriteIndex];
|
return sprite[2 + (counter % 2)][spriteIndex];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(SpriteBatch batch, int offsetX, int offsetY) {
|
public void render(SpriteBatch batch, int offsetX, int offsetY) {
|
||||||
super.render(batch, offsetX, offsetY);
|
super.render(batch, offsetX, offsetY);
|
||||||
|
|
||||||
// draw eyes so the ghost can see
|
// draw eyes so the ghost can see
|
||||||
|
if (state.frightTimer <= 0 || caught || returned)
|
||||||
batch.draw(sprite[1][currDirection.ordinal()], (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8));
|
batch.draw(sprite[1][currDirection.ordinal()], (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,6 +85,12 @@ public class Ghost extends MovableEntity {
|
|||||||
return;
|
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());
|
Vector2 ahead = new Vector2((int) pos.x, (int) pos.y).add(currDirection.getVector());
|
||||||
float shortest = Float.MAX_VALUE;
|
float shortest = Float.MAX_VALUE;
|
||||||
Direction nextDirection = null;
|
Direction nextDirection = null;
|
||||||
@ -96,21 +114,6 @@ public class Ghost extends MovableEntity {
|
|||||||
setNextDirection(nextDirection);
|
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 class FrightenedBehaviour extends Behaviour {
|
||||||
|
|
||||||
private Target target;
|
private Target target;
|
||||||
|
19
core/src/com/me/pacman/entity/ai/ReturnToBase.java
Normal file
19
core/src/com/me/pacman/entity/ai/ReturnToBase.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -13,6 +13,7 @@ import com.me.pacman.entity.Ghost;
|
|||||||
import com.me.pacman.entity.Pacman;
|
import com.me.pacman.entity.Pacman;
|
||||||
import com.me.pacman.entity.ai.BlinkyChaseBehaviour;
|
import com.me.pacman.entity.ai.BlinkyChaseBehaviour;
|
||||||
import com.me.pacman.entity.ai.BlinkyScatterBehaviour;
|
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.Level;
|
||||||
import com.me.pacman.level.LevelTile;
|
import com.me.pacman.level.LevelTile;
|
||||||
|
|
||||||
@ -38,6 +39,7 @@ public class PlayState extends LevelState {
|
|||||||
private float gameOverTimer;
|
private float gameOverTimer;
|
||||||
private float newGameTimer;
|
private float newGameTimer;
|
||||||
private float deathTimer;
|
private float deathTimer;
|
||||||
|
public float frightTimer;
|
||||||
|
|
||||||
private boolean pacmanCaught;
|
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) {
|
if (!pacmanCaught) {
|
||||||
for (Ghost ghost : ghosts) {
|
for (Ghost ghost : ghosts) {
|
||||||
@ -141,10 +151,14 @@ public class PlayState extends LevelState {
|
|||||||
ghost.update(dt);
|
ghost.update(dt);
|
||||||
|
|
||||||
if (ghost.onSameTile(pacman)) {
|
if (ghost.onSameTile(pacman)) {
|
||||||
|
if (frightTimer > 0) {
|
||||||
|
if (!ghost.caught) ghostCaught(ghost);
|
||||||
|
} else {
|
||||||
pacmanCaught();
|
pacmanCaught();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if (deathTimer <= 0) {
|
} else if (deathTimer <= 0) {
|
||||||
if (pacman.alive) {
|
if (pacman.alive) {
|
||||||
game.assets.deathSound.play(1f);
|
game.assets.deathSound.play(1f);
|
||||||
@ -220,7 +234,7 @@ public class PlayState extends LevelState {
|
|||||||
|
|
||||||
public void placeGhosts() {
|
public void placeGhosts() {
|
||||||
ghosts[0] = new Ghost(this, 14, 19.5f, Direction.LEFT, 0, new BlinkyChaseBehaviour(this), new BlinkyScatterBehaviour(this));
|
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() {
|
public void startGame() {
|
||||||
@ -250,7 +264,12 @@ public class PlayState extends LevelState {
|
|||||||
|
|
||||||
public void eatPowerPellet(float x, float y) {
|
public void eatPowerPellet(float x, float y) {
|
||||||
pelletEaten(x, 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;
|
score += 50;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,6 +280,11 @@ public class PlayState extends LevelState {
|
|||||||
pacman.moving = false;
|
pacman.moving = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ghostCaught(Ghost ghost) {
|
||||||
|
ghost.caught = true;
|
||||||
|
ghost.returned = false;
|
||||||
|
ghost.currentBehaviour = new ReturnToBase(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
|
Loading…
Reference in New Issue
Block a user