Adding robots to the game and the ability to click on them

This commit is contained in:
=
2026-04-26 13:44:15 +02:00
parent bd9de785b4
commit 21199f8026
12 changed files with 155 additions and 9 deletions
+38
View File
@@ -36,4 +36,42 @@ public partial class Camera3d : Camera3D
Position = new Vector3(Position.X, 10 - visibleLayer * 4, Position.Z);
}
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseButton mouse &&
mouse.ButtonIndex == MouseButton.Left &&
mouse.Pressed)
{
var camera = GetViewport().GetCamera3D();
Vector2 mousePos = mouse.Position;
Vector3 from = camera.ProjectRayOrigin(mousePos);
Vector3 to = from + camera.ProjectRayNormal(mousePos) * 1000f;
var spaceState = GetWorld3D().DirectSpaceState;
var query = PhysicsRayQueryParameters3D.Create(from, to);
query.CollisionMask = 1 << 1;
var result = spaceState.IntersectRay(query);
if (result.Count > 0)
{
Variant colliderVariant = result["collider"];
Node hit = colliderVariant.As<Node>();
GD.Print($"Clicked robot: {hit.Name}");
// Optional: call method on robot
if (hit is Node robot)
{
robot.Call("OnClicked");
}
}
}
}
}