diff --git a/Demo/Assets/CrashKonijn/GOAP/Demos/Complex/Actions/EatAction.cs b/Demo/Assets/CrashKonijn/GOAP/Demos/Complex/Actions/EatAction.cs index e8027889..6f6e3519 100644 --- a/Demo/Assets/CrashKonijn/GOAP/Demos/Complex/Actions/EatAction.cs +++ b/Demo/Assets/CrashKonijn/GOAP/Demos/Complex/Actions/EatAction.cs @@ -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; diff --git a/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Behaviours/SimpleHungerBehaviour.cs b/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Behaviours/SimpleHungerBehaviour.cs index ecb6c78f..69f1f5b9 100644 --- a/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Behaviours/SimpleHungerBehaviour.cs +++ b/Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Behaviours/SimpleHungerBehaviour.cs @@ -31,7 +31,7 @@ public void EnablePickup() { foreach (var pickupAppleAction in this.actionProvider.GetActions()) { - pickupAppleAction.Enable(); + this.actionProvider.Enable(pickupAppleAction); } } @@ -39,7 +39,8 @@ public void DisablePickup() { foreach (var pickupAppleAction in this.actionProvider.GetActions()) { - pickupAppleAction.Disable(ActionDisabler.ForTime(1f)); + + this.actionProvider.Disable(pickupAppleAction, ActionDisabler.ForTime(1f)); } } } diff --git a/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs b/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs index 25afedeb..737663de 100644 --- a/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs +++ b/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs @@ -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)) diff --git a/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IAction.cs b/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IAction.cs index 4f94d16a..88dd19ce 100644 --- a/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IAction.cs +++ b/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IAction.cs @@ -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); } } diff --git a/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IActionProvider.cs b/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IActionProvider.cs index 30f097eb..a4e18137 100644 --- a/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IActionProvider.cs +++ b/Package/Runtime/CrashKonijn.Agent.Core/Interfaces/IActionProvider.cs @@ -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); } } \ No newline at end of file diff --git a/Package/Runtime/CrashKonijn.Agent.Runtime/ActionBase.cs b/Package/Runtime/CrashKonijn.Agent.Runtime/ActionBase.cs index 6c3d1068..c8c43d49 100644 --- a/Package/Runtime/CrashKonijn.Agent.Runtime/ActionBase.cs +++ b/Package/Runtime/CrashKonijn.Agent.Runtime/ActionBase.cs @@ -6,8 +6,6 @@ public abstract class AgentActionBase where TActionData : IActionData, new() where TActionProperties : class, IActionProperties, new() { - private IActionDisabler disabler; - /// /// Gets the action data. /// @@ -50,54 +48,6 @@ public virtual void Created() { } /// The action data. public virtual void Start(IMonoAgent agent, TActionData data) { } - /// - /// Determines whether the action is enabled. This is used by the planner. - /// - /// The action receiver. - /// True if the action is enabled, otherwise false. - public bool IsEnabled(IActionReceiver agent) - { - return this.IsEnabled(agent, agent.Injector); - } - - /// - /// Determines whether the action is enabled. This is used by the planner. - /// - /// The action receiver. - /// The component references. - /// True if the action is enabled, otherwise false. - 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; - } - - /// - /// Enables the action. - /// - public void Enable() - { - this.disabler = null; - } - - /// - /// Disables the action. - /// - /// The action disabler. - public void Disable(IActionDisabler disabler) - { - this.disabler = disabler; - } - /// /// Called once before performing the action. Don't override this method, override the other BeforePerform method /// instead. diff --git a/Package/Runtime/CrashKonijn.Agent.Runtime/ActionProviderBase.cs b/Package/Runtime/CrashKonijn.Agent.Runtime/ActionProviderBase.cs index 2225acc3..d7a10aa0 100644 --- a/Package/Runtime/CrashKonijn.Agent.Runtime/ActionProviderBase.cs +++ b/Package/Runtime/CrashKonijn.Agent.Runtime/ActionProviderBase.cs @@ -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 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; + } } } \ No newline at end of file diff --git a/Package/Runtime/CrashKonijn.Goap.Core/Interfaces/IGoapAction.cs b/Package/Runtime/CrashKonijn.Goap.Core/Interfaces/IGoapAction.cs index c722a7af..345aecac 100644 --- a/Package/Runtime/CrashKonijn.Goap.Core/Interfaces/IGoapAction.cs +++ b/Package/Runtime/CrashKonijn.Goap.Core/Interfaces/IGoapAction.cs @@ -5,5 +5,8 @@ namespace CrashKonijn.Goap.Core public interface IGoapAction : IAction, IConnectable, IHasConfig { float GetCost(IActionReceiver agent, IComponentReference references, ITarget target); + bool IsEnabled(IActionReceiver agent); + void Enable(IActionReceiver receiver); + void Disable(IActionReceiver receiver, IActionDisabler disabler); } -} \ No newline at end of file +} diff --git a/Package/Runtime/CrashKonijn.Goap.Runtime/Behaviours/GoapActionBase.cs b/Package/Runtime/CrashKonijn.Goap.Runtime/Behaviours/GoapActionBase.cs index 676c3931..5da72bfe 100644 --- a/Package/Runtime/CrashKonijn.Goap.Runtime/Behaviours/GoapActionBase.cs +++ b/Package/Runtime/CrashKonijn.Goap.Runtime/Behaviours/GoapActionBase.cs @@ -150,5 +150,44 @@ public bool IsExecutable(IActionReceiver agent, bool conditionsMet) return true; } + + /// + /// Determines whether the action is enabled. This is used by the planner. + /// + /// The action receiver. + /// True if the action is enabled, otherwise false. + public bool IsEnabled(IActionReceiver agent) + { + return this.IsEnabled(agent, agent.Injector); + } + + /// + /// Determines whether the action is enabled. This is used by the planner. + /// + /// The action receiver. + /// The component references. + /// True if the action is enabled, otherwise false. + public virtual bool IsEnabled(IActionReceiver receiver, IComponentReference references) + { + return !receiver.ActionProvider.IsDisabled(this); + } + + /// + /// Enables the action. + /// + public void Enable(IActionReceiver receiver) + { + receiver.ActionProvider.Enable(this); + } + + /// + /// Disables the action. + /// + /// The action receiver + /// The action disabler. + public void Disable(IActionReceiver receiver, IActionDisabler disabler) + { + receiver.ActionProvider.Disable(this, disabler); + } } }