Change the index parameter when adding a slot to ItemContainer to automatically get the latest index , add a test slot for Packsack item

更改ItemContainer添加槽位时的index参数为自动获取最新索引,为Packsack加入一个测试用槽位
This commit is contained in:
霧雨烨 2024-06-14 12:44:31 +08:00
parent e5664f1553
commit 0f9e7d0283
4 changed files with 8 additions and 6 deletions

View File

@ -20,7 +20,7 @@ public partial class HotBar : HBoxContainer
NodeUtils.DeleteAllChild(this); NodeUtils.DeleteAllChild(this);
for (var i = 0; i < Config.HotBarSize; i++) for (var i = 0; i < Config.HotBarSize; i++)
{ {
_itemContainer.AddItemSlot(this, i); _itemContainer.AddItemSlot(this);
} }
} }

View File

@ -219,8 +219,7 @@ public interface IItemContainer : IEnumerable<ItemSlotNode>
/// <para>添加物品槽</para> /// <para>添加物品槽</para>
/// </summary> /// </summary>
/// <param name="rootNode"></param> /// <param name="rootNode"></param>
/// <param name="index"></param> void AddItemSlot(Node rootNode);
void AddItemSlot(Node rootNode, int index);
/// <summary> /// <summary>
/// <para>SelectTheNextItemSlot</para> /// <para>SelectTheNextItemSlot</para>

View File

@ -22,7 +22,7 @@ public class UniversalItemContainer : IItemContainer
{ {
private readonly PackedScene? _itemSlotPackedScene = GD.Load<PackedScene>("res://prefab/ui/ItemSlot.tscn"); private readonly PackedScene? _itemSlotPackedScene = GD.Load<PackedScene>("res://prefab/ui/ItemSlot.tscn");
private readonly List<ItemSlotNode>? _itemSlotNodes = new(); private readonly List<ItemSlotNode>? _itemSlotNodes = [];
/// <summary> /// <summary>
/// <para>Character</para> /// <para>Character</para>
@ -221,7 +221,7 @@ public class UniversalItemContainer : IItemContainer
/// <para>Add items tank</para> /// <para>Add items tank</para>
/// <para>添加物品槽</para> /// <para>添加物品槽</para>
/// </summary> /// </summary>
public void AddItemSlot(Node rootNode, int index) public void AddItemSlot(Node rootNode)
{ {
if (_itemSlotNodes == null || _itemSlotPackedScene == null) if (_itemSlotNodes == null || _itemSlotPackedScene == null)
{ {
@ -234,7 +234,7 @@ public class UniversalItemContainer : IItemContainer
return; return;
} }
itemSlotNode.IsSelect = index == _selectIndex; itemSlotNode.IsSelect = (_itemSlotNodes.Count ) == _selectIndex;
_itemSlotNodes.Add(itemSlotNode); _itemSlotNodes.Add(itemSlotNode);
} }

View File

@ -49,5 +49,8 @@ public partial class Packsack : RigidBody2D, IItem
{ {
base._Ready(); base._Ready();
ItemContainer = new UniversalItemContainer(); ItemContainer = new UniversalItemContainer();
//Test: Add one ItemSlot for pack
ItemContainer.AddItemSlot(this);
} }
} }