Beispiel #1
0
        void LateUpdate()
        {
            //if there's a current action and it is still running
            if (currentAction != null && currentAction.IsActionActive)
            {
                // Check the agent has a goal and has reached that goal
                if (currentAction.ConditionToExit())
                {
                    CompleteAction();
                    return;
                }

                //if a current action is active run its update tick
                if (currentAction.IsActionActive == true)
                {
                    currentAction.OnTick();
                }
                //if not action is active find a new plan
                else
                {
                    planner = null;
                }
                return;
            }

            // Check we have a planner and an actionQueue
            if (planner == null || actionQueue == null)
            {
                planner = new GPlanner();

                // Sort the goals in descending order and store them in sortedGoals
                var sortedGoals = from entry in goalsDictionary orderby entry.Value descending select entry;
                //look through each goal to find one that has an achievable plan
                foreach (KeyValuePair <SubGoal, int> sg in sortedGoals)
                {
                    actionQueue = planner.plan(actions, sg.Key.sGoals, agentPersonalState);
                    // If actionQueue is not = null then we must have a plan
                    if (actionQueue != null)
                    {
                        // Set the current goal
                        currentGoal = sg.Key;
                        break;
                    }
                }

                //clears action plan list and adds the new action plan from action queue in a list so we can view the plan in debugger.
                actionPlan.Clear();
                if (actionQueue != null)
                {
                    foreach (var action in actionQueue)
                    {
                        actionPlan.Add(action);
                    }
                }
            }


            // if we have a actionQueue but the count is == 0
            if (actionQueue != null && actionQueue.Count == 0)
            {
                // Check if currentGoal is removable
                if (currentGoal.remove)
                {
                    // Remove it
                    goalsDictionary.Remove(currentGoal);
                }
                // Set planner = null so it will trigger a new one
                planner = null;
            }


            // If there are still actions insie of actionQueue
            if (actionQueue != null && actionQueue.Count > 0)
            {
                // Remove the top action of the queue and put it in currentAction
                currentAction = actionQueue.Dequeue();
                //call actions OnEnter function and check if it returns true or false.
                if (currentAction.OnEnter())
                {
                    // Activate the current action
                    currentAction.IsActionActive = true;
                }
                else
                {
                    //empty action queue so we can find another plan.
                    actionQueue = null;
                }
            }
        }
Beispiel #2
0
        private void LateUpdate()
        {
            if (currentAction != null && currentAction.running)
            {
                float distanceToTarget = Vector3.Distance(currentAction.target.transform.position, this.transform.position);
                if (currentAction.agent.hasPath && distanceToTarget < 2f)
                {
                    if (!invoked)
                    {
                        Invoke(nameof(CompleteAction), currentAction.duration);
                        invoked = true;
                    }
                }
                return;
            }

            if (planner == null || actionQueue == null)
            {
                planner = new GPlanner();
                var sortedGoals = from entry in goals orderby entry.Value descending select entry;

                foreach (KeyValuePair <SubGoal, int> sg in sortedGoals)
                {
                    actionQueue = planner.Plan(actions, sg.Key.sgoals, beliefs);
                    if (actionQueue != null)
                    {
                        currentGoal = sg.Key;
                        break;
                    }
                }
            }

            if (actionQueue != null && actionQueue.Count == 0)
            {
                if (currentGoal.remove)
                {
                    goals.Remove(currentGoal);
                }
                planner = null;
            }

            if (actionQueue != null && actionQueue.Count > 0)
            {
                currentAction = actionQueue.Dequeue();
                if (currentAction.PrePerform())
                {
                    if (currentAction.target == null && currentAction.targetTag != "")
                    {
                        currentAction.target = GameObject.FindWithTag(currentAction.targetTag);
                    }

                    if (currentAction.target == null)
                    {
                        Debug.Log("Idiot");
                    }

                    if (currentAction.target != null)
                    {
                        currentAction.running = true;
                        //Debug.Log($"Setting DESTINATION OF {this.name} TO {currentAction.target.name} ");
                        currentAction.agent.SetDestination(currentAction.target.transform.position);
                    }
                }
                else
                {
                    actionQueue = null;
                }
            }
        }