Unity —有限狀態機FSM系統(2)

FSM狀態機

接上篇

PatrolState (巡邏狀態)

PatrolState巡邏狀態類,繼承與FSMState(狀態類),在巡邏狀態時,當與主角距離比較近時,觸發跟隨狀態,敵人進入跟隨狀態。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PatrolState : FSMState
{
    private List<Transform> paths = new List<Transform>();
    private int index = 0;
    private Transform player;

    public PatrolState(FSMSystem fsm) : base(fsm)
    {
        stateID = StateID.patrol;
        Transform pathTransform = GameObject.Find("path").transform;
        Transform[] chidren = pathTransform.GetComponentsInChildren<Transform>();
        player = GameObject.Find("Player").transform;
        foreach (var item in chidren)
        {
            if (item != pathTransform)
            {
                paths.Add(item);
            }
        }

    }

    public override void Act(GameObject npc)
    {
        npc.transform.LookAt(paths[index].position);     //朝向
        npc.transform.Translate(Vector3.forward * Time.deltaTime * 3);
        if(Vector3.Distance(npc.transform.position,paths[index].position) < 1)
        {
            index++;
            index %= paths.Count;
        }
    }

    public override void Reason(GameObject npc)
    {
        if (Vector3.Distance(player.position, npc.transform.position) < 3)
        {
            fsm.PerformTransition(Transiton.SeePlayer);
        }
    }
}
   

ChaseState (跟隨狀態)

ChaseState 繼承與FSMState(狀態類),當敵人距離主角比較遠的時候,觸發巡邏狀態,敵人回到巡邏狀態。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChaseState : FSMState
{
    private Transform player;
    public ChaseState(FSMSystem fsm) : base(fsm)
    {
        stateID = StateID.Chase;
        player = GameObject.Find("Player").transform;
    }

    public override void Act(GameObject npc)
    {
        npc.transform.LookAt(player.transform.position);
        npc.transform.Translate(Vector3.forward * Time.deltaTime * 2);
    }

    public override void Reason(GameObject npc)
    {
        if (Vector3.Distance(player.position, npc.transform.position) > 6)
        {
            fsm.PerformTransition(Transiton.LostPlayer);
        }
    }
}

Enemy(敵人類)

Enemy 持有FSMSystem(狀態管理類),通過Enemy來註冊敵人的狀態;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    private FSMSystem fsm;

    private void Awake()
    {
        InitFsm();
    }

    private void InitFsm()
    {
        fsm = new FSMSystem();
        FSMState patroState = new PatrolState(fsm);
        patroState.AddTransition(Transiton.SeePlayer, StateID.Chase);
        FSMState chaseState = new ChaseState(fsm);
        chaseState.AddTransition(Transiton.LostPlayer, StateID.patrol);

        fsm.AddSatte(patroState);
        fsm.AddSatte(chaseState);
    }

    private void Update()
    {
        fsm.Update(this.gameObject);
    }
}

簡單的效果

開始處於巡邏狀態
在這裏插入圖片描述
靠近,敵人處於跟隨主角狀態
在這裏插入圖片描述
主角離遠,回到巡邏狀態
在這裏插入圖片描述

發佈了37 篇原創文章 · 獲贊 11 · 訪問量 9153
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章