Add hover hints to items.

为物品添加悬浮提示。
This commit is contained in:
Cold-Mint 2024-04-29 23:25:03 +08:00
parent 159d3a7017
commit e204aae56a
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
12 changed files with 166 additions and 112 deletions

View File

@ -11,4 +11,4 @@ use_item,使用,Use
jump_down,跳下平台,Jump off platform
de,的,'s
default_player_name,白纸,blankPaper
unknown,未知,Unknown
item_prompt_debug,ID{0}\n名称{1}\n数量{2}\n最大叠加数量{3}\n数据类型{4},ID: {0}\n Name: {1}\n Quantity: {2}\n Maximum stacking quantity: {3}\n Data type: {4}
1 id zh en
11 jump_down 跳下平台 Jump off platform
12 de 's
13 default_player_name 白纸 blankPaper
14 unknown item_prompt_debug 未知 ID:{0}\n名称:{1}\n数量:{2}\n最大叠加数量:{3}\n数据类型:{4} Unknown ID: {0}\n Name: {1}\n Quantity: {2}\n Maximum stacking quantity: {3}\n Data type: {4}

Binary file not shown.

Binary file not shown.

View File

@ -24,8 +24,9 @@ grow_horizontal = 2
grow_vertical = 2
scale = Vector2(0.2, 0.2)
[node name="IconTextureRect" type="TextureRect" parent="BackgroundTexture/CenterContainer"]
[node name="IconTextureRect" type="TextureButton" parent="BackgroundTexture/CenterContainer"]
layout_mode = 2
tooltip_text = "老婆"
[node name="Control" type="Control" parent="."]
layout_mode = 2

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=5 format=3 uid="uid://dnnn2xyayiehk"]
[gd_scene load_steps=6 format=3 uid="uid://dnnn2xyayiehk"]
[ext_resource type="Texture2D" uid="uid://e6670ykyq145" path="res://sprites/weapon/staffOfTheUndead.png" id="1_ms3us"]
[ext_resource type="Script" path="res://scripts/weapon/ProjectileWeapon.cs" id="1_w8hhv"]
[ext_resource type="Texture2D" uid="uid://b2blj0yf4ohx3" path="res://icon.svg" id="2_l5lni"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_obcq2"]
size = Vector2(49, 5)
@ -16,6 +17,8 @@ script = ExtResource("1_w8hhv")
metadata/Projectiles = PackedStringArray("res://prefab/projectile/curseOfTheUndead.tscn")
metadata/Name = "staff_of_the_undead"
metadata/FiringIntervalArray = PackedInt64Array(5000, 500, 250)
metadata/Icon = ExtResource("2_l5lni")
metadata/ID = "StaffOfTheUndead"
[node name="Area2D" type="Area2D" parent="."]
collision_layer = 8

View File

@ -196,7 +196,23 @@ public partial class Player : CharacterTemplate
//捡起物品
if (Input.IsActionJustPressed("pick_up"))
{
PickUpAction();
var success = PickItem(PickAbleItem);
if (success)
{
if (PickAbleItem is WeaponTemplate weaponTemplate)
{
GameSceneNodeHolder.HotBar.AddItem(weaponTemplate);
}
PickAbleItem = null;
TotalNumberOfPickups--;
if (FloatLabel != null)
{
FloatLabel.QueueFree();
FloatLabel = null;
}
UpdateOperationTip();
}
}
if (Input.IsActionJustPressed("ui_down"))
@ -273,22 +289,7 @@ public partial class Player : CharacterTemplate
}
}
private async Task PickUpAction()
{
var success = PickItem(PickAbleItem);
if (success)
{
PickAbleItem = null;
TotalNumberOfPickups--;
if (FloatLabel != null)
{
FloatLabel.QueueFree();
FloatLabel = null;
}
UpdateOperationTip();
}
}
private Vector2 GetThrowVelocity()
{

View File

@ -0,0 +1,20 @@
using System;
using Godot;
namespace ColdMint.scripts.inventory;
/// <summary>
/// <para>Common goods</para>
/// <para>普通的物品</para>
/// </summary>
public class CommonItem : IItem
{
public string Id { get; set; }
public int Quantity { get; set; }
public int MaxStackQuantity { get; set; }
public Texture2D Icon { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Action<IItem> OnUse { get; set; }
public Func<IItem, Node> OnInstantiation { get; set; }
}

View File

@ -9,7 +9,7 @@ public interface IItem
/// <para>Item and ID</para>
/// <para>物品还有ID</para>
/// </summary>
string Id { get; }
string Id { get; set; }
/// <summary>
/// <para>Represents the quantity of this item</para>
@ -21,21 +21,20 @@ public interface IItem
/// <para>How many can this item stack up to</para>
/// <para>这个物品最多叠加到多少个</para>
/// </summary>
int MaxStackQuantity { get; }
int MaxStackQuantity { get; set; }
/// <summary>
/// <para>Items can be set with Icon</para>
/// <para>物品可以设置图标</para>
/// </summary>
Texture2D Icon { get; }
Texture2D Icon { get; set; }
/// <summary>
/// <para>Item has a name</para>
/// <para>物品有名称</para>
/// </summary>
string Name { get; }
string Name { get; set; }
string Namespace { get; }
/// <summary>
/// <para>When using items</para>

View File

@ -2,6 +2,10 @@
namespace ColdMint.scripts.inventory;
/// <summary>
/// <para>Item manager</para>
/// <para>物品管理器</para>
/// </summary>
public static class ItemManager
{
private static Dictionary<string, IItem> _dictionary = new Dictionary<string, IItem>();
@ -13,17 +17,10 @@ public static class ItemManager
/// <param name="item"></param>
public static void AddItem(IItem item)
{
var key = GetKey(item);
if (_dictionary.ContainsKey(key))
if (_dictionary.ContainsKey(item.Id))
{
return;
}
_dictionary.Add(key, item);
}
private static string GetKey(IItem item)
{
return item.Namespace + item.Id;
_dictionary.Add(item.Id, item);
}
}

View File

@ -10,8 +10,9 @@ public partial class ItemSlotNode : MarginContainer
{
private IItem? _item;
private TextureRect _backgroundTextureRect;
private TextureRect _iconTextureRect;
private TextureButton _iconTextureRect;
private Label _quantityLabel;
private Control _control;
/// <summary>
/// <para>Sets items for the item slot</para>
@ -25,10 +26,11 @@ public partial class ItemSlotNode : MarginContainer
{
if (item.Icon != null)
{
_iconTextureRect.Texture = item.Icon;
_iconTextureRect.TextureNormal = item.Icon;
}
_item = item;
UpdateTooltipText(item);
UpdateQuantityLabel(item.Quantity);
return true;
}
@ -50,11 +52,24 @@ public partial class ItemSlotNode : MarginContainer
}
_item.Quantity = newQuantity;
UpdateTooltipText(item);
UpdateQuantityLabel(newQuantity);
return true;
}
}
/// <summary>
/// <para>Update item tips</para>
/// <para>更新物品的提示内容</para>
/// </summary>
/// <param name="item"></param>
private void UpdateTooltipText(IItem item)
{
_control.TooltipText = string.Format(TranslationServer.Translate("item_prompt_debug"), item.Id,
TranslationServer.Translate(item.Name),
item.Quantity, item.MaxStackQuantity, item.GetType().Name);
}
/// <summary>
/// <para>Update quantity label</para>
/// <para>更新数量标签</para>
@ -83,8 +98,9 @@ public partial class ItemSlotNode : MarginContainer
{
_backgroundTextureRect =
GetNode<TextureRect>("BackgroundTexture");
_iconTextureRect = GetNode<TextureRect>("BackgroundTexture/CenterContainer/IconTextureRect");
_iconTextureRect = GetNode<TextureButton>("BackgroundTexture/CenterContainer/IconTextureRect");
_quantityLabel = GetNode<Label>("Control/QuantityLabel");
_control = GetNode<Control>("Control");
_quantityLabel.Visible = false;
}
}

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Text;
using ColdMint.scripts.camp;
using ColdMint.scripts.debug;
using ColdMint.scripts.inventory;
using Godot;
namespace ColdMint.scripts.loader.uiLoader;
@ -31,7 +32,12 @@ public partial class MainMenuLoader : UiLoaderTemplate
{
Directory.CreateDirectory(dataPath);
}
//Registered article
//注册物品
// var staffOfTheUndead = new CommonItem();
// staffOfTheUndead.Name = "item_staff_of_the_undead";
// staffOfTheUndead.Id = "staff_of_the_undead";
// staffOfTheUndead.Description = "";
//Registered camp
//注册阵营
var defaultCamp = new Camp(Config.CampId.Default);

