Add single item stack type, move the weapon namespace into the item namespace, and I think it's ready to be merged into the new interface

This commit is contained in:
霧雨烨 2024-06-12 00:51:40 +08:00
parent 0880feb298
commit 026c7ff32f
13 changed files with 94 additions and 54 deletions

Binary file not shown.

View File

@ -147,7 +147,6 @@ hotbar_previous={
[internationalization] [internationalization]
locale/translations=PackedStringArray("res://locals/DeathInfo.en.translation", "res://locals/DeathInfo.ja.translation", "res://locals/DeathInfo.zh.translation", "res://locals/InputMapping.en.translation", "res://locals/InputMapping.ja.translation", "res://locals/InputMapping.zh.translation", "res://locals/Log.en.translation", "res://locals/Log.ja.translation", "res://locals/Log.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation", "res://locals/Slogan.zh.translation", "res://locals/UI.en.translation", "res://locals/UI.ja.translation", "res://locals/UI.zh.translation", "res://locals/Weapon.en.translation", "res://locals/Weapon.ja.translation", "res://locals/Weapon.zh.translation") locale/translations=PackedStringArray("res://locals/DeathInfo.en.translation", "res://locals/DeathInfo.ja.translation", "res://locals/DeathInfo.zh.translation", "res://locals/InputMapping.en.translation", "res://locals/InputMapping.ja.translation", "res://locals/InputMapping.zh.translation", "res://locals/Log.en.translation", "res://locals/Log.ja.translation", "res://locals/Log.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation", "res://locals/Slogan.zh.translation", "res://locals/UI.en.translation", "res://locals/UI.ja.translation", "res://locals/UI.zh.translation", "res://locals/Weapon.en.translation", "res://locals/Weapon.ja.translation", "res://locals/Weapon.zh.translation")
locale/test="ja"
[layer_names] [layer_names]

View File

@ -1,5 +1,6 @@
using ColdMint.scripts.character; using ColdMint.scripts.character;
using ColdMint.scripts.weapon;
using ColdMint.scripts.item.weapon;
namespace ColdMint.scripts.behaviorTree.ai; namespace ColdMint.scripts.behaviorTree.ai;

View File

@ -1,13 +1,15 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using ColdMint.scripts.camp; using ColdMint.scripts.camp;
using ColdMint.scripts.damage; using ColdMint.scripts.damage;
using ColdMint.scripts.debug; using ColdMint.scripts.debug;
using ColdMint.scripts.health; using ColdMint.scripts.health;
using ColdMint.scripts.inventory; using ColdMint.scripts.inventory;
using ColdMint.scripts.utils; using ColdMint.scripts.utils;
using ColdMint.scripts.weapon; using ColdMint.scripts.item.weapon;
using Godot; using Godot;
namespace ColdMint.scripts.character; namespace ColdMint.scripts.character;
@ -69,9 +71,7 @@ public partial class CharacterTemplate : CharacterBody2D
///<para>Update finished items</para> ///<para>Update finished items</para>
///<para>更新完成后的物品</para> ///<para>更新完成后的物品</para>
/// </param> /// </param>
protected virtual void WhenUpdateCurrentItem(Node2D? currentItem) protected virtual void WhenUpdateCurrentItem(Node2D? currentItem) { }
{
}
//Define a pick up range //Define a pick up range
//定义一个拾起范围 //定义一个拾起范围
@ -219,6 +219,7 @@ public partial class CharacterTemplate : CharacterBody2D
//如果指定了战利品表,那么获取战利品表。 //如果指定了战利品表,那么获取战利品表。
_lootList = LootListManager.GetLootList(lootListId); _lootList = LootListManager.GetLootList(lootListId);
} }
if (MaxHp <= 0) if (MaxHp <= 0)
{ {
//If Max blood volume is 0 or less, set Max blood volume to 10 //If Max blood volume is 0 or less, set Max blood volume to 10
@ -467,9 +468,7 @@ public partial class CharacterTemplate : CharacterBody2D
_additionalForce = force; _additionalForce = force;
} }
protected virtual void OnHit(DamageTemplate damageTemplate) protected virtual void OnHit(DamageTemplate damageTemplate) { }
{
}
/// <summary> /// <summary>
/// <para>Handle the event of character death</para> /// <para>Handle the event of character death</para>
@ -599,22 +598,9 @@ 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)
{ {
if (_itemContainer == null) var itemSlotNode = _itemContainer?.GetItemSlotNode(index);
{
return;
}
var itemSlotNode = _itemContainer.GetItemSlotNode(index); var item = itemSlotNode?.GetItem();
if (itemSlotNode == null)
{
return;
}
var item = itemSlotNode.GetItem();
if (item == null)
{
return;
}
if (item is not Node2D node2D) if (item is not Node2D node2D)
{ {
@ -669,11 +655,11 @@ public partial class CharacterTemplate : CharacterBody2D
//在物品容器内移除物品 //在物品容器内移除物品
if (number < 0) if (number < 0)
{ {
itemSlotNode.RemoveItem(item.Quantity); itemSlotNode!.RemoveItem(item.Quantity);
} }
else else
{ {
itemSlotNode.RemoveItem(number); itemSlotNode!.RemoveItem(number);
} }
} }
@ -728,7 +714,5 @@ public partial class CharacterTemplate : CharacterBody2D
} }
protected virtual void HookPhysicsProcess(ref Vector2 velocity, double delta) protected virtual void HookPhysicsProcess(ref Vector2 velocity, double delta) { }
{
}
} }

