public void AddState(BasicAIState newState) { if (states.ContainsKey(newState.StateType)) { Debug.LogWarning("Trying to add duplicate state: " + newState.StateType.ToString() + " to " + GetBaseEntity().PrefabName); return; } newState.brain = this; newState.Reset(); states.Add(newState.StateType, newState); }
public virtual void Think(float delta) { if (!AI.think) { return; } lastThinkTime = UnityEngine.Time.time; if (sleeping) { return; } Age += delta; if (UseAIDesign) { Senses.Update(); UpdateGroup(); } if (CurrentState != null) { UpdateAgressionTimer(delta); StateStatus stateStatus = CurrentState.StateThink(delta); if (Events != null) { Events.Tick(delta, stateStatus); } } if (UseAIDesign || (CurrentState != null && !CurrentState.CanLeave())) { return; } float num = 0f; BasicAIState basicAIState = null; foreach (BasicAIState value in states.Values) { if (value != null && value.CanEnter()) { float weight = value.GetWeight(); if (weight > num) { num = weight; basicAIState = value; } } } if (basicAIState != CurrentState) { SwitchToState(basicAIState); } }
public bool SwitchToState(BasicAIState newState, int stateContainerID = -1) { if (newState == null || !newState.CanEnter()) { return(false); } if (CurrentState != null) { if (!CurrentState.CanLeave()) { return(false); } if (CurrentState == newState && !UseAIDesign) { return(false); } CurrentState.StateLeave(); } CurrentState = newState; CurrentState.StateEnter(); currentStateContainerID = stateContainerID; AddEvents(stateContainerID); return(true); }