Refactors

This commit is contained in:
Matt Low 2020-01-22 02:40:36 +04:00
parent b19daafc2b
commit f655fe2448
9 changed files with 51 additions and 53 deletions

View File

@ -9,7 +9,7 @@ import com.badlogic.gdx.utils.Array;
* *
* Scores are saved in a simple text file * Scores are saved in a simple text file
*/ */
public class HighScores { public class HighScoreManager {
public static final String FILE = "scores.dat"; public static final String FILE = "scores.dat";
@ -17,7 +17,7 @@ public class HighScores {
private Array<Score> scores = new Array<>(10); private Array<Score> scores = new Array<>(10);
public HighScores() { public HighScoreManager() {
file = Gdx.files.local(FILE); file = Gdx.files.local(FILE);
loadScores(); loadScores();
} }

View File

@ -18,12 +18,12 @@ public class PacDude extends Game {
public Viewport viewport; public Viewport viewport;
public Assets assets; public Assets assets;
public Sound sound; public SoundManager sound;
public SpriteBatch batch; public SpriteBatch batch;
public FontRenderer fontRenderer; public FontRenderer fontRenderer;
public HighScores highScores; public HighScoreManager highScores;
private State nextState; private State nextState;
@ -35,12 +35,12 @@ public class PacDude extends Game {
assets = new Assets(); assets = new Assets();
assets.loadAssets(); assets.loadAssets();
sound = new Sound(this); sound = new SoundManager(this);
batch = new SpriteBatch(); batch = new SpriteBatch();
fontRenderer = new FontRenderer(assets.font); fontRenderer = new FontRenderer(assets.font);
highScores = new HighScores(); highScores = new HighScoreManager();
Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClearColor(0, 0, 0, 1);
setNextState(new MenuState(this)); setNextState(new MenuState(this));

View File

@ -1,12 +1,10 @@
package com.me.pacman.level; package com.me.pacman;
import com.me.pacman.Constants;
import java.util.Arrays; import java.util.Arrays;
public class Modifiers { public class RoundModifiers {
private static final Modifiers[] ROUND_MODIFIERS; private static final RoundModifiers[] ROUND_MODIFIERS;
private float frightTime; private float frightTime;
@ -78,8 +76,8 @@ public class Modifiers {
return ROUND_MODIFIERS[cap(round)].scatterChaseSwitchTimings[scatterChaseTransition]; return ROUND_MODIFIERS[cap(round)].scatterChaseSwitchTimings[scatterChaseTransition];
} }
public Modifiers clone() { public RoundModifiers clone() {
Modifiers mod = new Modifiers(); RoundModifiers mod = new RoundModifiers();
mod.frightTime = frightTime; mod.frightTime = frightTime;
mod.pacmanSpeed = pacmanSpeed; mod.pacmanSpeed = pacmanSpeed;
mod.pacmanFrightSpeed = pacmanFrightSpeed; mod.pacmanFrightSpeed = pacmanFrightSpeed;
@ -100,11 +98,11 @@ public class Modifiers {
} }
static { static {
ROUND_MODIFIERS = new Modifiers[21]; ROUND_MODIFIERS = new RoundModifiers[21];
int round = 0; int round = 0;
// Round 1 // Round 1
Modifiers mod = new Modifiers(); RoundModifiers mod = new RoundModifiers();
mod.frightTime = 6f; mod.frightTime = 6f;
mod.pacmanSpeed = 0.8f; mod.pacmanSpeed = 0.8f;
mod.pacmanFrightSpeed = 0.9f; mod.pacmanFrightSpeed = 0.9f;

View File

@ -2,7 +2,7 @@ package com.me.pacman;
import java.util.Arrays; import java.util.Arrays;
public class Sound { public class SoundManager {
public enum Effect { public enum Effect {
BEGINNING, BEGINNING,
@ -28,7 +28,7 @@ public class Sound {
private long[] playing; private long[] playing;
private long[] looping; private long[] looping;
public Sound(PacDude game) { public SoundManager(PacDude game) {
this.game = game; this.game = game;
this.playing = new long[Effect.values().length]; this.playing = new long[Effect.values().length];
this.looping = new long[Effect.values().length]; this.looping = new long[Effect.values().length];

View File

@ -5,7 +5,7 @@ import com.me.pacman.Constants;
import com.me.pacman.entity.ai.BlinkyChaseBehaviour; import com.me.pacman.entity.ai.BlinkyChaseBehaviour;
import com.me.pacman.entity.ai.StaticTargetBehaviour; import com.me.pacman.entity.ai.StaticTargetBehaviour;
import com.me.pacman.entity.ai.Target; import com.me.pacman.entity.ai.Target;
import com.me.pacman.level.Modifiers; import com.me.pacman.RoundModifiers;
import com.me.pacman.state.PlayState; import com.me.pacman.state.PlayState;
public class Blinky extends Ghost { public class Blinky extends Ghost {
@ -20,10 +20,10 @@ public class Blinky extends Ghost {
protected float getNormalSpeed() { protected float getNormalSpeed() {
int round = state.round; int round = state.round;
if (!state.hasDied || !state.ghosts[3].inHouse) { if (!state.hasDied || !state.ghosts[3].inHouse) {
if (state.pelletsRemaining <= Modifiers.getElroy2DotsLeft(round)) { if (state.pelletsRemaining <= RoundModifiers.getElroy2DotsLeft(round)) {
return Modifiers.getElroy2Speed(round); return RoundModifiers.getElroy2Speed(round);
} else if (state.pelletsRemaining <= Modifiers.getElroy1DotsLeft(round)) { } else if (state.pelletsRemaining <= RoundModifiers.getElroy1DotsLeft(round)) {
return Modifiers.getElroy1Speed(round); return RoundModifiers.getElroy1Speed(round);
} }
} }
return super.getNormalSpeed(); return super.getNormalSpeed();

View File

@ -9,7 +9,7 @@ import com.me.pacman.entity.ai.Target;
import com.me.pacman.entity.path.EnterGhostHousePath; import com.me.pacman.entity.path.EnterGhostHousePath;
import com.me.pacman.entity.path.ExitGhostHousePath; import com.me.pacman.entity.path.ExitGhostHousePath;
import com.me.pacman.level.LevelTile; import com.me.pacman.level.LevelTile;
import com.me.pacman.level.Modifiers; import com.me.pacman.RoundModifiers;
import com.me.pacman.state.PlayState; import com.me.pacman.state.PlayState;
import java.util.ArrayList; import java.util.ArrayList;
@ -44,7 +44,7 @@ public class Ghost extends MovableEntity {
public Ghost(PlayState state, Vector2 pos, Direction direction, int spriteIndex, public Ghost(PlayState state, Vector2 pos, Direction direction, int spriteIndex,
Behaviour chaseBehaviour, Behaviour scatterBehaviour, boolean inHouse) { Behaviour chaseBehaviour, Behaviour scatterBehaviour, boolean inHouse) {
super(state, pos.x, pos.y, Modifiers.getGhostSpeed(0), true, direction, 0.1f); super(state, pos.x, pos.y, RoundModifiers.getGhostSpeed(0), true, direction, 0.1f);
this.state = state; this.state = state;
this.spriteIndex = spriteIndex; this.spriteIndex = spriteIndex;
this.chaseBehaviour = chaseBehaviour; this.chaseBehaviour = chaseBehaviour;
@ -84,7 +84,7 @@ public class Ghost extends MovableEntity {
} }
protected float getNormalSpeed() { protected float getNormalSpeed() {
return Modifiers.getGhostSpeed(state.round); return RoundModifiers.getGhostSpeed(state.round);
} }
@Override @Override
@ -95,9 +95,9 @@ public class Ghost extends MovableEntity {
LevelTile currentTile = state.level.getTile(pos); LevelTile currentTile = state.level.getTile(pos);
if (currentTile == null || currentTile == LevelTile.TUNNEL) { if (currentTile == null || currentTile == LevelTile.TUNNEL) {
speed = Modifiers.getGhostTunnelSpeed(state.round); speed = RoundModifiers.getGhostTunnelSpeed(state.round);
} else if (currentBehaviour instanceof FrightenedBehaviour) { } else if (currentBehaviour instanceof FrightenedBehaviour) {
speed = Modifiers.getGhostFrightSpeed(state.round); speed = RoundModifiers.getGhostFrightSpeed(state.round);
} else if (currentBehaviour instanceof ReturnToBase) { } else if (currentBehaviour instanceof ReturnToBase) {
speed = EYES_SPEED; speed = EYES_SPEED;
} else if (inHouse) { } else if (inHouse) {

View File

@ -4,7 +4,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.pacman.Constants; import com.me.pacman.Constants;
import com.me.pacman.level.LevelTile; import com.me.pacman.level.LevelTile;
import com.me.pacman.level.Modifiers; import com.me.pacman.RoundModifiers;
import com.me.pacman.state.PlayState; import com.me.pacman.state.PlayState;
public class Pacman extends MovableEntity { public class Pacman extends MovableEntity {
@ -50,9 +50,9 @@ public class Pacman extends MovableEntity {
} }
if (state.frightTimer > 0) { if (state.frightTimer > 0) {
speed = Modifiers.getPacmanFrightSpeed(state.round); speed = RoundModifiers.getPacmanFrightSpeed(state.round);
} else { } else {
speed = Modifiers.getPacmanSpeed(state.round); speed = RoundModifiers.getPacmanSpeed(state.round);
} }
super.update(dt); super.update(dt);

View File

@ -8,7 +8,7 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.pacman.Constants; import com.me.pacman.Constants;
import com.me.pacman.PacDude; import com.me.pacman.PacDude;
import com.me.pacman.Sound; import com.me.pacman.SoundManager;
public class MenuState extends LevelState { public class MenuState extends LevelState {
@ -35,7 +35,7 @@ public class MenuState extends LevelState {
logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
Gdx.input.setInputProcessor(this.new Controller()); Gdx.input.setInputProcessor(this.new Controller());
game.sound.play(Sound.Effect.BEGINNING); game.sound.play(SoundManager.Effect.BEGINNING);
switch(Gdx.app.getType()) { switch(Gdx.app.getType()) {
case Android: case Android:
@ -72,11 +72,11 @@ public class MenuState extends LevelState {
switch (keycode) { switch (keycode) {
case Input.Keys.UP: case Input.Keys.UP:
selectedOption = selectedOption > 0 ? --selectedOption : 1; selectedOption = selectedOption > 0 ? --selectedOption : 1;
game.sound.play(Sound.Effect.CHOMP_1); game.sound.play(SoundManager.Effect.CHOMP_1);
break; break;
case Input.Keys.DOWN: case Input.Keys.DOWN:
selectedOption = selectedOption < 1 ? ++selectedOption : 0; selectedOption = selectedOption < 1 ? ++selectedOption : 0;
game.sound.play(Sound.Effect.CHOMP_2); game.sound.play(SoundManager.Effect.CHOMP_2);
break; break;
case Input.Keys.ENTER: case Input.Keys.ENTER:
switch (selectedOption) { switch (selectedOption) {

View File

@ -10,13 +10,13 @@ import com.badlogic.gdx.math.Vector2;
import com.me.pacman.Constants; import com.me.pacman.Constants;
import com.me.pacman.PacDude; import com.me.pacman.PacDude;
import com.me.pacman.Score; import com.me.pacman.Score;
import com.me.pacman.Sound; import com.me.pacman.SoundManager;
import com.me.pacman.entity.*; import com.me.pacman.entity.*;
import com.me.pacman.entity.ai.ReturnToBase; import com.me.pacman.entity.ai.ReturnToBase;
import com.me.pacman.entity.path.EnterGhostHousePath; import com.me.pacman.entity.path.EnterGhostHousePath;
import com.me.pacman.level.Level; import com.me.pacman.level.Level;
import com.me.pacman.level.LevelTile; import com.me.pacman.level.LevelTile;
import com.me.pacman.level.Modifiers; import com.me.pacman.RoundModifiers;
import java.util.Random; import java.util.Random;
@ -102,25 +102,25 @@ public class PlayState extends LevelState {
for (Ghost ghost : ghosts) { for (Ghost ghost : ghosts) {
if ((ghost.currentBehaviour instanceof ReturnToBase if ((ghost.currentBehaviour instanceof ReturnToBase
|| ghost.currentPath instanceof EnterGhostHousePath) && !ghost.inHouse) { || ghost.currentPath instanceof EnterGhostHousePath) && !ghost.inHouse) {
if (!game.sound.isLooping(Sound.Effect.RETURN_BASE)) { if (!game.sound.isLooping(SoundManager.Effect.RETURN_BASE)) {
game.sound.stopLoops(); game.sound.stopLoops();
game.sound.loop(Sound.Effect.RETURN_BASE); game.sound.loop(SoundManager.Effect.RETURN_BASE);
} }
return; return;
} }
} }
if (frightTimer > 0) { if (frightTimer > 0) {
if (!game.sound.isLooping(Sound.Effect.FRIGHT)) { if (!game.sound.isLooping(SoundManager.Effect.FRIGHT)) {
game.sound.stopLoops(); game.sound.stopLoops();
game.sound.loop(Sound.Effect.FRIGHT); game.sound.loop(SoundManager.Effect.FRIGHT);
} }
return; return;
} }
if (!game.sound.isLooping(Sound.Effect.SIREN)) { if (!game.sound.isLooping(SoundManager.Effect.SIREN)) {
game.sound.stopLoops(); game.sound.stopLoops();
game.sound.loop(Sound.Effect.SIREN); game.sound.loop(SoundManager.Effect.SIREN);
} }
} }
@ -174,7 +174,7 @@ public class PlayState extends LevelState {
lastGhostCaptured = null; lastGhostCaptured = null;
secondsSinceLastDot = 0; secondsSinceLastDot = 0;
scatterChaseTimer = Modifiers.getScatterChaseTimer(round, scatterChaseTransition); scatterChaseTimer = RoundModifiers.getScatterChaseTimer(round, scatterChaseTransition);
random = new Random(897198256012865L); random = new Random(897198256012865L);
@ -194,7 +194,7 @@ public class PlayState extends LevelState {
initializeLevel(); initializeLevel();
game.sound.stopAll(); game.sound.stopAll();
game.sound.play(Sound.Effect.BEGINNING); game.sound.play(SoundManager.Effect.BEGINNING);
} }
private void newGame() { private void newGame() {
@ -211,11 +211,11 @@ public class PlayState extends LevelState {
round++; round++;
game.sound.stopAll(); game.sound.stopAll();
game.sound.play(Sound.Effect.BEGINNING_ALT); game.sound.play(SoundManager.Effect.BEGINNING_ALT);
} }
private void startGame() { private void startGame() {
game.sound.loop(Sound.Effect.SIREN); game.sound.loop(SoundManager.Effect.SIREN);
pacman.moving = true; pacman.moving = true;
} }
@ -256,7 +256,7 @@ public class PlayState extends LevelState {
updateBackgroundAudio(); updateBackgroundAudio();
return GameState.PLAYING; return GameState.PLAYING;
case PACMAN_CAUGHT_WAIT: case PACMAN_CAUGHT_WAIT:
game.sound.play(Sound.Effect.DEATH); game.sound.play(SoundManager.Effect.DEATH);
pacman.alive = false; pacman.alive = false;
return GameState.PACMAN_CAUGHT; return GameState.PACMAN_CAUGHT;
} }
@ -336,7 +336,7 @@ public class PlayState extends LevelState {
private void pelletEaten(float x, float y) { private void pelletEaten(float x, float y) {
level.setTile(x, y, LevelTile.EMPTY); level.setTile(x, y, LevelTile.EMPTY);
game.sound.play(pelletsEaten % 2 == 0? Sound.Effect.CHOMP_1 : Sound.Effect.CHOMP_2); game.sound.play(pelletsEaten % 2 == 0? SoundManager.Effect.CHOMP_1 : SoundManager.Effect.CHOMP_2);
pelletsEaten++; pelletsEaten++;
pelletsRemaining--; pelletsRemaining--;
@ -377,7 +377,7 @@ public class PlayState extends LevelState {
ghost.currentBehaviour = ghost.frightBehaviour; ghost.currentBehaviour = ghost.frightBehaviour;
ghost.reverse = true; ghost.reverse = true;
} }
frightTimer = Modifiers.getFrightTime(round); frightTimer = RoundModifiers.getFrightTime(round);
ghostsCaught = 0; ghostsCaught = 0;
score += 50; score += 50;
@ -385,7 +385,7 @@ public class PlayState extends LevelState {
} }
private void pacmanCaught() { private void pacmanCaught() {
game.sound.stopLooping(Sound.Effect.SIREN); game.sound.stopLooping(SoundManager.Effect.SIREN);
pacman.moving = false; pacman.moving = false;
pelletsEatenSinceDeath = 0; pelletsEatenSinceDeath = 0;
pelletsEatenSinceDeathCounterEnabled = true; pelletsEatenSinceDeathCounterEnabled = true;
@ -402,7 +402,7 @@ public class PlayState extends LevelState {
ghostsCaught++; ghostsCaught++;
score += ghostsCaught * 200; score += ghostsCaught * 200;
game.sound.play(Sound.Effect.EAT_GHOST); game.sound.play(SoundManager.Effect.EAT_GHOST);
setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT); setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT);
} }
@ -412,7 +412,7 @@ public class PlayState extends LevelState {
if (scatterChaseTimer <= 0) { if (scatterChaseTimer <= 0) {
scatter = !scatter; scatter = !scatter;
scatterChaseTransition++; scatterChaseTransition++;
scatterChaseTimer = Modifiers.getScatterChaseTimer(round, scatterChaseTransition); scatterChaseTimer = RoundModifiers.getScatterChaseTimer(round, scatterChaseTransition);
} }
} }
} }
@ -432,7 +432,7 @@ public class PlayState extends LevelState {
private void updateSecondsSinceLastDot(float dt) { private void updateSecondsSinceLastDot(float dt) {
secondsSinceLastDot += dt; secondsSinceLastDot += dt;
if (secondsSinceLastDot >= Modifiers.getForceLeaveSeconds(round)) { if (secondsSinceLastDot >= RoundModifiers.getForceLeaveSeconds(round)) {
// It's been 4 seconds since pacman last ate a dot, he's tryin' to avoid ghosts coming out! // It's been 4 seconds since pacman last ate a dot, he's tryin' to avoid ghosts coming out!
// We'll get him... // We'll get him...
for (int i = 1; i < 4; i++) { for (int i = 1; i < 4; i++) {