rj-action-library/Runtime/Sensors/Sensor.cs

71 lines
1.5 KiB
C#
Raw Normal View History

2025-01-03 12:09:23 +00:00
using Godot;
namespace Rokojori
{
[Tool]
2025-01-21 20:58:56 +00:00
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/Sensor.svg")]
2025-01-03 12:09:23 +00:00
public partial class Sensor: Resource
2025-01-21 20:58:56 +00:00
{
2025-01-03 12:09:23 +00:00
[Export]
public bool continous = false;
[ExportGroup("Read Only")]
[Export]
public float _value = 0;
2025-01-21 20:58:56 +00:00
2025-01-03 12:09:23 +00:00
[Export]
public bool _wasActive = false;
2025-01-21 20:58:56 +00:00
2025-01-03 12:09:23 +00:00
[Export]
public bool _active = false;
2025-01-21 20:58:56 +00:00
2025-01-03 12:09:23 +00:00
[Export]
public float _activeTreshold = 0.5f;
public void ProcessSensor( SensorRunner runner, float delta )
{
_wasActive = _active;
UpdateValue();
}
public bool isDown => ! _wasActive && _active;
public bool isUp => _wasActive && ! _active;
2025-01-21 20:58:56 +00:00
public bool isHold => _active;
2025-01-03 12:09:23 +00:00
public bool wasActive => _wasActive;
public bool isActive => _active;
2025-01-21 20:58:56 +00:00
public float value => _value;
2025-01-03 12:09:23 +00:00
2025-01-21 20:58:56 +00:00
public virtual int deviceID => 0;
public virtual bool isMultiDevice => false;
2025-01-03 12:09:23 +00:00
2025-01-21 20:58:56 +00:00
public virtual float GetValue( int device ) => value;
public virtual bool IsDown( int device ) => isDown;
public virtual bool IsUp( int device ) => isUp;
2025-01-03 12:09:23 +00:00
2025-01-21 20:58:56 +00:00
public virtual bool WasActive( int device ) => isUp;
public virtual bool IsActive( int device ) => isUp;
2025-01-03 12:09:23 +00:00
protected virtual void UpdateValue()
{
}
protected void SetBoolValue( bool activeState )
{
_active = activeState;
_value = activeState ? 1 : 0;
}
protected void SetFloatValue( float value )
{
_value = value;
_active = value > _activeTreshold;
}
}
}