Ejemplo n.º 1
0
        public void Set(BehaviorTreeContext context, BehaviorTask node, T value)
        {
            switch (MemoryScope)
            {
            case BlackboardMemoryScope.Global: context.Agent.Blackboard.Set(Key, value, null, null); break;

            case BlackboardMemoryScope.Tree: context.Agent.Blackboard.Set(Key, value, context.Tree.Id, null); break;

            case BlackboardMemoryScope.Node: context.Agent.Blackboard.Set(Key, value, context.Tree.Id, node.Id); break;
            }
        }
Ejemplo n.º 2
0
        public T Get(BehaviorTreeContext context, BehaviorTask node, T defaultValue = default(T))
        {
            switch (MemoryScope)
            {
            case BlackboardMemoryScope.Global: return(context.Agent.Blackboard.Get <T>(Key, null, null, defaultValue));

            case BlackboardMemoryScope.Tree: return(context.Agent.Blackboard.Get <T>(Key, context.Tree.Id, null, defaultValue));

            case BlackboardMemoryScope.Node: return(context.Agent.Blackboard.Get <T>(Key, context.Tree.Id, node.Id, defaultValue));
            }

            return(defaultValue);
        }
Ejemplo n.º 3
0
        public BehaviorStatus ExecuteOn(BehaviorAgent agent)
        {
            try
            {
                BehaviorTreeContext context = new BehaviorTreeContext
                {
                    Agent     = agent,
                    Tree      = this,
                    OpenNodes = new List <Guid>()
                };

                Status = Root.Behave(ref context);

                List <Guid> previousOpenNodes = agent.Blackboard.Get <List <Guid> >("openNodes", Id);
                List <Guid> currentOpenNodes  = context.OpenNodes;

                int start = 0;
                for (int i = 0; i < Math.Min(previousOpenNodes.Count, currentOpenNodes.Count); i++)
                {
                    start = i + 1;

                    if (previousOpenNodes[i] != currentOpenNodes[i])
                    {
                        break;
                    }
                }

                for (int i = previousOpenNodes.Count - 1; i >= start; i--)
                {
                    Root.GetTaskById(previousOpenNodes[i])?.CloseInternal(ref context);
                }

                agent.Blackboard.Set <List <Guid> >("openNodes", currentOpenNodes, Id);
                agent.Blackboard.Set <int>("nodeCount", context.NodeCount, Id);
            }
            catch (System.Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown when performing behavior tree: {ex}");
                Status = BehaviorStatus.Failure;
            }

            return(Status);
        }
Ejemplo n.º 4
0
        public BehaviorStatus Behave(ref BehaviorTreeContext context)
        {
            EnterInternal(ref context);

            if (!context.Agent.Blackboard.Get <bool>("isOpen", context.Tree.Id, this.Id))
            {
                OpenInternal(ref context);
            }

            BehaviorStatus status = BehaveInternal(ref context);

            if (status != BehaviorStatus.Running)
            {
                CloseInternal(ref context);
            }

            ExitInternal(ref context);

            return(status);
        }
Ejemplo n.º 5
0
 internal void ExitInternal(ref BehaviorTreeContext context)
 {
     context.OnExitNode(this.Id);
     OnExit(ref context);
 }
Ejemplo n.º 6
0
 internal void CloseInternal(ref BehaviorTreeContext context)
 {
     context.OnCloseNode(this.Id);
     context.Agent.Blackboard.Set <bool>("isOpen", false, context.Tree.Id, this.Id);
     OnClose(ref context);
 }
Ejemplo n.º 7
0
 internal BehaviorStatus BehaveInternal(ref BehaviorTreeContext context)
 {
     context.OnBehaveNode(this.Id);
     return(OnBehave(ref context));
 }
Ejemplo n.º 8
0
 internal void OpenInternal(ref BehaviorTreeContext context)
 {
     context.OnOpenNode(this.Id);
     context.Agent.Blackboard.Set <bool>("isOpen", true, context.Tree.Id, this.Id);
     OnOpen(ref context);
 }
Ejemplo n.º 9
0
 internal void EnterInternal(ref BehaviorTreeContext context)
 {
     context.OnEnterNode(this.Id);
     OnEnter(ref context);
 }
Ejemplo n.º 10
0
 protected virtual void OnExit(ref BehaviorTreeContext context)
 {
 }
Ejemplo n.º 11
0
 protected virtual void OnClose(ref BehaviorTreeContext context)
 {
 }
Ejemplo n.º 12
0
 protected virtual BehaviorStatus OnBehave(ref BehaviorTreeContext context) => BehaviorStatus.Success;
Ejemplo n.º 13
0
 protected virtual void OnOpen(ref BehaviorTreeContext context)
 {
 }