Skip to content

State Management

JGugino edited this page Jun 30, 2020 · 2 revisions

State Management

State Management inside of Luci2D is all handled by the StateManager class which handles all the heavy lifting of switching between states and making sure the right GameObject's and UIObject's are actived, and all you have to do is call a couple of methods.

Table of Contents

StateManager

Method Description Return
addState() Adds a new GameState to a HashMap<Integer, GameState> of disabled states with specified. void
removeState() Removes states based on specified ID. void
setActiveState() Sets the active state based on the specified ID, also enables/disables GameObject's, UIObject's, and ParticleSystem's for the corresponding state. void
getStateByID() Finds a GameState by the specified ID. GameState
getEnabledStates() Returns the HashMap for the enabledStates HashMap<Integer, GameState>
getDisabledStates() Returns the HashMap for the disabledStates HashMap<Integer, GameState>

addState()

Method Arguments
addState(int, GameState) (state ID, GameState)

removeState()

Method Arguments
removeState(int) (stateID)

setActiveState()

Method Arguments
setActiveState(int) (state ID)

getStateByID()

Method Arguments
getStateByID(int) (state ID)

Implementation

To begin using GameState's you need to create a new class that extends GameState. It should look something like this, note your argument, class, and package names may be different.

   package com.demo{

       import com.gugino.engine.states.GameState;

       public class DemoState extends GameState{

           public DemoState(int _id){
               super(_id);
           }

           public void start(GameManager _gm, Renderer _r){

           }

           public void update(GameManager _gm, double _delta){

           }

           public void render(GameManager _gm, Renderer _r){

           }
       }
   }

Once you have a base GameState setup you want to add your newly created state to the StateManager. This has to be done in your base Game class, if you don't have this class created goto to Setting up your project to learn how to set that up then come back here. If you already have this class created the following code will go into the start() method of that base Game class.

   public void start(GameManager _gm, Renderer _r){
       _gm.stateManager.addState(0, new DemoState(0));
       _gm.stateManager.setActiveState(0);
   }

So let's breakdown whats going on here, in the first line we are accessing an instance of the StateManager from the GameManager and then calling the addState() method on the StateManager which takes in the ID for the state and then an instance of the GameState which also takes in a ID due to its constuctor. The next line we are accessing the StateManager again but we are now calling the setActiveState() method which takes in the ID of the state to switch to in this case its set to the newly created state.