AppStates can be used to keep code manageable by keeping code separate in different state classes, such as game and GUI elements.
Here 3 state classes are written. This is State1.java with some overrides.
// State1.java package mygame; import com.jme3.app.Application; import com.jme3.app.state.AbstractAppState; import com.jme3.app.state.AppStateManager; class State1 extends AbstractAppState{ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); System.out.println("Initialized - 1"); } @Override public void stateAttached(AppStateManager stateManager) { super.stateAttached(stateManager); System.out.println("Attached - 1"); } @Override public void update(float tpf) { super.update(tpf); System.out.println("update - 1"); } @Override public void stateDetached(AppStateManager stateManager) { super.stateDetached(stateManager); System.out.println("Detached - 1"); } @Override public void cleanup() { super.cleanup(); System.out.println("Cleanup - 1"); } }
This is State2.java with some overrides. There are more overrides available.
// State2.java package mygame; import com.jme3.app.Application; import com.jme3.app.state.AbstractAppState; import com.jme3.app.state.AppStateManager; class State2 extends AbstractAppState{ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); System.out.println("Initialized - 2"); } @Override public void stateAttached(AppStateManager stateManager) { super.stateAttached(stateManager); System.out.println("Attached - 2"); } @Override public void update(float tpf) { super.update(tpf); System.out.println("update - 2"); } @Override public void stateDetached(AppStateManager stateManager) { super.stateDetached(stateManager); System.out.println("Detached - 2"); } @Override public void cleanup() { super.cleanup(); System.out.println("Cleanup - 2"); } }
This is State3.java with some overrides. These are just skeletons and they might be used in 2D GUI or parts of 3D scene graph.
// State3.java package mygame; import com.jme3.app.Application; import com.jme3.app.state.AbstractAppState; import com.jme3.app.state.AppStateManager; class State3 extends AbstractAppState{ @Override public void initialize(AppStateManager stateManager, Application app) { super.initialize(stateManager, app); System.out.println("Initialized - 3"); } @Override public void stateAttached(AppStateManager stateManager) { super.stateAttached(stateManager); System.out.println("Attached - 3"); } @Override public void update(float tpf) { super.update(tpf); System.out.println("update - 3"); } @Override public void stateDetached(AppStateManager stateManager) { super.stateDetached(stateManager); System.out.println("Detached - 3"); } @Override public void cleanup() { super.cleanup(); System.out.println("Cleanup - 3"); } }
We can start states, by attaching them to the stateManager.
// *** 1. Start (creating states and starting State1,State2) state1 = new State1(); state2 = new State2(); state3 = new State3(); System.out.println("Attaching state1 and state3."); stateManager.attach(state1); stateManager.attach(state3); // *** 1. End
For this simple example, different states are started and stopped according to this table.
// *** 2. Start (Starting and ending states) State1 active1 = stateManager.getState(State1.class); State2 active2 = stateManager.getState(State2.class); State3 active3 = stateManager.getState(State3.class); if (active1!=null) { System.out.println("state1 active"); } if (active2!=null) { System.out.println("state2 active"); } if (active3!=null) { System.out.println("state3 active"); } counter++; if (counter<60) { System.out.print("counter: " + counter); System.out.println("\tin SimpleUpdate()"); } if (counter==10) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } else if (counter==20) { System.out.println("Detaching state1."); stateManager.detach(stateManager.getState(State1.class)); System.out.println("Attaching state2."); stateManager.attach(state2); } else if (counter==30) { System.out.println("Attaching state3."); stateManager.attach(state3); } else if (counter==40) { System.out.println("Detaching state2."); stateManager.detach(stateManager.getState(State2.class)); } else if (counter==50) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } // *** 2. End
// JMonkey41.java package mygame; import com.jme3.app.SimpleApplication; public class JMonkey41 extends SimpleApplication { private int counter = 0; private State1 state1; private State2 state2; private State3 state3; public static void main(String[] args) { JMonkey41 app = new JMonkey41(); app.start(); } @Override public void simpleInitApp() { // *** 1. Start (creating states and starting State1,State2) state1 = new State1(); state2 = new State2(); state3 = new State3(); System.out.println("Attaching state1 and state3."); stateManager.attach(state1); stateManager.attach(state3); // *** 1. End } @Override public void simpleUpdate(float tpf) { // *** 2. Start (Starting and ending states) State1 active1 = stateManager.getState(State1.class); State2 active2 = stateManager.getState(State2.class); State3 active3 = stateManager.getState(State3.class); if (active1!=null) { System.out.println("state1 active"); } if (active2!=null) { System.out.println("state2 active"); } if (active3!=null) { System.out.println("state3 active"); } counter++; if (counter<60) { System.out.print("counter: " + counter); System.out.println("\tin SimpleUpdate()"); } if (counter==10) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } else if (counter==20) { System.out.println("Detaching state1."); stateManager.detach(stateManager.getState(State1.class)); System.out.println("Attaching state2."); stateManager.attach(state2); } else if (counter==30) { System.out.println("Attaching state3."); stateManager.attach(state3); } else if (counter==40) { System.out.println("Detaching state2."); stateManager.detach(stateManager.getState(State2.class)); } else if (counter==50) { System.out.println("Detaching state3."); stateManager.detach(stateManager.getState(State3.class)); } // *** 2. End } }
No comments:
Post a Comment