From 1b440b828fec56f244a28c91e140bd45186321c8 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Tue, 25 Jun 2024 23:54:37 +0800 Subject: [PATCH] =?UTF-8?q?Fixed=20an=20issue=20where=20the=20number=20of?= =?UTF-8?q?=20weapons=20to=20be=20picked=20up=20after=20being=20thrown=20i?= =?UTF-8?q?s=200,=20making=20it=20impossible=20to=20throw=20them=20again.?= =?UTF-8?q?=20=E8=A7=A3=E5=86=B3=E6=AD=A6=E5=99=A8=E6=89=94=E5=87=BA?= =?UTF-8?q?=E5=90=8E=E5=86=8D=E6=8D=A1=E8=B5=B7=E6=95=B0=E9=87=8F=E4=B8=BA?= =?UTF-8?q?0=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E5=86=8D?= =?UTF-8?q?=E6=AC=A1=E6=89=94=E5=87=BA=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/character/CharacterTemplate.cs | 9 ++++---- scripts/inventory/ItemSlotNode.cs | 30 ++++++++++++++++++++++++-- scripts/utils/NodeUtils.cs | 2 +- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index 99e9b14..072bb1e 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -664,14 +664,13 @@ public partial class CharacterTemplate : CharacterBody2D { //Remove the item from the item container //从物品容器内取出物品 - var item = itemSlotNode.GetItem(); - + var item = itemSlotNode.CreateItemInstance(1); if (item is not Node2D node2D) { return; } - - NodeUtils.CallDeferredReparent(NodeUtils.FindContainerNode(this, GetNode("/root")), this); + + NodeUtils.CallDeferredAddChild(NodeUtils.FindContainerNode(node2D, GetNode("/root")), node2D); switch (item) { case PickAbleTemplate pickAbleTemplate: @@ -684,6 +683,7 @@ public partial class CharacterTemplate : CharacterBody2D var timer = new Timer(); pickAbleTemplate.AddChild(timer); timer.WaitTime = _itemCollisionRecoveryTime; + timer.Autostart = true; timer.OneShot = true; timer.Timeout += () => { @@ -694,7 +694,6 @@ public partial class CharacterTemplate : CharacterBody2D pickAbleTemplate.SetCollisionMaskValue(Config.LayerNumber.Platform, true); timer.QueueFree(); }; - timer.Start(); pickAbleTemplate.Sleeping = false; //Setting an initial speed of 0 for items here prevents the problem of throwing items too fast. //在这里给物品设置一个为0的初始速度,可防止扔出物品时速度过快的问题。 diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index 7a120f1..e527273 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -121,6 +121,12 @@ public partial class ItemSlotNode : MarginContainer } var duplicate = node2D.Duplicate(); + if (duplicate is not Node2D newNode2D) + { + return null; + } + + newNode2D.GlobalPosition = node2D.GlobalPosition; if (duplicate is not IItem newItem) { return null; @@ -138,6 +144,26 @@ public partial class ItemSlotNode : MarginContainer return newItem; } + /// + /// Empty the inventory + /// 清空物品栏内的物品 + /// + public void ClearItem() + { + if (_item == null) + { + return; + } + + if (_item is Node node) + { + node.QueueFree(); + } + + _item = null; + UpdateAllDisplay(); + } + public override void _DropData(Vector2 atPosition, Variant data) { if (_iconTextureRect == null) @@ -162,7 +188,7 @@ public partial class ItemSlotNode : MarginContainer return; } - itemSlotNode.Item = null; + itemSlotNode.ClearItem(); AddItem(item); } @@ -344,7 +370,7 @@ public partial class ItemSlotNode : MarginContainer _item.Quantity -= removeNumber; if (_item.Quantity <= 0) { - Item = null; + ClearItem(); } return removeNumber; diff --git a/scripts/utils/NodeUtils.cs b/scripts/utils/NodeUtils.cs index ec4309b..f739729 100644 --- a/scripts/utils/NodeUtils.cs +++ b/scripts/utils/NodeUtils.cs @@ -46,7 +46,7 @@ public static class NodeUtils /// public static void CallDeferredReparent(Node parentNode, Node childNode) { - parentNode.CallDeferred("reparent", childNode); + childNode.CallDeferred("reparent", parentNode); }