Skip to content

Commit

Permalink
Added fail condition for GOAPPlanner. Added Awareness.GetNearest.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastisme committed Apr 2, 2022
1 parent 8a6868b commit 6d59a51
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
34 changes: 20 additions & 14 deletions Assets/Runtime/Actions/PickupWood.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace GOAP{
public class PickUpWood : GOAPAction
{
Movement movement;
Vision vision;
GameObject targetWood;
Awareness awareness;
Detectable targetWood;

public override float GetCost(){
return 0.0f;
}
public override void Setup(ref WorldState worldState){
movement = GetComponent<Movement>();
vision = GetComponent<Vision>();
awareness = GetComponent<Awareness>();
this.worldState = worldState;
requiredState = new WorldState();
requiredState.boolKeys["WoodNearby"] = true;
Expand All @@ -26,22 +26,28 @@ public override void Setup(ref WorldState worldState){

public override void OnActivated()
{
//targetWood = vision.GetNearest(objectTag:"Wood");
movement.GoTo(targetWood);
}
targetWood = awareness.GetNearest("Wood");
if (targetWood != null){
movement.GoTo(targetWood);
}


}

public override void OnTick(){
/*
if (targetWood == null){
targetWood = vision.GetNearest(objectTag:"Wood");
if (CanRun()){
if (targetWood == null){
targetWood = awareness.GetNearest("Wood");
if (targetWood == null)
{
StopAction();
return;
}
}
movement.GoTo(targetWood);
}
if (movement.AtTarget()){
worldState.boolKeys["HoldingWood"] = true;
if (movement.AtTarget()){
worldState.boolKeys["HoldingWood"] = true;
}
}
*/
}
}
}
24 changes: 24 additions & 0 deletions Assets/Runtime/Awareness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ public Dictionary<Detectable,NearbyObject> nearbyObjects {

Dictionary<string, int> nearbyObjectCounts = new Dictionary<string, int>();

public Detectable GetNearest(string name){
if (!(nearbyObjectCounts.ContainsKey(name)) || nearbyObjectCounts[name] >=0){
return null;
}
int totalCount = nearbyObjectCounts[name];
int count = 0;
Detectable nearest;
float minDistance = -1f;
foreach(var obj in nearbyObjects){
if (obj.Key.name == name){
float distance = (obj.Key.transform.position - transform.position).sqrMagnitude;
if (minDistance < 0 || distance < minDistance){
nearest = obj.Key;
minDistance = distance;
count++;
if (count == totalCount){
return nearest;
}
}
}
}
return null;
}

public void SetWorldState(ref WorldState worldState){
this.worldState = worldState;
}
Expand Down
17 changes: 16 additions & 1 deletion Assets/Runtime/GOAPPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,22 @@ void OnTickGoals(){
}

void OnTickActivePlan(){
if (!(activeGoal != null && activePlan != null)){return;}

// Nothing to run
if (!(activeGoal != null && activePlan != null)){ return; }

// Plan no longer viable
if (!(activePlan[activeActionIdx].CanRun())){ OnFailActivePlan(); }

activePlan[activeActionIdx].OnTick();

if (activePlan[activeActionIdx].outputState.IsSubset(worldState)){
// Action complete
activePlan[activeActionIdx].OnDeactivated();
activeActionIdx++;

if (activeActionIdx < activePlan.Count){
// Start new action
activePlan[activeActionIdx].OnActivated();
}
else{
Expand All @@ -111,7 +121,12 @@ void OnCompleteActivePlan(){
activeGoal.OnDeactivated();
activeGoal = null;
activePlan = null;
}

void OnFailActivePlan(){
activeGoal.OnDeactivated();
activeGoal = null;
activePlan = null;
}

void GetHighestPriorityGoal(out Goal chosenGoal, out List<GOAPAction> chosenPlan){
Expand Down
9 changes: 9 additions & 0 deletions Assets/Runtime/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Sensors;

[RequireComponent(typeof(NavMeshAgent))]
public class Movement : MonoBehaviour
Expand Down Expand Up @@ -42,6 +43,14 @@ public void GoTo(GameObject target, float speed){
GoTo(target:target.transform.position, speed:speed);
}

public void GoTo(Detectable target){
GoTo(target.transform.position);
}

public void GoTo(Detectable target, float speed){
GoTo(target:target.transform.position, speed:speed);
}

public bool AtTarget(){
return nav.remainingDistance < nav.stoppingDistance;
}
Expand Down

0 comments on commit 6d59a51

Please sign in to comment.