Created
December 27, 2018 05:52
-
-
Save worthingtonjg/d86ceb62c4f13e2b0fc850cfa29c31ce to your computer and use it in GitHub Desktop.
Controller for simple state machine
This file contains hidden or 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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class StateController<T> : MonoBehaviour where T : BaseState { | |
protected T currentState; | |
public void ChangeState(T newState) { | |
#if STATEMACHINE_DEBUG | |
Debug.Log ("[State Controller] Setting new state: " + newState); | |
#endif | |
if (this.currentState != null) { | |
this.PrepareStateForDeinit(this.currentState); | |
this.currentState.DeinitState(); | |
} | |
this.currentState = newState; | |
if (this.currentState != null) { | |
this.PrepareStateForInit(this.currentState); | |
this.currentState.InitState(); | |
} | |
} | |
protected virtual void Update () { | |
if (this.currentState != null) { | |
this.currentState.UpdateState(); | |
} | |
} | |
protected virtual void PrepareStateForDeinit (T state) { | |
} | |
protected virtual void PrepareStateForInit (T state) { | |
} | |
protected virtual void HandleMessage() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment