设计模式——状态机(AI)(Unity3D中的应用)

在Unity3D中对状态模式的应用当属于有限状态机的使用,利用状态机管理游戏物体的状态的管理,实现一个AI系统。

状态机类:

[RequireComponent(typeof(NavMeshAgent))]
public class StateManager : MonoBehaviour {

    private List listState = new List (); 
    private AbsState currentState;

    public float sight;
    public float stoppingDistance;
    public Transform targetTF;
    public float findInterval;
    public string[] tags;
    public Transform[] listTarget;

    private NavMeshAgent navMeshAgent;

    private void Start () {
        navMeshAgent = GetComponent ();
    }

    private void OnEnable()
    {
        InvokeRepeating ("FindTarget",0,findInterval);
        ConfigState ();
    }

    private void Disable()
    {
        CancelInvoke ("FindTarget");
    }

    private void ConfigState()
    {
        PatrolState patrolState = new PatrolState ();
        patrolState.AddTrigger (TriggerID.SawPlayer,StateID.Pursue);

        PursueState pursueState = new PursueState ();
        pursueState.AddTrigger (TriggerID.LosePlayer,StateID.Patrol);

        listState.Add (patrolState);
        listState.Add (pursueState);

        ChangeState (StateID.Patrol);
    }

    private void Update () 
    {
        if (null == currentState)
            return;
        currentState.Action (this);
        currentState.Reason (this);
    }

    public void ChangeState(StateID stateId)
    {
        currentState = listState.Find ((e) => e.stateId == stateId);
    }

    public void FindTarget()
    {
        List list = new List ();
        for(int i = 0;i < tags.Length;i++)
        {
            GameObject[] objects = GameObject.FindGameObjectsWithTag (tags[i]);
            list.AddRange (Array.FindAll(objects,(e)=>e.tag == tags[i]));
        }
        GameObject[] array = list.FindAll ((e)=>Vector3.Distance(this.transform.position,e.transform.position)< sight).ToArray();
        if (array.Length > 0)
        {
            targetTF = Min (array,(e)=>Vector3.Distance(this.transform.position,e.transform.position)).transform;
        }

    }

    public delegate K SelectHandler(T t); 
    private T Min(T[] array, SelectHandler handler) where K :IComparable,IComparable
    {
        T min = array [0];
        for(int i = 1;i < array.Length ;i++)
        {
            if (handler(min).CompareTo(handler(array [i]))<0)
                min = array [i];                
        }
        return min;
    }

    public void MoveToTarget(Transform targetTF)
    {
        navMeshAgent.SetDestination (targetTF.position);
    }
}

巡逻状态:

public class PatrolState : AbsState
{
    private int currentIndex;

    public override void Init ()
    {
        stateId = StateID.Patrol;
    }

    public override void Action (StateManager stateManage)
    {
        base.Action (stateManage);
        if (Vector3.Distance (stateManage.listTarget [currentIndex].position, stateManage.transform.position)
             < stateManage.stoppingDistance)
            currentIndex = ++currentIndex % stateManage.listTarget.Length;

        stateManage.MoveToTarget (stateManage.listTarget [currentIndex]);
    }
}

条件类:

public class SawPlayerTrigger : AbsTrigger {

    protected override void Init ()
    {
        triggerId = TriggerID.SawPlayer;
    }

    public override bool CheckTrigger (StateManager stateManager)
    {
        if(null != stateManager.targetTF)
            return Vector3.Distance(stateManager.targetTF.position,stateManager.transform.position) 
                < stateManager.sight;                   
        return false;
    }
}

总结:
状态改变引起行的变化是状态模式解决的问题
状态模式将条件分支封装到单独类中,每一个状态类单独存在,不依赖其他对象,方便了当增加新的状态时,不影响已有状态类

下载地址:http://download.csdn.net/detail/piratecode/9615325

你可能感兴趣的:(Unity3D,状态机,状态模式,AI,unity3d)