A little more refactoring

Declare methods before calling for easier following
This commit is contained in:
Matt Low 2020-01-05 02:57:09 +04:00
parent 389533d394
commit ab94395eac
4 changed files with 236 additions and 238 deletions

View File

@ -23,7 +23,7 @@ public class Clyde extends Ghost {
if (state.pelletsEatenSinceDeath >= 32) {
state.pelletsEatenSinceDeathCounterEnabled = false;
}
} else if (dotCounter >= DOT_LIMIT) {
} else if (pelletCounter >= DOT_LIMIT) {
leaveHouse();
}
}

View File

@ -36,7 +36,7 @@ public class Ghost extends MovableEntity {
public boolean inHouse;
public boolean caught;
public int dotCounter;
public int pelletCounter;
public Ghost(PlayState state, Vector2 pos, Direction direction, int spriteIndex,
Behaviour chaseBehaviour, Behaviour scatterBehaviour, boolean inHouse) {

View File

@ -24,7 +24,7 @@ public class Inky extends Ghost {
if (state.pelletsEatenSinceDeath >= 17) {
leaveHouse();
}
} else if (dotCounter >= DOT_LIMIT) {
} else if (pelletCounter >= DOT_LIMIT) {
leaveHouse();
}
}

View File

@ -3,6 +3,7 @@ package com.me.pacman.state;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
@ -42,7 +43,7 @@ public class PlayState extends LevelState {
private long sirenId;
private int pelletCount;
private int pelletsRemaining;
private int pelletsEaten;
public int pelletsEatenSinceDeath;
@ -53,9 +54,6 @@ public class PlayState extends LevelState {
private int round;
private boolean paused = false;
private float modeTimer;
private GameState mode;
public float secondsSinceLastDot;
public boolean scatter;
@ -69,10 +67,125 @@ public class PlayState extends LevelState {
public Pacman pacman;
public Ghost[] ghosts;
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);
}
}
private GameState state;
private float stateTimer;
public PlayState(PacDude game) {
super(game);
}
private void setGameState(GameState state) {
this.state = state;
this.stateTimer = state.timer;
}
private void spawnGhosts() {
ghosts = new Ghost[4];
ghosts[0] = new Blinky(this, new Vector2(GHOST_SPAWN_POINTS[0]), GHOST_SPAWN_DIRS[0]);
ghosts[1] = new Pinky(this, new Vector2(GHOST_SPAWN_POINTS[1]), GHOST_SPAWN_DIRS[1]);
ghosts[2] = new Inky(this, new Vector2(GHOST_SPAWN_POINTS[2]), GHOST_SPAWN_DIRS[2]);
ghosts[3] = new Clyde(this, new Vector2(GHOST_SPAWN_POINTS[3]), GHOST_SPAWN_DIRS[3]);
}
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].currentPath = null;
ghosts[i].caught = false;
ghosts[i].inHouse = i > 0;
ghosts[i].updateBehaviour();
}
}
private void initializeLevel() {
level = new Level(game,"level");
pelletsRemaining = level.getTileCount(LevelTile.PELLET) + level.getTileCount(LevelTile.POWER_PELLET);
pacman = new Pacman(this, false);
}
private void initializeRound() {
scatter = true;
scatterCount = 1;
pelletsEaten = 0;
pelletsEatenSinceDeath = 0;
pelletsEatenSinceDeathCounterEnabled = false;
spawnGhosts();
}
private void resetField() {
lives--;
frightTimer = 0f;
ghostsCaught = 0;
lastGhostCaptured = null;
secondsSinceLastDot = 0;
scatterChaseTimer = scatter? 7f : 20f;
random = new Random(897198256012865L);
pacman = new Pacman(this, false);
resetGhosts();
}
private void preNewGame() {
score = 0;
lives = 3;
round = 1;
initializeLevel();
game.assets.beginning.play(1.0f);
}
private void newGame() {
initializeRound();
resetField();
}
private void newRound() {
initializeLevel();
initializeRound();
resetField();
round++;
game.assets.siren.stop(sirenId);
game.assets.beginning_alt.play(1.0f);
}
private void startGame() {
sirenId = game.assets.siren.loop(1.0f);
pacman.moving = true;
}
@Override
public void setup() {
levelBackground = game.assets.getLevelBackground();
@ -85,6 +198,109 @@ public class PlayState extends LevelState {
setGameState(GameState.PRE_NEW_GAME);
}
public void stateTransition() {
switch (state) {
// The state 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;
}
}
private void pelletEaten(float x, float y) {
level.setTile(x, y, LevelTile.EMPTY);
Sound chomp = pelletsEaten % 2 == 0? game.assets.chomp_1 : game.assets.chomp_2;
chomp.play(1.0f);
pelletsEaten++;
pelletsRemaining--;
if (pelletsEatenSinceDeathCounterEnabled) {
// Increase global dot counter when enabled
pelletsEatenSinceDeath++;
} else {
// else increment appropriate ghost's individual counter
for (Ghost ghost : ghosts) {
// Increment the dot counter of the first encountered ghost in the house
// Ghosts are ordered Blinky, Pinky, Inky, Clyde. Blinky's inHouse is always false,
// Increase the dotCounter of only one ghost: the first of the above list still in the house.
if (!ghost.inHouse) continue;
ghost.pelletCounter++;
break;
}
}
secondsSinceLastDot = 0;
if (pelletsRemaining == 0) {
setGameState(GameState.ROUND_WON_WAIT);
}
}
public void eatPellet(float x, float y) {
pelletEaten(x, y);
score += 10;
}
public void eatPowerPellet(float x, float y) {
pelletEaten(x, y);
for (Ghost ghost : ghosts) {
if (ghost.currentBehaviour instanceof ReturnToBase) continue;
ghost.caught = false;
ghost.currentBehaviour = ghost.frightBehaviour;
ghost.currDirection = ghost.currDirection.getOpposite();
}
frightTimer = 6f;
ghostsCaught = 0;
score += 50;
}
private void pacmanCaught() {
game.assets.siren.stop(sirenId);
pacman.moving = false;
pelletsEatenSinceDeath = 0;
pelletsEatenSinceDeathCounterEnabled = true;
setGameState(GameState.PACMAN_CAUGHT_WAIT);
}
private void ghostCaught(Ghost ghost) {
ghost.caught = true;
ghost.currentBehaviour = new ReturnToBase(this);
lastGhostCaptured = ghost;
ghostsCaught++;
score += ghostsCaught * 200;
setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT);
}
@Override
public void render() {
level.render(0, 16);
@ -95,21 +311,21 @@ public class PlayState extends LevelState {
game.batch.draw(lifeSprite, i * 16, 0);
}
if (mode == GameState.GHOST_CAUGHT_POINTS_WAIT) {
if (state == 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 (mode == GameState.ROUND_WON) {
if (state == GameState.ROUND_WON) {
// draw flashing level background
game.batch.draw((int) (modeTimer * 4) % 2 == 0? levelBackground : winBackground, 0, 16);
game.batch.draw((int) (stateTimer * 4) % 2 == 0? levelBackground : winBackground, 0, 16);
return;
} else {
game.batch.draw(levelBackground, 0, 16);
}
switch (mode) {
switch (state) {
case PRE_NEW_GAME:
case PACMAN_CAUGHT:
case ROUND_WON:
@ -117,7 +333,7 @@ public class PlayState extends LevelState {
break;
default:
for (Ghost ghost : ghosts) {
if (mode == GameState.GHOST_CAUGHT_POINTS_WAIT && ghost == lastGhostCaptured) {
if (state == 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);
@ -129,7 +345,7 @@ public class PlayState extends LevelState {
game.assets.getFont().setColor(Color.YELLOW);
game.assets.getFont().draw(game.batch, "paused", 90, 151);
} else {
switch (mode) {
switch (state) {
case PRE_NEW_GAME:
case NEW_ROUND_WAIT:
case START_ROUND_WAIT:
@ -155,7 +371,7 @@ public class PlayState extends LevelState {
dt = 1/60f;
}
switch (mode) {
switch (state) {
case PLAYING:
if (frightTimer <= 0) {
if (scatter || scatterCount < 4) {
@ -221,206 +437,14 @@ public class PlayState extends LevelState {
}
break;
default:
modeTimer -= dt;
if (modeTimer <= 0) {
modeTransition();
stateTimer -= dt;
if (stateTimer <= 0) {
stateTransition();
}
break;
}
}
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;
ghostsCaught = 0;
lastGhostCaptured = null;
secondsSinceLastDot = 0;
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() {
initializeRound();
resetField();
}
public void newRound() {
initializeLevel();
initializeRound();
resetField();
round++;
game.assets.siren.stop(sirenId);
game.assets.beginning_alt.play(1.0f);
}
public void startGame() {
sirenId = game.assets.siren.loop(1.0f);
pacman.moving = true;
}
private void spawnGhosts() {
ghosts = new Ghost[4];
ghosts[0] = new Blinky(this, new Vector2(GHOST_SPAWN_POINTS[0]), GHOST_SPAWN_DIRS[0]);
ghosts[1] = new Pinky(this, new Vector2(GHOST_SPAWN_POINTS[1]), GHOST_SPAWN_DIRS[1]);
ghosts[2] = new Inky(this, new Vector2(GHOST_SPAWN_POINTS[2]), GHOST_SPAWN_DIRS[2]);
ghosts[3] = new Clyde(this, new Vector2(GHOST_SPAWN_POINTS[3]), GHOST_SPAWN_DIRS[3]);
}
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].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 (pelletsEaten % 2 == 0) {
game.assets.chomp_1.play(1.0f);
} else {
game.assets.chomp_2.play(1.0f);
}
pelletsEaten++;
pelletCount--;
if (pelletsEatenSinceDeathCounterEnabled) {
// Increase global dot counter when enabled
pelletsEatenSinceDeath++;
} else {
// else increment appropriate ghost's individual counter
for (Ghost ghost : ghosts) {
// Increment the dot counter of the first encountered ghost in the house
// Ghosts are ordered Blinky, Pinky, Inky, Clyde. Blinky's inHouse is always false,
// Increase the dotCounter of only one ghost: the first of the above list still in the house.
if (!ghost.inHouse) continue;
ghost.dotCounter++;
break;
}
}
secondsSinceLastDot = 0;
if (pelletCount == 0) {
setGameState(GameState.ROUND_WON_WAIT);
}
}
public void eatPellet(float x, float y) {
pelletEaten(x, y);
score += 10;
}
public void eatPowerPellet(float x, float y) {
pelletEaten(x, y);
for (Ghost ghost : ghosts) {
if (ghost.currentBehaviour instanceof ReturnToBase) continue;
ghost.caught = false;
ghost.currentBehaviour = ghost.frightBehaviour;
ghost.currDirection = ghost.currDirection.getOpposite();
}
frightTimer = 6f;
ghostsCaught = 0;
score += 50;
}
private void pacmanCaught() {
game.assets.siren.stop(sirenId);
pacman.moving = false;
pelletsEatenSinceDeath = 0;
pelletsEatenSinceDeathCounterEnabled = true;
setGameState(GameState.PACMAN_CAUGHT_WAIT);
}
private void ghostCaught(Ghost ghost) {
ghost.caught = true;
ghost.currentBehaviour = new ReturnToBase(this);
lastGhostCaptured = ghost;
ghostsCaught++;
score += ghostsCaught * 200;
setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT);
}
@Override
public void dispose() {
Gdx.input.setInputProcessor(null);
}
private final class Controller extends InputAdapter {
@Override
@ -455,35 +479,9 @@ 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;
@Override
public void dispose() {
Gdx.input.setInputProcessor(null);
}
}