This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Voxel-Build 115: Extract logic into States
Every screen update and render has moved into a State, to reduce the amount of code present in the global update and render functions and to keep things clean.
- Loading branch information
Showing
11 changed files
with
337 additions
and
210 deletions.
There are no files selected for viewing
119 changes: 25 additions & 94 deletions
119
src/main/java/net/guerra24/voxel/client/core/GlobalStates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,130 +1,61 @@ | ||
package net.guerra24.voxel.client.core; | ||
|
||
import java.util.Random; | ||
|
||
import net.guerra24.voxel.client.api.API; | ||
import net.guerra24.voxel.client.core.states.GameSPState; | ||
import net.guerra24.voxel.client.core.states.InPauseState; | ||
import net.guerra24.voxel.client.core.states.LoadingState; | ||
import net.guerra24.voxel.client.core.states.MainMenuState; | ||
import net.guerra24.voxel.client.core.states.OptionsState; | ||
import net.guerra24.voxel.client.graphics.opengl.Display; | ||
import net.guerra24.voxel.client.input.Keyboard; | ||
import net.guerra24.voxel.client.menu.MainMenu; | ||
import net.guerra24.voxel.client.menu.OptionsMenu; | ||
import net.guerra24.voxel.client.menu.PauseMenu; | ||
import net.guerra24.voxel.client.resources.GameResources; | ||
import net.guerra24.voxel.client.resources.Loader; | ||
import net.guerra24.voxel.client.world.WorldsHandler; | ||
import net.guerra24.voxel.universal.util.vector.Vector3f; | ||
|
||
public class GlobalStates { | ||
|
||
public boolean loop = false; | ||
private GameState state; | ||
|
||
private MainMenu mainMenu; | ||
private PauseMenu pauseMenu; | ||
private OptionsMenu optionsMenu; | ||
public GameState state; | ||
|
||
public enum GameState { | ||
GAME_SP, MAINMENU, IN_PAUSE, LOADING_WORLD, OPTIONS; | ||
GAME_SP(new GameSPState()), | ||
MAINMENU(new MainMenuState()), | ||
IN_PAUSE(new InPauseState()), | ||
LOADING_WORLD(new LoadingState()), | ||
OPTIONS(new OptionsState()); | ||
|
||
GameState(State state) { | ||
this.state = state; | ||
} | ||
|
||
State state; | ||
} | ||
|
||
public GlobalStates(Loader loader) { | ||
mainMenu = new MainMenu(loader); | ||
pauseMenu = new PauseMenu(); | ||
optionsMenu = new OptionsMenu(); | ||
loop = true; | ||
state = GameState.MAINMENU; | ||
} | ||
|
||
public void updateUpdateThread(GameResources gm, WorldsHandler worlds, API api, Display display) { | ||
|
||
if (state == GameState.MAINMENU && mainMenu.getPlayButton().pressed()) { | ||
state = GameState.LOADING_WORLD; | ||
Random seed; | ||
if (VoxelVariables.isCustomSeed) { | ||
seed = new Random(VoxelVariables.seed.hashCode()); | ||
} else { | ||
seed = new Random(); | ||
} | ||
worlds.getActiveWorld().startWorld("World-0", seed, 0, api, gm); | ||
gm.getCamera().setMouse(); | ||
gm.getSoundSystem().stop("menu1"); | ||
gm.getSoundSystem().rewind("menu1"); | ||
state = GameState.GAME_SP; | ||
} | ||
|
||
if (state == GameState.MAINMENU && mainMenu.getExitButton().pressed()) { | ||
loop = false; | ||
} | ||
|
||
if (state == GameState.IN_PAUSE && pauseMenu.getBackToMain().pressed()) { | ||
worlds.getActiveWorld().clearDimension(gm); | ||
gm.getSoundSystem().play("menu1"); | ||
gm.getCamera().setPosition(new Vector3f(0, 0, 1)); | ||
gm.getCamera().setPitch(0); | ||
gm.getCamera().setYaw(0); | ||
state = GameState.MAINMENU; | ||
gm.getSoundSystem().setVolume("menu1", 1f); | ||
} | ||
|
||
if (state == GameState.MAINMENU && mainMenu.getOptionsButton().pressed()) { | ||
gm.getCamera().setPosition(new Vector3f(-1.4f, -3.4f, 1.4f)); | ||
state = GameState.OPTIONS; | ||
} | ||
|
||
if (state == GameState.OPTIONS && optionsMenu.getExitButton().pressed()) { | ||
gm.getCamera().setPosition(new Vector3f(0, 0, 1)); | ||
state = GameState.MAINMENU; | ||
} | ||
if (state == GameState.MAINMENU) { | ||
if (mainMenu.getPlayButton().insideButton()) | ||
mainMenu.getList().get(0).changeScale(0.074f); | ||
else | ||
mainMenu.getList().get(0).changeScale(0.07f); | ||
if (mainMenu.getExitButton().insideButton()) | ||
mainMenu.getList().get(2).changeScale(0.074f); | ||
else | ||
mainMenu.getList().get(2).changeScale(0.07f); | ||
if (mainMenu.getOptionsButton().insideButton()) | ||
mainMenu.getList().get(1).changeScale(0.074f); | ||
else | ||
mainMenu.getList().get(1).changeScale(0.07f); | ||
|
||
} | ||
|
||
if (state == GameState.OPTIONS) { | ||
if (optionsMenu.getExitButton().insideButton()) | ||
mainMenu.getList().get(3).changeScale(0.074f); | ||
else | ||
mainMenu.getList().get(3).changeScale(0.07f); | ||
} | ||
|
||
if (state == GameState.GAME_SP && !display.isDisplayFocused() && !VoxelVariables.debug) { | ||
gm.getCamera().unlockMouse(); | ||
state = GameState.IN_PAUSE; | ||
} | ||
|
||
public void doUpdate(Voxel voxel, float delta) { | ||
|
||
state.state.update(voxel, this, delta); | ||
|
||
if (Display.isCloseRequested()) | ||
loop = false; | ||
|
||
while (Keyboard.next()) { | ||
if (state == GameState.GAME_SP && Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { | ||
gm.getCamera().unlockMouse(); | ||
voxel.getGameResources().getCamera().unlockMouse(); | ||
state = GameState.IN_PAUSE; | ||
} else if (state == GameState.IN_PAUSE && Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) { | ||
gm.getCamera().setMouse(); | ||
voxel.getGameResources().getCamera().setMouse(); | ||
state = GameState.GAME_SP; | ||
} | ||
} | ||
} | ||
|
||
public void updateRenderThread(GameResources gm, WorldsHandler worlds, API api, Display display) { | ||
public void doRender(Voxel voxel, float delta) { | ||
state.state.render(voxel, this, delta); | ||
} | ||
|
||
public GameState getState() { | ||
return state; | ||
} | ||
|
||
public MainMenu getMainMenu() { | ||
return mainMenu; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.guerra24.voxel.client.core; | ||
|
||
public interface State { | ||
|
||
void update(Voxel voxel, GlobalStates states, float delta); | ||
|
||
void render(Voxel voxel, GlobalStates states, float delta); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.