Fixed When switching items, players would have multiple items stacked on their hands.

修复切换物品时,玩家手上会叠加多个物品。
This commit is contained in:
Cold-Mint 2024-09-30 08:56:00 +08:00
parent c1c3fce58a
commit bfec07a0de
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
5 changed files with 62 additions and 0 deletions

View File

@ -839,6 +839,7 @@ public partial class CharacterTemplate : CharacterBody2D
}
ProtectedItemContainer?.RemoveItem(index, 1);
originalItem.QueueFreeSelf();
}

View File

@ -15,6 +15,24 @@ public interface IItem
/// </summary>
string Id { get; set; }
/// <summary>
/// <para>ShowSelf</para>
/// <para>显示自身</para>
/// </summary>
void ShowSelf();
/// <summary>
/// <para>QueueFreeSelf</para>
/// <para>销毁自身</para>
/// </summary>
void QueueFreeSelf();
/// <summary>
/// <para>HideSelf</para>
/// <para>隐藏自身</para>
/// </summary>
void HideSelf();
/// <summary>
/// <para>Icon of current item</para>
/// <para>当前项目的图标</para>

View File

@ -10,6 +10,22 @@ public class PlaceholderItem : IItem
{
public int Index { get; set; }
public string Id { get; set; }
public void ShowSelf()
{
}
public void QueueFreeSelf()
{
}
public void HideSelf()
{
}
public Texture2D Icon { get; }
public string Name { get; }
public string? Description { get; } = null;

View File

@ -118,6 +118,10 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
_itemDictionary[nextAvailableIndex] = item;
item.Index = nextAvailableIndex;
item.ItemContainer = this;
if (nextAvailableIndex != _selectIndex)
{
item.HideSelf();
}
UpdateNextAvailableIndex();
UpdateSelectStatus(nextAvailableIndex, item);
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
@ -154,6 +158,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
{
//New items are fully shared.
//新物品完全被均摊。
item.HideSelf();
return originalQuantity;
}
@ -179,6 +184,10 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
_itemDictionary[finalNextAvailableIndex] = item;
item.Index = finalNextAvailableIndex;
item.ItemContainer = this;
if (finalNextAvailableIndex != _selectIndex)
{
item.HideSelf();
}
UpdateNextAvailableIndex();
UpdateSelectStatus(finalNextAvailableIndex, item);
ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent
@ -380,6 +389,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
var oldItem = GetItem(oldIndex);
if (oldItem != null)
{
oldItem.HideSelf();
oldItem.IsSelect = false;
}
@ -388,6 +398,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
var newItem = GetItem(newIndex);
if (newItem != null)
{
newItem.ShowSelf();
newItem.IsSelect = true;
}

View File

@ -20,6 +20,22 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
//不要导出此字段因为ID是在yaml内指定的。
public virtual string Id { get; set; } = "ID";
[Export] protected Texture2D? UniqueIcon { get; set; }
public void ShowSelf()
{
Show();
}
public void QueueFreeSelf()
{
QueueFree();
}
public void HideSelf()
{
Hide();
}
public Texture2D Icon => UniqueIcon ?? ItemTypeManager.DefaultIconOf(Id);
public new string Name