Skip to content

Commit

Permalink
Fixes #319
Browse files Browse the repository at this point in the history
  • Loading branch information
crashkonijn committed Dec 19, 2024
1 parent 8919489 commit ca3aad8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override IActionRunState Perform(IMonoAgent agent, Data data, IActionCont

public override void End(IMonoAgent agent, Data data)
{
this.Disable(ActionDisabler.ForTime(5f));
this.Disable(agent, ActionDisabler.ForTime(5f));

if (data.Eatable == null)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ public void EnablePickup()
{
foreach (var pickupAppleAction in this.actionProvider.GetActions<PickupAppleAction>())
{
pickupAppleAction.Enable();
this.actionProvider.Enable(pickupAppleAction);
}
}

public void DisablePickup()
{
foreach (var pickupAppleAction in this.actionProvider.GetActions<PickupAppleAction>())
{
pickupAppleAction.Disable(ActionDisabler.ForTime(1f));

this.actionProvider.Disable(pickupAppleAction, ActionDisabler.ForTime(1f));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private void UpdateClasses(EditorWindowValues values, INode graphNode, IMonoGoap
return;
}

if (graphNode.Action is not IAction action)
if (graphNode.Action is not IGoapAction action)
return;

if (!action.IsEnabled(provider.Receiver))
Expand Down
3 changes: 0 additions & 3 deletions Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,5 @@ public interface IAction
void Complete(IMonoAgent agent, IActionData data);

bool IsExecutable(IActionReceiver agent, bool conditionsMet);
bool IsEnabled(IActionReceiver agent);
void Enable();
void Disable(IActionDisabler disabler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ public interface IActionProvider
{
IActionReceiver Receiver { get; set; }
void ResolveAction();
bool IsDisabled(IAction action);
void Enable(IAction action);
void Disable(IAction action, IActionDisabler disabler);
}
}
50 changes: 0 additions & 50 deletions Package/Runtime/CrashKonijn.Agent.Runtime/ActionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ public abstract class AgentActionBase<TActionData, TActionProperties>
where TActionData : IActionData, new()
where TActionProperties : class, IActionProperties, new()
{
private IActionDisabler disabler;

/// <summary>
/// Gets the action data.
/// </summary>
Expand Down Expand Up @@ -50,54 +48,6 @@ public virtual void Created() { }
/// <param name="data">The action data.</param>
public virtual void Start(IMonoAgent agent, TActionData data) { }

/// <summary>
/// Determines whether the action is enabled. This is used by the planner.
/// </summary>
/// <param name="agent">The action receiver.</param>
/// <returns>True if the action is enabled, otherwise false.</returns>
public bool IsEnabled(IActionReceiver agent)
{
return this.IsEnabled(agent, agent.Injector);
}

/// <summary>
/// Determines whether the action is enabled. This is used by the planner.
/// </summary>
/// <param name="receiver">The action receiver.</param>
/// <param name="references">The component references.</param>
/// <returns>True if the action is enabled, otherwise false.</returns>
public virtual bool IsEnabled(IActionReceiver receiver, IComponentReference references)
{
if (this.disabler == null)
return true;

if (receiver is not IMonoAgent agent)
return true;

if (this.disabler.IsDisabled(agent))
return false;

this.Enable();
return true;
}

/// <summary>
/// Enables the action.
/// </summary>
public void Enable()
{
this.disabler = null;
}

/// <summary>
/// Disables the action.
/// </summary>
/// <param name="disabler">The action disabler.</param>
public void Disable(IActionDisabler disabler)
{
this.disabler = disabler;
}

/// <summary>
/// Called once before performing the action. Don't override this method, override the other BeforePerform method
/// instead.
Expand Down
30 changes: 29 additions & 1 deletion Package/Runtime/CrashKonijn.Agent.Runtime/ActionProviderBase.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
using CrashKonijn.Agent.Core;
using System.Collections.Generic;
using CrashKonijn.Agent.Core;
using UnityEngine;

namespace CrashKonijn.Agent.Runtime
{
public abstract class ActionProviderBase : MonoBehaviour, IActionProvider
{
private Dictionary<IAction, IActionDisabler> disablers = new();

public abstract IActionReceiver Receiver { get; set; }
public abstract void ResolveAction();

public bool IsDisabled(IAction action)
{
if (!this.disablers.TryGetValue(action, out var disabler))
return true;

if (this.Receiver is not IMonoAgent agent)
return true;

if (disabler.IsDisabled(agent))
return false;

this.Enable(action);
return true;
}

public void Enable(IAction action)
{
this.disablers.Remove(action);
}

public void Disable(IAction action, IActionDisabler disabler)
{
this.disablers[action] = disabler;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ namespace CrashKonijn.Goap.Core
public interface IGoapAction : IAction, IConnectable, IHasConfig<IActionConfig>
{
float GetCost(IActionReceiver agent, IComponentReference references, ITarget target);
bool IsEnabled(IActionReceiver agent);
void Enable(IActionReceiver receiver);
void Disable(IActionReceiver receiver, IActionDisabler disabler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,44 @@ public bool IsExecutable(IActionReceiver agent, bool conditionsMet)

return true;
}

/// <summary>
/// Determines whether the action is enabled. This is used by the planner.
/// </summary>
/// <param name="agent">The action receiver.</param>
/// <returns>True if the action is enabled, otherwise false.</returns>
public bool IsEnabled(IActionReceiver agent)
{
return this.IsEnabled(agent, agent.Injector);
}

/// <summary>
/// Determines whether the action is enabled. This is used by the planner.
/// </summary>
/// <param name="receiver">The action receiver.</param>
/// <param name="references">The component references.</param>
/// <returns>True if the action is enabled, otherwise false.</returns>
public virtual bool IsEnabled(IActionReceiver receiver, IComponentReference references)
{
return !receiver.ActionProvider.IsDisabled(this);
}

/// <summary>
/// Enables the action.
/// </summary>
public void Enable(IActionReceiver receiver)
{
receiver.ActionProvider.Enable(this);
}

/// <summary>
/// Disables the action.
/// </summary>
/// <param name="receiver">The action receiver</param>
/// <param name="disabler">The action disabler.</param>
public void Disable(IActionReceiver receiver, IActionDisabler disabler)
{
receiver.ActionProvider.Disable(this, disabler);
}
}
}

0 comments on commit ca3aad8

Please sign in to comment.