Removed static fields in Game state.

Mostly merged from #8 (72e8a7e).

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-14 23:57:50 -05:00
parent 64d961a8f9
commit 03095733df
5 changed files with 36 additions and 33 deletions

View File

@ -250,11 +250,10 @@ public class GameScore {
/** /**
* Loads all game score images. * Loads all game score images.
* @param dir the image directory
* @throws SlickException * @throws SlickException
*/ */
public void loadImages() throws SlickException { public void loadImages(File dir) throws SlickException {
File dir = MusicController.getOsuFile().getFile().getParentFile();
// combo burst images // combo burst images
if (comboBurstImages != null) { if (comboBurstImages != null) {
for (int i = 0; i < comboBurstImages.length; i++) { for (int i = 0; i < comboBurstImages.length; i++) {

View File

@ -68,9 +68,9 @@ public class Game extends BasicGameState {
RESTART_LOSE = 3; // health is zero: no-continue/force restart RESTART_LOSE = 3; // health is zero: no-continue/force restart
/** /**
* Current restart state. * Minimum time before start of song, in milliseconds, to process skip-related actions.
*/ */
private static byte restart; private static final int SKIP_OFFSET = 2000;
/** /**
* The associated OsuFile object. * The associated OsuFile object.
@ -80,7 +80,7 @@ public class Game extends BasicGameState {
/** /**
* The associated GameScore object (holds all score data). * The associated GameScore object (holds all score data).
*/ */
private static GameScore score; private GameScore score;
/** /**
* Current hit object index in OsuHitObject[] array. * Current hit object index in OsuHitObject[] array.
@ -117,6 +117,11 @@ public class Game extends BasicGameState {
*/ */
private int[] hitResultOffset; private int[] hitResultOffset;
/**
* Current restart state.
*/
private byte restart;
/** /**
* Current break index in breaks ArrayList. * Current break index in breaks ArrayList.
*/ */
@ -137,11 +142,6 @@ public class Game extends BasicGameState {
*/ */
private MenuButton skipButton; private MenuButton skipButton;
/**
* Minimum time before start of song, in milliseconds, to process skip-related actions.
*/
private static final int SKIP_OFFSET = 2000;
/** /**
* Current timing point index in timingPoints ArrayList. * Current timing point index in timingPoints ArrayList.
*/ */
@ -222,6 +222,7 @@ public class Game extends BasicGameState {
// create the associated GameScore object // create the associated GameScore object
score = new GameScore(width, height); score = new GameScore(width, height);
((GameRanking) game.getState(Opsu.STATE_GAMERANKING)).setGameScore(score);
// playfield background // playfield background
try { try {
@ -874,7 +875,7 @@ public class Game extends BasicGameState {
// load other images... // load other images...
((GamePauseMenu) game.getState(Opsu.STATE_GAMEPAUSEMENU)).loadImages(); ((GamePauseMenu) game.getState(Opsu.STATE_GAMEPAUSEMENU)).loadImages();
score.loadImages(); score.loadImages(osu.getFile().getParentFile());
} }
/** /**
@ -943,13 +944,8 @@ public class Game extends BasicGameState {
/** /**
* Sets/returns whether entering the state will restart it. * Sets/returns whether entering the state will restart it.
*/ */
public static void setRestart(byte restart) { Game.restart = restart; } public void setRestart(byte restart) { this.restart = restart; }
public static byte getRestart() { return Game.restart; } public byte getRestart() { return restart; }
/**
* Returns the associated GameScore object.
*/
public static GameScore getGameScore() { return score; }
/** /**
* Returns whether or not the track is in the lead-in time state. * Returns whether or not the track is in the lead-in time state.

View File

@ -66,6 +66,7 @@ public class GamePauseMenu extends BasicGameState {
private StateBasedGame game; private StateBasedGame game;
private Input input; private Input input;
private int state; private int state;
private Game gameState;
public GamePauseMenu(int state) { public GamePauseMenu(int state) {
this.state = state; this.state = state;
@ -76,20 +77,21 @@ public class GamePauseMenu extends BasicGameState {
throws SlickException { throws SlickException {
this.container = container; this.container = container;
this.game = game; this.game = game;
input = container.getInput(); this.input = container.getInput();
this.gameState = (Game) game.getState(Opsu.STATE_GAME);
} }
@Override @Override
public void render(GameContainer container, StateBasedGame game, Graphics g) public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException { throws SlickException {
// background // background
if (Game.getRestart() != Game.RESTART_LOSE) if (gameState.getRestart() != Game.RESTART_LOSE)
GameImage.PAUSE_OVERLAY.getImage().draw(); GameImage.PAUSE_OVERLAY.getImage().draw();
else else
GameImage.FAIL_BACKGROUND.getImage().draw(); GameImage.FAIL_BACKGROUND.getImage().draw();
// draw buttons // draw buttons
if (Game.getRestart() != Game.RESTART_LOSE) if (gameState.getRestart() != Game.RESTART_LOSE)
continueButton.draw(); continueButton.draw();
retryButton.draw(); retryButton.draw();
backButton.draw(); backButton.draw();
@ -124,21 +126,21 @@ public class GamePauseMenu extends BasicGameState {
switch (key) { switch (key) {
case Input.KEY_ESCAPE: case Input.KEY_ESCAPE:
// 'esc' will normally unpause, but will return to song menu if health is zero // 'esc' will normally unpause, but will return to song menu if health is zero
if (Game.getRestart() == Game.RESTART_LOSE) { if (gameState.getRestart() == Game.RESTART_LOSE) {
MusicController.stop(); MusicController.stop();
MusicController.playAt(MusicController.getOsuFile().previewTime, true); MusicController.playAt(MusicController.getOsuFile().previewTime, true);
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else { } else {
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
Game.setRestart(Game.RESTART_FALSE); gameState.setRestart(Game.RESTART_FALSE);
game.enterState(Opsu.STATE_GAME); game.enterState(Opsu.STATE_GAME);
} }
break; break;
case Input.KEY_R: case Input.KEY_R:
// restart // restart
if (input.isKeyDown(Input.KEY_RCONTROL) || input.isKeyDown(Input.KEY_LCONTROL)) { if (input.isKeyDown(Input.KEY_RCONTROL) || input.isKeyDown(Input.KEY_LCONTROL)) {
Game.setRestart(Game.RESTART_MANUAL); gameState.setRestart(Game.RESTART_MANUAL);
game.enterState(Opsu.STATE_GAME); game.enterState(Opsu.STATE_GAME);
} }
break; break;
@ -153,7 +155,7 @@ public class GamePauseMenu extends BasicGameState {
if (button == Input.MOUSE_MIDDLE_BUTTON) if (button == Input.MOUSE_MIDDLE_BUTTON)
return; return;
boolean loseState = (Game.getRestart() == Game.RESTART_LOSE); boolean loseState = (gameState.getRestart() == Game.RESTART_LOSE);
// if music faded out (i.e. health is zero), don't process any actions before FADEOUT_TIME // if music faded out (i.e. health is zero), don't process any actions before FADEOUT_TIME
if (loseState && System.currentTimeMillis() - pauseStartTime < FADEOUT_TIME) if (loseState && System.currentTimeMillis() - pauseStartTime < FADEOUT_TIME)
@ -161,11 +163,11 @@ public class GamePauseMenu extends BasicGameState {
if (continueButton.contains(x, y) && !loseState) { if (continueButton.contains(x, y) && !loseState) {
SoundController.playSound(SoundEffect.MENUBACK); SoundController.playSound(SoundEffect.MENUBACK);
Game.setRestart(Game.RESTART_FALSE); gameState.setRestart(Game.RESTART_FALSE);
game.enterState(Opsu.STATE_GAME); game.enterState(Opsu.STATE_GAME);
} else if (retryButton.contains(x, y)) { } else if (retryButton.contains(x, y)) {
SoundController.playSound(SoundEffect.MENUHIT); SoundController.playSound(SoundEffect.MENUHIT);
Game.setRestart(Game.RESTART_MANUAL); gameState.setRestart(Game.RESTART_MANUAL);
game.enterState(Opsu.STATE_GAME); game.enterState(Opsu.STATE_GAME);
} else if (backButton.contains(x, y)) { } else if (backButton.contains(x, y)) {
MusicController.pause(); // lose state MusicController.pause(); // lose state
@ -179,7 +181,7 @@ public class GamePauseMenu extends BasicGameState {
public void enter(GameContainer container, StateBasedGame game) public void enter(GameContainer container, StateBasedGame game)
throws SlickException { throws SlickException {
pauseStartTime = System.currentTimeMillis(); pauseStartTime = System.currentTimeMillis();
if (Game.getRestart() == Game.RESTART_LOSE) { if (gameState.getRestart() == Game.RESTART_LOSE) {
MusicController.fadeOut(FADEOUT_TIME); MusicController.fadeOut(FADEOUT_TIME);
SoundController.playSound(SoundEffect.FAIL); SoundController.playSound(SoundEffect.FAIL);
} else } else

View File

@ -75,8 +75,6 @@ public class GameRanking extends BasicGameState {
this.game = game; this.game = game;
this.input = container.getInput(); this.input = container.getInput();
score = Game.getGameScore();
int width = container.getWidth(); int width = container.getWidth();
int height = container.getHeight(); int height = container.getHeight();
@ -170,7 +168,7 @@ public class GameRanking extends BasicGameState {
if (retryButton.contains(x, y)) { if (retryButton.contains(x, y)) {
OsuFile osu = MusicController.getOsuFile(); OsuFile osu = MusicController.getOsuFile();
Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString())); Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString()));
Game.setRestart(Game.RESTART_MANUAL); ((Game) game.getState(Opsu.STATE_GAME)).setRestart(Game.RESTART_MANUAL);
SoundController.playSound(SoundEffect.MENUHIT); SoundController.playSound(SoundEffect.MENUHIT);
game.enterState(Opsu.STATE_GAME, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); game.enterState(Opsu.STATE_GAME, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else if (exitButton.contains(x, y)) { } else if (exitButton.contains(x, y)) {
@ -192,4 +190,12 @@ public class GameRanking extends BasicGameState {
Utils.getBackButton().setScale(1f); Utils.getBackButton().setScale(1f);
SoundController.playSound(SoundEffect.APPLAUSE); SoundController.playSound(SoundEffect.APPLAUSE);
} }
/**
* Sets the associated GameScore object.
* @param score the GameScore
*/
public void setGameScore(GameScore score) {
this.score = score;
}
} }

View File

@ -712,7 +712,7 @@ public class SongMenu extends BasicGameState {
Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString())); Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString()));
OsuParser.parseHitObjects(osu); OsuParser.parseHitObjects(osu);
HitSound.setSampleSet(osu.sampleSet); HitSound.setSampleSet(osu.sampleSet);
Game.setRestart(Game.RESTART_NEW); ((Game) game.getState(Opsu.STATE_GAME)).setRestart(Game.RESTART_NEW);
game.enterState(Opsu.STATE_GAME, new FadeOutTransition(Color.black), new FadeInTransition(Color.black)); game.enterState(Opsu.STATE_GAME, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} }
} }