(temp):ItemSlot has already changed into new interfaces

This commit is contained in:
霧雨烨 2024-06-12 17:57:38 +08:00
parent e9656d6992
commit 9b3701b49c
6 changed files with 285 additions and 129 deletions

View File

@ -41,14 +41,7 @@ public partial class CharacterTemplate : CharacterBody2D
//Item containers are used to store items. //Item containers are used to store items.
//物品容器用于存储物品。 //物品容器用于存储物品。
private IItemContainer? _itemContainer; public IItemContainer? ItemContainer { get; set; }
public IItemContainer? ItemContainer
{
get => _itemContainer;
set => _itemContainer = value;
}
//Items currently held //Items currently held
//当前持有的物品 //当前持有的物品
@ -282,14 +275,14 @@ public partial class CharacterTemplate : CharacterBody2D
return false; return false;
} }
if (_itemContainer == null) if (ItemContainer == null)
{ {
return false; return false;
} }
//Get the currently selected node //Get the currently selected node
//拿到当前选择的节点 //拿到当前选择的节点
var itemSlotNode = _itemContainer.GetSelectItemSlotNode(); var itemSlotNode = ItemContainer.GetSelectItemSlotNode();
if (itemSlotNode == null) if (itemSlotNode == null)
{ {
return false; return false;
@ -297,7 +290,7 @@ public partial class CharacterTemplate : CharacterBody2D
//First check if we can pick up the item. //First check if we can pick up the item.
//先检查我们能否拾起此物品。 //先检查我们能否拾起此物品。
var canPick = _itemContainer.CanAddItem(item); var canPick = ItemContainer.CanAddItem(item);
if (!canPick) if (!canPick)
{ {
return false; return false;
@ -305,7 +298,7 @@ public partial class CharacterTemplate : CharacterBody2D
//Is it successfully added to the container? //Is it successfully added to the container?
//再检查是否成功的添加到容器内了? //再检查是否成功的添加到容器内了?
var addSuccess = _itemContainer.AddItem(item); var addSuccess = ItemContainer.AddItem(item);
if (!addSuccess) if (!addSuccess)
{ {
return false; return false;
@ -504,7 +497,7 @@ public partial class CharacterTemplate : CharacterBody2D
/// <param name="node"></param> /// <param name="node"></param>
protected virtual void EnterThePickingRangeBody(Node node) protected virtual void EnterThePickingRangeBody(Node node)
{ {
if (node is not IItem) if (node is not IItem_New item)
{ {
return; return;
} }
@ -519,7 +512,7 @@ public partial class CharacterTemplate : CharacterBody2D
/// <param name="node"></param> /// <param name="node"></param>
protected virtual void ExitThePickingRangeBody(Node node) protected virtual void ExitThePickingRangeBody(Node node)
{ {
if (node is not IItem) if (node is not IItem_New)
{ {
return; return;
} }
@ -549,12 +542,12 @@ public partial class CharacterTemplate : CharacterBody2D
{ {
//If the item container is null, then return //If the item container is null, then return
//如果物品容器为null那么返回 //如果物品容器为null那么返回
if (_itemContainer == null) if (ItemContainer == null)
{ {
return; return;
} }
var len = _itemContainer.GetItemSlotCount(); var len = ItemContainer.GetItemSlotCount();
if (len == 0) if (len == 0)
{ {
return; return;
@ -599,7 +592,7 @@ public partial class CharacterTemplate : CharacterBody2D
/// </param> /// </param>
protected void ThrowItem(int index, int number, Vector2 velocity) protected void ThrowItem(int index, int number, Vector2 velocity)
{ {
var itemSlotNode = _itemContainer?.GetItemSlotNode(index); var itemSlotNode = ItemContainer?.GetItemSlotNode(index);
var item = itemSlotNode?.GetItem(); var item = itemSlotNode?.GetItem();

View File

@ -339,7 +339,7 @@ public partial class HotBar : HBoxContainer, IItemContainer
{ {
return false; return false;
} }
return itemSlotNode.SetItem(item); return itemSlotNode.ReplaceItemStack(item);
} }
public int GetSelectIndex() public int GetSelectIndex()
@ -374,7 +374,7 @@ public partial class HotBar : HBoxContainer, IItemContainer
foreach (var itemSlotNode in _itemSlotNodes) foreach (var itemSlotNode in _itemSlotNodes)
{ {
if (itemSlotNode.CanSetItem(item)) if (itemSlotNode.CanAddItem(item))
{ {
//If there is an item slot to put this item in, then we return it. //If there is an item slot to put this item in, then we return it.
//如果有物品槽可放置此物品,那么我们返回它。 //如果有物品槽可放置此物品,那么我们返回它。

View File

@ -42,7 +42,49 @@ public partial class ItemSlotNode : MarginContainer
/// <para>获取物品槽内的物品堆</para> /// <para>获取物品槽内的物品堆</para>
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public IItemStack GetItemStack() => _itemStack; public IItemStack? GetItemStack() => _itemStack;
/// <summary>
/// <para>If present, get the item at the top of the item stack in this slot</para>
/// <para>如果存在,获取该槽位中物品堆顶部的物品</para>
/// </summary>
public IItem_New? GetItem() => _itemStack?.GetItem();
/// <summary>
/// <para>If present, remove an item in this slot and return it.</para>
/// <para>如果存在,移除该槽位中的一个物品并将其返回</para>
/// </summary>
/// <seealso cref="PickItems"/>
public IItem_New? PickItem()
{
if (_itemStack is null) return null;
var result = _itemStack.PickItem();
if (_itemStack.Quantity == 0) ClearSlot();
else UpdateAllDisplay();
return result;
}
/// <summary>
/// <para>Remove the specified number of items and return them as a new item stack</para>
/// <para>取出当前物品槽中指定数量的物品,并作为新的物品堆返回</para>
/// </summary>
/// <param name="value">
/// <para>Quantity to be taken out, inputs below zero represent all items</para>
/// <para>要取出的数量小于0的输入代表全部物品</para>
/// </param>
/// <seealso cref="PickItem"/>
public IItemStack? PickItems(int value)
{
if (_itemStack is null) return null;
var result = _itemStack.PickItems(value);
if (_itemStack.Quantity == 0) ClearSlot();
else UpdateAllDisplay();
return result;
}
/// <summary> /// <summary>
/// <para>Removes the specified number of items from the item slot</para> /// <para>Removes the specified number of items from the item slot</para>
@ -53,6 +95,9 @@ public partial class ItemSlotNode : MarginContainer
/// <para>The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0</para> /// <para>The remaining number, if the number of items in the current item stack is less than the specified number. Otherwise,0</para>
/// <para>若物品槽内物品少于指定的数量返回相差的数量。否则返回0</para> /// <para>若物品槽内物品少于指定的数量返回相差的数量。否则返回0</para>
/// </returns> /// </returns>
/// <remarks>
/// <para>会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用</para>
/// </remarks>
public int RemoveItem(int number) public int RemoveItem(int number)
{ {
if (_itemStack == null) if (_itemStack == null)
@ -64,133 +109,139 @@ public partial class ItemSlotNode : MarginContainer
//If the specified number of items is removed, the number of items is less than or equal to 0. Then we empty the inventory. //If the specified number of items is removed, the number of items is less than or equal to 0. Then we empty the inventory.
//如果移除指定数量的物品后物品数量小于或等于0。那么我们清空物品栏。 //如果移除指定数量的物品后物品数量小于或等于0。那么我们清空物品栏。
if (_itemStack.Quantity == 0) ClearSlot(); if (_itemStack.Quantity == 0) ClearSlot();
else else UpdateAllDisplay();
{
UpdateTooltipText(_itemStack);
UpdateQuantityLabel(_itemStack.Quantity);
}
return result; return result;
// var newNumber = _item.Quantity - number;
// if (newNumber <= 0)
// {
// //If the specified number of items is removed, the number of items is less than or equal to 0. Then we return the removal successful and empty the inventory.
// //如果移除指定数量的物品后物品数量小于或等于0。那么我们返回移除成功并清空物品栏。
// ClearItem();
// return true;
// }
// else
// {
// _item.Quantity = newNumber;
// UpdateTooltipText(_item);
// UpdateQuantityLabel(_item.Quantity);
// return true;
// }
} }
/// <summary>
/// <para>Remove item stack from slot and return it, equivalent to ReplaceItemStack(null)</para>
/// <para>从当前槽位中移出并返回物品堆等价于ReplaceItemStack(null)</para>
/// <seealso cref="ReplaceItemStack"/>
/// </summary>
public IItemStack? RemoveItemStack() => ReplaceItemStack(null);
/// <summary> /// <summary>
/// <para>Empty the item slot</para> /// <para>Empty the item slot</para>
/// <para>清空物品槽</para> /// <para>清空当前物品槽</para>
/// </summary> /// </summary>
/// <remarks> /// <remarks>
///<para>This method does not calculate how many items should be left. If you want to remove a specified number of items, call the <see cref="RemoveItem"/> method.</para> ///<para>This method will remove all items stored in the item slots from the game, if this is not what you want to do, consider using the <see cref="RemoveItemStack"/> method.</para>
///<para>此方法不会计算物品应该剩余多少个。若您希望移除指定数量的物品,请调用<see cref="RemoveItem"/>方法。</para> ///<para>此方法会从游戏中移除储存于物品槽中的所有物品,若这不是您希望的操作,请考虑使用<see cref="RemoveItemStack"/>方法。</para>
/// </remarks> /// </remarks>
public void ClearSlot() public void ClearSlot()
{ {
_itemStack?.ClearStack();
_itemStack = null; _itemStack = null;
if (_iconTextureRect != null) UpdateAllDisplay();
{
_iconTextureRect.Texture = null;
} }
if (_control != null)
{
_control.TooltipText = null;
}
_quantityLabel?.Hide();
}
//Todo: I searched until here.
/// <summary> /// <summary>
/// <para>Can the specified item be placed in the item slot?</para> /// <para>Can the specified item be placed in the item slot?</para>
/// <para>指定的物品是否可设置在物品槽内?</para> /// <para>指定的物品是否可设置在物品槽内?</para>
/// </summary> /// </summary>
/// <param name="item"></param> /// <param name="item"></param>
/// <returns></returns> /// <returns></returns>
public bool CanSetItem(IItem item) public bool CanAddItem(IItem_New item)
{ {
if (_item == null) if (_itemStack == null) return true;
{ return _itemStack.CanAddItem(item);
return true;
}
//This inventory already has items, but the items in this inventory are not the same as the incoming items
//这个物品栏已经有物品了,但是这个物品栏的物品和传入的物品不一样
if (_item.Id != item.Id)
{
return false;
}
var newQuantity = _item.Quantity + item.Quantity;
if (newQuantity > item.MaxStackQuantity)
{
//If the amount of the current item exceeds the maximum stack amount after placing it in this inventory
//如果将当前物品放置到这个物品栏后,数量超过了最大叠加数量
return false;
}
return true;
} }
/// <summary> /// <summary>
/// <para>Sets items for the item slot</para> /// <para>
/// <para>为物品槽设置物品</para> /// Set item stack for this slot, this will completely replace current item stack.
/// If you want the item stack to be added to current stack, use the <see cref="AddItemStack"/>.
/// </para>
/// <para>为物品槽设置物品堆,将完全替换掉当前物品堆。如果想要物品堆叠加至该物品堆,请使用<see cref="AddItemStack"/></para>
/// </summary> /// </summary>
/// <param name="item"></param> /// <returns>
/// <returns></returns> /// <para>The item stack that was previously in this slot</para>
public bool SetItem(IItem item) /// <para>该槽位中原本的物品堆</para>
/// </returns>
public IItemStack? ReplaceItemStack(IItemStack? newItemStack)
{ {
if (!CanSetItem(item)) var result = _itemStack;
{ _itemStack = newItemStack;
return false;
UpdateAllDisplay();
return result;
} }
if (_item == null) /// <summary>
/// <para>Try to add an item to this slot, if it can't be added to this slot, return false</para>
/// <para>尝试向当前槽位中加入物品如果该物品不能被放入该槽位返回false</para>
/// </summary>
public bool AddItem(IItem_New item)
{ {
if (item.Icon != null && _iconTextureRect != null) bool result;
if (_itemStack is null)
{ {
_iconTextureRect.Texture = item.Icon; _itemStack = IItemStack.FromItem(item);
} result = true;
_item = item;
UpdateTooltipText(item);
UpdateQuantityLabel(item.Quantity);
return true;
} }
else else
{ {
var newQuantity = _item.Quantity + item.Quantity; result = _itemStack.AddItem(item);
_item.Quantity = newQuantity;
UpdateTooltipText(item);
UpdateQuantityLabel(newQuantity);
return true;
} }
if (result)
{
UpdateAllDisplay();
}
return result;
}
/// <summary>
/// <para>Try to combine an item stack into this slot</para>
/// <para>尝试将一个物品堆合并至该槽位中</para>
/// </summary>
/// <returns>
/// <para>Number of items remaining in the source item pile after the operation is completed</para>
/// <para>操作完成后,源物品堆中剩余的物品数</para>
/// </returns>
public int AddItemStack(IItemStack itemStack)
{
int result;
if (_itemStack is null)
{
_itemStack = itemStack;
result = 0;
}
else
{
result = _itemStack.TakeFrom(itemStack);
}
UpdateAllDisplay();
return result;
}
/// <summary>
/// <para>Update all displays of this slot</para>
/// <para>更新该槽位的一切显示信息</para>
/// </summary>
private void UpdateAllDisplay()
{
UpdateIconTexture();
UpdateQuantityLabel();
UpdateTooltipText();
} }
/// <summary> /// <summary>
/// <para>Update item tips</para> /// <para>Update item tips</para>
/// <para>更新物品的提示内容</para> /// <para>更新物品的提示内容</para>
/// </summary> /// </summary>
/// <param name="item"></param> private void UpdateTooltipText()
private void UpdateTooltipText(IItem item)
{ {
if (_control == null) if (_control == null) return;
if (_itemStack == null)
{ {
_control.TooltipText = null;
return; return;
} }
@ -199,16 +250,16 @@ public partial class ItemSlotNode : MarginContainer
var debugText = TranslationServerUtils.Translate("item_prompt_debug"); var debugText = TranslationServerUtils.Translate("item_prompt_debug");
if (debugText != null) if (debugText != null)
{ {
_control.TooltipText = string.Format(debugText, item.Id, _control.TooltipText = string.Format(debugText, _itemStack.Id,
TranslationServerUtils.Translate(item.Name), TranslationServerUtils.Translate(_itemStack.Name),
item.Quantity, item.MaxStackQuantity, item.GetType().Name, _itemStack.Quantity, _itemStack.MaxQuantity, _itemStack.GetType().Name,
TranslationServerUtils.Translate(item.Description)); TranslationServerUtils.Translate(_itemStack.Description));
} }
} }
else else
{ {
_control.TooltipText = TranslationServerUtils.Translate(item.Name) + "\n" + _control.TooltipText = TranslationServerUtils.Translate(_itemStack.Name) + "\n" +
TranslationServerUtils.Translate(item.Description); TranslationServerUtils.Translate(_itemStack.Description);
} }
} }
@ -216,28 +267,36 @@ public partial class ItemSlotNode : MarginContainer
/// <para>Update quantity label</para> /// <para>Update quantity label</para>
/// <para>更新数量标签</para> /// <para>更新数量标签</para>
/// </summary> /// </summary>
/// <param name="quantity"></param> private void UpdateQuantityLabel()
private void UpdateQuantityLabel(int? quantity)
{ {
if (_quantityLabel == null) if (_quantityLabel == null)
{ {
return; return;
} }
switch (quantity) switch (_itemStack?.Quantity)
{ {
case null: case null or 1:
_quantityLabel.Hide(); _quantityLabel.Hide();
return; return;
case > 1: default:
//When the quantity is greater than 1, we display the quantity. //When the quantity is not null or 1, we display the quantity.
//当数量大于1时我们显示数量 //当数量不为null或1时我们显示数量
_quantityLabel.Text = quantity.ToString(); _quantityLabel.Text = _itemStack?.Quantity.ToString();
_quantityLabel.Show(); _quantityLabel.Show();
break; break;
default: }
_quantityLabel.Hide(); }
break;
/// <summary>
/// <para>Update texture of the icon rect</para>
/// <para>更新显示的物品图标</para>
/// </summary>
private void UpdateIconTexture()
{
if (_iconTextureRect != null)
{
_iconTextureRect.Texture = _itemStack?.Icon;
} }
} }

View File

@ -4,6 +4,9 @@ using Godot;
namespace ColdMint.scripts.item; namespace ColdMint.scripts.item;
/// <summary>
/// <para>Item stack in an inventory slot</para>
/// </summary>
public interface IItemStack public interface IItemStack
{ {
/// <summary> /// <summary>
@ -36,10 +39,81 @@ public interface IItemStack
/// </summary> /// </summary>
string? Description { get; } string? Description { get; }
/// <summary>
/// <para>Determine whether a specified item can be accommodated</para>
/// <para>判断能否容纳指定物品</para>
/// </summary>
/// <returns></returns>
public bool CanAddItem(IItem_New item);
/// <summary> /// <summary>
/// <para>Removes the specified number of items from current item stack</para> /// <para>Hold a given item</para>
/// <para>在当前物品堆移除指定数量的物品</para> /// </summary>
/// <param name="item">Item to hold by current stack</param>
/// <returns>Whether successful</returns>
public bool AddItem(IItem_New item);
/// <summary>
/// <para>判断能从指定物品堆中接收的物品数量</para>
/// </summary>
/// <param name="itemStack">
/// <para>向该物品堆中放入物品的物品堆</para>
/// <para>Item stack to add to the current stack</para>
/// </param>
/// <returns></returns>
public int CanTakeFrom(IItemStack itemStack);
/// <summary>
/// <para>将指定物品堆中尽可能多的物品移动至当前物品堆中,被移入当前堆的物品应从原物品堆中移除。</para>
/// </summary>
/// <param name="itemStack">
/// <para>被移入当前堆的物品堆</para>
/// </param>
/// <returns>
/// <para>操作结束后原物品堆中剩余的物品数</para>
/// </returns>
public int TakeFrom(IItemStack itemStack);
/// <summary>
/// <para>Get item instance at the top of current stack without removing it from stack</para>
/// <para>获取当前物品堆顶部的物品实例而不取出该物品</para>
/// <seealso cref="PickItem"/>
/// </summary>
/// <returns></returns>
public IItem_New? GetItem();
/// <summary>
/// <para>Pop the item instance at the top of current item stack and return it</para>
/// <para>取出当前物品堆顶部的物品实例并返回该物品</para>
/// <seealso cref="GetItem"/><seealso cref="PickItems"/>
/// </summary>
/// <returns></returns>
public IItem_New? PickItem();
/// <summary>
/// <para>Remove the specified number of items and return them as a new item stack</para>
/// <para>取出当前堆中指定数量的物品,并作为新的物品堆返回</para>
/// <seealso cref="PickItem"/>
/// </summary>
/// <param name="value">
/// <para>Quantity to be taken out, inputs below zero represent all items</para>
/// <para>要取出的数量小于0的输入代表全部物品</para>
/// </param>
/// <returns>
/// <para>The item stack that is taken out, can be null if out nothing, should not be the current item stack itself</para>
/// <para>取出的物品堆没有取出物品时可为null不应是当前物品堆自身</para>
/// </returns>
public IItemStack? PickItems(int value);
/// <summary>
/// <para>
/// Removes the specified number of items from current item stack,removed items should be removed from the game<br/>
/// If you don't want remove them from game, consider <see cref="PickItem"/> and <see cref="PickItems"/>
/// </para>
/// <para>
/// 在当前物品堆移除指定数量的物品,被移除的物品应当从游戏中移除。<br/>
/// 如果您不想将它们从游戏中移除,请考虑: <see cref="PickItem"/>、<see cref="PickItems"/>
/// </para>
/// </summary> /// </summary>
/// <param name="number"></param> /// <param name="number"></param>
/// <returns> /// <returns>
@ -48,6 +122,11 @@ public interface IItemStack
/// </returns> /// </returns>
public int RemoveItem(int number); public int RemoveItem(int number);
/// <summary>
/// <para>Clear current stack, which means should dispose all items inside current stack here</para>
/// </summary>
public void ClearStack();
/// <summary> /// <summary>
/// Create a new ItemStack with the given item as the first item /// Create a new ItemStack with the given item as the first item
/// </summary> /// </summary>

View File

@ -28,4 +28,9 @@ public interface IItem_New
/// <param name="owner">Owner of current item, if any</param> /// <param name="owner">Owner of current item, if any</param>
/// <param name="targetGlobalPosition">Target position, such as the position of the cursor when used by the player</param> /// <param name="targetGlobalPosition">Target position, such as the position of the cursor when used by the player</param>
void Use(Node2D? owner, Vector2 targetGlobalPosition); void Use(Node2D? owner, Vector2 targetGlobalPosition);
/// <summary>
/// <para>Execute when current item be removed from game.</para>
/// </summary>
void Destroy();
} }

View File

@ -1,21 +1,41 @@
using ColdMint.scripts.inventory; using System;
using ColdMint.scripts.inventory;
using Godot; using Godot;
namespace ColdMint.scripts.item; namespace ColdMint.scripts.item;
/// <summary> /// <summary>
/// <para>Item stack in inventory</para> /// <para>Item stack of single item</para>
/// </summary> /// </summary>
//maybe we'd move this into inventory namespace //maybe we'd move this into inventory namespace
public readonly struct SingleItemStack(IItem_New item) : IItemStack public struct SingleItemStack(IItem_New item) : IItemStack
{ {
public IItem_New Item { get; init; } = item; public IItem_New Item { get; init; } = item;
public string Id => Item.Id; public string Id => Item.Id;
public int MaxQuantity => 1; public int MaxQuantity => 1;
public int Quantity => 1; public int Quantity { get; set; } = 1;
public Texture2D Icon => Item.Icon; public Texture2D Icon => Item.Icon;
public string Name => Item.Name; public string Name => Item.Name;
public string? Description => Item.Description; public string? Description => Item.Description;
public bool CanAddItem(IItem_New item) => false;
public bool AddItem(IItem_New item) => false;
public int CanTakeFrom(IItemStack itemStack) => 0;
public int TakeFrom(IItemStack itemStack) => 0;
public int RemoveItem(int number)
{
}
public void ClearStack()
{
throw new System.NotImplementedException();
}
} }