From 9408651ea81dcecb6cd662c912632a5646b8e316 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 29 Aug 2025 12:41:08 +0800 Subject: [PATCH] :sparkles: Item magnet --- Scripts/System/ItemPickup.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Scripts/System/ItemPickup.cs b/Scripts/System/ItemPickup.cs index 62020e6..d979ef6 100644 --- a/Scripts/System/ItemPickup.cs +++ b/Scripts/System/ItemPickup.cs @@ -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()