73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using Godot;
|
|
using System;
|
|
namespace Cosmobox
|
|
{
|
|
public partial class DevUi : Control
|
|
{
|
|
[Export] public Player player;
|
|
[Export] public Container itemDevList; // 改为Container类型更准确
|
|
|
|
public override void _Ready()
|
|
{
|
|
InitializeItemButtons();
|
|
}
|
|
|
|
private void InitializeItemButtons()
|
|
{
|
|
// // 清空现有按钮(如果有)
|
|
// foreach (Node child in itemDevList.GetChildren())
|
|
// {
|
|
// child.QueueFree();
|
|
// }
|
|
|
|
// 获取所有物品
|
|
var items = ItemResourceManager.GetAllItems();
|
|
|
|
// 为每个物品创建按钮
|
|
foreach (var itemPair in items)
|
|
{
|
|
var button = new Button();
|
|
button.Text = $"{itemPair.Key}"; // 显示物品名称
|
|
|
|
// 绑定点击事件,使用闭包捕获当前物品名称
|
|
string currentItemName = itemPair.Key;
|
|
button.Pressed += () => OnItemButtonPressed(currentItemName);
|
|
|
|
itemDevList.AddChild(button);
|
|
}
|
|
}
|
|
|
|
private void OnItemButtonPressed(string itemName)
|
|
{
|
|
// 添加物品到背包
|
|
bool success = AddItem(itemName);
|
|
|
|
if (success)
|
|
{
|
|
GD.Print($"成功添加物品: {itemName}");
|
|
}
|
|
else
|
|
{
|
|
GD.PrintErr($"添加物品失败: {itemName}");
|
|
}
|
|
}
|
|
|
|
public bool AddItem(string itemName, int amount = 1)
|
|
{
|
|
var item = ItemResourceManager.GetItem(itemName);
|
|
if (item == null)
|
|
{
|
|
GD.PrintErr($"物品不存在: {itemName}");
|
|
return false;
|
|
}
|
|
|
|
return player.bagItem.AddItem(item, amount);
|
|
}
|
|
public bool ClearItems()
|
|
{
|
|
player.bagItem.Items.Clear();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
} |