2025-06-17 11:57:59 +02:00
|
|
|
|
using Cirno.Scripts.Misc;
|
|
|
|
|
|
using Godot;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Cirno.Scripts.Components.Actors;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class MouseAimProvider3D : Node3D, IMouseAimProvider
|
|
|
|
|
|
{
|
2025-06-19 10:50:25 +02:00
|
|
|
|
private Viewport _viewport;
|
|
|
|
|
|
|
|
|
|
|
|
public override void _Ready()
|
|
|
|
|
|
{
|
|
|
|
|
|
_viewport = GetViewport();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 11:57:59 +02:00
|
|
|
|
public Vector2 GetMouseAimInput()
|
|
|
|
|
|
{
|
2025-06-19 10:50:25 +02:00
|
|
|
|
var mouseWorldPos = GetMouseWorldPosition(this.GlobalPosition.Y);
|
2025-06-17 11:57:59 +02:00
|
|
|
|
|
2025-06-19 10:50:25 +02:00
|
|
|
|
var direction = (mouseWorldPos - this.GlobalPosition).Normalized();
|
2025-06-17 11:57:59 +02:00
|
|
|
|
|
2025-06-19 10:50:25 +02:00
|
|
|
|
return new Vector2(direction.X, direction.Z);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Vector3 GetMouseWorldPosition(float planeHeight = 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var mousePos = _viewport.GetMousePosition();
|
|
|
|
|
|
var rayOrigin = CameraController3D.Instance.ProjectRayOrigin(mousePos);
|
|
|
|
|
|
var rayDirection = CameraController3D.Instance.ProjectRayNormal(mousePos);
|
|
|
|
|
|
|
|
|
|
|
|
var plane = new Plane(Vector3.Up, planeHeight);
|
|
|
|
|
|
|
|
|
|
|
|
var intersection = plane.IntersectsRay(rayOrigin, rayDirection);
|
|
|
|
|
|
|
|
|
|
|
|
return intersection ?? Vector3.Zero;
|
2025-06-17 11:57:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|