From 53f830d5a6303e492df708ca6ea1099adfae7b41 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Fri, 21 Jun 2024 22:55:31 +0800 Subject: [PATCH] =?UTF-8?q?Fixed=20an=20issue=20where=20items=20could=20no?= =?UTF-8?q?t=20be=20thrown.=20=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=89=94=E5=87=BA=E7=89=A9=E5=93=81=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/character/CharacterTemplate.cs | 9 ++--- scripts/inventory/HotBar.cs | 3 +- scripts/inventory/ItemSlotNode.cs | 40 +++++++++++++++++---- scripts/inventory/UniversalItemContainer.cs | 14 ++++++-- scripts/pickable/PickAbleTemplate.cs | 2 +- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index bd30162..b74d21f 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -61,7 +61,7 @@ public partial class CharacterTemplate : CharacterBody2D public Node2D? CurrentItem { get => _currentItem; - set + protected set { _currentItem = value; WhenUpdateCurrentItem(_currentItem); @@ -332,7 +332,6 @@ public partial class CharacterTemplate : CharacterBody2D pickAbleTemplate.Sleeping = true; } - var selectItem = selectItemSlotNode.GetItem(); if (_currentItem == null) { //If the selected item slot in the item container is a newly picked item, and there is no item in the hand, then we put the selected item into the hand. @@ -664,8 +663,8 @@ public partial class CharacterTemplate : CharacterBody2D /// private void ThrowOneItem(ItemSlotNode itemSlotNode, Vector2 velocity) { - //Pick an item from the item container - //从物品容器内取出一个物品 + //Remove the item from the item container + //从物品容器内取出物品 var item = itemSlotNode.GetItem(); if (item is not Node2D node2D) @@ -717,6 +716,8 @@ public partial class CharacterTemplate : CharacterBody2D rigidBody2D.LinearVelocity = velocity; break; } + + itemSlotNode.RemoveItem(1); } /// diff --git a/scripts/inventory/HotBar.cs b/scripts/inventory/HotBar.cs index 20c7464..16ae362 100644 --- a/scripts/inventory/HotBar.cs +++ b/scripts/inventory/HotBar.cs @@ -12,13 +12,14 @@ namespace ColdMint.scripts.inventory; public partial class HotBar : HBoxContainer { private IItemContainer? _itemContainer; - + Action? SelectedItemSlotChangeEvent { get; set; } public override void _Ready() { base._Ready(); _itemContainer = new UniversalItemContainer(); + _itemContainer.SupportSelect = true; _itemContainer.SelectedItemSlotChangeEvent += SelectedItemSlotChangeEvent; NodeUtils.DeleteAllChild(this); for (var i = 0; i < Config.HotBarSize; i++) diff --git a/scripts/inventory/ItemSlotNode.cs b/scripts/inventory/ItemSlotNode.cs index ac62e69..ce083aa 100644 --- a/scripts/inventory/ItemSlotNode.cs +++ b/scripts/inventory/ItemSlotNode.cs @@ -19,7 +19,18 @@ public partial class ItemSlotNode : MarginContainer private bool _isSelect; private Texture2D? _backgroundTexture; private Texture2D? _backgroundTextureWhenSelect; - private IItem? _item = null; + private IItem? _item; + + public IItem? Item + { + set + { + //Set item + //设置物品 + _item = value; + UpdateAllDisplay(); + } + } public override void _Ready() { @@ -79,6 +90,14 @@ public partial class ItemSlotNode : MarginContainer return _item == null; } + /// + /// Get the items in the item container + /// 获取物品容器内的物品 + /// + /// + ///There may be multiple quantities + ///数量可能有多个 + /// public IItem? GetItem() { return _item; @@ -100,15 +119,16 @@ public partial class ItemSlotNode : MarginContainer } var itemSlotNode = data.As(); - var itemStack = itemSlotNode._item; - if (itemStack == null) + var item = itemSlotNode._item; + if (item == null) { - //Return null when trying to get the source item heap. - //尝试获取源物品堆时返回null。 + //Return null when trying to get the source item. + //尝试获取源物品时返回null。 return; } - // ReplaceItemStack(itemStack); + itemSlotNode.Item = null; + Item = item; } public bool IsSelect @@ -275,6 +295,11 @@ public partial class ItemSlotNode : MarginContainer //实际移除的数量 var removeNumber = number < 0 ? _item.Quantity : number; _item.Quantity -= removeNumber; + if (_item.Quantity <= 0) + { + Item = null; + } + return removeNumber; } @@ -282,7 +307,8 @@ public partial class ItemSlotNode : MarginContainer { if (_item == null) { - return false; + Item = item; + return true; } var newQuantity = item.Quantity + _item.Quantity; diff --git a/scripts/inventory/UniversalItemContainer.cs b/scripts/inventory/UniversalItemContainer.cs index 160e455..2e5f853 100644 --- a/scripts/inventory/UniversalItemContainer.cs +++ b/scripts/inventory/UniversalItemContainer.cs @@ -166,7 +166,15 @@ public class UniversalItemContainer : IItemContainer return null; } - itemSlotNode.IsSelect = (_itemSlotNodes.Count) == _selectIndex; + if (SupportSelect) + { + itemSlotNode.IsSelect = _itemSlotNodes.Count == _selectIndex; + } + else + { + itemSlotNode.IsSelect = false; + } + _itemSlotNodes.Add(itemSlotNode); return itemSlotNode; } @@ -216,7 +224,7 @@ public class UniversalItemContainer : IItemContainer PrivateSelectItemSlot(oldSelectIndex, newSelectIndex); } - + /// /// Select an item slot /// 选中某个物品槽 @@ -241,7 +249,7 @@ public class UniversalItemContainer : IItemContainer }); _selectIndex = newSelectIndex; } - + /// /// HideItem /// 隐藏某个物品 diff --git a/scripts/pickable/PickAbleTemplate.cs b/scripts/pickable/PickAbleTemplate.cs index 85fd831..4a04051 100644 --- a/scripts/pickable/PickAbleTemplate.cs +++ b/scripts/pickable/PickAbleTemplate.cs @@ -37,7 +37,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem [Export] private int _maxContactInjury = 2; public string? Description => UniqueDescription ?? ItemTypeManager.DefaultDescriptionOf(Id); - public int Quantity { get; set; } + public int Quantity { get; set; } = 1; /// /// The number of tile maps that come into contact with this item