View File

@ -1,11 +1,13 @@
using System; using System;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ColdMint.scripts.damage; using ColdMint.scripts.damage;
using ColdMint.scripts.deathInfo; using ColdMint.scripts.deathInfo;
using ColdMint.scripts.map.events; using ColdMint.scripts.map.events;
using ColdMint.scripts.utils; using ColdMint.scripts.utils;
using ColdMint.scripts.weapon; using ColdMint.scripts.item.weapon;
using Godot; using Godot;
namespace ColdMint.scripts.character; namespace ColdMint.scripts.character;

View File

@ -396,8 +396,7 @@ public partial class HotBar : HBoxContainer, IItemContainer
return; return;
} }
var itemSlotNode = _itemSlotPackedScene.Instantiate() as ItemSlotNode; if (_itemSlotPackedScene.Instantiate() is not ItemSlotNode itemSlotNode)
if (itemSlotNode == null)
{ {
return; return;
} }

View File

@ -1,4 +1,6 @@
using Godot; using System;
using Godot;
namespace ColdMint.scripts.item; namespace ColdMint.scripts.item;
@ -17,7 +19,7 @@ public interface IItemStack
/// <summary> /// <summary>
/// <para>Quantity of current stack</para> /// <para>Quantity of current stack</para>
/// </summary> /// </summary>
int Quantity { get; set; } int Quantity { get; }
/// <summary> /// <summary>
/// <para>Icon of current item</para> /// <para>Icon of current item</para>
@ -33,4 +35,17 @@ public interface IItemStack
/// <para>Description of current item, which may show in inventory</para> /// <para>Description of current item, which may show in inventory</para>
/// </summary> /// </summary>
string? Description { get; } string? Description { get; }
/// <summary>
/// Create a new ItemStack with the given item as the first item
/// </summary>
public static IItemStack? FromItem(IItem_New item) => ItemTypeManager.StackTypeOf(item.Id) switch
{
StackType.Common => throw new NotImplementedException(),
StackType.Unique => throw new NotImplementedException(),
StackType.Unstackable => new SingleItemStack(item),
null => null,
_ => throw new ArgumentException()
};
} }

View File

