Skip to content

Commit

Permalink
Merge pull request #277 from crashkonijn/feature/added-config-callbacks
Browse files Browse the repository at this point in the history
Feature: added config callbacks
  • Loading branch information
crashkonijn authored Oct 9, 2024
2 parents 1103be6 + 5dbc79e commit 6622c69
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using CrashKonijn.Goap.Demos.Complex.Targets;
using CrashKonijn.Goap.Demos.Complex.WorldKeys;
using CrashKonijn.Goap.Runtime;
using UnityEngine;

namespace CrashKonijn.Goap.Demos.Complex.Factories
{
Expand All @@ -17,7 +18,7 @@ public class CleanerAgentTypeConfigFactory : AgentTypeFactoryBase
public override IAgentTypeConfig Create()
{
var builder = new AgentTypeBuilder(SetIds.Cleaner);

builder.AddCapability<BaseCapability>();
builder.AddCapability<WanderCapability>();
builder.AddCapability<HungerCapability>();
Expand All @@ -27,13 +28,17 @@ public override IAgentTypeConfig Create()
capability.AddGoal<CleanItemsGoal>()
.SetBaseCost(20)
.AddCondition<ItemsOnFloor>(Comparison.SmallerThanOrEqual, 0);

capability.AddAction<HaulItemAction>()
.SetTarget<HaulTarget>()
.AddEffect<ItemsOnFloor>(EffectType.Decrease)
.AddCondition<ItemsOnFloor>(Comparison.GreaterThanOrEqual, 1)
.SetMoveMode(ActionMoveMode.PerformWhileMoving);

.SetMoveMode(ActionMoveMode.PerformWhileMoving)
.SetCallback((action) =>
{
Debug.Log($"Action callback: {action}");
});

capability.AddWorldSensor<ItemOnFloorSensor>()
.SetKey<ItemsOnFloor>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CrashKonijn.Agent.Core;
using System;
using CrashKonijn.Agent.Core;

namespace CrashKonijn.Goap.Core
{
Expand All @@ -14,4 +15,9 @@ public interface IActionConfig : IClassConfig
public ActionMoveMode MoveMode { get; }
public IActionProperties Properties { get; }
}
}

public interface IClassCallbackConfig
{
public Action<object> Callback { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ private List<IGoapAction> GetActions(IAgentTypeConfig config)

actions.ForEach(x =>
{
if (x.Config is IClassCallbackConfig classCallbackConfig)
classCallbackConfig.Callback?.Invoke(x);

injector.Inject(x);
x.Created();
});
Expand All @@ -79,6 +82,9 @@ private List<IGoal> GetGoals(IAgentTypeConfig config)

goals.ForEach(x =>
{
if (x.Config is IClassCallbackConfig classCallbackConfig)
classCallbackConfig.Callback?.Invoke(x);

x.Index = index;
index++;

Expand All @@ -95,6 +101,9 @@ private List<IWorldSensor> GetWorldSensors(IAgentTypeConfig config)

worldSensors.ForEach(x =>
{
if (x.Config is IClassCallbackConfig classCallbackConfig)
classCallbackConfig.Callback?.Invoke(x);

injector.Inject(x);
x.Created();
});
Expand All @@ -109,6 +118,9 @@ private List<ITargetSensor> GetTargetSensors(IAgentTypeConfig config)

targetSensors.ForEach(x =>
{
if (x.Config is IClassCallbackConfig classCallbackConfig)
classCallbackConfig.Callback?.Invoke(x);

injector.Inject(x);
x.Created();
});
Expand All @@ -123,6 +135,9 @@ private List<IMultiSensor> GetMultiSensors(IAgentTypeConfig config)

multiSensor.ForEach(x =>
{
if (x.Config is IClassCallbackConfig classCallbackConfig)
classCallbackConfig.Callback?.Invoke(x);

injector.Inject(x);
x.Created();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,56 @@

namespace CrashKonijn.Goap.Runtime
{
public class ActionBuilder
public class ActionBuilder<T> : ActionBuilder
where T : IAction
{
private readonly ActionConfig config;
private readonly List<ICondition> conditions = new();
private readonly List<IEffect> effects = new();
private readonly WorldKeyBuilder worldKeyBuilder;
private readonly TargetKeyBuilder targetKeyBuilder;
private readonly Type actionType;

public ActionBuilder(Type actionType, WorldKeyBuilder worldKeyBuilder, TargetKeyBuilder targetKeyBuilder)
{
this.actionType = actionType;
this.worldKeyBuilder = worldKeyBuilder;
this.targetKeyBuilder = targetKeyBuilder;
public ActionBuilder(WorldKeyBuilder worldKeyBuilder, TargetKeyBuilder targetKeyBuilder) : base(typeof(T), worldKeyBuilder, targetKeyBuilder) { }

var propType = this.GetPropertiesType();

this.config = new ActionConfig
{
Name = actionType.Name,
ClassType = actionType.AssemblyQualifiedName,
BaseCost = 1,
StoppingDistance = 0.5f,
RequiresTarget = true,
ValidateConditions = true,
Properties = (IActionProperties) Activator.CreateInstance(propType),
};
}

public ActionBuilder SetTarget<TTargetKey>()
public ActionBuilder<T> SetTarget<TTargetKey>()
where TTargetKey : ITargetKey
{
this.config.Target = this.targetKeyBuilder.GetKey<TTargetKey>();
return this;
}

public ActionBuilder SetBaseCost(float baseCost)
public ActionBuilder<T> SetBaseCost(float baseCost)
{
this.config.BaseCost = baseCost;
return this;
}

public ActionBuilder SetRequiresTarget(bool requiresTarget)
public ActionBuilder<T> SetRequiresTarget(bool requiresTarget)
{
this.config.RequiresTarget = requiresTarget;
return this;
}

public ActionBuilder SetValidateConditions(bool validate)
public ActionBuilder<T> SetValidateConditions(bool validate)
{
this.config.ValidateConditions = validate;
return this;
}

public ActionBuilder SetStoppingDistance(float inRange)
public ActionBuilder<T> SetStoppingDistance(float inRange)
{
this.config.StoppingDistance = inRange;
return this;
}

[Obsolete("Use `SetStoppingDistance(float inRange)` instead.")]
public ActionBuilder SetInRange(float inRange)
public ActionBuilder<T> SetInRange(float inRange)
{
this.config.StoppingDistance = inRange;
return this;
}

public ActionBuilder SetMoveMode(ActionMoveMode moveMode)
public ActionBuilder<T> SetMoveMode(ActionMoveMode moveMode)
{
this.config.MoveMode = moveMode;
return this;
}

public ActionBuilder AddCondition<TWorldKey>(Comparison comparison, int amount)
public ActionBuilder<T> AddCondition<TWorldKey>(Comparison comparison, int amount)
where TWorldKey : IWorldKey
{
this.conditions.Add(new Condition
Expand All @@ -93,7 +69,7 @@ public ActionBuilder AddCondition<TWorldKey>(Comparison comparison, int amount)
}

[Obsolete("Use `AddEffect<TWorldKey>(EffectType type)` instead.")]
public ActionBuilder AddEffect<TWorldKey>(bool increase)
public ActionBuilder<T> AddEffect<TWorldKey>(bool increase)
where TWorldKey : IWorldKey
{
this.effects.Add(new Effect
Expand All @@ -105,7 +81,7 @@ public ActionBuilder AddEffect<TWorldKey>(bool increase)
return this;
}

public ActionBuilder AddEffect<TWorldKey>(EffectType type)
public ActionBuilder<T> AddEffect<TWorldKey>(EffectType type)
where TWorldKey : IWorldKey
{
this.effects.Add(new Effect
Expand All @@ -117,15 +93,51 @@ public ActionBuilder AddEffect<TWorldKey>(EffectType type)
return this;
}

public ActionBuilder SetProperties(IActionProperties properties)
public ActionBuilder<T> SetProperties(IActionProperties properties)
{
this.ValidateProperties(properties);

this.config.Properties = properties;
return this;
}

private void ValidateProperties(IActionProperties properties)
public ActionBuilder<T> SetCallback(Action<T> callback)
{
this.config.Callback = (obj) => callback((T) obj);
return this;
}
}

public class ActionBuilder
{
protected readonly ActionConfig config;
protected readonly List<ICondition> conditions = new();
protected readonly List<IEffect> effects = new();
protected readonly WorldKeyBuilder worldKeyBuilder;
protected readonly TargetKeyBuilder targetKeyBuilder;
protected readonly Type actionType;

public ActionBuilder(Type actionType, WorldKeyBuilder worldKeyBuilder, TargetKeyBuilder targetKeyBuilder)
{
this.actionType = actionType;
this.worldKeyBuilder = worldKeyBuilder;
this.targetKeyBuilder = targetKeyBuilder;

var propType = this.GetPropertiesType();

this.config = new ActionConfig
{
Name = actionType.Name,
ClassType = actionType.AssemblyQualifiedName,
BaseCost = 1,
StoppingDistance = 0.5f,
RequiresTarget = true,
ValidateConditions = true,
Properties = (IActionProperties) Activator.CreateInstance(propType),
};
}

protected void ValidateProperties(IActionProperties properties)
{
var actionPropsType = this.GetPropertiesType();

Expand All @@ -135,7 +147,7 @@ private void ValidateProperties(IActionProperties properties)
throw new ArgumentException($"The provided properties do not match the expected type '{actionPropsType.Name}'.", nameof(properties));
}

private Type GetPropertiesType()
protected Type GetPropertiesType()
{
var baseType = this.actionType.BaseType;

Expand All @@ -161,10 +173,10 @@ public IActionConfig Build()
return this.config;
}

public static ActionBuilder Create<TAction>(WorldKeyBuilder worldKeyBuilder, TargetKeyBuilder targetKeyBuilder)
public static ActionBuilder<TAction> Create<TAction>(WorldKeyBuilder worldKeyBuilder, TargetKeyBuilder targetKeyBuilder)
where TAction : IAction
{
return new ActionBuilder(typeof(TAction), worldKeyBuilder, targetKeyBuilder);
return new ActionBuilder<TAction>(worldKeyBuilder, targetKeyBuilder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public CapabilityBuilder(string name)
this.capabilityConfig = new CapabilityConfig(name);
}

public ActionBuilder AddAction<TAction>()
public ActionBuilder<TAction> AddAction<TAction>()
where TAction : IAction
{
var actionBuilder = ActionBuilder.Create<TAction>(this.worldKeyBuilder, this.targetKeyBuilder);
Expand All @@ -107,7 +107,7 @@ public ActionBuilder AddAction<TAction>()
return actionBuilder;
}

public GoalBuilder AddGoal<TGoal>()
public GoalBuilder<TGoal> AddGoal<TGoal>()
where TGoal : IGoal
{
var goalBuilder = GoalBuilder.Create<TGoal>(this.worldKeyBuilder);
Expand All @@ -117,7 +117,7 @@ public GoalBuilder AddGoal<TGoal>()
return goalBuilder;
}

public WorldSensorBuilder AddWorldSensor<TWorldSensor>()
public WorldSensorBuilder<TWorldSensor> AddWorldSensor<TWorldSensor>()
where TWorldSensor : IWorldSensor
{
var worldSensorBuilder = WorldSensorBuilder.Create<TWorldSensor>(this.worldKeyBuilder);
Expand All @@ -127,7 +127,7 @@ public WorldSensorBuilder AddWorldSensor<TWorldSensor>()
return worldSensorBuilder;
}

public TargetSensorBuilder AddTargetSensor<TTargetSensor>()
public TargetSensorBuilder<TTargetSensor> AddTargetSensor<TTargetSensor>()
where TTargetSensor : ITargetSensor
{
var targetSensorBuilder = TargetSensorBuilder.Create<TTargetSensor>(this.targetKeyBuilder);
Expand All @@ -137,10 +137,10 @@ public TargetSensorBuilder AddTargetSensor<TTargetSensor>()
return targetSensorBuilder;
}

public MultiSensorBuilder AddMultiSensor<TMultiSensor>()
public MultiSensorBuilder<TMultiSensor> AddMultiSensor<TMultiSensor>()
where TMultiSensor : IMultiSensor
{
var multiSensorBuilder = MultiSensorBuilder.Create<TMultiSensor>(this.worldKeyBuilder);
var multiSensorBuilder = MultiSensorBuilder.Create<TMultiSensor>();

this.multiSensorBuilders.Add(multiSensorBuilder);

Expand Down
Loading

0 comments on commit 6622c69

Please sign in to comment.