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);
}