void LateUpdate() { if (currentAction != null && currentAction.running) { float distanceToTarget = Vector3.Distance(currentAction.target.transform.position, this.transform.position); if (currentAction.agent.hasPath && distanceToTarget < 2.0f) { if (!invoked) { Invoke("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.targetTag != null) { currentAction.running = true; currentAction.agent.SetDestination(currentAction.target.transform.position); } } else { actionQueue = null; } } }
void LateUpdate() { if (mCurrentAction != null && mCurrentAction.Running) { mCurrentAction.Perform(); if (!mCurrentAction.Running) { mCurrentAction.PostPerform(); mCurrentAction = null; } else { return; } } if (mPlanner == null || mActionQueue == null) { mPlanner = new GPlanner(); var sortedGoals = from entry in mGoals orderby entry.Value descending select entry; foreach (KeyValuePair <GAgentSubGoal, int> sg in sortedGoals) { mActionQueue = mPlanner.Plan(mActions, sg.Key.SubGoals, Beliefs); if (mActionQueue != null) { mCurrentGoal = sg.Key; break; } } } if (mActionQueue != null && mActionQueue.Count == 0) { if (mCurrentGoal.Remove) { mGoals.Remove(mCurrentGoal); } mPlanner = null; } if (mActionQueue != null && mActionQueue.Count > 0) { GAction newAction = mActionQueue.Dequeue(); //if (newAction != currentAction) // { mCurrentAction = newAction; if (!mCurrentAction.PrePerform()) { mActionQueue = null; } // } } }
private void Start() { stateMachine = new GFSM(); availableActions = new HashSet <GAction>(); currentActions = new Queue <GAction>(); planner = new GPlanner(); FindDataProvider(); FindBlackBoard(); CreatePlanningState(); CreatePerformActionState(); stateMachine.PushState(planningState); LoadActions(); }
void LateUpdate() { //if there's a current action and it is still running if (currentAction != null && currentAction.running) { // Find the distance to the target float distanceToTarget = Vector3.Distance(destination, this.transform.position); //Debug.Log(currentAction.agent.hasPath + " " + distanceToTarget); // Check the agent has a goal and has reached that goal if (distanceToTarget < 2f)//currentAction.agent.remainingDistance < 0.5f) { // Debug.Log("Distance to Goal: " + currentAction.agent.remainingDistance); if (!invoked) { //if the action movement is complete wait //a certain duration for it to be completed Invoke("CompleteAction", currentAction.duration); invoked = true; } } 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 goals 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, beliefs); // If actionQueue is not = null then we must have a plan if (actionQueue != null) { // Set the current goal currentGoal = sg.Key; break; } } } // Have we an actionQueue if (actionQueue != null && actionQueue.Count == 0) { // Check if currentGoal is removable if (currentGoal.remove) { // Remove it goals.Remove(currentGoal); } // Set planner = null so it will trigger a new one planner = null; } // Do we still have actions if (actionQueue != null && actionQueue.Count > 0) { // Remove the top action of the queue and put it in currentAction currentAction = actionQueue.Dequeue(); if (currentAction.PrePerform()) { // Get our current object if (currentAction.target == null && currentAction.targetTag != "") { // Activate the current action currentAction.target = GameObject.FindWithTag(currentAction.targetTag); } if (currentAction.target != null) { // Activate the current action currentAction.running = true; // Pass in the office then look for its cube destination = currentAction.target.transform.position; Transform dest = currentAction.target.transform.Find("Destination"); // Check we got it if (dest != null) { destination = dest.position; } // Pass Unities AI the destination for the agent currentAction.agent.SetDestination(destination); } } else { // Force a new plan actionQueue = null; } } }
void LateUpdate() { // there is an action running, check if it has finished if (currentAction != null && currentAction.running) { // check if the agent has reached its destination if (currentAction.agent.hasPath && currentAction.agent.remainingDistance < 1f) { Debug.Log(gameObject.name + " has completed the action: " + currentAction.actionName); // run the complition of the action if (!invoked) { Invoke("CompleteAction", currentAction.duration); invoked = true; } } return; } if (planner == null || actionQueue == null) { // the agent has no plan create a new one 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, null); if (actionQueue != null) { currentGoal = sg.Key; break; } } } if (actionQueue != null) { if (actionQueue.Count == 0) { // action queue is empty if (currentGoal.remove) { goals.Remove(currentGoal); } planner = null; } else if (actionQueue.Count > 0) { // prepare the next action of the plan currentAction = actionQueue.Dequeue(); Debug.Log(gameObject.name + " is preparing a new action: " + currentAction.actionName); if (currentAction.PrePerform()) { if (currentAction.target == null && currentAction.targetTag != "") { currentAction.target = GameObject.FindWithTag(currentAction.targetTag); } if (currentAction.target != null) { currentAction.running = true; currentAction.agent.SetDestination(currentAction.target.transform.position); } } else { actionQueue = null; } } } }
public void CancelAction() { Debug.Log("Action Cancelled"); currentAction = null; planner = null; }
void LateUpdate() { if (currentAction != null && currentAction.running) { float distanceToTarget = Vector3.Distance(destination, this.transform.position); if (distanceToTarget < 2f)//currentAction.agent.remainingDistance < 1f { if (!invoked) { Invoke("CompleteAction", currentAction.duration); invoked = true; } } return; } if (planner == null || actionQueue == null) { planner = new GPlanner(); //IOrderedEnumerable<KeyValuePair<SubGoal, int>> sortedGoals = from entry in goals orderby entry.Value descending select entry; IOrderedEnumerable <KeyValuePair <SubGoal, int> > sortedGoals = goals.OrderByDescending(x => x.Value); foreach (KeyValuePair <SubGoal, int> subGoal in sortedGoals) { actionQueue = planner.Plan(actions, subGoal.Key.sgoals, beliefs); if (actionQueue != null) { currentGoal = subGoal.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.FindGameObjectWithTag(currentAction.targetTag); } if (currentAction.target != null) { currentAction.running = true; destination = currentAction.target.transform.position; Transform dest = currentAction.target.transform.Find("Destination"); if (dest != null) { destination = dest.position; } currentAction.agent.SetDestination(destination); } } else { actionQueue = null;//any action failed, re-plan } } }
void LateUpdate() { if (CurrentAction != null && CurrentAction.Running) { float distanceToTarget = Vector3.Distance //Manual distance Buffer for accurate action transitions (CurrentAction.Target.transform.position, this.transform.position); if (CurrentAction.Agent.hasPath && distanceToTarget < 2f) { //&& CurrentAction.Agent.remainingDistance < 1f if (!_invoked) { Invoke("CompleteAction", CurrentAction.Duration); _invoked = true; } } return; } if (_planner == null || _actionQue == null) { _planner = new GPlanner(); var sortedGoals = //Takes Goals and orders them based on the value (THIS IS RAD!) from entry in Goals orderby entry.Value descending select entry; foreach (KeyValuePair <SubGoal, int> sg in sortedGoals) {//Takes most important goal and assigns a plan through the planner _actionQue = _planner.Plan(Actions, sg.Key.SubGoals, Beliefs); if (_actionQue != null) { _currentGoal = sg.Key; break; } } } if (_actionQue != null && _actionQue.Count == 0) { if (_currentGoal.Remove) { Goals.Remove(_currentGoal); } _planner = null; } if (_actionQue != null && _actionQue.Count > 0) { CurrentAction = _actionQue.Dequeue(); if (CurrentAction.PrePerform()) { if (CurrentAction.Target == null && CurrentAction.TargetTag != "") { CurrentAction.Target = GameObject.FindWithTag(CurrentAction.TargetTag); } if (CurrentAction.Target != null) { CurrentAction.Running = true; CurrentAction.Agent.SetDestination (CurrentAction.Target.transform.position); } } else { _actionQue = null; //Tries a new plan } } }