2025-01-03 12:09:23 +00:00
|
|
|
|
|
|
|
using Godot;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Rokojori
|
|
|
|
{
|
|
|
|
[Tool]
|
|
|
|
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/RJSensor.svg")]
|
|
|
|
public partial class SensorManager: Node
|
|
|
|
{
|
|
|
|
[Export]
|
|
|
|
public bool initializeOnReady = true;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public Sensor[] sensors;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public SensorGroup[] sensorGroups;
|
|
|
|
|
|
|
|
[Export]
|
|
|
|
public bool processSensors = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<SensorRunner> runners = new List<SensorRunner>();
|
2025-01-08 18:46:17 +00:00
|
|
|
List<iOnInputSensor> inputers = new List<iOnInputSensor>();
|
2025-01-03 12:09:23 +00:00
|
|
|
Dictionary<Sensor,SensorRunner> sensorToRunner = new Dictionary<Sensor, SensorRunner>();
|
|
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
if ( ! initializeOnReady )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateRunners();
|
|
|
|
}
|
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
public override void _Input( InputEvent ev )
|
|
|
|
{
|
|
|
|
inputers.ForEach(
|
|
|
|
( inp )=>
|
|
|
|
{
|
|
|
|
inp._Input( ev );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
public override void _Process( double delta )
|
|
|
|
{
|
|
|
|
if ( Engine.IsEditorHint() || ! processSensors )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
runners.ForEach( r => r.Update( (float) delta ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Register( Sensor s, SensorInputHandler sih )
|
|
|
|
{
|
|
|
|
var sensorRunner = sensorToRunner[ s ];
|
|
|
|
|
|
|
|
if ( sensorRunner.listeners.Contains( sih ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sensorRunner.listeners.Add( sih );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unregister( Sensor s, SensorInputHandler sih )
|
|
|
|
{
|
|
|
|
var sensorRunner = sensorToRunner[ s ];
|
|
|
|
sensorRunner.listeners.Remove( sih );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Register( SensorInputHandler handler, params Sensor[] sensors )
|
|
|
|
{
|
|
|
|
var sm = Unique<SensorManager>.Get();
|
|
|
|
|
|
|
|
|
|
|
|
foreach ( var s in sensors )
|
|
|
|
{
|
|
|
|
if ( s == null )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
( handler as Node ).LogInfo( "Registrating" );
|
|
|
|
sm.Register( s, handler );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Unregister( SensorInputHandler handler, params Sensor[] sensors )
|
|
|
|
{
|
|
|
|
var sm = Unique<SensorManager>.Get();
|
|
|
|
|
|
|
|
foreach ( var s in sensors )
|
|
|
|
{
|
|
|
|
if ( s == null )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
( handler as Node ).LogInfo( "Unregistrating" );
|
|
|
|
|
|
|
|
sm.Unregister( s, handler );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateRunners()
|
|
|
|
{
|
|
|
|
if ( sensors == null )
|
|
|
|
{
|
|
|
|
sensors = new Sensor[]{};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( sensorGroups == null )
|
|
|
|
{
|
|
|
|
sensorGroups = new SensorGroup[]{};
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( var s in sensors )
|
|
|
|
{
|
|
|
|
runners.Add( new SensorRunner( s ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( var g in sensorGroups )
|
|
|
|
{
|
|
|
|
foreach ( var s in g.sensors )
|
|
|
|
{
|
|
|
|
runners.Add( new SensorRunner( s ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
runners.ForEach(
|
|
|
|
r =>
|
|
|
|
{
|
|
|
|
sensorToRunner[ r.sensor ] = r;
|
|
|
|
|
|
|
|
if ( r.sensor is iOnInputSensor oi )
|
|
|
|
{
|
|
|
|
inputers.Add( oi );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2025-01-03 12:09:23 +00:00
|
|
|
|
2025-01-08 18:46:17 +00:00
|
|
|
|
2025-01-03 12:09:23 +00:00
|
|
|
this.LogInfo( "Created runners:", runners.Count );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|