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
*/
public class HighScores {
public class HighScoreManager {
public static final String FILE = "scores.dat";
@ -17,7 +17,7 @@ public class HighScores {
private Array<Score> scores = new Array<>(10);
public HighScores() {
public HighScoreManager() {
file = Gdx.files.local(FILE);
loadScores();
}

View File

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

View File

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

View File

@ -2,7 +2,7 @@ package com.me.pacman;
import java.util.Arrays;
public class Sound {
public class SoundManager {
public enum Effect {
BEGINNING,
@ -28,7 +28,7 @@ public class Sound {
private long[] playing;
private long[] looping;
public Sound(PacDude game) {
public SoundManager(PacDude game) {
this.game = game;
this.playing = 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.StaticTargetBehaviour;
import com.me.pacman.entity.ai.Target;
import com.me.pacman.level.Modifiers;
import com.me.pacman.RoundModifiers;
import com.me.pacman.state.PlayState;
public class Blinky extends Ghost {
@ -20,10 +20,10 @@ public class Blinky extends Ghost {
protected float getNormalSpeed() {
int round = state.round;
if (!state.hasDied || !state.ghosts[3].inHouse) {
if (state.pelletsRemaining <= Modifiers.getElroy2DotsLeft(round)) {
return Modifiers.getElroy2Speed(round);
} else if (state.pelletsRemaining <= Modifiers.getElroy1DotsLeft(round)) {
return Modifiers.getElroy1Speed(round);
if (state.pelletsRemaining <= RoundModifiers.getElroy2DotsLeft(round)) {
return RoundModifiers.getElroy2Speed(round);
} else if (state.pelletsRemaining <= RoundModifiers.getElroy1DotsLeft(round)) {
return RoundModifiers.getElroy1Speed(round);
}
}
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.ExitGhostHousePath;
import com.me.pacman.level.LevelTile;
import com.me.pacman.level.Modifiers;
import com.me.pacman.RoundModifiers;
import com.me.pacman.state.PlayState;
import java.util.ArrayList;
@ -44,7 +44,7 @@ public class Ghost extends MovableEntity {
public Ghost(PlayState state, Vector2 pos, Direction direction, int spriteIndex,
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.spriteIndex = spriteIndex;
this.chaseBehaviour = chaseBehaviour;
@ -84,7 +84,7 @@ public class Ghost extends MovableEntity {
}
protected float getNormalSpeed() {
return Modifiers.getGhostSpeed(state.round);
return RoundModifiers.getGhostSpeed(state.round);
}
@Override
@ -95,9 +95,9 @@ public class Ghost extends MovableEntity {
LevelTile currentTile = state.level.getTile(pos);
if (currentTile == null || currentTile == LevelTile.TUNNEL) {
speed = Modifiers.getGhostTunnelSpeed(state.round);
speed = RoundModifiers.getGhostTunnelSpeed(state.round);
} else if (currentBehaviour instanceof FrightenedBehaviour) {
speed = Modifiers.getGhostFrightSpeed(state.round);
speed = RoundModifiers.getGhostFrightSpeed(state.round);
} else if (currentBehaviour instanceof ReturnToBase) {
speed = EYES_SPEED;
} else if (inHouse) {

View File

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

View File

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

View File

@ -10,13 +10,13 @@ import com.badlogic.gdx.math.Vector2;
import com.me.pacman.Constants;
import com.me.pacman.PacDude;
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.ai.ReturnToBase;
import com.me.pacman.entity.path.EnterGhostHousePath;
import com.me.pacman.level.Level;
import com.me.pacman.level.LevelTile;
import com.me.pacman.level.Modifiers;
import com.me.pacman.RoundModifiers;
import java.util.Random;
@ -102,25 +102,25 @@ public class PlayState extends LevelState {
for (Ghost ghost : ghosts) {
if ((ghost.currentBehaviour instanceof ReturnToBase
|| 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.loop(Sound.Effect.RETURN_BASE);
game.sound.loop(SoundManager.Effect.RETURN_BASE);
}
return;
}
}
if (frightTimer > 0) {
if (!game.sound.isLooping(Sound.Effect.FRIGHT)) {
if (!game.sound.isLooping(SoundManager.Effect.FRIGHT)) {
game.sound.stopLoops();
game.sound.loop(Sound.Effect.FRIGHT);
game.sound.loop(SoundManager.Effect.FRIGHT);
}
return;
}
if (!game.sound.isLooping(Sound.Effect.SIREN)) {
if (!game.sound.isLooping(SoundManager.Effect.SIREN)) {
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;
secondsSinceLastDot = 0;
scatterChaseTimer = Modifiers.getScatterChaseTimer(round, scatterChaseTransition);
scatterChaseTimer = RoundModifiers.getScatterChaseTimer(round, scatterChaseTransition);
random = new Random(897198256012865L);
@ -194,7 +194,7 @@ public class PlayState extends LevelState {
initializeLevel();
game.sound.stopAll();
game.sound.play(Sound.Effect.BEGINNING);
game.sound.play(SoundManager.Effect.BEGINNING);
}
private void newGame() {
@ -211,11 +211,11 @@ public class PlayState extends LevelState {
round++;
game.sound.stopAll();
game.sound.play(Sound.Effect.BEGINNING_ALT);
game.sound.play(SoundManager.Effect.BEGINNING_ALT);
}
private void startGame() {
game.sound.loop(Sound.Effect.SIREN);
game.sound.loop(SoundManager.Effect.SIREN);
pacman.moving = true;
}
@ -256,7 +256,7 @@ public class PlayState extends LevelState {
updateBackgroundAudio();
return GameState.PLAYING;
case PACMAN_CAUGHT_WAIT:
game.sound.play(Sound.Effect.DEATH);
game.sound.play(SoundManager.Effect.DEATH);
pacman.alive = false;
return GameState.PACMAN_CAUGHT;
}
@ -336,7 +336,7 @@ public class PlayState extends LevelState {
private void pelletEaten(float x, float y) {
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++;
pelletsRemaining--;
@ -377,7 +377,7 @@ public class PlayState extends LevelState {
ghost.currentBehaviour = ghost.frightBehaviour;
ghost.reverse = true;
}
frightTimer = Modifiers.getFrightTime(round);
frightTimer = RoundModifiers.getFrightTime(round);
ghostsCaught = 0;
score += 50;
@ -385,7 +385,7 @@ public class PlayState extends LevelState {
}
private void pacmanCaught() {
game.sound.stopLooping(Sound.Effect.SIREN);
game.sound.stopLooping(SoundManager.Effect.SIREN);
pacman.moving = false;
pelletsEatenSinceDeath = 0;
pelletsEatenSinceDeathCounterEnabled = true;
@ -402,7 +402,7 @@ public class PlayState extends LevelState {
ghostsCaught++;
score += ghostsCaught * 200;
game.sound.play(Sound.Effect.EAT_GHOST);
game.sound.play(SoundManager.Effect.EAT_GHOST);
setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT);
}
@ -412,7 +412,7 @@ public class PlayState extends LevelState {
if (scatterChaseTimer <= 0) {
scatter = !scatter;
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) {
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!
// We'll get him...
for (int i = 1; i < 4; i++) {