From 16b52df702165b2d8d74dd87cb0ed2de9e99a23f Mon Sep 17 00:00:00 2001 From: copygirl Date: Mon, 1 Jan 2024 18:13:45 +0100 Subject: [PATCH] Prevent trying to parent items to themselves --- objects/Grid.cs | 11 +++++++++++ player/PickupController.cs | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/objects/Grid.cs b/objects/Grid.cs index 414b541..a450e19 100644 --- a/objects/Grid.cs +++ b/objects/Grid.cs @@ -34,6 +34,17 @@ public partial class Grid : Area3D } + /// Returns whether the specified item is contained in this or any nested grids. + public bool ContainsItem(Item item) + { + while (item?.GetParentOrNull() is Grid grid) { + if (grid == this) return true; + item = grid.GetParent() as Item; + } + return false; + } + + public Vector3I LocalToGrid(Vector3 pos) => (Vector3I)((pos + _halfGridActualSize) / StepSize); public Vector3 GridToLocal(Vector3I pos) diff --git a/player/PickupController.cs b/player/PickupController.cs index c687db4..6e7670c 100644 --- a/player/PickupController.cs +++ b/player/PickupController.cs @@ -88,7 +88,9 @@ public partial class PickupController : Node3D EnsureCurrentItemValid(); if (HasItemsHeld) { - if ((RayToMouseCursor() is RayResult ray) && (ray.Collider is Grid grid)) { + if ((RayToMouseCursor() is RayResult ray) && (ray.Collider is Grid grid) + // Ensure item is not being added to itself or nested items. + && !((grid.GetParent() == CurrentItem) || grid.ContainsItem(CurrentItem))) { var transform = CurrentItem.GlobalTransform with { Origin = ray.Position }; transform = grid.Snap(transform, ray.Normal, CurrentItem); _preview.GlobalTransform = transform;