UniversalItemContainer refactored done

UniversalItemContainer调教完成
This commit is contained in:
霧雨烨 2024-06-13 01:18:55 +08:00
parent 76daa88bac
commit 16b3a5a106
5 changed files with 216 additions and 56 deletions

View File

@ -3,6 +3,7 @@ using Godot;
namespace ColdMint.scripts.inventory;
/*
/// <summary>
/// <para>Common goods</para>
/// <para>普通的物品</para>
@ -18,3 +19,4 @@ public class CommonItem : IItem
public Action<IItem>? OnUse { get; set; }
public Func<IItem, Node>? OnInstantiation { get; set; }
}
*/

View File

@ -1,3 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using ColdMint.scripts.item;
using Godot;
@ -30,6 +34,17 @@ public interface IItemContainer
/// <returns></returns>
bool AddItem(IItem_New item);
/// <summary>
/// <para>Add an stack of items to this container</para>
/// <para>向当前容器中存入一堆物品</para>
/// </summary>
/// <param name="itemStack"></param>
/// <returns>
/// <para>If the source item stack is empty after the operation is completed</para>
/// <para>操作完成后,源物品堆是否被取空</para>
/// </returns>
bool AddItemStack(IItemStack itemStack);
/// <summary>
/// <para>Gets the selected location</para>
/// <para>获取选中的位置</para>
@ -44,13 +59,41 @@ public interface IItemContainer
/// <returns></returns>
ItemSlotNode? GetSelectItemSlotNode();
/// <summary>
/// <para>If present, remove an item from the slot at the currently selected location and return it.</para>
/// <para>如果存在,移除当前选中位置的槽位中的一个物品并将其返回</para>
/// </summary>
/// <seealso cref="PickItemFromItemSlot"/><seealso cref="PickItemsFromItemSlotBySelectIndex"/>
IItem_New? PickItemFromItemSlotBySelectIndex();
/// <summary>
/// <para>Remove the specified number of items from the item slot at the currently selected location, 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="PickItemsFromItemSlot"/><seealso cref="PickItemFromItemSlotBySelectIndex"/>
IItemStack? PickItemsFromItemSlotBySelectIndex( int value);
/// <summary>
/// <para>Removes an item from the inventory at the currently selected location</para>
/// <para>移除当前选中位置物品栏内的物品</para>
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
bool RemoveItemFromItemSlotBySelectIndex(int number);
/// <param name="number">
/// <para>Quantity to be removed, inputs below zero represent all items</para>
/// <para>要删除的数量小于0的输入代表全部物品</para>
/// </param>
/// <returns>
/// <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>
/// </returns>
/// <remarks>
/// <para>Will remove the removed items from the game, if that is not the intent, consider using the <see cref="PickItemsFromItemSlotBySelectIndex"/></para>
/// <para>会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用<see cref="PickItemsFromItemSlotBySelectIndex"/></para>
/// </remarks>
int RemoveItemFromItemSlotBySelectIndex(int number);
/// <summary>
/// <para>Gets the number of item slots</para>
@ -67,25 +110,90 @@ public interface IItemContainer
/// <returns></returns>
ItemSlotNode? GetItemSlotNode(int index);
/// <summary>
/// <para>If present, remove an item from the slot in the specified location and return it.</para>
/// <para>如果存在,移除指定位置的槽位中的一个物品并将其返回</para>
/// </summary>
/// <seealso cref="PickItemsFromItemSlot"/>
IItem_New? PickItemFromItemSlot(int itemSlotIndex);
/// <summary>
/// <para>Remove the specified number of items from the item slot in the specified location, and return them as a new item stack</para>
/// <para>取出指定位置的物品槽中指定数量的物品,并作为新的物品堆返回</para>
/// </summary>
/// <param name="itemSlotIndex"></param>
/// <param name="value">
/// <para>Quantity to be taken out, inputs below zero represent all items</para>
/// <para>要取出的数量小于0的输入代表全部物品</para>
/// </param>
/// <seealso cref="PickItemFromItemSlot"/>
IItemStack? PickItemsFromItemSlot(int itemSlotIndex, int value);
/// <summary>
/// <para>Removes an item from the item slot in the specified location</para>
/// <para>在指定位置的物品槽内移除物品</para>
/// </summary>
/// <param name="itemSlotIndex"></param>
/// <param name="number"></param>
/// <returns></returns>
bool RemoveItemFromItemSlot(int itemSlotIndex, int number);
/// <param name="number">
/// <para>Quantity to be removed, inputs below zero represent all items</para>
/// <para>要删除的数量小于0的输入代表全部物品</para>
/// </param>
/// <returns>
/// <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>
/// </returns>
/// <remarks>
/// <para>Will remove the removed items from the game, if that is not the intent, consider using the <see cref="PickItemsFromItemSlot"/></para>
/// <para>会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用<see cref="PickItemsFromItemSlot"/></para>
/// </remarks>
int RemoveItemFromItemSlot(int itemSlotIndex, int number);
/// <summary>
/// <para>Based on the given item, match the item slots where it can be placed</para>
/// <para>Based on the given item, match the item slots where it can be added to </para>
/// <para>根据给定的物品,匹配可放置它的物品槽</para>
/// </summary>
/// <param name="item"></param>
/// <returns>
///<para>Return null if there is no slot to place the item in</para>
///<para>若没有槽可放置此物品则返回null</para>
/// <para>Return null if there is no slot to place the item in</para>
/// <para>若没有槽可放置此物品则返回null</para>
/// </returns>
ItemSlotNode? Matching(IItem_New item);
ItemSlotNode? Match(IItem_New item);
/// <summary>
/// <para>Based on the given item stack, match the item slots where it can be added to</para>
/// <para>根据给定的物品堆,匹配可放置它的物品槽</para>
/// </summary>
/// <param name="stack"></param>
/// <returns>
/// <para>Return null if there is no slot to add the item slot in</para>
/// <para>若没有槽可放置此物品堆则返回null</para>
/// </returns>
ItemSlotNode? Match(IItemStack stack);
/// <summary>
/// <para>Match the first item slot that has item stack that satisfies the predicate</para>
/// <para>匹配首个拥有满足指定条件的物品堆的物品槽</para>
/// </summary>
/// <param name="predicate"></param>
/// <returns>
/// <para>Return null if there is no slot satisfies the predicate</para>
/// <para>若没有满足条件的槽位返回null</para>
/// </returns>
/// <seealso cref="MatchAll"/>
ItemSlotNode? Match(Func<IItemStack?, bool> predicate);
/// <summary>
/// <para>Match all item slots that has item stack that satisfies the predicate</para>
/// <para>匹配所有拥有满足指定条件的物品堆的物品槽</para>
/// </summary>
/// <param name="predicate"></param>
/// <returns>
/// <para>IEnumerable for the item slot matched to, will be empty if there's no slot satisfies the predicate</para>
/// <para>包含匹配到的槽位的IEnumerable当没有满足条件的槽位时为空</para>
/// </returns>
/// <seealso cref="Match(Func{IItemStack?,bool})"/>
IEnumerable<ItemSlotNode> MatchAll(Func<IItemStack?, bool> predicate);
/// <summary>
/// <para>AddItemSlot</para>

