Fixed an issue where the number of weapons to be picked up after being thrown is 0, making it impossible to throw them again.

解决武器扔出后再捡起数量为0,导致无法再次扔出的问题。
This commit is contained in:
Cold-Mint 2024-06-25 23:54:37 +08:00
parent dea683dbc5
commit 1b440b828f
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
3 changed files with 33 additions and 8 deletions

View File

@ -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的初始速度可防止扔出物品时速度过快的问题。

View File

@ -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;
}
/// <summary>
/// <para>Empty the inventory</para>
/// <para>清空物品栏内的物品</para>
/// </summary>
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;

View File

@ -46,7 +46,7 @@ public static class NodeUtils
/// <param name="childNode"></param>
public static void CallDeferredReparent(Node parentNode, Node childNode)
{
parentNode.CallDeferred("reparent", childNode);
childNode.CallDeferred("reparent", parentNode);
}