void Start() { stateMachine = new FSM(); availableActions = new HashSet <GOAP.Action>(); currentActions = new Queue <GOAP.Action>(); planner = new GOAP.Planner(); FindDataProvider(); CreateIdleState(); CreateMoveToState(); CreatePerformActionState(); stateMachine.PushState(idleState); LoadActions(); }
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; } } }
// 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 } } }