View File

@ -90,13 +90,17 @@ public partial class ItemSlotNode : MarginContainer
/// <para>Removes the specified number of items from the item slot</para>
/// <para>在物品槽内移除指定数量的物品</para>
/// </summary>
/// <param name="number"></param>
/// <param name="number">
/// <para>Quantity to be removed, inputs below zero represent all items</para>
/// <para>要删除的数量小于0的输入代表全部物品</para>
/// </param>
/// <returns>
/// <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>
/// </returns>
/// <remarks>
/// <para>会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用</para>
/// <para>Will remove the removed items from the game, if that is not the intent, consider using the <see cref="PickItems"/></para>
/// <para>会将移除的物品从游戏中删除,如果目的并非如此,请考虑使用<see cref="PickItems"/></para>
/// </remarks>
public int RemoveItem(int number)
{
@ -200,16 +204,16 @@ public partial class ItemSlotNode : MarginContainer
/// <para>尝试将一个物品堆合并至该槽位中</para>
/// </summary>
/// <returns>
/// <para>Number of items remaining in the source item pile after the operation is completed</para>
/// <para>操作完成后,源物品堆中剩余的物品数</para>
/// <para>If the source item stack is empty after the operation is completed</para>
/// <para>操作完成后,源物品堆是否被取空</para>
/// </returns>
public int AddItemStack(IItemStack itemStack)
public bool AddItemStack(IItemStack itemStack)
{
int result;
bool result;
if (_itemStack is null)
{
_itemStack = itemStack;
result = 0;
result = false;
}
else
{

View File

@ -1,6 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using ColdMint.scripts.character;
using ColdMint.scripts.item;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.inventory;
@ -30,20 +35,34 @@ public class UniversalItemContainer : IItemContainer
//_selectIndex默认为0.
private int _selectIndex;
public bool CanAddItem(IItem item)
public bool CanAddItem(IItem_New item)
{
return Matching(item) != null;
return Match(item) != null;
}
public bool AddItem(IItem item)
public bool AddItem(IItem_New item)
{
var itemSlotNode = Matching(item);
var itemSlotNode = Match(item);
if (itemSlotNode == null)
{
return false;
}
return itemSlotNode.SetItem(item);
return itemSlotNode.AddItem(item);
}
public bool AddItemStack(IItemStack itemStack)
{
while (true)
{
var itemSlotNode = Match(itemStack);
if (itemSlotNode == null)
return false;
if (itemSlotNode.AddItemStack(itemStack))
return true;
}
}
public int GetSelectIndex()
@ -68,10 +87,11 @@ public class UniversalItemContainer : IItemContainer
return null;
}
public bool RemoveItemFromItemSlotBySelectIndex(int number)
{
return RemoveItemFromItemSlot(_selectIndex, number);
}
public IItem_New? PickItemFromItemSlotBySelectIndex() => PickItemFromItemSlot(_selectIndex);
public IItemStack? PickItemsFromItemSlotBySelectIndex(int value) => PickItemsFromItemSlot(_selectIndex, value);
public int RemoveItemFromItemSlotBySelectIndex(int number) => RemoveItemFromItemSlot(_selectIndex, number);
public int GetItemSlotCount()
{
@ -94,42 +114,65 @@ public class UniversalItemContainer : IItemContainer
return _itemSlotNodes[safeIndex];
}
public bool RemoveItemFromItemSlot(int itemSlotIndex, int number)
public IItem_New? PickItemFromItemSlot(int itemSlotIndex)
{
if (_itemSlotNodes == null)
{
return false;
}
if (_itemSlotNodes == null) return null;
var safeIndex = GetSafeIndex(itemSlotIndex);
if (safeIndex == UnknownIndex)
{
return false;
return null;
}
var itemSlot = _itemSlotNodes[safeIndex];
return itemSlot.PickItem();
}
public IItemStack? PickItemsFromItemSlot(int itemSlotIndex, int value)
{
if (_itemSlotNodes == null) return null;
var safeIndex = GetSafeIndex(itemSlotIndex);
if (safeIndex == UnknownIndex)
{
return null;
}
var itemSlot = _itemSlotNodes[safeIndex];
return itemSlot.PickItems(value);
}
public int RemoveItemFromItemSlot(int itemSlotIndex, int number)
{
if (_itemSlotNodes == null) return number;
var safeIndex = GetSafeIndex(itemSlotIndex);
if (safeIndex == UnknownIndex)
{
return number;
}
var itemSlot = _itemSlotNodes[safeIndex];
return itemSlot.RemoveItem(number);
}
public ItemSlotNode? Matching(IItem item)
public ItemSlotNode? Match(IItem_New item)
{
if (_itemSlotNodes == null || _itemSlotNodes.Count == 0)
{
return null;
//Find and return the first slot that can hold this item, if the list is null or not found, return null
//寻找并返回第一个遇到的可放置此物品的物品槽若列表为空或不存在将返回null
return _itemSlotNodes?.FirstOrDefault(itemSlotNode => itemSlotNode.CanAddItem(item));
}
foreach (var itemSlotNode in _itemSlotNodes)
public ItemSlotNode? Match(IItemStack stack)
{
if (itemSlotNode.CanSetItem(item))
{
//If there is an item slot to put this item in, then we return it.
//如果有物品槽可放置此物品,那么我们返回它。
return itemSlotNode;
}
throw new NotImplementedException();
}
return null;
public ItemSlotNode? Match(Func<IItemStack?, bool> predicate)
{
throw new NotImplementedException();
}
public IEnumerable<ItemSlotNode> MatchAll(Func<IItemStack?, bool> predicate)
{
throw new NotImplementedException();
}
@ -139,8 +182,8 @@ public class UniversalItemContainer : IItemContainer
/// </summary>
/// <param name="itemSlotIndex"></param>
/// <returns>
///<para>-1 is returned on failure, and the index that does not result in an out-of-bounds subscript is returned on success</para>
///<para>失败返回-1成功返回不会导致下标越界的索引</para>
/// <para>-1 is returned on failure, and the index that does not result in an out-of-bounds subscript is returned on success</para>
/// <para>失败返回-1成功返回不会导致下标越界的索引</para>
/// </returns>
private int GetSafeIndex(int itemSlotIndex)
{

View File

@ -70,9 +70,9 @@ public interface IItemStack
/// <para>被移入当前堆的物品堆</para>
/// </param>
/// <returns>
/// <para>操作结束后原物品堆中剩余的物品数</para>
/// <para>操作结束后原物品堆是否为空</para>
/// </returns>
public int TakeFrom(IItemStack itemStack);
public bool TakeFrom(IItemStack itemStack);
/// <summary>
/// <para>Get item instance at the top of current stack without removing it from stack</para>
@ -112,10 +112,13 @@ public interface IItemStack
/// </para>
/// <para>
/// 在当前物品堆移除指定数量的物品,被移除的物品应当从游戏中移除。<br/>
/// 如果您不想将它们从游戏中移除,请考虑: <see cref="PickItem"/>、<see cref="PickItems"/>
/// 如果您并不打算将它们从游戏中移除,请考虑使用 <see cref="PickItem"/> 和 <see cref="PickItems"/>
/// </para>
/// </summary>
/// <param name="number"></param>
/// <param name="number">
/// <para>Quantity to be removed, inputs below zero represent all items</para>
/// <para>要删除的数量小于0的输入代表全部物品</para>
/// </param>
/// <returns>
/// <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>