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

56 lines
1.2 KiB
C#
Raw Normal View History

2024-07-25 05:40:31 +00:00
using Godot;
namespace Rokojori
{
public class Sensors
{
public static bool IsDown( Sensor sensor, int device = 0 )
{
return sensor == null ? false : sensor.IsDown( device );
}
2025-01-08 18:46:17 +00:00
public static bool IsActive( Sensor sensor )
2024-07-25 05:40:31 +00:00
{
2025-01-08 18:46:17 +00:00
return sensor == null ? false : sensor.isActive;
2024-07-25 05:40:31 +00:00
}
public static float GetValue( Sensor sensor, float scale = 1, float deadZone = 0.3f )
2024-07-25 05:40:31 +00:00
{
if ( sensor == null )
{
return 0;
}
var rawValue = sensor.value;
var clamped = MathX.NormalizeClamped( rawValue, deadZone, 1 );
return clamped * scale;
2024-07-25 05:40:31 +00:00
}
public static float PolarAxis( Sensor negative, Sensor positive, float scale = 1, float deadZone = 0.3f )
2024-07-25 05:40:31 +00:00
{
return GetValue( negative, -scale, deadZone ) + GetValue( positive, scale, deadZone );
2024-07-25 05:40:31 +00:00
}
2025-05-21 20:44:28 +00:00
public static Vector2 FourDirectional( Sensor left, Sensor right, Sensor up, Sensor down, bool normalize = true, float deadZone = 0.3f )
{
var x = PolarAxis( left, right, 1, deadZone );
var y = PolarAxis( up, down, 1, deadZone );
var v = new Vector2( x, y );
if ( normalize && v.Length() > 1 )
{
v = v.Normalized();
}
return v;
}
2024-07-25 05:40:31 +00:00
}
}