From 63680a94104b76b0e36d941a25daf509149474d5 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Mon, 23 Sep 2024 21:03:39 +0800 Subject: [PATCH] =?UTF-8?q?Add=20an=20event=20where=20item=20data=20change?= =?UTF-8?q?s.=20=E5=8A=A0=E5=85=A5=E7=89=A9=E5=93=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=94=B9=E5=8F=98=E7=9A=84=E4=BA=8B=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- locals/Log.csv | 1 - scripts/Config.cs | 23 +++++++++++++ scripts/inventory/IItemContainer.cs | 10 +++--- .../inventory/ItemContainerDisplayTemplate.cs | 14 ++++++++ scripts/inventory/ItemSlotNode.cs | 31 ++++-------------- scripts/inventory/PlaceholderItem.cs | 32 +++++++++++++++++++ scripts/inventory/UniversalItemContainer.cs | 28 ++++++++++++---- scripts/map/events/ItemDataChangeEvent.cs | 16 ++++++++++ scripts/pickable/PickAbleTemplate.cs | 4 +-- 9 files changed, 122 insertions(+), 37 deletions(-) create mode 100644 scripts/inventory/PlaceholderItem.cs create mode 100644 scripts/map/events/ItemDataChangeEvent.cs diff --git a/locals/Log.csv b/locals/Log.csv index 1d732ec..35fb472 100644 --- a/locals/Log.csv +++ b/locals/Log.csv @@ -57,7 +57,6 @@ log_backpack_not_allowed,不允许添加到背包。,Not allowed to add to backp log_item_is_null,物品为空。,Item is null.,アイテムが空です。 log_item_id_not_same,物品ID不同。,Item ID is different.,アイテムIDが異なります。 log_max_quantity_exceeded,超过最大数量。,Exceeded maximum quantity.,最大数量を超えました。 -log_item_slot_is_selected_and_not_allowed,已选择物品槽,不允许添加。,"Item slot is selected, not allowed to add.",アイテムスロットが選択されており、追加は許可されていません。 log_patrol_enemy_detected,检测到敌人。,Enemy detected.,敵を検出しました。 log_attacker_or_target_is_null,攻击者或目标为空。,Attacker or target is null.,攻撃者またはターゲットが空です。 log_in_the_same_camp,在同一阵营。,In the same camp.,同じ陣営です。 diff --git a/scripts/Config.cs b/scripts/Config.cs index 0c7a679..b271ac2 100644 --- a/scripts/Config.cs +++ b/scripts/Config.cs @@ -210,6 +210,29 @@ public static class Config /// public const string TimeInterval = "TimeInterval"; } + + /// + /// Item data changes the event type + /// 物品数据改变事件类型 + /// + public enum ItemDataChangeEventType + { + /// + /// add + /// 添加 + /// + Add, + /// + /// Quantity Added + /// 物品数量增加 + /// + QuantityAdded, + /// + /// remove + /// 移除 + /// + Remove + } public enum OsEnum { diff --git a/scripts/inventory/IItemContainer.cs b/scripts/inventory/IItemContainer.cs index 6206b60..ab1d22f 100644 --- a/scripts/inventory/IItemContainer.cs +++ b/scripts/inventory/IItemContainer.cs @@ -8,10 +8,6 @@ namespace ColdMint.scripts.inventory; /// item container /// 物品容器 /// -/// -///Item containers can store items. Things like backpacks and Hotbars are containers with visual pages. -///物品容器可以储存物品。像背包和hotbar是具有可视化页面的容器。 -/// public interface IItemContainer : IEnumerable { /// @@ -20,6 +16,12 @@ public interface IItemContainer : IEnumerable /// Action? SelectedItemChangeEvent { get; set; } + /// + /// This event is triggered when the item's data changes, such as the number increases, decreases, or new items are added to the container + /// 当物品的数据发生改变时,例如数量增加,减少,或者新物品被添加到容器内触发此事件 + /// + Action? ItemDataChangeEvent { get; set; } + /// /// Can the specified item be added to the container? /// 指定的物品是否可添加到容器内? diff --git a/scripts/inventory/ItemContainerDisplayTemplate.cs b/scripts/inventory/ItemContainerDisplayTemplate.cs index 0799215..73e7cac 100644 --- a/scripts/inventory/ItemContainerDisplayTemplate.cs +++ b/scripts/inventory/ItemContainerDisplayTemplate.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Threading.Tasks; using ColdMint.scripts.map.events; namespace ColdMint.scripts.inventory; @@ -19,10 +20,12 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay if (_itemContainer != null) { _itemContainer.SelectedItemChangeEvent -= OnSelectedItemChangeEvent; + _itemContainer.ItemDataChangeEvent -= OnItemDataChangeEvent; } _itemContainer = itemContainer; _itemContainer.SelectedItemChangeEvent += OnSelectedItemChangeEvent; + _itemContainer.ItemDataChangeEvent += OnItemDataChangeEvent; var totalCapacity = itemContainer.GetTotalCapacity(); var currentCapacity = ItemDisplayList.Count; var capacityDifference = totalCapacity - currentCapacity; @@ -53,6 +56,17 @@ public abstract class ItemContainerDisplayTemplate : IItemContainerDisplay UpdateData(itemContainer, adjustedEndIndex); } + private void OnItemDataChangeEvent(ItemDataChangeEvent itemDataChangeEvent) + { + if (_itemContainer == null) + { + return; + } + + var usedCapacity = _itemContainer.GetUsedCapacity(); + UpdateDataForSingleLocation(_itemContainer, itemDataChangeEvent.NewIndex, usedCapacity); + } + private void OnSelectedItemChangeEvent(SelectedItemChangeEvent selectedItemChangeEvent) { if (_itemContainer == null) diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index c8507da..7ef5566 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -1,6 +1,4 @@ -using System; using ColdMint.scripts.debug; -using ColdMint.scripts.pickable; using ColdMint.scripts.utils; using Godot; @@ -16,7 +14,6 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay private TextureRect? _iconTextureRect; private Label? _quantityLabel; private Control? _control; - private bool _isSelect; private Texture2D? _backgroundTexture; private Texture2D? _backgroundTextureWhenSelect; private IItem? _item; @@ -31,12 +28,11 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay _quantityLabel = GetNode public bool BackpackAllowed { get; set; } - public bool IsSelect - { - get => _isSelect; - set - { - UpdateBackground(value); - _isSelect = value; - } - } private void UpdateBackground(bool isSelect) { @@ -330,7 +308,12 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay public void Update(IItem? item) { - UpdateAllDisplay(); + if (item is not PlaceholderItem) + { + _item = item; + UpdateAllDisplay(); + } + UpdateBackground(item is { IsSelect: true }); } public void ShowSelf() diff --git a/scripts/inventory/PlaceholderItem.cs b/scripts/inventory/PlaceholderItem.cs new file mode 100644 index 0000000..01ec472 --- /dev/null +++ b/scripts/inventory/PlaceholderItem.cs @@ -0,0 +1,32 @@ +using Godot; + +namespace ColdMint.scripts.inventory; + +/// +/// A blank item used to occupy a space in the display. The IsSelect property is set to true so that the item display always appears selected when drawn. +/// 物品容器显示器内用于占位的空白物品。IsSelect属性设置为true,使得物品显示器绘制时总是显示为选中状态。 +/// +public class PlaceholderItem : IItem +{ + public string Id { get; set; } + public Texture2D Icon { get; } + public string Name { get; } + public string? Description { get; } = null; + public int Quantity { get; set; } = 1; + public int MaxQuantity { get; } = 1; + public bool IsSelect { get; set; } = true; + + public int MergeableItemCount(IItem other, int unallocatedQuantity) + { + return 0; + } + + public IItem? CreateItem(int number) + { + return null; + } + + public void Use(Node2D? owner, Vector2 targetGlobalPosition) + { + } +} \ No newline at end of file diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index fc1a2cb..254794e 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -37,6 +37,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } public Action? SelectedItemChangeEvent { get; set; } + public Action? ItemDataChangeEvent { get; set; } public bool CanAddItem(IItem item) { @@ -75,6 +76,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer return unallocatedQuantity < 1; } + public int AddItem(IItem item) { if (item.MaxQuantity == 1) @@ -87,12 +89,19 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer } _itemList.Add(item); + ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent + { + NewItem = item, + NewIndex = _itemList.Count - 1, + Type = Config.ItemDataChangeEventType.QuantityAdded + }); return item.Quantity; } //There can be more than one item, try to share equally. //物品可有多个,尝试均摊。 var originalQuantity = item.Quantity; + var index = 0; foreach (var unitItem in _itemList) { var number = unitItem.MergeableItemCount(item, item.Quantity); @@ -103,19 +112,20 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer item.Quantity -= number; unitItem.Quantity += number; + ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent + { + NewItem = unitItem, + NewIndex = index, + Type = Config.ItemDataChangeEventType.QuantityAdded + }); if (item.Quantity < 1) { //New items are fully shared. //新物品完全被均摊。 return originalQuantity; } - } - if (item.Quantity < 1) - { - //After traversing to the last item, the new item is fully shared. - //在遍历到最后的物品,新物品完全被均摊。 - return originalQuantity; + index++; } //New items have some left over. @@ -130,6 +140,12 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer //Add the rest to the container. //添加剩余到容器内。 _itemList.Add(item); + ItemDataChangeEvent?.Invoke(new ItemDataChangeEvent + { + NewItem = item, + NewIndex = _itemList.Count - 1, + Type = Config.ItemDataChangeEventType.Add + }); return originalQuantity; } diff --git a/scripts/map/events/ItemDataChangeEvent.cs b/scripts/map/events/ItemDataChangeEvent.cs new file mode 100644 index 0000000..92b549b --- /dev/null +++ b/scripts/map/events/ItemDataChangeEvent.cs @@ -0,0 +1,16 @@ +using ColdMint.scripts.inventory; + +namespace ColdMint.scripts.map.events; + +/// +/// Item Data Change Event +/// 项目数据改变事件 +/// +public class ItemDataChangeEvent +{ + public Config.ItemDataChangeEventType Type { get; set; } + public int OldIndex { get; set; } + public int NewIndex { get; set; } + public IItem? NewItem { get; set; } + public IItem? OldItem { get; set; } +} \ No newline at end of file diff --git a/scripts/pickable/PickAbleTemplate.cs b/scripts/pickable/PickAbleTemplate.cs index ed14771..83b61e4 100644 --- a/scripts/pickable/PickAbleTemplate.cs +++ b/scripts/pickable/PickAbleTemplate.cs @@ -76,11 +76,11 @@ public partial class PickAbleTemplate : RigidBody2D, IItem /// public bool Picked { get; set; } - public int MaxQuantity { get; set; } + public int MaxQuantity { get; set; } = 1; public bool IsSelect { get; set; } private Label? _tipLabel; - + public IItem? CreateItem(int number) { if (number == 0)