From f655fe2448773cdad3811919ef3082cab8ce9af3 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Wed, 22 Jan 2020 02:40:36 +0400 Subject: [PATCH] Refactors --- ...{HighScores.java => HighScoreManager.java} | 4 +- core/src/com/me/pacman/PacDude.java | 8 ++-- .../Modifiers.java => RoundModifiers.java} | 16 ++++---- .../pacman/{Sound.java => SoundManager.java} | 4 +- core/src/com/me/pacman/entity/Blinky.java | 10 ++--- core/src/com/me/pacman/entity/Ghost.java | 10 ++--- core/src/com/me/pacman/entity/Pacman.java | 6 +-- core/src/com/me/pacman/state/MenuState.java | 8 ++-- core/src/com/me/pacman/state/PlayState.java | 38 +++++++++---------- 9 files changed, 51 insertions(+), 53 deletions(-) rename core/src/com/me/pacman/{HighScores.java => HighScoreManager.java} (96%) rename core/src/com/me/pacman/{level/Modifiers.java => RoundModifiers.java} (95%) rename core/src/com/me/pacman/{Sound.java => SoundManager.java} (98%) diff --git a/core/src/com/me/pacman/HighScores.java b/core/src/com/me/pacman/HighScoreManager.java similarity index 96% rename from core/src/com/me/pacman/HighScores.java rename to core/src/com/me/pacman/HighScoreManager.java index 192113a..30dddb5 100644 --- a/core/src/com/me/pacman/HighScores.java +++ b/core/src/com/me/pacman/HighScoreManager.java @@ -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 scores = new Array<>(10); - public HighScores() { + public HighScoreManager() { file = Gdx.files.local(FILE); loadScores(); } diff --git a/core/src/com/me/pacman/PacDude.java b/core/src/com/me/pacman/PacDude.java index bb96e4c..e6b9ea5 100644 --- a/core/src/com/me/pacman/PacDude.java +++ b/core/src/com/me/pacman/PacDude.java @@ -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)); diff --git a/core/src/com/me/pacman/level/Modifiers.java b/core/src/com/me/pacman/RoundModifiers.java similarity index 95% rename from core/src/com/me/pacman/level/Modifiers.java rename to core/src/com/me/pacman/RoundModifiers.java index e94f444..8b57052 100644 --- a/core/src/com/me/pacman/level/Modifiers.java +++ b/core/src/com/me/pacman/RoundModifiers.java @@ -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; diff --git a/core/src/com/me/pacman/Sound.java b/core/src/com/me/pacman/SoundManager.java similarity index 98% rename from core/src/com/me/pacman/Sound.java rename to core/src/com/me/pacman/SoundManager.java index 537b7de..f3917c8 100644 --- a/core/src/com/me/pacman/Sound.java +++ b/core/src/com/me/pacman/SoundManager.java @@ -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]; diff --git a/core/src/com/me/pacman/entity/Blinky.java b/core/src/com/me/pacman/entity/Blinky.java index 2cef977..efa0456 100644 --- a/core/src/com/me/pacman/entity/Blinky.java +++ b/core/src/com/me/pacman/entity/Blinky.java @@ -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(); diff --git a/core/src/com/me/pacman/entity/Ghost.java b/core/src/com/me/pacman/entity/Ghost.java index 2d1c0e8..d509338 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -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) { diff --git a/core/src/com/me/pacman/entity/Pacman.java b/core/src/com/me/pacman/entity/Pacman.java index d0c1f66..21d4e3d 100644 --- a/core/src/com/me/pacman/entity/Pacman.java +++ b/core/src/com/me/pacman/entity/Pacman.java @@ -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); diff --git a/core/src/com/me/pacman/state/MenuState.java b/core/src/com/me/pacman/state/MenuState.java index b189990..475f277 100644 --- a/core/src/com/me/pacman/state/MenuState.java +++ b/core/src/com/me/pacman/state/MenuState.java @@ -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) { diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index c4333b6..809a007 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -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++) {