View File

@ -13,11 +13,17 @@ namespace ColdMint.scripts.weapon;
/// <para>WeaponTemplate</para>
/// <para>武器模板</para>
/// </summary>
public partial class WeaponTemplate : RigidBody2D
public partial class WeaponTemplate : RigidBody2D, IItem
{
public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
public string Name => GetMeta("Name", "").AsString();
public string Id { get; set; }
public int Quantity { get; set; }
public int MaxStackQuantity { get; set; }
public Texture2D Icon { get; set; }
public string Name { get; set; }
public Action<IItem> OnUse { get; set; }
public Func<IItem, Node> OnInstantiation { get; set; }
/// <summary>
/// <para>Owner</para>
@ -61,6 +67,11 @@ public partial class WeaponTemplate : RigidBody2D
_rayCast2D = GetNode<RayCast2D>("RayCast2D");
_area2D = GetNode<Area2D>("Area2D");
_area2D.BodyEntered += OnBodyEnter;
Id = GetMeta("ID", "1").AsString();
Quantity = GetMeta("Quantity", "1").AsInt32();
MaxStackQuantity = GetMeta("MaxStackQuantity", Config.MaxStackQuantity).AsInt32();
Icon = GetMeta("Icon", "").As<Texture2D>();
Name = GetMeta("Name", "").AsString();
_firingInterval = TimeSpan.FromMilliseconds(GetMeta("FiringInterval", "100").AsInt64());
_minContactInjury = GetMeta("MinContactInjury", "1").AsInt32();
_maxContactInjury = GetMeta("MaxContactInjury", "2").AsInt32();