using System; using Goap.Agent.Core; using Goap.Core; namespace Goap.Runtime { public abstract class LocalWorldSensorBase : ILocalWorldSensor { public IWorldKey Key => this.Config.Key; public virtual ISensorTimer Timer => SensorTimer.Always; public IWorldSensorConfig Config { get; private set; } public void SetConfig(IWorldSensorConfig config) => this.Config = config; /// /// Called when the sensor is created. /// public abstract void Created(); /// /// Called when the sensor needs to update. Use this for caching data. /// public abstract void Update(); public Type[] GetKeys() => new[] { this.Key.GetType() }; /// /// Senses the world data using this sensor. /// /// The world data. /// The action receiver. /// Use this to get cached component references on the agent public void Sense(IWorldData data, IActionReceiver agent, IComponentReference references) { var state = data.GetWorldState(TypeReSolveHelper.ResolveType(this.Key)); if (!this.Timer.ShouldSense(state?.Timer)) return; data.SetState(this.Key, this.Sense(agent, references)); } /// /// Senses the world data using the specified action receiver and component references. /// /// The action receiver. /// Use this to get cached component references on the agent. /// The sensed value. public virtual SenseValue Sense(IActionReceiver agent, IComponentReference references) { #pragma warning disable CS0618 // Type or member is obsolete return this.Sense(agent as IMonoAgent, references); #pragma warning restore CS0618 // Type or member is obsolete } [Obsolete("This should not be called anymore! Use 'Sense(IActionReceiver agent, IComponentReference references)' instead.")] public virtual SenseValue Sense(IMonoAgent agent, IComponentReference references) { throw new GoapException("This should not be called anymore!"); } } }