Exemplo n.º 1
0
    public void DoTransition(Transition trans)//transit state
    {
        if (trans == Transition.NullTransition)
        {
            Debug.LogError("empty transition");
            return;
        }
        StateID id = currentState.GetOutPutState(trans);

        if (id == StateID.NullStateID)
        {
            Debug.LogWarning("Unable to perform transition");
            return;
        }
        if (states.ContainsKey(id) == false)
        {
            Debug.LogError("Cannot find this state");
            return;
        }
        FSMstate state = states[id];

        currentState.DoAfterLeave();
        currentState = state;
        currentID    = id;
        currentState.DoBeforeEnter();
    }
Exemplo n.º 2
0
        //通过接受过渡状态来改变当前状态
        public void PerformTransition(Transition Trans)
        {
            //该状态未空则打印错误信息并且返回
            if (Trans == Transition.NullTransition)
            {
                Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");
                return;
            }
            //得到当前状态通过过渡状态改变后的状态
            StateID id = currentState.GetState(Trans);

            //若该状态未空则返回
            if (id == StateID.NullStateID)
            {
                Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " +
                               " for transition " + Trans.ToString());
                return;
            }
            //更新当前状态
            currentStateID = id;
            foreach (FSMstate state in states)
            {
                if (state.ID == currentStateID)
                {
                    currentState.DoBeforeLeaving(ai, po);
                    currentState = state;
                    currentState.DoBeforeEntering();
                    break;
                }
            }
        }
Exemplo n.º 3
0
        //为有限状态机置入新的状态
        public void AddState(FSMstate s)
        {
            //在添加前检测是否为空
            if (s == null)
            {
                Debug.LogError("FSM ERROR: Null reference is not allowed");
            }
            //添加第一个状态
            if (states.Count == 0)
            {
                states.Add(s);
                currentState   = s;
                currentStateID = s.ID;
                return;
            }

            //若状态未被添加过则添加进集合
            foreach (FSMstate state in states)
            {
                if (state.ID == s.ID)
                {
                    Debug.LogError("FSM ERROR: Impossible to add state " +
                                   s.ID.ToString() + " because state has already been added");
                    return;
                }
                states.Add(s);
            }
        }
Exemplo n.º 4
0
 private void StatePatrol()
 {
     m_ani.SetBool("idle", true);
     m_ani.SetBool("attack", false);
     m_ani.SetBool("death", false);
     m_ani.SetBool("run", false);
     agent.ResetPath();
     if (Enemy_trigger.is_trigger == true)
     {
         curState = FSMstate.Chase;
     }
 }
Exemplo n.º 5
0
    void StatePatrol()
    {
        inputEnabled = true;
        Dup          = 1;
        Dright       = 0;
        UpdateDmagDvec(Dup, Dright);
        Enemy_Waypoint();

        if (Enemy_trigger.is_trigger == true)
        {
            curState = FSMstate.Chase;
            checkWp  = false;
        }
    }
Exemplo n.º 6
0
 void StateAttack()
 {
     inputEnabled = false;
     Dup          = 0;
     Dright       = 0;
     UpdateDmagDvec(Dup, Dright);
     transform.LookAt(player.position);
     attack = true;
     if (Vector3.Distance(this.transform.position, player.position) >= 4)
     {
         curState = FSMstate.Chase;
         attack   = false;
     }
 }
Exemplo n.º 7
0
    private void StateAttack()
    {
        AnimatorStateInfo stateInfo = m_ani.GetCurrentAnimatorStateInfo(0);

        m_ani.SetBool("idle", false);
        m_ani.SetBool("attack", true);
        m_ani.SetBool("death", false);
        m_ani.SetBool("run", false);
        agent.ResetPath();


        if (Vector3.Distance(m_transform.position, m_player.transform.position) > 4f)
        {
            curState = FSMstate.Chase;
        }
    }
Exemplo n.º 8
0
 private void StateChase()
 {
     m_ani.SetBool("idle", false);
     m_ani.SetBool("attack", false);
     m_ani.SetBool("death", false);
     m_ani.SetBool("run", true);
     agent.SetDestination(m_player.transform.position);
     if (Vector3.Distance(m_transform.position, m_player.transform.position) < 2f)
     {
         curState = FSMstate.Attack;
     }
     else if (Vector3.Distance(m_transform.position, m_player.transform.position) > 8f)
     {
         curState = FSMstate.Patrol;
     }
 }
