Spielzustand-Automaten
Aus Spieleprogrammierer-Wiki
(Unterschied zwischen Versionen)
[unmarkierte Version] | [unmarkierte Version] |
(→StateManager) |
(→StateManager) |
||
Zeile 318: | Zeile 318: | ||
_currentState.Render(); | _currentState.Render(); | ||
} | } | ||
+ | } | ||
+ | </sourcecode> | ||
+ | }} | ||
+ | |||
+ | {{Spoiler| | ||
+ | <xh4>Java</xh4> | ||
+ | | | ||
+ | <sourcecode lang="java" tab="4"> | ||
+ | import java.util.HashMap; | ||
+ | |||
+ | public class StateManager { | ||
+ | // Speichert alle Zustände als Assoziatives Array | ||
+ | private HashMap<String, State> _statesById; | ||
+ | // Aktueller Zustand | ||
+ | private State _currentState; | ||
+ | |||
+ | // erzeugt ein StateManager-Objekt | ||
+ | public StateManager() { | ||
+ | _statesById = new HashMap<String, State>(); | ||
+ | } | ||
+ | |||
+ | // Registriert einen Zustand für den Manager an Hand der ID. Diese muss | ||
+ | // eindeutig und nicht leer sein. | ||
+ | public void AddState(String id, State state) { | ||
+ | if (id == null || id.equals("")) | ||
+ | throw new IllegalArgumentException("Id has to be a valid string"); | ||
+ | if (state == null) | ||
+ | throw new NullPointerException("state"); | ||
+ | if (_statesById.containsKey(id)) | ||
+ | throw new IllegalArgumentException("State with ID " + id + " already exists."); | ||
+ | |||
+ | _statesById.put(id, state); | ||
+ | } | ||
+ | |||
+ | // Setzt den aktuellen Zustand | ||
+ | public void SetState(String id) { | ||
+ | State nextState = _statesById.get(id); | ||
+ | // Wenn der Zustand im Manager registriert ist | ||
+ | if (nextState != null) { | ||
+ | // und nicht der aktuelle ist | ||
+ | if (nextState != _currentState) { | ||
+ | // Rufe OnLeave() vom alten Zustand auf, falls es einen gab | ||
+ | if (_currentState != null) { | ||
+ | _currentState.onLeave(); | ||
+ | } | ||
+ | |||
+ | // setze den neuen aktuellen Zustand | ||
+ | _currentState = nextState; | ||
+ | |||
+ | // Initialisiere, wenn noch nicht getan | ||
+ | if (!_currentState.isInitialized()) { | ||
+ | _currentState.initialize(); | ||
+ | } | ||
+ | |||
+ | // Da zu einem neuen Zustand gewechselt worden ist, rufe das Eingangsereignis auf | ||
+ | _currentState.onEnter(); | ||
+ | } | ||
+ | } else { | ||
+ | // ansonsten gibt es diesen Zustand nicht! | ||
+ | throw new IllegalArgumentException("State with Id " + id + " does not exist."); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Aktualisert den Spielzustand. der Rückgabewert gibt an, ob das Programm weiterläuft. | ||
+ | // Es läuft nicht weiter, wenn der aktuelle Zustand verlassen und kein weiterer Zustand zugewiesen wird. | ||
+ | public boolean Update(float elapsedTime) { | ||
+ | // Es muss ein Zustand gesetzt sein! | ||
+ | if (_currentState == null) | ||
+ | throw new IllegalArgumentException("Current state not set."); | ||
+ | |||
+ | // aktualisere den Zustand und schaue, ob der Zustand eventuell verlassen werden soll | ||
+ | boolean running = _currentState.onUpdate(elapsedTime); | ||
+ | |||
+ | // wenn er verlassen werden soll und der Folgezustand gesetzt ist | ||
+ | String nextState = _currentState.getNextState(); | ||
+ | if (!running && (nextState == null || nextState.equals(""))) { | ||
+ | // Wechsle den Zustand | ||
+ | SetState(nextState); | ||
+ | |||
+ | // Programm läuft weiter | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | // | ||
+ | return running; | ||
+ | } | ||
+ | |||
+ | // Zeichnet den aktuellen Zustand. | ||
+ | public void Render() { | ||
+ | // Es muss ein Zustand gesetzt sein! | ||
+ | if (_currentState == null) | ||
+ | throw new IllegalArgumentException("Current state not set."); | ||
+ | |||
+ | // Zeichne den aktuellen Zustand | ||
+ | _currentState.onRender(); | ||
+ | } | ||
} | } | ||
</sourcecode> | </sourcecode> |
Version vom 7. November 2011, 13:44 Uhr
Klicke hier, um diese Version anzusehen.