protected IEnumerator ChangeStateCoroutine(AIBehaviourState newBehaviourState, float timeToStayInNewState = -1, float delay = -1) { if (newBehaviourState == currentBehaviourState) { yield break; } float timeToWait = delay; float timeRemainingInNewState = timeToStayInNewState; while (timeToWait > 0) { timeToWait -= Time.deltaTime; yield return(null); } //Switch to new SwitchState(newBehaviourState); while (timeRemainingInNewState > 0) { timeRemainingInNewState -= Time.deltaTime; yield return(null); } if (timeRemainingInNewState < 0 && timeToStayInNewState > 0) { //Switch back to previous SwitchState(previousBehaviourState); } }
protected void SwitchState(AIBehaviourState newBehaviourState) { if (newBehaviourState == currentBehaviourState) { return; } previousBehaviourState = currentBehaviourState; currentBehaviourState = newBehaviourState; }
public virtual void Reset() { currentSubBehaviour = 0; this.state = AIBehaviourState.Idle; if (subbehaviours != null) { for (int index = 0; index < subbehaviours.Count; index++) { subbehaviours[index].Reset(); } } }
public virtual void OnBehaviourFinish(int finishedBehaviour) { // try to start next behaviour if there is something else // if not, just finish this behaviour if (finishedBehaviour == subbehaviours.Count - 1) { this.state = AIBehaviourState.Finished; } else { this.state = AIBehaviourState.Working; currentSubBehaviour++; } }
public virtual void IterateSubBehaviours(GameTime gameTime) { if (subbehaviours.Count > 0) { // check state of the current subbehaviour if (subbehaviours[currentSubBehaviour].State == AIBehaviourState.Idle || subbehaviours[currentSubBehaviour].State == AIBehaviourState.Working) { this.state = AIBehaviourState.Working; subbehaviours[currentSubBehaviour].Iterate(gameTime); } else { if (subbehaviours[currentSubBehaviour].State == AIBehaviourState.Failed) { this.OnBehaviourFailure(currentSubBehaviour); } else if (subbehaviours[currentSubBehaviour].State == AIBehaviourState.Finished) { this.OnBehaviourFinish(currentSubBehaviour); } } } }
public void ChangeState(AIBehaviourState newBehaviourState, float timeToStayInNewState = -1, float delay = -1) { // if (changingStateCoroutine != null) StopCoroutine(changingStateCoroutine); changingStateCoroutine = StartCoroutine(ChangeStateCoroutine(newBehaviourState, timeToStayInNewState, delay)); }
public AIBehaviour() { state = AIBehaviourState.Idle; currentSubBehaviour = 0; }
public virtual void OnBehaviourFailure(int failedBehaviour) { this.state = AIBehaviourState.Failed; }