Significant amount of refactoring:
- Removed several mode timer variables and implemented a GameMode enum w/ associated timer. - Removed individual chase/scatter timer, replace with single timer - Hopefully made level/round initializing methods more coherent
This commit is contained in:
parent
ecaf86a57a
commit
389533d394
@ -117,10 +117,11 @@ public class Ghost extends MovableEntity {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(currentBehaviour instanceof FrightenedBehaviour || currentBehaviour instanceof ReturnToBase)) {
|
||||
if ((currentBehaviour == chaseBehaviour && !state.chase) || (currentBehaviour == scatterBehaviour && state.chase)) {
|
||||
setNextDirection(currDirection.getOpposite());
|
||||
}
|
||||
if (currentBehaviour == chaseBehaviour && state.scatter) {
|
||||
setNextDirection(currDirection.getOpposite());
|
||||
updateBehaviour();
|
||||
} else if (currentBehaviour == scatterBehaviour && !state.scatter) {
|
||||
setNextDirection(currDirection.getOpposite());
|
||||
updateBehaviour();
|
||||
}
|
||||
|
||||
@ -198,7 +199,7 @@ public class Ghost extends MovableEntity {
|
||||
if (state.frightTimer > 0 && !caught) {
|
||||
currentBehaviour = frightBehaviour;
|
||||
} else {
|
||||
currentBehaviour = state.chase ? chaseBehaviour : scatterBehaviour;
|
||||
currentBehaviour = state.scatter ? scatterBehaviour : chaseBehaviour;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,16 +35,15 @@ public class PlayState extends LevelState {
|
||||
};
|
||||
|
||||
private Texture levelBackground, winBackground;
|
||||
private BitmapFont font;
|
||||
|
||||
private TextureRegion lifeSprite;
|
||||
private BitmapFont font;
|
||||
|
||||
public Random random;
|
||||
|
||||
private long sirenId;
|
||||
|
||||
private int pelletCount;
|
||||
private int pelletEatenCount;
|
||||
private int pelletsEaten;
|
||||
|
||||
public int pelletsEatenSinceDeath;
|
||||
public boolean pelletsEatenSinceDeathCounterEnabled;
|
||||
@ -54,24 +53,17 @@ public class PlayState extends LevelState {
|
||||
private int round;
|
||||
private boolean paused = false;
|
||||
|
||||
private float readyTimer;
|
||||
private float gameOverTimer;
|
||||
private float newGameTimer;
|
||||
private float deathTimer;
|
||||
private float pointsTimer;
|
||||
private float roundClearedTimer;
|
||||
private float modeTimer;
|
||||
private GameState mode;
|
||||
|
||||
public float frightTimer;
|
||||
public float secondsSinceLastDot;
|
||||
|
||||
private float chaseTimer;
|
||||
private float scatterTimer;
|
||||
public boolean scatter;
|
||||
private int scatterCount;
|
||||
private float scatterChaseTimer;
|
||||
|
||||
public boolean chase;
|
||||
|
||||
private boolean pacmanCaught;
|
||||
private int ghostsCaught;
|
||||
public float frightTimer; // remaining fright time
|
||||
private int ghostsCaught; // number of ghosts caught since last power pellet
|
||||
private Ghost lastGhostCaptured;
|
||||
|
||||
public Pacman pacman;
|
||||
@ -89,7 +81,8 @@ public class PlayState extends LevelState {
|
||||
lifeSprite = game.assets.pacman[2][1];
|
||||
Gdx.input.setInputProcessor(new Controller());
|
||||
|
||||
newGame();
|
||||
preNewGame();
|
||||
setGameState(GameState.PRE_NEW_GAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -102,42 +95,52 @@ public class PlayState extends LevelState {
|
||||
game.batch.draw(lifeSprite, i * 16, 0);
|
||||
}
|
||||
|
||||
if (pointsTimer > 0) {
|
||||
if (mode == GameState.GHOST_CAUGHT_POINTS_WAIT) {
|
||||
game.batch.draw(game.assets.points[0][ghostsCaught-1], (lastGhostCaptured.pos.x * 8) - 8, (lastGhostCaptured.pos.y * 8) + 8);
|
||||
} else {
|
||||
pacman.render(game.batch, 0, 16);
|
||||
}
|
||||
|
||||
if (roundClearedTimer > 0 && roundClearedTimer <= 2) {
|
||||
if (mode == GameState.ROUND_WON) {
|
||||
// draw flashing level background
|
||||
game.batch.draw((int) (roundClearedTimer * 4) % 2 == 0? levelBackground : winBackground, 0, 16);
|
||||
game.batch.draw((int) (modeTimer * 4) % 2 == 0? levelBackground : winBackground, 0, 16);
|
||||
return;
|
||||
} else {
|
||||
game.batch.draw(levelBackground, 0, 16);
|
||||
}
|
||||
|
||||
if (pacman.alive && newGameTimer <= 0) {
|
||||
for (Ghost ghost : ghosts) {
|
||||
if (pointsTimer > 0 && ghost == lastGhostCaptured) {
|
||||
continue;
|
||||
switch (mode) {
|
||||
case PRE_NEW_GAME:
|
||||
case PACMAN_CAUGHT:
|
||||
case ROUND_WON:
|
||||
case GAME_OVER:
|
||||
break;
|
||||
default:
|
||||
for (Ghost ghost : ghosts) {
|
||||
if (mode == GameState.GHOST_CAUGHT_POINTS_WAIT && ghost == lastGhostCaptured) {
|
||||
game.batch.draw(game.assets.points[0][ghostsCaught - 1], (ghost.pos.x * 8) - 8, (ghost.pos.y * 8) + 8);
|
||||
} else {
|
||||
ghost.render(game.batch, 0, 16);
|
||||
}
|
||||
}
|
||||
ghost.render(game.batch, 0, 16);
|
||||
}
|
||||
}
|
||||
|
||||
if (readyTimer > 0) {
|
||||
game.assets.getFont().setColor(Color.YELLOW);
|
||||
game.assets.getFont().draw(game.batch, "ready!", 92, 127);
|
||||
}
|
||||
|
||||
if (gameOverTimer > 0) {
|
||||
game.assets.getFont().setColor(Color.RED);
|
||||
game.assets.getFont().draw(game.batch, "game over", 78, 127);
|
||||
}
|
||||
|
||||
if (paused) {
|
||||
game.assets.getFont().setColor(Color.YELLOW);
|
||||
game.assets.getFont().draw(game.batch, "paused", 90, 151);
|
||||
} else {
|
||||
switch (mode) {
|
||||
case PRE_NEW_GAME:
|
||||
case NEW_ROUND_WAIT:
|
||||
case START_ROUND_WAIT:
|
||||
game.assets.getFont().setColor(Color.YELLOW);
|
||||
game.assets.getFont().draw(game.batch, "ready!", 92, 127);
|
||||
break;
|
||||
case GAME_OVER:
|
||||
game.assets.getFont().setColor(Color.RED);
|
||||
game.assets.getFont().draw(game.batch, "game over", 78, 127);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,181 +155,175 @@ public class PlayState extends LevelState {
|
||||
dt = 1/60f;
|
||||
}
|
||||
|
||||
pointsTimer -= dt;
|
||||
if (pointsTimer > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (roundClearedTimer > 0) {
|
||||
roundClearedTimer -= dt;
|
||||
if (roundClearedTimer <= 0) {
|
||||
newRound();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (gameOverTimer > 0) {
|
||||
gameOverTimer -= dt;
|
||||
if (gameOverTimer <= 0) {
|
||||
newGame();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (readyTimer > 0) {
|
||||
readyTimer -= dt;
|
||||
if (readyTimer <= 0) {
|
||||
startGame();
|
||||
} else if (newGameTimer <= 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (newGameTimer > 0) {
|
||||
newGameTimer -= dt;
|
||||
if (newGameTimer <= 0) {
|
||||
setupGame();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (frightTimer > 0) {
|
||||
frightTimer -= dt;
|
||||
if (frightTimer <= 0) {
|
||||
for (Ghost ghost : ghosts) {
|
||||
if (ghost.currentBehaviour instanceof ReturnToBase) continue;
|
||||
ghost.caught = false;
|
||||
ghost.currentBehaviour = ghost.chaseBehaviour;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// pause chase/scatter timer when frightened
|
||||
if (scatterTimer > 0) {
|
||||
scatterTimer -= dt;
|
||||
if (scatterTimer <= 0) {
|
||||
chase = true;
|
||||
if (scatterCount < 4) {
|
||||
chaseTimer = 20f;
|
||||
switch (mode) {
|
||||
case PLAYING:
|
||||
if (frightTimer <= 0) {
|
||||
if (scatter || scatterCount < 4) {
|
||||
scatterChaseTimer -= dt;
|
||||
if (scatterChaseTimer <= 0) {
|
||||
scatter = !scatter;
|
||||
scatterChaseTimer = scatter ? 7f : 20f;
|
||||
if (scatter) {
|
||||
scatterCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (chaseTimer > 0) {
|
||||
chaseTimer -= dt;
|
||||
if (chaseTimer <= 0) {
|
||||
chase = false;
|
||||
scatterTimer = 7f;
|
||||
scatterCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
secondsSinceLastDot += dt;
|
||||
if (secondsSinceLastDot >= 4) {
|
||||
// It's been 4 seconds since pacman last ate a dot, he's tryin' to avoid ghosts coming out!
|
||||
// We'll get him...
|
||||
for (int i = 1; i < 4; i++) {
|
||||
Ghost ghost = ghosts[i];
|
||||
if (ghost.inHouse) {
|
||||
ghost.leaveHouse();
|
||||
secondsSinceLastDot = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!pacmanCaught) {
|
||||
for (Ghost ghost : ghosts) {
|
||||
ghost.update(dt);
|
||||
|
||||
if (ghost.onSameTile(pacman)) {
|
||||
if (frightTimer > 0 && !ghost.caught) {
|
||||
ghostCaught(ghost);
|
||||
} else if (frightTimer <= 0 || !(ghost.currentBehaviour instanceof ReturnToBase)) {
|
||||
pacmanCaught();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (deathTimer <= 0) {
|
||||
if (pacman.alive) {
|
||||
game.assets.deathSound.play(1f);
|
||||
}
|
||||
|
||||
pacman.alive = false;
|
||||
|
||||
if (pacman.deathFrame == 13) {
|
||||
if (lives > 0) {
|
||||
setupGame();
|
||||
readyTimer = 2f;
|
||||
} else {
|
||||
gameOverTimer = 2f;
|
||||
frightTimer -= dt;
|
||||
if (frightTimer <= 0) {
|
||||
for (Ghost ghost : ghosts) {
|
||||
if (ghost.currentBehaviour instanceof ReturnToBase) continue;
|
||||
ghost.caught = false;
|
||||
ghost.currentBehaviour = ghost.chaseBehaviour;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
deathTimer -= dt;
|
||||
}
|
||||
|
||||
pacman.update(dt);
|
||||
secondsSinceLastDot += dt;
|
||||
if (secondsSinceLastDot >= 4) {
|
||||
// It's been 4 seconds since pacman last ate a dot, he's tryin' to avoid ghosts coming out!
|
||||
// We'll get him...
|
||||
for (int i = 1; i < 4; i++) {
|
||||
Ghost ghost = ghosts[i];
|
||||
if (ghost.inHouse) {
|
||||
ghost.leaveHouse();
|
||||
secondsSinceLastDot = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pacman.update(dt);
|
||||
for (Ghost ghost : ghosts) {
|
||||
ghost.update(dt);
|
||||
|
||||
if (ghost.onSameTile(pacman)) {
|
||||
if (frightTimer > 0 && !ghost.caught) {
|
||||
ghostCaught(ghost);
|
||||
continue;
|
||||
}
|
||||
if (!(ghost.currentBehaviour instanceof ReturnToBase)) {
|
||||
pacmanCaught();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case PACMAN_CAUGHT:
|
||||
pacman.update(dt);
|
||||
if (pacman.deathFrame == 13) {
|
||||
if (lives > 0) {
|
||||
resetField();
|
||||
setGameState(GameState.START_ROUND_WAIT);
|
||||
} else {
|
||||
setGameState(GameState.GAME_OVER);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
modeTimer -= dt;
|
||||
if (modeTimer <= 0) {
|
||||
modeTransition();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void initializeField() {
|
||||
public void modeTransition() {
|
||||
switch (mode) {
|
||||
// The mode we're transitioning /from/
|
||||
case PRE_NEW_GAME:
|
||||
newGame();
|
||||
setGameState(GameState.START_ROUND_WAIT);
|
||||
break;
|
||||
case START_ROUND_WAIT:
|
||||
case NEW_ROUND_WAIT:
|
||||
startGame();
|
||||
setGameState(GameState.PLAYING);
|
||||
break;
|
||||
case ROUND_WON_WAIT:
|
||||
setGameState(GameState.ROUND_WON);
|
||||
break;
|
||||
case ROUND_WON:
|
||||
newRound();
|
||||
setGameState(GameState.NEW_ROUND_WAIT);
|
||||
break;
|
||||
case GAME_OVER:
|
||||
preNewGame();
|
||||
setGameState(GameState.PRE_NEW_GAME);
|
||||
break;
|
||||
case GHOST_CAUGHT_POINTS_WAIT:
|
||||
setGameState(GameState.PLAYING);
|
||||
break;
|
||||
case PACMAN_CAUGHT_WAIT:
|
||||
game.assets.deathSound.play(1f);
|
||||
pacman.alive = false;
|
||||
setGameState(GameState.PACMAN_CAUGHT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void initializeLevel() {
|
||||
level = new Level(game,"level");
|
||||
pelletCount = level.getTileCount(LevelTile.PELLET) + level.getTileCount(LevelTile.POWER_PELLET);
|
||||
pacman = new Pacman(this, false);
|
||||
}
|
||||
|
||||
public void initializeRound() {
|
||||
scatter = true;
|
||||
scatterCount = 1;
|
||||
|
||||
pelletsEaten = 0;
|
||||
pelletsEatenSinceDeath = 0;
|
||||
pelletsEatenSinceDeathCounterEnabled = false;
|
||||
|
||||
spawnGhosts();
|
||||
}
|
||||
|
||||
public void resetField() {
|
||||
lives--;
|
||||
frightTimer = 0f;
|
||||
deathTimer = 0f;
|
||||
pacmanCaught = false;
|
||||
ghostsCaught = 0;
|
||||
lastGhostCaptured = null;
|
||||
secondsSinceLastDot = 0;
|
||||
|
||||
if (chase) {
|
||||
chaseTimer = 20f;
|
||||
} else {
|
||||
scatterTimer = 7f;
|
||||
}
|
||||
scatterChaseTimer = scatter? 7f : 20f;
|
||||
|
||||
random = new Random(897198256012865L);
|
||||
|
||||
pacman = new Pacman(this, false);
|
||||
resetGhosts();
|
||||
}
|
||||
|
||||
public void preNewGame() {
|
||||
score = 0;
|
||||
lives = 3;
|
||||
round = 1;
|
||||
|
||||
initializeLevel();
|
||||
|
||||
game.assets.beginning.play(1.0f);
|
||||
}
|
||||
|
||||
public void newGame() {
|
||||
pelletCount = 0;
|
||||
pelletEatenCount = 0;
|
||||
score = 0;
|
||||
lives = 3;
|
||||
round = 0;
|
||||
|
||||
newGameTimer = 2.3f;
|
||||
|
||||
newRound();
|
||||
initializeRound();
|
||||
resetField();
|
||||
}
|
||||
|
||||
public void newRound() {
|
||||
initializeLevel();
|
||||
initializeRound();
|
||||
resetField();
|
||||
|
||||
round++;
|
||||
|
||||
level = new Level(game,"level");
|
||||
pelletCount = level.getTileCount(LevelTile.PELLET);
|
||||
pelletCount += level.getTileCount(LevelTile.POWER_PELLET);
|
||||
pelletsEatenSinceDeath = 0;
|
||||
pelletsEatenSinceDeathCounterEnabled = false;
|
||||
|
||||
initializeField();
|
||||
spawnGhosts();
|
||||
|
||||
chase = false;
|
||||
scatterCount = 1;
|
||||
|
||||
game.assets.siren.stop(sirenId);
|
||||
if (round == 1) {
|
||||
readyTimer = 4.2f;
|
||||
game.assets.beginning_alt.play(1.0f);
|
||||
} else {
|
||||
readyTimer = 4.3f;
|
||||
game.assets.beginning.play(1.0f);
|
||||
}
|
||||
game.assets.beginning_alt.play(1.0f);
|
||||
}
|
||||
|
||||
public void setupGame() {
|
||||
lives--;
|
||||
initializeField();
|
||||
resetGhosts();
|
||||
public void startGame() {
|
||||
sirenId = game.assets.siren.loop(1.0f);
|
||||
pacman.moving = true;
|
||||
}
|
||||
|
||||
private void spawnGhosts() {
|
||||
@ -337,30 +334,26 @@ public class PlayState extends LevelState {
|
||||
ghosts[3] = new Clyde(this, new Vector2(GHOST_SPAWN_POINTS[3]), GHOST_SPAWN_DIRS[3]);
|
||||
}
|
||||
|
||||
public void startGame() {
|
||||
pacman.moving = true;
|
||||
sirenId = game.assets.siren.loop(1.0f);
|
||||
}
|
||||
|
||||
public void resetGhosts() {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (ghosts[i] == null) continue;
|
||||
ghosts[i].pos = new Vector2(GHOST_SPAWN_POINTS[i]);
|
||||
ghosts[i].currDirection = GHOST_SPAWN_DIRS[i];
|
||||
ghosts[i].currentBehaviour = chase? ghosts[i].chaseBehaviour : ghosts[i].scatterBehaviour;
|
||||
ghosts[i].currentPath = null;
|
||||
ghosts[i].caught = false;
|
||||
ghosts[i].inHouse = i > 0;
|
||||
ghosts[i].updateBehaviour();
|
||||
}
|
||||
}
|
||||
|
||||
private void pelletEaten(float x, float y) {
|
||||
level.setTile(x, y, LevelTile.EMPTY);
|
||||
if (pelletEatenCount % 2 == 0) {
|
||||
if (pelletsEaten % 2 == 0) {
|
||||
game.assets.chomp_1.play(1.0f);
|
||||
} else {
|
||||
game.assets.chomp_2.play(1.0f);
|
||||
}
|
||||
pelletEatenCount++;
|
||||
pelletsEaten++;
|
||||
pelletCount--;
|
||||
|
||||
if (pelletsEatenSinceDeathCounterEnabled) {
|
||||
@ -381,7 +374,7 @@ public class PlayState extends LevelState {
|
||||
secondsSinceLastDot = 0;
|
||||
|
||||
if (pelletCount == 0) {
|
||||
roundClearedTimer = 3f;
|
||||
setGameState(GameState.ROUND_WON_WAIT);
|
||||
}
|
||||
}
|
||||
|
||||
@ -405,11 +398,11 @@ public class PlayState extends LevelState {
|
||||
|
||||
private void pacmanCaught() {
|
||||
game.assets.siren.stop(sirenId);
|
||||
deathTimer = 1f;
|
||||
pacmanCaught = true;
|
||||
pacman.moving = false;
|
||||
pelletsEatenSinceDeath = 0;
|
||||
pelletsEatenSinceDeathCounterEnabled = true;
|
||||
|
||||
setGameState(GameState.PACMAN_CAUGHT_WAIT);
|
||||
}
|
||||
|
||||
private void ghostCaught(Ghost ghost) {
|
||||
@ -419,7 +412,8 @@ public class PlayState extends LevelState {
|
||||
lastGhostCaptured = ghost;
|
||||
ghostsCaught++;
|
||||
score += ghostsCaught * 200;
|
||||
pointsTimer = 1f;
|
||||
|
||||
setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -453,7 +447,7 @@ public class PlayState extends LevelState {
|
||||
paused = !paused;
|
||||
break;
|
||||
case Input.Keys.N:
|
||||
newGame();
|
||||
preNewGame();
|
||||
break;
|
||||
}
|
||||
return super.keyDown(keycode);
|
||||
@ -461,4 +455,35 @@ public class PlayState extends LevelState {
|
||||
|
||||
}
|
||||
|
||||
public enum GameState {
|
||||
|
||||
PRE_NEW_GAME(2.3f),
|
||||
NEW_ROUND_WAIT(4.2f),
|
||||
START_ROUND_WAIT(2f),
|
||||
ROUND_WON_WAIT(1f),
|
||||
ROUND_WON(2f),
|
||||
PACMAN_CAUGHT,
|
||||
PACMAN_CAUGHT_WAIT(1f),
|
||||
GHOST_CAUGHT_POINTS_WAIT(1f),
|
||||
GAME_OVER(2f),
|
||||
PLAYING,
|
||||
;
|
||||
|
||||
final float timer;
|
||||
|
||||
GameState(float timer) {
|
||||
this.timer = timer;
|
||||
}
|
||||
|
||||
GameState() {
|
||||
this(0f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setGameState(GameState mode) {
|
||||
this.mode = mode;
|
||||
this.modeTimer = mode.timer;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user