Created
November 12, 2012 03:19
-
-
Save benloong/4057307 to your computer and use it in GitHub Desktop.
simple FSM using delegate and coroutine
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 UnityEngine; | |
using System.Collections.Generic; | |
public class SimpleFSM : MonoBehaviour { | |
public delegate IEnumerator StateHandler(); | |
public enum State { | |
Idle, | |
Attack, | |
PullBack, | |
} | |
Dictionary<State, StateHandler> _stateHandlers = new Dictionary<State, StateHandler>(); | |
State _current; | |
bool isAlive = true; | |
// Use this for initialization | |
IEnumerator Start () { | |
_stateHandlers[State.Idle] = Idle; | |
_stateHandlers[State.Attack] = Attack; | |
_stateHandlers[State.PullBack] = PullBack; | |
while(isAlive) { | |
Debug.Log("Updating"); | |
yield return StartCoroutine(_stateHandlers[_current]()); | |
} | |
} | |
IEnumerator Idle() { | |
Debug.Log("===========Enter Idle state==========="); | |
while(_current == State.Idle) { | |
//Debug.Log("IDLE"); | |
yield return null; | |
} | |
Debug.Log("===========Exit Idle state==========="); | |
yield return null; | |
} | |
IEnumerator Attack() { | |
Debug.Log("===========Enter Attack state==========="); | |
while(_current == State.Attack) { | |
//Debug.Log("Attack"); | |
yield return null; | |
} | |
Debug.Log("===========Exit Attack state==========="); | |
yield return null; | |
} | |
IEnumerator PullBack() { | |
Debug.Log("===========Enter PullBack state==========="); | |
while(_current == State.PullBack) { | |
//Debug.Log("PullBack"); | |
yield return null; | |
} | |
Debug.Log("===========Exit PullBack state==========="); | |
yield return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment