Skip to content

Commit

Permalink
Merge pull request #23 from crashkonijn/feature/added-goal-complete-e…
Browse files Browse the repository at this point in the history
…vent

Feature/added goal complete event
  • Loading branch information
crashkonijn authored May 10, 2023
2 parents 1f0334f + c7ca264 commit ec36688
Show file tree
Hide file tree
Showing 27 changed files with 219 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Demo/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ PlayerSettings:
androidFullscreenMode: 1
defaultIsNativeResolution: 0
macRetinaSupport: 1
runInBackground: 0
runInBackground: 1
captureSingleScreen: 0
muteOtherAudioSources: 0
Prepare IOS For Recording: 0
Expand Down
7 changes: 7 additions & 0 deletions Package/Documentation/Classes/AgentBehaviour.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace Demos.Complex.Behaviours
this.agent.Events.OnActionStop += this.OnActionStop;
this.agent.Events.OnGoalStart += this.OnGoalStart;
this.agent.Events.OnNoActionFound += this.OnNoActionFound;
this.agent.Events.OnGoalCompleted += this.OnGoalCompleted;
}

private void OnDisable()
Expand All @@ -97,6 +98,7 @@ namespace Demos.Complex.Behaviours
this.agent.Events.OnActionStop -= this.OnActionStop;
this.agent.Events.OnGoalStart -= this.OnGoalStart;
this.agent.Events.OnNoActionFound -= this.OnNoActionFound;
this.agent.Events.OnGoalCompleted -= this.OnGoalCompleted;
}

private void OnActionStart(IActionBase action)
Expand All @@ -115,6 +117,11 @@ namespace Demos.Complex.Behaviours
// Gets called when a goal is started
}

private void OnGoalCompleted(IGoalBase goal)
{
// Gets called when a goal is completed
}

private void OnNoActionFound(IGoalBase goal)
{
// Gets called when no action is found for a goal
Expand Down
Binary file modified Package/Documentation/images/lifecycle_goap_runner_run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Package/Runtime/CrashKonijn.Goap.Resolver/CostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CrashKonijn.Goap.Resolver
{
public class CostBuilder
public class CostBuilder : ICostBuilder
{
private readonly List<IAction> actionIndexList;
private float[] costList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CrashKonijn.Goap.Resolver
{
public class ExecutableBuilder
public class ExecutableBuilder : IExecutableBuilder
{
private readonly List<IAction> actionIndexList;
private bool[] executableList;
Expand Down
8 changes: 4 additions & 4 deletions Package/Runtime/CrashKonijn.Goap.Resolver/GraphResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace CrashKonijn.Goap.Resolver
{
public class GraphResolver
public class GraphResolver : IGraphResolver
{
private readonly List<Node> indexList;
private readonly List<IAction> actionIndexList;
Expand Down Expand Up @@ -47,17 +47,17 @@ public IResolveHandle StartResolve(RunData runData)
return new ResolveHandle(this, this.map, runData);
}

public ExecutableBuilder GetExecutableBuilder()
public IExecutableBuilder GetExecutableBuilder()
{
return new ExecutableBuilder(this.actionIndexList);
}

public PositionBuilder GetPositionBuilder()
public IPositionBuilder GetPositionBuilder()
{
return new PositionBuilder(this.actionIndexList);
}

public CostBuilder GetCostBuilder()
public ICostBuilder GetCostBuilder()
{
return new CostBuilder(this.actionIndexList);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CrashKonijn.Goap.Resolver.Interfaces
{
public interface ICostBuilder
{
CostBuilder SetCost(IAction action, float cost);
float[] Build();
void Clear();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CrashKonijn.Goap.Resolver.Interfaces
{
public interface IExecutableBuilder
{
ExecutableBuilder SetExecutable(IAction action, bool executable);
void Clear();
bool[] Build();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CrashKonijn.Goap.Resolver.Models;

namespace CrashKonijn.Goap.Resolver.Interfaces
{
public interface IGraphResolver
{
IResolveHandle StartResolve(RunData runData);
IExecutableBuilder GetExecutableBuilder();
IPositionBuilder GetPositionBuilder();
ICostBuilder GetCostBuilder();
Graph GetGraph();
int GetIndex(IAction action);
IAction GetAction(int index);
void Dispose();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Unity.Mathematics;
using UnityEngine;

namespace CrashKonijn.Goap.Resolver.Interfaces
{
public interface IPositionBuilder
{
PositionBuilder SetPosition(IAction action, Vector3 position);
float3[] Build();
void Clear();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace CrashKonijn.Goap.Resolver
{
public class PositionBuilder
public class PositionBuilder : IPositionBuilder
{
private readonly List<IAction> actionIndexList;
private float3[] executableList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public struct AgentDebugGraph
{
public List<IGoalBase> Goals { get; set; }
public List<IActionBase> Actions { get; set; }
public GoapConfig Config { get; set; }
public IGoapConfig Config { get; set; }
}
}
8 changes: 7 additions & 1 deletion Package/Runtime/CrashKonijn.Goap/Behaviours/AgentEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ public void GoalStart(IGoalBase goal)
{
this.OnGoalStart?.Invoke(goal);
}


public event GoalDelegate OnGoalCompleted;
public void GoalCompleted(IGoalBase goal)
{
this.OnGoalCompleted?.Invoke(goal);
}

public event GoalDelegate OnNoActionFound;
public void NoActionFound(IGoalBase goal)
{
Expand Down
2 changes: 1 addition & 1 deletion Package/Runtime/CrashKonijn.Goap/Classes/GoapConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace CrashKonijn.Goap.Classes
{
public class GoapConfig
public class GoapConfig : IGoapConfig
{
public IConditionObserver ConditionObserver { get; set; }
public IKeyResolver KeyResolver { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions Package/Runtime/CrashKonijn.Goap/Classes/GoapSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class GoapSet : IGoapSet
{
public string Id { get; }
public IAgentCollection Agents { get; } = new AgentCollection();
public GoapConfig GoapConfig { get; }
public IGoapConfig GoapConfig { get; }
public ISensorRunner SensorRunner { get; }

private List<IGoalBase> goals;
private List<IActionBase> actions;

public GoapSet(string id, GoapConfig config, List<IGoalBase> goals, List<IActionBase> actions, SensorRunner sensorRunner)
public GoapSet(string id, IGoapConfig config, List<IGoalBase> goals, List<IActionBase> actions, SensorRunner sensorRunner)
{
this.Id = id;
this.GoapConfig = config;
Expand Down
4 changes: 2 additions & 2 deletions Package/Runtime/CrashKonijn.Goap/Classes/GoapSetFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace CrashKonijn.Goap.Classes
{
public class GoapSetFactory
{
private readonly GoapConfig goapConfig;
private readonly IGoapConfig goapConfig;
private IGoapSetConfigValidatorRunner goapSetConfigValidatorRunner = new GoapSetConfigValidatorRunner();

public GoapSetFactory(GoapConfig goapConfig)
public GoapSetFactory(IGoapConfig goapConfig)
{
this.goapConfig = goapConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Linq;
using CrashKonijn.Goap.Interfaces;
using CrashKonijn.Goap.Resolver;
using CrashKonijn.Goap.Resolver.Models;

namespace CrashKonijn.Goap.Classes.Runners
Expand All @@ -14,7 +15,7 @@ public class GoapRunner : IGoapRunner
public float RunTime { get; private set; }
public float CompleteTime { get; private set; }

public void Register(IGoapSet set) => this.sets.Add(set, new GoapSetJobRunner(set));
public void Register(IGoapSet set) => this.sets.Add(set, new GoapSetJobRunner(set, new GraphResolver(set.GetAllNodes().ToArray(), set.GoapConfig.KeyResolver)));

public void Run()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace CrashKonijn.Goap.Classes.Runners
public class GoapSetJobRunner
{
private readonly IGoapSet goapSet;
private readonly GraphResolver resolver;
private readonly IGraphResolver resolver;

private List<JobRunHandle> resolveHandles = new();
private readonly ExecutableBuilder executableBuilder;
private readonly PositionBuilder positionBuilder;
private readonly CostBuilder costBuilder;
private readonly IExecutableBuilder executableBuilder;
private readonly IPositionBuilder positionBuilder;
private readonly ICostBuilder costBuilder;

public GoapSetJobRunner(IGoapSet goapSet)
public GoapSetJobRunner(IGoapSet goapSet, IGraphResolver graphResolver)
{
this.goapSet = goapSet;
this.resolver = new GraphResolver(goapSet.GetAllNodes().ToArray(), goapSet.GoapConfig.KeyResolver);
this.resolver = graphResolver;

this.executableBuilder = this.resolver.GetExecutableBuilder();
this.positionBuilder = this.resolver.GetPositionBuilder();
Expand Down Expand Up @@ -56,6 +56,12 @@ private void Run(GlobalWorldData globalData, IMonoAgent agent)

var localData = this.goapSet.SensorRunner.SenseLocal(globalData, agent);

if (this.IsGoalCompleted(localData, agent))
{
agent.Events.GoalCompleted(agent.CurrentGoal);
return;
}

this.FillBuilders(localData, agent);

this.resolveHandles.Add(new JobRunHandle(agent)
Expand Down Expand Up @@ -93,6 +99,14 @@ private void FillBuilders(LocalWorldData localData, IMonoAgent agent)
}
}

private bool IsGoalCompleted(LocalWorldData localData, IMonoAgent agent)
{
var conditionObserver = this.goapSet.GoapConfig.ConditionObserver;
conditionObserver.SetWorldData(localData);

return agent.CurrentGoal.Conditions.All(x => conditionObserver.IsMet(x));
}

public void Complete()
{
foreach (var resolveHandle in this.resolveHandles)
Expand Down
3 changes: 3 additions & 0 deletions Package/Runtime/CrashKonijn.Goap/Interfaces/IAgentEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public interface IAgentEvents
event GoalDelegate OnGoalStart;
void GoalStart(IGoalBase goal);

event GoalDelegate OnGoalCompleted;
void GoalCompleted(IGoalBase goal);

event GoalDelegate OnNoActionFound;
void NoActionFound(IGoalBase goal);
}
Expand Down
11 changes: 11 additions & 0 deletions Package/Runtime/CrashKonijn.Goap/Interfaces/IGoapConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using CrashKonijn.Goap.Observers;

namespace CrashKonijn.Goap.Interfaces
{
public interface IGoapConfig
{
IConditionObserver ConditionObserver { get; set; }
IKeyResolver KeyResolver { get; set; }
IGoapInjector GoapInjector { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package/Runtime/CrashKonijn.Goap/Interfaces/IGoapSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CrashKonijn.Goap.Interfaces
public interface IGoapSet
{
string Id { get; }
GoapConfig GoapConfig { get; }
IGoapConfig GoapConfig { get; }
IAgentCollection Agents { get; }
ISensorRunner SensorRunner { get; }
void Register(IMonoAgent agent);
Expand Down
Loading

0 comments on commit ec36688

Please sign in to comment.