Fixed an issue where items could not be thrown.

修复无法扔出物品的问题。
This commit is contained in:
Cold-Mint 2024-06-21 22:55:31 +08:00
parent 309a7b73d2
commit 53f830d5a6
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
5 changed files with 52 additions and 16 deletions

View File

@ -61,7 +61,7 @@ public partial class CharacterTemplate : CharacterBody2D
public Node2D? CurrentItem public Node2D? CurrentItem
{ {
get => _currentItem; get => _currentItem;
set protected set
{ {
_currentItem = value; _currentItem = value;
WhenUpdateCurrentItem(_currentItem); WhenUpdateCurrentItem(_currentItem);
@ -332,7 +332,6 @@ public partial class CharacterTemplate : CharacterBody2D
pickAbleTemplate.Sleeping = true; pickAbleTemplate.Sleeping = true;
} }
var selectItem = selectItemSlotNode.GetItem();
if (_currentItem == null) 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. //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
/// </param> /// </param>
private void ThrowOneItem(ItemSlotNode itemSlotNode, Vector2 velocity) 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(); var item = itemSlotNode.GetItem();
if (item is not Node2D node2D) if (item is not Node2D node2D)
@ -717,6 +716,8 @@ public partial class CharacterTemplate : CharacterBody2D
rigidBody2D.LinearVelocity = velocity; rigidBody2D.LinearVelocity = velocity;
break; break;
} }
itemSlotNode.RemoveItem(1);
} }
/// <summary> /// <summary>

View File

@ -19,6 +19,7 @@ public partial class HotBar : HBoxContainer
{ {
base._Ready(); base._Ready();
_itemContainer = new UniversalItemContainer(); _itemContainer = new UniversalItemContainer();
_itemContainer.SupportSelect = true;
_itemContainer.SelectedItemSlotChangeEvent += SelectedItemSlotChangeEvent; _itemContainer.SelectedItemSlotChangeEvent += SelectedItemSlotChangeEvent;
NodeUtils.DeleteAllChild(this); NodeUtils.DeleteAllChild(this);
for (var i = 0; i < Config.HotBarSize; i++) for (var i = 0; i < Config.HotBarSize; i++)

View File

@ -19,7 +19,18 @@ public partial class ItemSlotNode : MarginContainer
private bool _isSelect; private bool _isSelect;
private Texture2D? _backgroundTexture; private Texture2D? _backgroundTexture;
private Texture2D? _backgroundTextureWhenSelect; private Texture2D? _backgroundTextureWhenSelect;
private IItem? _item = null; private IItem? _item;
public IItem? Item
{
set
{
//Set item
//设置物品
_item = value;
UpdateAllDisplay();
}
}
public override void _Ready() public override void _Ready()
{ {
@ -79,6 +90,14 @@ public partial class ItemSlotNode : MarginContainer
return _item == null; return _item == null;
} }
/// <summary>
/// <para>Get the items in the item container</para>
/// <para>获取物品容器内的物品</para>
/// </summary>
/// <returns>
///<para>There may be multiple quantities</para>
///<para>数量可能有多个</para>
/// </returns>
public IItem? GetItem() public IItem? GetItem()
{ {
return _item; return _item;
@ -100,15 +119,16 @@ public partial class ItemSlotNode : MarginContainer
} }
var itemSlotNode = data.As<ItemSlotNode>(); var itemSlotNode = data.As<ItemSlotNode>();
var itemStack = itemSlotNode._item; var item = itemSlotNode._item;
if (itemStack == null) if (item == null)
{ {
//Return null when trying to get the source item heap. //Return null when trying to get the source item.
//尝试获取源物品时返回null。 //尝试获取源物品时返回null。
return; return;
} }
// ReplaceItemStack(itemStack); itemSlotNode.Item = null;
Item = item;
} }
public bool IsSelect public bool IsSelect
@ -275,6 +295,11 @@ public partial class ItemSlotNode : MarginContainer
//实际移除的数量 //实际移除的数量
var removeNumber = number < 0 ? _item.Quantity : number; var removeNumber = number < 0 ? _item.Quantity : number;
_item.Quantity -= removeNumber; _item.Quantity -= removeNumber;
if (_item.Quantity <= 0)
{
Item = null;
}
return removeNumber; return removeNumber;
} }
@ -282,7 +307,8 @@ public partial class ItemSlotNode : MarginContainer
{ {
if (_item == null) if (_item == null)
{ {
return false; Item = item;
return true;
} }
var newQuantity = item.Quantity + _item.Quantity; var newQuantity = item.Quantity + _item.Quantity;

View File

@ -166,7 +166,15 @@ public class UniversalItemContainer : IItemContainer
return null; return null;
} }
itemSlotNode.IsSelect = (_itemSlotNodes.Count) == _selectIndex; if (SupportSelect)
{
itemSlotNode.IsSelect = _itemSlotNodes.Count == _selectIndex;
}
else
{
itemSlotNode.IsSelect = false;
}
_itemSlotNodes.Add(itemSlotNode); _itemSlotNodes.Add(itemSlotNode);
return itemSlotNode; return itemSlotNode;
} }

View File

@ -37,7 +37,7 @@ public partial class PickAbleTemplate : RigidBody2D, IItem
[Export] private int _maxContactInjury = 2; [Export] private int _maxContactInjury = 2;
public string? Description => UniqueDescription ?? ItemTypeManager.DefaultDescriptionOf(Id); public string? Description => UniqueDescription ?? ItemTypeManager.DefaultDescriptionOf(Id);
public int Quantity { get; set; } public int Quantity { get; set; } = 1;
/// <summary> /// <summary>
/// <para>The number of tile maps that come into contact with this item</para> /// <para>The number of tile maps that come into contact with this item</para>