void Update() { if (plan.Count == 0 && nextAction == null) // Player doesn't have a plan so it must replan { MakePlan(); // Displaying the new plan if there is a new plan (when all goals are achieved the plan will be empty) if (plan.Count != 0) planDisplay.GetComponent<Plan>().DisplayPlan(new List<AbstractAction>(plan)); } else if (nextAction == null) // Previous action is done, so player moves on to next action { nextAction = plan.Dequeue(); } else // Player is working on finishing the action stored in nextAction { if (!nextAction.IsInRange(this.transform)) // Player must be in range of trader/caravan to do action { player.MoveTowards(nextAction.GetActionObject()); } else if (timeStamp == -1) // Player has to stay by trader/caravan 0.5s to do action, so we must track time { timeStamp = Time.time; } else if (Time.time - timeStamp >= 0.5) // Player has stayed by trader/caravan 0.5s, so action can be executed { if (!nextAction.PreConditionsSatisfied(player.GetInventory())) // Something has changed since plan was made, action can't be done { planDisplay.GetComponent<Plan>().DisplayMessage("Plan failed, replanning"); plan = new Queue<AbstractAction>(); // We empty plan so planner can replan with new world state nextAction = null; } else // Action can be executed { (int[],int[]) updatedState = nextAction.DoAction(player.GetInventory()); player.ChangeInventory(updatedState.Item1); // Updating inventory caravan.UpdateSpices(updatedState.Item2); // Updating caravan nextAction = null; // Action has been executed, so nextAction is cleared timeStamp = -1; // Time counter is reset } } } }