diff --git a/scripts/Config.cs b/scripts/Config.cs index dfb3d06..1943972 100644 --- a/scripts/Config.cs +++ b/scripts/Config.cs @@ -170,12 +170,12 @@ public static class Config ///在禁用版本隔离时用的 /// public const string DefaultVersionName = "Default"; - + /// /// EmptyVariant /// 空变量 /// - public static readonly Variant EmptyVariant = new(); + public static readonly Variant EmptyVariant = new(); /// @@ -216,7 +216,7 @@ public static class Config /// public const string TimeInterval = "TimeInterval"; } - + /// /// Item data changes the event type /// 物品数据改变事件类型 @@ -238,12 +238,17 @@ public static class Config /// 移除 /// Remove, - + /// /// Replace /// 被替换 /// - Replace + Replace, + /// + /// Clear + /// 被清空 + /// + Clear } public enum OsEnum diff --git a/scripts/inventory/HotBar.cs b/scripts/inventory/HotBar.cs index 79bf7ea..3a041ff 100644 --- a/scripts/inventory/HotBar.cs +++ b/scripts/inventory/HotBar.cs @@ -15,10 +15,7 @@ public partial class HotBar : HBoxContainer public override void _Ready() { base._Ready(); - var universalItemContainer = new UniversalItemContainer(Config.HotBarSize) - { - EnablePlaceholder = true - }; + var universalItemContainer = new UniversalItemContainer(Config.HotBarSize); _itemContainer = universalItemContainer; _itemContainer.SupportSelect = true; _itemContainerDisplay = new ItemSlotContainerDisplay(this); diff --git a/scripts/inventory/IItem.cs b/scripts/inventory/IItem.cs index 783091a..d5f18af 100644 --- a/scripts/inventory/IItem.cs +++ b/scripts/inventory/IItem.cs @@ -4,6 +4,11 @@ namespace ColdMint.scripts.inventory; public interface IItem { + /// + /// The position of the item in the container + /// 物品在容器内的位置 + /// + public int Index { get; set; } /// /// ID of current item /// 当前物品的ID diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index c06f23d..da46460 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -50,8 +50,12 @@ public interface IItemContainer /// Gets a placeholder object /// 获取占位符对象 /// + /// + ///index + ///占位符代替的索引 + /// /// - IItem? GetPlaceHolderItem(); + IItem GetPlaceHolderItem(int index); /// /// Gets the selected location @@ -88,7 +92,13 @@ public interface IItemContainer /// bool ReplaceItem(int index, IItem item); - bool ReplaceItem(IItem oldItem, IItem newItem); + /// + /// ClearItem + /// 清理物品 + /// + /// + /// + bool ClearItem(int index); /// /// Gets the item in the specified location, equivalent to @@ -128,14 +138,6 @@ public interface IItemContainer /// int RemoveItem(int itemIndex, int number); - /// - /// Remove item - /// 移除物品 - /// - /// - /// - int RemoveItem(IItem item, int number); - /// /// Gets the used capacity of the item container /// 获取物品容器已使用的容量 diff --git a/scripts/inventory/ItemContainerDisplayTemplate.cs b/scripts/inventory/ItemContainerDisplayTemplate.cs index 40ac4e5..93a4050 100644 --- a/scripts/inventory/ItemContainerDisplayTemplate.cs +++ b/scripts/inventory/ItemContainerDisplayTemplate.cs @@ -118,12 +118,16 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay var item = itemContainer.GetItem(index); if (item == null) { - item = itemContainer.GetPlaceHolderItem(); + item = itemContainer.GetPlaceHolderItem(index); } - if (item != null) + if (itemContainer.SupportSelect) { item.IsSelect = index == itemContainer.GetSelectIndex(); } + else + { + item.IsSelect = false; + } itemDisplay.Update(item); itemDisplay.ShowSelf(); } diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index d413f85..0d81db8 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -78,6 +78,12 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay public override void _DropData(Vector2 atPosition, Variant data) { + //The item is empty and the corresponding item container cannot be retrieved. + //物品为空,无法获取对应的物品容器。 + if (Item is null) + { + return; + } var type = data.VariantType; if (type == Variant.Type.Nil) { @@ -89,10 +95,20 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay { return; } - if (Item is null || Item is PlaceholderItem) + + if (Item is PlaceholderItem placeholderItem) { - sourceItem.ItemContainer?.RemoveItem(sourceItem, -1); - Item?.ItemContainer?.ReplaceItem(Item, sourceItem); + var placeholderItemContainer = placeholderItem.ItemContainer; + var sourceItemContainer = sourceItem.ItemContainer; + var sourceItemIndex = sourceItem.Index; + if (placeholderItemContainer != null) + { + placeholderItemContainer.ReplaceItem(placeholderItem.Index, sourceItem); + } + if (sourceItemContainer != null) + { + sourceItemContainer.ClearItem(sourceItemIndex); + } } } diff --git a/scripts/inventory/Packsack.cs b/scripts/inventory/Packsack.cs index c0729f5..5a351f0 100644 --- a/scripts/inventory/Packsack.cs +++ b/scripts/inventory/Packsack.cs @@ -35,18 +35,30 @@ public partial class Packsack : PickAbleTemplate if (control is PacksackUi packsackUi) { packsackUi.Title = Name; - packsackUi.ItemContainer = ItemContainer; + packsackUi.ItemContainer = SelfItemContainer; } }); } - public IItemContainer? ItemContainer { get; private set; } + public override void CopyAttributes(Node node) + { + base.CopyAttributes(node); + if (node is Packsack packsack) + { + SelfItemContainer = packsack.SelfItemContainer; + } + } + + public IItemContainer? SelfItemContainer { get; set; } public override void _Ready() { base._Ready(); - ItemContainer = new UniversalItemContainer(NumberSlots); - ItemContainer.SupportSelect = false; + if (SelfItemContainer == null) + { + SelfItemContainer = new UniversalItemContainer(NumberSlots); + SelfItemContainer.SupportSelect = false; + } GameSceneDepend.DynamicUiGroup?.RegisterControl(Path, () => { var packedScene = GD.Load(Path); diff --git a/scripts/inventory/PlaceholderItem.cs b/scripts/inventory/PlaceholderItem.cs index 6437dbd..7826eb4 100644 --- a/scripts/inventory/PlaceholderItem.cs +++ b/scripts/inventory/PlaceholderItem.cs @@ -8,6 +8,7 @@ namespace ColdMint.scripts.inventory; /// public class PlaceholderItem : IItem { + public int Index { get; set; } public string Id { get; set; } public Texture2D Icon { get; } public string Name { get; } diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index e38e95c..889f9fc 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -110,6 +110,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer var nextAvailableIndex = _nextAvailableIndex; _itemDictionary[nextAvailableIndex] = item; + item.Index = nextAvailableIndex; item.ItemContainer = this; UpdateNextAvailableIndex(); UpdateSelectStatus(nextAvailableIndex, item); @@ -169,6 +170,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } var finalNextAvailableIndex = _nextAvailableIndex; _itemDictionary[finalNextAvailableIndex] = item; + item.Index = finalNextAvailableIndex; item.ItemContainer = this; UpdateNextAvailableIndex(); UpdateSelectStatus(finalNextAvailableIndex, item); @@ -182,11 +184,16 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } public bool SupportSelect { get; set; } - public bool EnablePlaceholder { get; set; } - public IItem? GetPlaceHolderItem() + + public IItem GetPlaceHolderItem(int index) { - return EnablePlaceholder ? new PlaceholderItem() : null; + var placeholderItem = new PlaceholderItem + { + Index = index, + ItemContainer = this + }; + return placeholderItem; } public int GetSelectIndex() @@ -208,6 +215,8 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer { var oldItem = GetItem(index); _itemDictionary[index] = item; + item.Index = index; + item.ItemContainer = this; ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent { NewItem = item, @@ -219,11 +228,24 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer return true; } - public bool ReplaceItem(IItem oldItem, IItem newItem) + + public bool ClearItem(int index) { - var index = GetIndexByItem(oldItem); - return index != UnknownIndex && ReplaceItem(index, newItem); + var result = _itemDictionary.Remove(index); + if (result) + { + ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent + { + NewItem = null, + NewIndex = index, + OldIndex = index, + OldItem = null, + Type = Config.ItemDataChangeEventType.Clear + }); + } + return result; } + public int RemoveSelectItem(int number) { @@ -276,37 +298,6 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer return removed; } - public int RemoveItem(IItem item, int number) - { - var index = GetIndexByItem(item); - return index == UnknownIndex ? 0 : RemoveItem(index, number); - } - - - /// - /// Find the corresponding index based on the item object - /// 根据物品对象查找对应的索引 - /// - /// - /// - private int GetIndexByItem(IItem item) - { - if (totalCapacity <= 0) - { - return UnknownIndex; - } - for (var i = 0; i < totalCapacity; i++) - { - var contains = _itemDictionary.ContainsKey(i); - if (!contains) continue; - if (item == _itemDictionary[i]) - { - return i; - } - } - return UnknownIndex; - } - public int GetUsedCapacity() { return _itemDictionary.Count; diff --git a/scripts/pickable/PickAbleTemplate.cs b/scripts/pickable/PickAbleTemplate.cs index f1a9985..d70a31a 100644 --- a/scripts/pickable/PickAbleTemplate.cs +++ b/scripts/pickable/PickAbleTemplate.cs @@ -15,6 +15,7 @@ namespace ColdMint.scripts.pickable; /// public partial class PickAbleTemplate : RigidBody2D, IItem { + public int Index { get; set; } //Do not export this field because the ID is specified within yaml. //不要导出此字段,因为ID是在yaml内指定的。 public virtual string Id { get; set; } = "ID"; @@ -292,7 +293,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem /// 请在此函数内复制节点属性 /// /// - public void CopyAttributes(Node node) + public virtual void CopyAttributes(Node node) { if (node is not PickAbleTemplate pickAbleTemplate) {