@ -13,13 +13,17 @@ public readonly struct ItemType
/// <summary> /// <summary>
/// <para>A function returns a new item instance of this type</para> /// <para>A function returns a new item instance of this type</para>
/// </summary> /// </summary>
public Func<IItem_New> Getter { get; init; } public Func<IItem_New> NewItemFunc { get; init; }
/// <summary> /// <summary>
/// <para>Default icon of items of this type</para> /// <para>Default icon of items of this type</para>
/// </summary> /// </summary>
public Texture2D? Texture { get; init; } public Texture2D? Icon { get; init; }
/// <summary> /// <summary>
/// <para>Max number in item stack of this type</para> /// <para>Max number in item stack of this type</para>
/// </summary> /// </summary>
public int MaxStackQuantity { get; init; } public int MaxStackQuantity { get; init; }
/// <summary>
/// <para>Determines how items of this type will be stacked</para>
/// </summary>
public StackType StackType { get; init; }
} }

View File

@ -28,7 +28,7 @@ public static class ItemTypeManager
/// <para>Returns null when the id is not registered.</para> /// <para>Returns null when the id is not registered.</para>
/// </summary> /// </summary>
public static IItem_New? NewItem(string id) => public static IItem_New? NewItem(string id) =>
Registry.TryGetValue(id, out var itemType) ? itemType.Getter() : null; Registry.TryGetValue(id, out var itemType) ? itemType.NewItemFunc() : null;
/// <summary> /// <summary>
/// Get the translated default name of the item type for the given id /// Get the translated default name of the item type for the given id
@ -54,6 +54,9 @@ public static class ItemTypeManager
/// </returns> /// </returns>
public static Texture2D DefaultIconOf(string id) => public static Texture2D DefaultIconOf(string id) =>
Registry.TryGetValue(id, out var itemType) Registry.TryGetValue(id, out var itemType)
? itemType.Texture ?? DefaultTexture ? itemType.Icon ?? DefaultTexture
: DefaultTexture; : DefaultTexture;
public static int MaxStackQuantityOf(string id) => Registry.TryGetValue(id, out var itemType) ? itemType.MaxStackQuantity : 0;
public static StackType? StackTypeOf(string id) => Registry.TryGetValue(id, out var itemType) ? itemType.StackType : null;
} }

View File

@ -0,0 +1,21 @@
using ColdMint.scripts.inventory;
using Godot;
namespace ColdMint.scripts.item;
/// <summary>
/// <para>Item stack in inventory</para>
/// </summary>
//maybe we'd move this into inventory namespace
public readonly struct SingleItemStack(IItem_New item) : IItemStack
{
public IItem_New Item { get; init; } = item;
public string Id => Item.Id;
public int MaxQuantity => 1;
public int Quantity => 1;
public Texture2D Icon => Item.Icon;
public string Name => Item.Name;
public string? Description => Item.Description;
}

View File

@ -0,0 +1,8 @@
namespace ColdMint.scripts.item;
public enum StackType
{
Common,
Unique,
Unstackable,
}

View File

@ -1,9 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using ColdMint.scripts.debug; using ColdMint.scripts.debug;
using ColdMint.scripts.projectile; using ColdMint.scripts.projectile;
using Godot; using Godot;
namespace ColdMint.scripts.weapon; namespace ColdMint.scripts.item.weapon;
/// <summary> /// <summary>
/// <para>Projectile weapons</para> /// <para>Projectile weapons</para>

View File

@ -1,11 +1,13 @@
using System; using System;
using ColdMint.scripts.camp; using ColdMint.scripts.camp;
using ColdMint.scripts.character; using ColdMint.scripts.character;
using ColdMint.scripts.damage; using ColdMint.scripts.damage;
using ColdMint.scripts.inventory; using ColdMint.scripts.inventory;
using Godot; using Godot;
namespace ColdMint.scripts.weapon; namespace ColdMint.scripts.item.weapon;
/// <summary> /// <summary>
/// <para>WeaponTemplate</para> /// <para>WeaponTemplate</para>