Add an event where item data changes.
加入物品数据改变的事件。
This commit is contained in:
parent
234241b74a
commit
63680a9410
|
@ -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.,同じ陣営です。
|
||||
|
|
|
|
@ -210,6 +210,29 @@ public static class Config
|
|||
/// </summary>
|
||||
public const string TimeInterval = "TimeInterval";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Item data changes the event type</para>
|
||||
/// <para>物品数据改变事件类型</para>
|
||||
/// </summary>
|
||||
public enum ItemDataChangeEventType
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>add</para>
|
||||
/// <para>添加</para>
|
||||
/// </summary>
|
||||
Add,
|
||||
/// <summary>
|
||||
/// <para>Quantity Added</para>
|
||||
/// <para>物品数量增加</para>
|
||||
/// </summary>
|
||||
QuantityAdded,
|
||||
/// <summary>
|
||||
/// <para>remove</para>
|
||||
/// <para>移除</para>
|
||||
/// </summary>
|
||||
Remove
|
||||
}
|
||||
|
||||
public enum OsEnum
|
||||
{
|
||||
|
|
|
@ -8,10 +8,6 @@ namespace ColdMint.scripts.inventory;
|
|||
/// <para>item container</para>
|
||||
/// <para>物品容器</para>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
///<para>Item containers can store items. Things like backpacks and Hotbars are containers with visual pages.</para>
|
||||
///<para>物品容器可以储存物品。像背包和hotbar是具有可视化页面的容器。</para>
|
||||
/// </remarks>
|
||||
public interface IItemContainer : IEnumerable<IItem>
|
||||
{
|
||||
/// <summary>
|
||||
|
@ -20,6 +16,12 @@ public interface IItemContainer : IEnumerable<IItem>
|
|||
/// </summary>
|
||||
Action<SelectedItemChangeEvent>? SelectedItemChangeEvent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para>This event is triggered when the item's data changes, such as the number increases, decreases, or new items are added to the container</para>
|
||||
/// <para>当物品的数据发生改变时,例如数量增加,减少,或者新物品被添加到容器内触发此事件</para>
|
||||
/// </summary>
|
||||
Action<ItemDataChangeEvent>? ItemDataChangeEvent { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// <para>Can the specified item be added to the container?</para>
|
||||
/// <para>指定的物品是否可添加到容器内?</para>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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<Label>("Control/QuantityLabel");
|
||||
_control = GetNode<Control>("Control");
|
||||
_quantityLabel.Hide();
|
||||
UpdateBackground(_isSelect);
|
||||
}
|
||||
|
||||
public override Variant _GetDragData(Vector2 atPosition)
|
||||
{
|
||||
if (_isSelect || _iconTextureRect == null)
|
||||
if (_iconTextureRect == null)
|
||||
{
|
||||
return new Variant();
|
||||
}
|
||||
|
@ -51,14 +47,6 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
|
|||
|
||||
public override bool _CanDropData(Vector2 atPosition, Variant data)
|
||||
{
|
||||
if (_isSelect)
|
||||
{
|
||||
//Do not place items in the selected item slot, even if the item slot is empty.
|
||||
//禁止在已选中的物品槽内放置物品,即使物品槽是空的。
|
||||
LogCat.Log("item_slot_is_selected_and_not_allowed");
|
||||
return false;
|
||||
}
|
||||
|
||||
//If the preplaced slot does not have an icon, the preplaced slot is not allowed.
|
||||
//如果预放置的槽位没有图标,那么不允许放置。
|
||||
if (_iconTextureRect == null)
|
||||
|
@ -129,7 +117,6 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
|
|||
}
|
||||
|
||||
|
||||
|
||||
public override void _DropData(Vector2 atPosition, Variant data)
|
||||
{
|
||||
if (_iconTextureRect == null)
|
||||
|
@ -188,15 +175,6 @@ public partial class ItemSlotNode : MarginContainer, IItemDisplay
|
|||
/// </summary>
|
||||
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()
|
||||
|
|
32
scripts/inventory/PlaceholderItem.cs
Normal file
32
scripts/inventory/PlaceholderItem.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using Godot;
|
||||
|
||||
namespace ColdMint.scripts.inventory;
|
||||
|
||||
/// <summary>
|
||||
/// <para>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.</para>
|
||||
/// <para>物品容器显示器内用于占位的空白物品。IsSelect属性设置为true,使得物品显示器绘制时总是显示为选中状态。</para>
|
||||
/// </summary>
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
|
@ -37,6 +37,7 @@ public class UniversalItemContainer(int totalCapacity) : IItemContainer
|
|||
}
|
||||
|
||||
public Action<SelectedItemChangeEvent>? SelectedItemChangeEvent { get; set; }
|
||||
public Action<ItemDataChangeEvent>? 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;
|
||||
}
|
||||
|
||||
|
|
16
scripts/map/events/ItemDataChangeEvent.cs
Normal file
16
scripts/map/events/ItemDataChangeEvent.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using ColdMint.scripts.inventory;
|
||||
|
||||
namespace ColdMint.scripts.map.events;
|
||||
|
||||
/// <summary>
|
||||
/// <para>Item Data Change Event</para>
|
||||
/// <para>项目数据改变事件</para>
|
||||
/// </summary>
|
||||
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; }
|
||||
}
|
|
@ -76,11 +76,11 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
|
|||
/// </summary>
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user