From bfec07a0de01732c25744679bf2d9a22abe688c2 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Mon, 30 Sep 2024 08:56:00 +0800 Subject: [PATCH] =?UTF-8?q?Fixed=20When=20switching=20items,=20players=20w?= =?UTF-8?q?ould=20have=20multiple=20items=20stacked=20on=20their=20hands.?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E5=88=87=E6=8D=A2=E7=89=A9=E5=93=81?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E7=8E=A9=E5=AE=B6=E6=89=8B=E4=B8=8A=E4=BC=9A?= =?UTF-8?q?=E5=8F=A0=E5=8A=A0=E5=A4=9A=E4=B8=AA=E7=89=A9=E5=93=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/character/CharacterTemplate.cs | 1 + scripts/inventory/IItem.cs | 18 ++++++++++++++++++ scripts/inventory/PlaceholderItem.cs | 16 ++++++++++++++++ scripts/inventory/UniversalItemContainer.cs | 11 +++++++++++ scripts/pickable/PickAbleTemplate.cs | 16 ++++++++++++++++ 5 files changed, 62 insertions(+) diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index faa6cf8..294cdd6 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -839,6 +839,7 @@ public partial class CharacterTemplate : CharacterBody2D } ProtectedItemContainer?.RemoveItem(index, 1); + originalItem.QueueFreeSelf(); } diff --git a/scripts/inventory/IItem.cs b/scripts/inventory/IItem.cs index 9a16648..70e2242 100644 --- a/scripts/inventory/IItem.cs +++ b/scripts/inventory/IItem.cs @@ -14,6 +14,24 @@ public interface IItem /// 当前物品的ID /// string Id { get; set; } + + /// + /// ShowSelf + /// 显示自身 + /// + void ShowSelf(); + + /// + /// QueueFreeSelf + /// 销毁自身 + /// + void QueueFreeSelf(); + + /// + /// HideSelf + /// 隐藏自身 + /// + void HideSelf(); /// /// Icon of current item diff --git a/scripts/inventory/PlaceholderItem.cs b/scripts/inventory/PlaceholderItem.cs index 1037615..f26e0aa 100644 --- a/scripts/inventory/PlaceholderItem.cs +++ b/scripts/inventory/PlaceholderItem.cs @@ -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; diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 4f1cd2d..97146f3 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -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; } diff --git a/scripts/pickable/PickAbleTemplate.cs b/scripts/pickable/PickAbleTemplate.cs index 6b9ce6f..8d91e54 100644 --- a/scripts/pickable/PickAbleTemplate.cs +++ b/scripts/pickable/PickAbleTemplate.cs @@ -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