131 lines
2.6 KiB
C#
131 lines
2.6 KiB
C#
|
|
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>();
|
|
Dictionary<Sensor,SensorRunner> sensorToRunner = new Dictionary<Sensor, SensorRunner>();
|
|
|
|
|
|
public override void _Ready()
|
|
{
|
|
if ( ! initializeOnReady )
|
|
{
|
|
return;
|
|
}
|
|
|
|
CreateRunners();
|
|
}
|
|
|
|
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 ) );
|
|
}
|
|
}
|
|
|
|
runners.ForEach( r => sensorToRunner[ r.sensor ] = r );
|
|
|
|
this.LogInfo( "Created runners:", runners.Count );
|
|
}
|
|
}
|
|
} |