godot-plateformer/addons/phantom_camera/scripts/phantom_camera_host/PhantomCameraHost.cs

114 lines
3.5 KiB
C#
Raw Normal View History

2025-12-29 11:54:31 +08:00
using Godot;
#nullable enable
namespace PhantomCamera;
public enum InterpolationMode
{
Auto,
Idle,
Physics
}
public static class PhantomCameraHostExtensions
{
public static PhantomCameraHost AsPhantomCameraHost(this Node node)
{
return new PhantomCameraHost(node);
}
}
public class PhantomCameraHost()
{
public Node Node { get; } = null!;
2025-12-31 13:07:31 +08:00
public PhantomCameraHost(GodotObject node) : this()
2025-12-29 11:54:31 +08:00
{
2025-12-31 13:07:31 +08:00
Node = node as Node;
2025-12-29 11:54:31 +08:00
2025-12-31 13:07:31 +08:00
var callablePCamBecameActive = Callable.From<Node>(pCam => PCamBecameActive?.Invoke(pCam));
var callablePCamBecameInactive = Callable.From<Node>(pCam => PCamBecameInactive?.Invoke(pCam));
2025-12-29 11:54:31 +08:00
2025-12-31 13:07:31 +08:00
Node.Connect(SignalName.PCamBecameActive, callablePCamBecameActive);
Node.Connect(SignalName.PCamBecameInactive, callablePCamBecameInactive);
2025-12-29 11:54:31 +08:00
}
public delegate void PCamBecameActiveEventHandler(Node pCam);
public delegate void PCamBecameInactiveEventHandler(Node pCam);
public event PCamBecameActiveEventHandler? PCamBecameActive;
public event PCamBecameInactiveEventHandler? PCamBecameInactive;
private readonly Callable _callablePCamBecameActive;
private readonly Callable _callablePCamBecameInactive;
2025-12-31 13:07:31 +08:00
2025-12-29 11:54:31 +08:00
// For when Godot becomes the minimum version
// public InterpolationMode InterpolationMode
// {
// get => (InterpolationMode)(int)Node.Call(MethodName.GetInterpolationMode);
// set => Node.Call(MethodName.SetInterpolationMode, (int)value);
// }
public int HostLayers
{
get => (int)Node.Call(PhantomCamera.MethodName.GetHostLayers);
set => Node.Call(PhantomCamera.MethodName.SetHostLayers, value);
}
public void SetHostLayersValue(int layer, bool value) => Node.Call(MethodName.SetHostLayersValue, layer, value);
public Camera2D? Camera2D => (Camera2D?)Node.Get(PropertyName.Camera2D);
public Camera3D? Camera3D => (Camera3D?)Node.Get(PropertyName.Camera3D);
public InterpolationMode InterpolationMode
{
get => (InterpolationMode)(int)Node.Call(MethodName.GetInterpolationMode);
set => Node.Call(MethodName.SetInterpolationMode, (int)value);
}
public bool TriggerPhantomCameraTween => (bool)Node.Call(MethodName.GetTriggerPhantomCameraTween);
2025-12-31 13:07:31 +08:00
public PhantomCamera? GetActivePhantomCamera()
2025-12-29 11:54:31 +08:00
{
var result = Node.Call(MethodName.GetActivePhantomCamera);
2025-12-31 13:07:31 +08:00
if (result.Obj is Node2D node2D)
{
return new PhantomCamera2D(node2D);
}
if (result.Obj is Node3D node3D)
{
return new PhantomCamera3D(node3D);
}
return null;
2025-12-29 11:54:31 +08:00
}
public static class PropertyName
{
2025-12-31 13:07:31 +08:00
public static readonly StringName Camera2D = new("camera_2d");
public static readonly StringName Camera3D = new("camera_3d");
2025-12-29 11:54:31 +08:00
}
public static class MethodName
{
2025-12-31 13:07:31 +08:00
public static readonly StringName GetActivePhantomCamera = new("get_active_pcam");
public static readonly StringName GetTriggerPhantomCameraTween = new("get_trigger_pcam_tween");
2025-12-29 11:54:31 +08:00
2025-12-31 13:07:31 +08:00
public static readonly StringName GetInterpolationMode = new("get_interpolation_mode");
public static readonly StringName SetInterpolationMode = new("set_interpolation_mode");
2025-12-29 11:54:31 +08:00
2025-12-31 13:07:31 +08:00
public static readonly StringName SetHostLayersValue = new("set_host_layers_value");
2025-12-29 11:54:31 +08:00
}
public static class SignalName
{
2025-12-31 13:07:31 +08:00
public static readonly StringName PCamBecameActive = new("pcam_became_active");
public static readonly StringName PCamBecameInactive = new("pcam_became_inactive");
2025-12-29 11:54:31 +08:00
}
}