/// /// SURGE FRAMEWORK /// Author: Bob Berkebile /// Email: bobb@pixelplacement.com /// /// Base class for States to be used as children of StateMachines. /// /// using UnityEngine; using System.Collections; namespace Pixelplacement { public class State : MonoBehaviour { //Public Properties: /// /// Gets a value indicating whether this instance is the first state in this state machine. /// public bool IsFirst { get { return transform.GetSiblingIndex () == 0; } } /// /// Gets a value indicating whether this instance is the last state in this state machine. /// public bool IsLast { get { return transform.GetSiblingIndex () == transform.parent.childCount - 1; } } /// /// Gets or sets the state machine. /// public StateMachine StateMachine { get { if (_stateMachine == null) { _stateMachine = transform.parent.GetComponent(); if (_stateMachine == null) { Debug.LogError("States must be the child of a StateMachine to operate."); return null; } } return _stateMachine; } } //Private Variables: StateMachine _stateMachine; //Public Methods /// /// Changes the state. /// public void ChangeState(int childIndex) { StateMachine.ChangeState(childIndex); } /// /// Changes the state. /// public void ChangeState (GameObject state) { StateMachine.ChangeState (state.name); } /// /// Changes the state. /// public void ChangeState (string state) { StateMachine.ChangeState (state); } /// /// Change to the next state if possible. /// public GameObject Next (bool exitIfLast = false) { return StateMachine.Next (exitIfLast); } /// /// Change to the previous state if possible. /// public GameObject Previous (bool exitIfFirst = false) { return StateMachine.Previous (exitIfFirst); } /// /// Exit the current state. /// public void Exit () { StateMachine.Exit (); } protected Coroutine StartCoroutineIfActive(IEnumerator coroutine) { if (gameObject.activeInHierarchy) { return StartCoroutine(coroutine); } else { return null; } } } }