public override void OnInspectorGUI() { aIStatesSO = (AIStatesSO)target; addStateType = (AIStateType)EditorGUILayout.EnumPopup("Add state type:", addStateType); if (GUILayout.Button("Create State")) { switch (addStateType) { case AIStateType.Walk: { aIStatesSO.aiStates.Add(new AI_Walk()); return; } default: return; } } EditorUtility.SetDirty(target); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); base.OnInspectorGUI(); }
public override AIStateType OnUpdate() { AIStateType sharedUpdateStateType = base.SharedUpdate(AIStateType.Alert); if (sharedUpdateStateType != AIStateType.Alert) { return(sharedUpdateStateType); } _alertTimer += Time.deltaTime; if (_alertTimer >= _alertDelay) { _alertTimer = 0f; return(AIStateType.Idle); } if (!_stateMachine.isCrawling && !_alertAnimation) { _turningTimer += Time.deltaTime; if (_turningTimer >= _turningDelay) { _turningTimer = 0f; if (_stateMachine.animator != null) { int whichTurn = Random.Range(0, 2); _stateMachine.animator.SetBool(whichTurn == 0 ? _stateMachine.turningRightHash : _stateMachine.turningLeftHash, true); _stateMachine.animator.SetBool(whichTurn == 0 ? _stateMachine.turningLeftHash : _stateMachine.turningRightHash, false); } } } return(AIStateType.Alert); }
protected virtual void Update() { if (_currentState == null) { return; } AIStateType newAIStateType = _currentState.OnUpdate(); //每次,更新了[AIState] 并初始化掉,起始方法 if (newAIStateType != _currentStateType) { AIState newState = null; if (_states.TryGetValue(newAIStateType, out newState)) { _currentState.OnExitState(); //当前状态退出 newState.OnEnterState(); //new状态进入 _currentState = newState; //当前状态更新 } //增加一层,检验层。状态不见,idle接上. else if (_states.TryGetValue(AIStateType.Idle, out newState)) { _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } _currentState = newState; } }
public override AIStateType OnUpdate() { AIStateType sharedUpdateStateType = base.SharedUpdate(AIStateType.Idle); if (sharedUpdateStateType != AIStateType.Idle) { return(sharedUpdateStateType); } _idleTimer += Time.deltaTime; if (_idleTimer > _idleTime) { _idleTimer = 0f; if (_stateMachine.IsStateEnabled(AIStateType.Patrol)) { return(AIStateType.Patrol); } else { return(AIStateType.Idle); } } return(AIStateType.Idle); }
protected virtual void Update() { if (_currentState == null) { return; } //this is whats pulling out the functions from the sate claas that we are currently assigned AIStateType newStateType = _currentState.OnUpdate(); if (newStateType != _currentStateType) { AIState newState = null; if (_states.TryGetValue(newStateType, out newState)) { //can find correct state _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } else if (_states.TryGetValue(AIStateType.IDLE, out newState)) { //can find correct state _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } _currentStateType = newStateType; } }
// ------------------------------------------------------------------- // Name : Update // Desc : Called by Unity each frame. Gives the current state a // chance to update itself and perform transitions. // ------------------------------------------------------------------- protected virtual void Update() { if (_currentState == null) { return; } AIStateType newStateType = _currentState.OnUpdate(); if (newStateType != _currentStateType) { AIState newState = null; if (_states.TryGetValue(newStateType, out newState)) { _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } else if (_states.TryGetValue(AIStateType.Idle, out newState)) { _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } _currentStateType = newStateType; } }
public override void Init() { knockdownController = GetComponent <AIKnockDownController>(); knockdownController.Init(); currentState = startState; combatState.Init(this); switch (currentState) { case AIStateType.IDLE: idleState.Init(this); break; case AIStateType.PATROL: patrolState.Init(this); break; case AIStateType.COMBAT: break; case AIStateType.DUMMY: break; case AIStateType.SEARCH: break; case AIStateType.KNOCKEDDOWN: break; } searchState.Init(this); }
public override AIStateType OnUpdate() { AIStateType sharedUpdateStateType = base.SharedUpdate(AIStateType.Attacking); if (sharedUpdateStateType != AIStateType.Attacking) { return(sharedUpdateStateType); } if (_stateMachine.currentTarget != null) { float distanceToTarget = (_stateMachine.currentTarget.position - transform.position).magnitude; // If we are far from the target, we return to pursuing if (distanceToTarget > _stateMachine.stoppingDistance) { if (_intersectAttack || !_intersectAttack && !_stateMachine.isAttacking) { return(AIStateType.Pursuit); } } // We keep lerping the zombie towards the character position if (_lookAtThePlayer) { Quaternion destinationRotation = Quaternion.LookRotation(_stateMachine.currentTarget.position - transform.position); transform.rotation = Quaternion.Lerp(transform.rotation, destinationRotation, 2 * Time.deltaTime); } } // If there is no target else { return(AIStateType.Alert); } return(AIStateType.Attacking); }
protected virtual void Update() { if (_currentState == null) { return; } // Try updating the current state and see what the next state it returns AIStateType newStateType = _currentState.OnUpdate(); // If the returned state is different from the current one, then we transition to it. if (newStateType != _currentStateType) { AIState newState = null; if (_states.TryGetValue(newStateType, out newState)) { _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } else if (_states.TryGetValue(AIStateType.Idle, out newState)) { // If we can't find the state, we got back to IDlEing _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } // Update the next state type. _currentStateType = newStateType; } }
// ------------------------------------------------------------------- // Name : Update // Desc : Called by Unity each frame. Gives the current state a // chance to update itself and perform transitions. // ------------------------------------------------------------------- protected virtual void Update() { //Debug.Log("Root Position Ref Count "+_rootPositionRefCount); if (_currentState == null) { return; } AIStateType newStateType = _currentState.OnUpdate(); if (newStateType != _currentStateType) { AIState newState = null; if (_states.TryGetValue(newStateType, out newState)) { _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } else if (_states.TryGetValue(AIStateType.Idle, out newState)) { _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } _currentStateType = newStateType; } }
/// <summary> /// 获取状态 /// </summary> /// <param name="AIState"></param> /// <returns></returns> private StateBase GetState(AIStateType AIState) { if (this.m_StateDic.ContainsKey(AIState)) { return(this.m_StateDic[AIState]); } StateBase state; switch (AIState) { case AIStateType.Attack: state = new AttackState(this.m_ActorBev, this.m_ActorAI); break; case AIStateType.Dead: state = new DeadState(this.m_ActorBev, this.m_ActorAI); break; case AIStateType.Hurt: state = new HurtState(this.m_ActorBev, this.m_ActorAI); break; case AIStateType.Idle: state = new IdleState(this.m_ActorBev, this.m_ActorAI); break; default: Debug.Log(AIState.ToString() + " not exsit "); return(null); } this.m_StateDic.Add(AIState, state); return(state); }
protected void ChangeToNewState(AIStateType newStateType) { if (newStateType != _currentStateType) { AIState newState = null; if (_states.TryGetValue(newStateType, out newState)) { stopAllStates = false; _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } else if (newStateType == AIStateType.None) { stopAllStates = true; } else if (_states.TryGetValue(AIStateType.Idle, out newState)) { _currentState.OnExitState(); newState.OnEnterState(); _currentState = newState; } _currentStateType = newStateType; } }
public void OnUpdate() { for (int i = 0; i < stateMachineList.Length; i++) { switch (aiState) { case AIStateType.Condition: stateMachineList[i].CheckCondition(); break; case AIStateType.StateUpdate: stateMachineList[i].OnUpdate(); break; } } switch (aiState) { case AIStateType.Condition: aiState = AIStateType.StateUpdate; break; case AIStateType.StateUpdate: aiState = AIStateType.Condition; break; } }
public void OnDisable() { // When the zombie get disabled (usually by the quality manager), we should set the target to null // So that when we re-enable him, we shouldn't find him attacking the air _currentTarget = null; _currentStateType = AIStateType.Idle; }
/// <summary> /// 切换状态 /// </summary> /// <param name="AIState"></param> public void ChangeState(AIStateType AIState) { if (AIState == this.m_CurState.AIState) return; this.m_CurState.Exit(); this.m_PreState = this.m_CurState; this.m_CurState = this.GetState(AIState); this.m_CurState.Enter(); }
public void OrderDrink(System.Action <AIStateType> callback, System.Action bad_drink, AIStateType callback_arg) { // Npc is at the bar ordering a drink this.npc_at_bar = true; this.callback = callback; this.callback_arg = callback_arg; this.bad_drink = bad_drink; }
void Move(Transform goal, AIStateType callback_state) { // Set state to moving this.current_state = AIStateType.moving; // Move to location this.moveTo.MoveToLocation(goal, new System.Action <AIStateType>(this.UpdateState), callback_state); }
public MonsterAIBehaviour(List <string> resourcesID, string monsterID) { this.monsterID = monsterID; aiState = AIStateType.StateUpdate; LoadData(resourcesID); }
private void UpdateTransition() { AIStateType eNewState = m_pTransition.Transition(this, m_eCurAIState); if (m_eCurAIState != eNewState) { ChangeState(eNewState); } }
public bool IsStateEnabled(AIStateType stateType) { if (_statesDictionary.ContainsKey(stateType)) { return(_statesDictionary[stateType].enabled); } return(false); }
protected void registerAIState(AIStateType key, IAIState aiState) { if (this._stateMap.ContainsKey(key)) { Debug.LogError("registerAIState():已经存在对应的key:" + key); return; } this._stateMap.Add(key, aiState); }
public override void ChangeAIState <T>(AIStateType stateType) { if (AIStateType != stateType) { AIStateType = stateType; ActorFsmStateBase state = m_Fsm.GetState <T>(); state.ChangeState <T>(); } }
public override void ChangeAIState(AIStateType stateType) { if (AIStateType != stateType) { AIStateType = stateType; AIFsmStateBase curState = m_AIFsm.CurrentState as AIFsmStateBase; if (curState == null) { return; } switch (stateType) { case AIStateType.Empty: curState.ChangeState <AIEmptyState>(); break; case AIStateType.Idle: curState.ChangeState <AIIdleState>(); break; case AIStateType.Follow: curState.ChangeState <AIFollowState>(); break; case AIStateType.Flee: curState.ChangeState <AIFleeState>(); break; case AIStateType.Patrol: curState.ChangeState <AIPatrolState>(); break; case AIStateType.Escape: curState.ChangeState <AIEscapeState>(); break; case AIStateType.Back: curState.ChangeState <AIBackState>(); break; case AIStateType.Fight: curState.ChangeState <AIFightState>(); break; case AIStateType.Dead: curState.ChangeState <AIDeadState>(); break; case AIStateType.Chase: curState.ChangeState <AIChaseState>(); break; } } }
/// <summary> /// Should be called by the AIBrain's owner each Update() /// </summary> public void Update() { AIStateType newState = FindBestEligibleAIState(); if (m_CurrentState != newState) { m_Logics[newState].Initialize(); } m_CurrentState = newState; m_Logics[m_CurrentState].Update(); }
protected virtual bool canRunAIState(AIStateType key) { foreach (AIStateType type in _needTargetAIState) { if (key == type) { return(this._target != null); } } return(true); }
// ------------------------------------------------------------------- // Name : Update // Desc : Called by Unity each frame. Gives the current state a // chance to update itself and perform transitions. // ------------------------------------------------------------------- protected virtual void Update() { if (_currentState == null || stopAllStates) { return; } AIStateType newStateType = _currentState.OnUpdate(); ChangeToNewState(newStateType); }
/// <summary> /// 切换状态 /// </summary> /// <param name="AIState"></param> public void ChangeState(AIStateType AIState) { if (AIState == this.m_CurState.AIState) { return; } this.m_CurState.Exit(); this.m_PreState = this.m_CurState; this.m_CurState = this.GetState(AIState); this.m_CurState.Enter(); }
public override void runAIState(AIStateType key) { if (canRunAIState(key)) { base.runAIState(key); } else { base.runAIState(AIStateType.FREE); } }
public override AIStateType OnUpdate() { // Determine if we want fleeing AI - Fleeing will always override current state; bool fleeing = false; if (fleeing) { returnStateType = AIStateType.Fleeing; } return(returnStateType); }
void UpdateState(AIStateType new_type) { // Check if switching from idle if (this.current_state == AIStateType.idle && new_type != AIStateType.idle) { // Set the location to three this.locationManager.GetComponent <LocationController>().leaveIdleLocation(this.idle_location); } // Decide how to handle this state transition switch (new_type) { case AIStateType.idle: { // Set state to idle this.current_state = new_type; break; } case AIStateType.order: { // Order a drink this.current_state = new_type; break; } case AIStateType.waiting_for_order: { // Ordering a drink this.current_state = new_type; break; } case AIStateType.deposit_drink: { // Ordering a drink this.current_state = new_type; break; } case AIStateType.deposited_drink: { // Ordering a drink this.current_state = new_type; break; } case AIStateType.order_complete: { // Take a drink from the bar this.current_state = new_type; break; } default: { print("Unkown State Type"); break; } } }
private void OnDrawGizmos() { Handles.color = Color.black; Handles.Label(transform.position + new Vector3(0, 2.2f, 0), currentState.ToString()); if (Application.isPlaying) { return; } currentState = startState; }
/// <summary> /// 设置状态 /// </summary> /// <param name="AIState"></param> public void SetCurState(AIStateType AIState) { this.m_CurState = this.GetState(AIState); this.m_CurState.Enter(); }
/// <summary> /// 获取状态 /// </summary> /// <param name="AIState"></param> /// <returns></returns> private StateBase GetState(AIStateType AIState) { if (this.m_StateDic.ContainsKey(AIState)) { return this.m_StateDic[AIState]; } StateBase state; switch (AIState) { case AIStateType.Attack: state = new AttackState(this.m_ActorBev,this.m_ActorAI); break; case AIStateType.Dead: state = new DeadState(this.m_ActorBev, this.m_ActorAI); break; case AIStateType.Hurt: state = new HurtState(this.m_ActorBev, this.m_ActorAI); break; case AIStateType.Idle: state = new IdleState(this.m_ActorBev, this.m_ActorAI); break; default: Debug.Log(AIState.ToString() + " not exsit "); return null; } this.m_StateDic.Add(AIState, state); return state; }