Пример #1
0
 // Start is called before the first frame update
 public virtual void Start()
 {
     Action[] acts = this.GetComponents <Action>();
     foreach (Action a in acts)
     {
         actions.Add(a);
     }
     for (int i = 0; i < myGoals.Length; i++)
     {
         Goal    g  = myGoals[i];
         SubGoal sg = new SubGoal(g.goal, g.value, g.shouldRemove);
         goalsDic.Add(sg, g.priority);
     }
 }
Пример #2
0
        public virtual void Start()
        {
            Action[] acts = GetComponents <Action>();

            actions.Clear();
            //sets the action name of each action to its actionName
            foreach (Action a in acts)
            {
                actions.Add(a);
                a.actionName = a.GetType().Name;
            }

            //add each goal to goals dictionary
            for (int i = 0; i < myGoals.Length; i++)
            {
                Goals   g  = myGoals[i];
                SubGoal sg = new SubGoal(g.goal, g.goalValue, g.shouldRemoveOnComplete);
                goalsDictionary.Add(sg, g.priority);
            }
        }
Пример #3
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;
                }
            }
        }
Пример #4
0
        private void LateUpdate()
        {
            if (CurrentAction != null && CurrentAction.Running)
            {
                if (CurrentAction.Agent.hasPath && CurrentAction.Agent.remainingDistance < 1f)
                {
                    Invoke("CompleteAction", CurrentAction.Duration);
                    _invoked = true;
                }

                return;
            }

            if (_planner == null || _actionQueue == null)
            {
                _planner = new Planner();

                var sortedGoals = Goals.OrderByDescending(entry => entry.Value);

                foreach (var goal in sortedGoals)
                {
                    _actionQueue = _planner.Plan(Actions, goal.Key.SubGoals, null);
                    if (_actionQueue == null)
                    {
                        continue;
                    }

                    CurrentGoal = goal.Key;
                    break;
                }
            }

            if (_actionQueue != null && _actionQueue.Count == 0)
            {
                if (CurrentGoal.Once)
                {
                    Goals.Remove(CurrentGoal);
                }

                _planner = null;
            }

            if (_actionQueue != null && _actionQueue.Count > 0)
            {
                CurrentAction = _actionQueue.Dequeue();
                if (CurrentAction.PrePerform())
                {
                    // Find Target

                    if (CurrentAction.Target != null)
                    {
                        CurrentAction.Running = true;
                        CurrentAction.Agent.SetDestination(CurrentAction.Target.transform.position);
                    }
                }
                else
                {
                    // Force a new plan!
                    _actionQueue = null;
                }
            }
        }
Пример #5
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;
                }
            }
        }
Пример #6
0
        // Update is called once per frame
        public virtual void LateUpdate()
        {
            if (currentAction != null && currentAction.running)
            {
                float distToTarget = Vector3.Distance(currentAction.vec3Destination, transform.position);
                if (distToTarget < distanceToTargetThreshold)
                {
                    if (!invoked)
                    {
                        Invoke("CompleteAction", currentAction.duration);
                        invoked = true;
                    }
                }
                return;
            }

            if (planner == null || actionQueue == null)
            {
                planner = new Planner();

                var sortedGoals = from entry in goalsDic orderby entry.Value descending select entry;

                foreach (KeyValuePair <SubGoal, int> sg in sortedGoals)
                {
                    actionQueue = planner.Plan(actions, sg.Key.subGoals, beliefs); // trying to create a plan for the most important goal
                    if (actionQueue != null)
                    {
                        currentGoal = sg.Key;
                        break;
                    }
                }
            }
            if (actionQueue != null && actionQueue.Count == 0)
            {
                if (currentGoal.remove)
                {
                    goalsDic.Remove(currentGoal);
                }
                planner = null;
            }
            // sets our action and gets the target
            if (actionQueue != null && actionQueue.Count > 0)
            {
                currentAction = actionQueue.Dequeue(); // remove the action from the queue and make it the current action
                if (currentAction.EnterAction())
                {
                    if (currentAction.gameObjTarget == null && currentAction.targetTag != "")          // Doesnt have a target but has a target tag
                    {
                        currentAction.gameObjTarget = GameObject.FindWithTag(currentAction.targetTag); // Set the target to what is tagged
                    }
                    if (currentAction.gameObjTarget != null)                                           // if I have a target
                    {
                        currentAction.running = true;

                        currentAction.vec3Destination = currentAction.gameObjTarget.transform.position;
                        Transform dest = currentAction.gameObjTarget.transform.Find("Destination");
                        if (dest != null)
                        {
                            currentAction.vec3Destination = dest.position;
                        }

                        currentAction.navAgent.SetDestination(currentAction.vec3Destination);
                    }
                }
                else
                {
                    actionQueue = null; // go get a new plan
                }
            }
        }