Skip to content

Instantly share code, notes, and snippets.

@worthingtonjg
Created December 27, 2018 05:52
Show Gist options
  • Save worthingtonjg/d86ceb62c4f13e2b0fc850cfa29c31ce to your computer and use it in GitHub Desktop.
Save worthingtonjg/d86ceb62c4f13e2b0fc850cfa29c31ce to your computer and use it in GitHub Desktop.
Controller for simple state machine
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