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

150 lines
2.8 KiB
C#
Raw Normal View History

2024-05-05 07:52:06 +00:00
using Godot;
namespace Rokojori
{
[Tool][GlobalClass, Icon("res://addons/rokojori_action_library/Icons/OnEvent.svg") ]
2024-05-05 07:52:06 +00:00
public partial class OnSensor: Node
{
2025-11-26 14:23:59 +00:00
[Export]
2025-01-08 18:46:17 +00:00
public Sensor sensor;
2024-05-05 07:52:06 +00:00
[Export]
2025-01-08 18:46:17 +00:00
public Action onStart;
2024-05-05 07:52:06 +00:00
[Export]
2025-01-08 18:46:17 +00:00
public Action onActive;
2024-05-05 07:52:06 +00:00
[Export]
2025-11-26 14:23:59 +00:00
public Action onEnd;
[Export]
public Action onDouble;
public enum DoubleMode
{
Auto,
Trigger_OnStart_And_OnDouble,
Trigger_OnDouble_Only
}
[Export]
public DoubleMode doubleMode = DoubleMode.Auto;
[Export]
public float doubleDuration = 0.3f;
2024-05-05 07:52:06 +00:00
2025-07-25 08:13:35 +00:00
[Export]
public bool onlyWhenNotConsumed = true;
[Export]
public bool consumeEvent = true;
[ExportGroup("Condition")]
2025-07-22 14:08:22 +00:00
[Export]
public Condition condition;
[Export]
public SceneCondition sceneCondition;
2025-11-26 14:23:59 +00:00
float _lastDownTime = 0;
float _lastUpTime = 0;
2025-07-25 08:13:35 +00:00
public override void _Process( double delta )
2024-05-05 07:52:06 +00:00
{
2025-07-22 14:08:22 +00:00
if ( sensor == null )
{
return;
}
2024-05-05 07:52:06 +00:00
2025-07-25 08:13:35 +00:00
if ( onlyWhenNotConsumed && sensor.consumed )
{
2025-10-24 11:38:51 +00:00
this.LogInfo( "Sensor Consumed" );
2025-07-25 08:13:35 +00:00
return;
}
2025-07-22 14:08:22 +00:00
if ( condition != null && ! condition.Evaluate() )
2024-05-05 07:52:06 +00:00
{
return;
}
2025-07-22 14:08:22 +00:00
if ( sceneCondition != null && ! sceneCondition.Evaluate() )
{
return;
}
2025-10-24 11:38:51 +00:00
// this.LogInfo( "On Sensor", sensor.value, sensor.isDown );
2025-11-26 14:23:59 +00:00
var timeNow = Time.GetTicksMsec() / 1000.0f;
2025-07-22 14:08:22 +00:00
if ( sensor.isDown )
2024-05-05 07:52:06 +00:00
{
2025-11-26 14:23:59 +00:00
var elapsedSinceLast = timeNow - _lastDownTime;
_lastDownTime = timeNow;
var isDouble = elapsedSinceLast < doubleDuration;
var executesStart = true;
var executesDouble = false;
2025-07-25 08:13:35 +00:00
2025-11-26 14:23:59 +00:00
if ( isDouble )
2025-07-25 08:13:35 +00:00
{
2025-11-26 14:23:59 +00:00
executesDouble = true;
if ( doubleMode == DoubleMode.Trigger_OnDouble_Only ||
doubleMode == DoubleMode.Auto && onDouble != null )
{
executesStart = false;
}
}
if ( executesStart )
{
Action.Trigger( onStart );
if ( consumeEvent && onStart != null )
{
sensor.Consume();
}
}
if ( executesDouble )
{
Action.Trigger( onDouble );
if ( consumeEvent && onDouble != null )
{
sensor.Consume();
}
2025-07-25 08:13:35 +00:00
}
2025-11-26 14:23:59 +00:00
2024-05-05 07:52:06 +00:00
}
2025-07-22 14:08:22 +00:00
if ( sensor.isHold )
2024-05-05 07:52:06 +00:00
{
2025-01-08 18:46:17 +00:00
Action.Trigger( onActive );
2025-07-25 08:13:35 +00:00
if ( consumeEvent && onActive != null )
{
sensor.Consume();
return;
}
2024-05-05 07:52:06 +00:00
}
2025-07-22 14:08:22 +00:00
if ( sensor.isUp )
2024-05-05 07:52:06 +00:00
{
2025-11-26 14:23:59 +00:00
_lastUpTime = timeNow;
2025-07-25 08:13:35 +00:00
Action.Trigger( onEnd );
if ( consumeEvent && onEnd != null )
{
sensor.Consume();
return;
}
2024-05-05 07:52:06 +00:00
}
}
}
}