Esempio n. 1
0
    /// <summary>
    /// 执行转换
    /// </summary>
    /// <param name="trans">转换条件</param>
    public void PerformTransition(Transition trans)
    {
        if (trans == Transition.NullTransition)
        {
            Debug.LogError("敌人状态 要执行的转换条件为空" + trans);
            return;
        }

        StateID nextStateID = _currentState.GetOutputState(trans);

        if (nextStateID == StateID.NullState)
        {
            Debug.LogError("敌人状态 要执行的转换状态为空" + CurrentState);
            return;
        }

        foreach (EnemyFSMState fsmState in _states)
        {
            if (fsmState.StateID == nextStateID)
            {
                _currentState.DoBeforeLeaving();
                //Debug.Log("状态转出"+_currentState.StateID);
                _currentState = fsmState;
                //Debug.Log("状态转入"+_currentState.StateID);
                _currentState.DoBeforeEntering();
                return;
            }
        }
    }
Esempio n. 2
0
 private void OnTriggerExit2D(Collider2D other)
 {
     if (other.CompareTag("Player"))
     {
         currentState = EnemyFSMState.MOVE;
     }
 }
Esempio n. 3
0
    public void AddState(EnemyFSMState state)
    {
        if (_states == null)
        {
            _states = new List <EnemyFSMState>();
        }

        if (state == null)
        {
            Debug.LogError("要添加的状态为空");
            return;
        }

        if (_states.Count == 0)
        {
            _states.Add(state);
            _currentState = state;
            _currentState.DoBeforeEntering();
            return;
        }
        foreach (EnemyFSMState fsmState in _states)
        {
            if (fsmState.StateID == state.StateID)
            {
                Debug.Log("要添加的状态ID" + state.StateID + "已添加");
                return;
            }
        }
        _states.Add(state);
    }
Esempio n. 4
0
    /// <summary>
    /// This method places new states inside the FSM,
    /// or prints an ERROR message if the state was already inside the List.
    /// First state added is also the initial state.
    /// </summary>
    public void AddState(EnemyFSMState s)
    {
        // Check for Null reference before deleting
        if (s == null)
        {
            Debug.LogError("EnemyFSM ERROR: Null reference is not allowed");
        }

        // First State inserted is also the Initial state,
        //   the state the machine is in when the simulation begins
        if (states.Count == 0)
        {
            states.Add(s);
            currentState   = s;
            currentStateID = s.ID;
            return;
        }

        // Add the state to the List if it's not inside it
        foreach (EnemyFSMState state in states)
        {
            if (state.ID == s.ID)
            {
                Debug.LogError("EnemyFSM ERROR: Impossible to add state " + s.ID.ToString() +
                               " because state has already been added");
                return;
            }
        }
        states.Add(s);
    }
Esempio n. 5
0
 private void OnCollisionEnter2D(Collision2D col)
 {
     if (col.gameObject.CompareTag("Player"))
     {
         timeWaitedSinceAttack = 0;
         Attack();
         currentState = EnemyFSMState.WAIT;
     }
 }
Esempio n. 6
0
    void Wait()
    {
        timeWaited += Time.deltaTime;

        if (timeWaited > waitTime)
        {
            currentState = EnemyFSMState.MOVE;
            timeWaited   = 0f;
        }
    }
Esempio n. 7
0
    void Follow()
    {
        if (enemyMovement == true)
        {
            transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
        }

        if (dist > visionRadious)
        {
            currentState = EnemyFSMState.MOVE;
        }
    }
Esempio n. 8
0
 void ChangePath()
 {
     currentPathNode++;
     if (currentPathNode >= path.Length)
     {
         currentPathNode = 0;
     }
     if (dist <= visionRadious)
     {
         currentState = EnemyFSMState.FOLLOW;
     }
     else
     {
         currentState = EnemyFSMState.WAIT;
     }
 }
Esempio n. 9
0
    void Move()
    {
        if (enemyMovement == true)
        {
            this.transform.position = Vector3.MoveTowards(this.transform.position, path[currentPathNode].position, speed * Time.deltaTime);
        }

        if (dist <= visionRadious)
        {
            currentState = EnemyFSMState.FOLLOW;
        }
        else if (Vector3.Distance(this.transform.position, path[currentPathNode].position) < 0.001f)
        {
            currentState = EnemyFSMState.CHANGE_PATH;
        }
    }
Esempio n. 10
0
    void Follow()
    {
        if (enemyMovement == true)
        {
            transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
        }

        Vector3 diff = target - transform.position;

        if (Mathf.Abs(diff.x) > Mathf.Abs(diff.y))
        {
            if (diff.x > 0)
            {
                anim.Play("Bat_Idle");
            }
            else
            {
                anim.Play("Bat_Idle");
            }
        }
        else
        {
            if (diff.y > 0)
            {
                anim.Play("Bat_Back");
            }
            else
            {
                anim.Play("Bat_Idle");
            }
        }

        if (dist > visionRadious)
        {
            currentState = EnemyFSMState.MOVE;
        }
    }
Esempio n. 11
0
    /// <summary>
    /// This method tries to change the state the FSM is in based on
    /// the current state and the transition passed. If current state
    ///  doesn't have a target state for the transition passed,
    /// an ERROR message is printed.
    /// </summary>
    public void PerformTransition(EnemyTransition trans)
    {
        // Check for NullTransition before changing the current state
        if (trans == EnemyTransition.NullTransition)
        {
            Debug.LogError("EnemyFSM ERROR: NullTransition is not allowed for a real transition");
            return;
        }

        // Check if the currentState has the transition passed as argument
        EnemyStateID id = currentState.GetOutputState(trans);

        if (id == EnemyStateID.NullStateID)
        {
            Debug.LogError("EnemyFSM ERROR: State " + currentStateID.ToString() + " does not have a target state " +
                           " for transition " + trans.ToString());
            return;
        }

        // Update the currentStateID and currentState
        currentStateID = id;
        foreach (EnemyFSMState state in states)
        {
            if (state.ID == currentStateID)
            {
                // Do the post processing of the state before setting the new one
                currentState.DoBeforeLeaving();

                currentState = state;

                // Reset the state to its desired condition before it can reason or act
                currentState.DoBeforeEntering();
                break;
            }
        }
    } // PerformTransition()