Exemplo n.º 9
0
 void StateChase()
 {
     inputEnabled = true;
     Dup          = 2;
     Dright       = 0;
     UpdateDmagDvec(Dup, Dright);
     transform.LookAt(player.position);
     agent.SetDestination(player.position);
     if (Vector3.Distance(this.transform.position, player.position) <= 2)
     {
         curState = FSMstate.Attack;
     }
     if (Vector3.Distance(this.transform.position, player.position) >= 6)
     {
         curState = FSMstate.Patrol;
         checkWp  = true;
     }
 }
Exemplo n.º 10
0
    public MovementLayer()
    {
        Refresh = false;
        //////////////Movement Layer/////////////////////
        //idle
        _state[0] = new FSMstate((int)STATE.idle, 4);
        _state[0].AddTransition((int)INPUT.command_idle, (int)STATE.idle);
        _state[0].AddTransition((int)INPUT.command_move, (int)STATE.move);
        _state[0].AddTransition((int)INPUT.command_attack, (int)STATE.attack);
        _state[0].AddTransition((int)INPUT.command_die, (int)STATE.die);

        //move
        _state[1] = new FSMstate((int)STATE.move, 4);
        _state[1].AddTransition((int)INPUT.command_idle, (int)STATE.idle);
        _state[1].AddTransition((int)INPUT.command_attack, (int)STATE.attack);
        _state[1].AddTransition((int)INPUT.complete_arrival, (int)STATE.idle);
        _state[1].AddTransition((int)INPUT.command_die, (int)STATE.die);

        //attack
        _state[2] = new FSMstate((int)STATE.attack, 4);
        _state[1].AddTransition((int)INPUT.command_idle, (int)STATE.idle);
        _state[2].AddTransition((int)INPUT.command_move, (int)STATE.move);
        _state[2].AddTransition((int)INPUT.complete_arrival, (int)STATE.idle);
        _state[2].AddTransition((int)INPUT.command_die, (int)STATE.die);

        //die
        _state[3] = new FSMstate((int)STATE.die, 1);
        _state[3].AddTransition((int)INPUT.complete_die, (int)STATE.revive);

        //revive
        _state[4] = new FSMstate((int)STATE.revive, 1);
        _state[4].AddTransition((int)INPUT.complete_revive, (int)STATE.idle);

        _fsm = new FSMclass((int)STATE.idle);
        _fsm.AddState(_state[0]);
        _fsm.AddState(_state[1]);
        _fsm.AddState(_state[2]);
        _fsm.AddState(_state[3]);
        _fsm.AddState(_state[4]);

        //idle init
        //_CurrentState = (STATE)_fsm.StateTransition((int)INPUT.command_idle);
        _CurrentState = (STATE)_fsm.GetCurrentState();
    }
Exemplo n.º 11
0
 public void AddState(FSMstate s)
 {
     if (s == null)
     {
         Debug.LogError("FSM state is empty");
         return;
     }
     if (currentState == null)
     {
         currentState = s;
         currentID    = s.ID;
     }
     if (states.ContainsKey(s.ID))
     {
         Debug.LogError("state already exist");
         return;
     }
     states.Add(s.ID, s);
 }
Exemplo n.º 12
0
    void Update()
    {
        //UpdateDmagDvec(Dup,Dright);
        switch (curState)
        {
        case FSMstate.Patrol:
            StatePatrol();
            break;

        case FSMstate.Chase:
            StateChase();
            break;

        case FSMstate.Attack:
            StateAttack();
            break;
        }
        if (stateManager.HP <= 0)
        {
            inputEnabled = false;
            curState     = FSMstate.None;
            Destroy(this.gameObject, 5.0f);
        }
    }
Exemplo n.º 13
0
 void Start()
 {
     curState = FSMstate.Patrol;
     agent    = this.GetComponent <NavMeshAgent>();
     m_player = GameObject.FindGameObjectWithTag("Player").GetComponent <Player>();
 }
Exemplo n.º 14
0
 void Start()
 {
     curState = FSMstate.Patrol;
     agent    = this.GetComponent <NavMeshAgent>();
     //Enemy_randomwayPoint();
 }