生物AI的触发和动作

时间:2019-05-15 03:21:57

标签: c# unity3d artificial-intelligence

尽管我的生物AI可以正常工作(大部分情况下),但我感觉我的设置方式效率极低,并且可能犯了一些编程错误。我想重写它以使其更干净,更有效并且更易于维护,但是我不确定从哪里开始。

在我的生物AI中,我有一系列触发器,例如OnSpawn,OnDeath或OnCollisionEnter。每个触发器中都有一系列动作,例如“施法”或“播放动画”。当满足触发器的条件时,将处理其动作列表,以检查其是否不在我们的处理列表中,将其添加,然后播放其关联的动作。当不满足触发器的条件时,将从该进程列表中删除动作列表,并且类似地通过一些删除功能进行处理以清除行为。

一些我简化的代码:

    void Update()
    {
        if (canAct && !dead)
        {
            CheckTriggers();
            PlayAllActions();
       }
     }


   private void CheckTriggers()
    {
        for (int i = 0; i < actions.Length; i++)
        {
            switch (actions[i].trigger)
            {
                case ActionTrigger.Trigger.OnCollisionEnter:
                    if (isColliding)
                        AddActionList(actions[i].actionSetList);
                    else
                        RemoveActionList(actions[i].actionSetList);
                    break;

                case ActionTrigger.Trigger.UponBeingAttacked:
                    if (hasBeenAttacked)
                        AddActionList(actions[i].actionSetList);
                    break;
            }
        }
    }

    public void AddActionList(ActionSetList actionSetList)
    {
        bool containsItem = existingActionsList.Any(item => item == actionSetList);
        if (containsItem)
            return;

        existingActionsList.Add(actionSetList);
    }

    private void PlayAllActions()
    {
        if (existingActionsList.Count > 0)
            for (int i = 0; i < existingActionsList.Count; i++)
                ActionPlayEffect(existingActionsList[i]);
    }

    public void ActionPlayEffect(ActionSetList actionSetList)
    {
        for (int i = 0; i < actionSetList.Length; i++)
        {
            switch (actionSetList[i].type)
            {
                case ActionSet.Type.CastSpell:
                    if (spellRoutine == null && actionSetList[i].cooldownTimeRemaining <= 0)
                        spellRoutine = StartCoroutine(Cast(actionSetList[i]));
                    break;

                case ActionSet.Type.PlayAnim:
                    if (!isInActionPose)
                    {
                        animator.SetTrigger("ActionTrigger");
                        animator.SetInteger("Action", (int)actionSetList[i].animToPlay+1);
                        isInActionPose = true;
                    }
                    break;
            }
        }
    }

    public void RemoveActionList(ActionSetList actionSetList)
    {
        bool containsItem = existingActionsList.Any(item => item == actionSetList);
        if (containsItem)
        {
            ActionRemoveEffect(actionSetList);
            existingActionsList.Remove(actionSetList);
        }
    }

    public void ActionRemoveEffect(ActionSetList actionSetList)
    {
        for (int i = 0; i < actionSetList.Length; i++)
        {
            switch (actionSetList[i].type)
            {
                case ActionSet.Type.CastSpell:
                    CancelCast();
                    break;

                case ActionSet.Type.PlayAnim:
                    animator.SetTrigger("ActionTrigger");
                    animator.SetInteger("Action", 0);
                    isInActionPose = false;
                    break;
            }
        }
    }

我该怎么做才能构建更高效的生物AI?

1 个答案:

答案 0 :(得分:0)

我可能会使用delegates来编写类似的系统。

在某种程度上,委托可以被认为是持有方法列表的变量。如果执行该委托,则将执行其持有的所有方法。

这将允许您将这样的方法添加到要在需要时调用的方法列表中。

delegate void OnSpawn(GameObject gameObject); //Create delegate type
public OnSpawn onSpawn; //Create variable from delegate type

void SetUpStats(Gameobject gameObject){
    //Set hp, initialize spells
}

void SetUpAnimations(GameObject gameObject){
    //Initialize animations
}

void PlaySpawnSound(GameObject gameObject){
    //Play a spawn sound
}

void Start(){
    if (onSpawn == null) //Add content to delegate
    {
        onSpawn = new OnSpawn(SetUpStats); //You may be able to write onSpawn = SetUpStats; instead, for shorter code. But please test it.
        onSpawn += SetUpAnimations;
        onSpawn += PlaySpawnSound;
    }
}

void Spawn(){
    onSpawn(gameObject); 

    //Call delegate, invoking all methods stored in it. 
    //Note that they all receive the same input. In this case the gameObject.
    //You can give them any input you want, so long as you define it in the delegate type.
    //I chose a gameObject as you can get all components and more from it.
}

如果您有任何疑问或想知道的事情,请告诉我。