Prevent trying to parent items to themselves

main
copygirl 11 months ago
parent de12776f90
commit 16b52df702
  1. 11
      objects/Grid.cs
  2. 4
      player/PickupController.cs

@ -34,6 +34,17 @@ public partial class Grid : Area3D
} }
/// <summary> Returns whether the specified item is contained in this or any nested grids. </summary>
public bool ContainsItem(Item item)
{
while (item?.GetParentOrNull<Node>() is Grid grid) {
if (grid == this) return true;
item = grid.GetParent() as Item;
}
return false;
}
public Vector3I LocalToGrid(Vector3 pos) public Vector3I LocalToGrid(Vector3 pos)
=> (Vector3I)((pos + _halfGridActualSize) / StepSize); => (Vector3I)((pos + _halfGridActualSize) / StepSize);
public Vector3 GridToLocal(Vector3I pos) public Vector3 GridToLocal(Vector3I pos)

@ -88,7 +88,9 @@ public partial class PickupController : Node3D
EnsureCurrentItemValid(); EnsureCurrentItemValid();
if (HasItemsHeld) { 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 }; var transform = CurrentItem.GlobalTransform with { Origin = ray.Position };
transform = grid.Snap(transform, ray.Normal, CurrentItem); transform = grid.Snap(transform, ray.Normal, CurrentItem);
_preview.GlobalTransform = transform; _preview.GlobalTransform = transform;

Loading…
Cancel
Save