summaryrefslogtreecommitdiff
path: root/Floor.cs
blob: 6ed3ecf329ff7d94c61585163845a23e2d8c0fe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Godot;
using System;

public partial class Floor : StaticBody3D
{
	public override void _Input(InputEvent @event)
	{
		base._Input(@event);
		if (@event is InputEventMouseButton ev && ev.Pressed)
		{
			switch (ev.ButtonIndex)
			{
				case MouseButton.Left:
				{
					var camera = Global.camera;
					if (camera != null)
					{
						var mousePos = GetViewport().GetMousePosition();
						var rayOrigin = camera.ProjectRayOrigin(mousePos);
						var rayEnd = rayOrigin + camera.ProjectRayNormal(mousePos) * 1000f;
						var query = PhysicsRayQueryParameters3D.Create(rayOrigin, rayEnd);
						if (Global.player != null)
							query.Exclude = new Godot.Collections.Array<Rid> { ((CollisionObject3D)Global.player).GetRid() };
						var result = GetWorld3D().DirectSpaceState.IntersectRay(query);
						if (result.Count > 0 && (GodotObject)result["collider"] == this)
						{
							var hitPos = (Vector3)result["position"];
							GD.Print("Floor hit at: " + hitPos);
						}
					}
					break;
				}
				case MouseButton.Right:
					GD.Print("Right Click");
					break;
			}
		}
	}
}