Item magnet

This commit is contained in:
2025-08-29 12:41:08 +08:00
parent 75cd807187
commit 9408651ea8

View File

@@ -9,10 +9,12 @@ public partial class ItemPickup : Node2D
[Export] public string ItemId { get; set; } = "";
[Export] public int Quantity { get; set; } = 1;
[Export] public bool Infinite { get; set; } = false;
[Export] public float MagnetRange { get; set; } = 64f;
private Sprite2D _sprite;
private Label _quantityLabel;
private Sprite2D _shadowSprite;
private Node2D _playerTarget;
// Called when the node enters the scene tree
public override void _Ready()
@@ -53,6 +55,23 @@ public partial class ItemPickup : Node2D
_shadowSprite.ZIndex = _sprite.ZIndex - 1;
AddChild(_shadowSprite);
}
_playerTarget = GetTree().GetFirstNodeInGroup(PickupGroupName) as Node2D;
if (_playerTarget == null)
_playerTarget = GetTree().GetFirstNodeInGroup("Player") as Node2D;
}
public override void _Process(double delta)
{
if (_playerTarget != null)
{
var distance = Position.DistanceTo(_playerTarget.Position);
if (distance <= MagnetRange)
{
float speed = 10f;
Position = Position.Lerp(_playerTarget.Position, (float)delta * speed);
}
}
}
private void UpdateTexture()