コード例 #1
0
ファイル: Brain.cs プロジェクト: xiankang/OpenWorld
    // Update is called once per frame
    void Update()
    {
        if (!GetReady())
        {
            return;
        }

        Behavior behavior = null;

        if (_behaviors.Count < MAX_BEHAVIORS)
        {
            foreach (UnitCommand cmd in _commands)
            {
                if (_behaviors.Count > 0)
                {
                    behavior = _behaviors.Peek();
                    if (behavior.IsForced() && !cmd._forced)
                    {
                        continue;
                    }
                }

                if (ProcessCommand(cmd))
                {
                    _commands.Erase(cmd);
                    break;
                }
                else
                {
                    _commands.Erase(cmd);
                    continue;
                }
            }
        }

        if (_behaviors.Count > 0)
        {
            behavior = _behaviors.Peek();

            if (behavior.GetBrain() != this)
            {
                Debug.LogWarning("Behavior's unit does not match it's owner's unit");
            }
            if (behavior.GetSelf() != _unit)
            {
                Debug.LogWarning("Behavior's unit does not match brain's unit, the behavior will try to control a different unit");
            }

            if (behavior.Validate())
            {
                if ((behavior.GetFlags() & Behavior.BSR_NEW) != 0)
                {
                    behavior.BeginBehavior();
                }
                else
                {
                    behavior.Update();
                }
            }

            if (behavior.IsEnd())
            {
                behavior.EndBehavior();

                //这里根据toll判断是否可以被打断
                bool nonInterupting = false;

                _behaviors.Dequeue();

                //reset next behavior
                if (_behaviors.Count > 0 && _behaviors.Peek().ShouldReset() && !nonInterupting)
                {
                    _behaviors.Peek().Reset();
                }
            }
        }

        bool isProcessed = false;

        for (int i = 0; i < (int)EActionStateIDs.ASID_COUNT; ++i)
        {
            if (_actionStates[i].IsActive())
            {
                if (!_actionStates[i].ContinueStateAction())
                {
                    _actionStates[i].EndState(3);
                }
                else
                {
                    isProcessed = true;
                }
            }
        }
        if (isProcessed == false)
        {
            UnpauseNextActionState();
        }
    }