初次提交
This commit is contained in:
45
Script/UI/BagItemUI.cs
Normal file
45
Script/UI/BagItemUI.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Godot;
|
||||
using System;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public partial class BagItemUI : Button
|
||||
{
|
||||
[Export] public TextureRect icon;
|
||||
[Export] public Label count, itemName;
|
||||
[Export] public Control itemNamePanel;
|
||||
[Export] public Texture2D emptyIcon;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
itemNamePanel.Visible = false;
|
||||
}
|
||||
|
||||
public void SetItem(BagItem bagItem)
|
||||
{
|
||||
if (bagItem == null || bagItem.itemResource == null)
|
||||
{
|
||||
SetEmpty();
|
||||
return;
|
||||
}
|
||||
itemName.Text = bagItem.itemResource.ItemName;
|
||||
icon.Texture = bagItem.itemResource.Icon ?? emptyIcon;
|
||||
count.Text = bagItem.amount > 0 ? bagItem.amount.ToString() : "空";
|
||||
}
|
||||
|
||||
public void SetEmpty()
|
||||
{
|
||||
icon.Texture = emptyIcon;
|
||||
count.Text = "空";
|
||||
}
|
||||
void _on_mouse_entered()
|
||||
{
|
||||
itemNamePanel.Visible = true;
|
||||
// GD.Print("鼠标进入");
|
||||
}
|
||||
void _on_mouse_exited()
|
||||
{
|
||||
itemNamePanel.Visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
1
Script/UI/BagItemUI.cs.uid
Normal file
1
Script/UI/BagItemUI.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cc7chrmpuw4vu
|
101
Script/UI/BagUI.cs
Normal file
101
Script/UI/BagUI.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using Godot;
|
||||
using System;
|
||||
namespace Cosmobox
|
||||
{
|
||||
public partial class BagUI : Control
|
||||
{
|
||||
[Export] public Player player;
|
||||
[Export] public Label text;
|
||||
[Export] public Panel background;
|
||||
[Export] public Control itemContainer;
|
||||
[Export] public float animationTime = 0.5f;
|
||||
|
||||
[Export] public PackedScene itemUI;
|
||||
private float animationTimeRun = 0;
|
||||
private bool isOpen = false;
|
||||
|
||||
private Vector2 labelOriginalPosition;
|
||||
private Vector2 backgroundOriginalPosition;
|
||||
|
||||
public bool IsOpen
|
||||
{
|
||||
get { return isOpen; }
|
||||
}
|
||||
public override void _Ready()
|
||||
{
|
||||
labelOriginalPosition = text.Position;
|
||||
backgroundOriginalPosition = background.Position;
|
||||
// animationTimeRun = 0.01f;
|
||||
text.Position = new Vector2(text.Position.X, labelOriginalPosition.Y - text.Size.Y);
|
||||
background.Position = new Vector2(background.Position.X, backgroundOriginalPosition.Y + (background.Size.Y + 50));
|
||||
isOpen = false;
|
||||
|
||||
}
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (animationTimeRun > 0)
|
||||
{
|
||||
animationTimeRun -= (float)delta;
|
||||
if (isOpen)
|
||||
{
|
||||
text.Position = new Vector2(text.Position.X, labelOriginalPosition.Y - (animationTimeRun / animationTime) * text.Size.Y);
|
||||
background.Position = new Vector2(background.Position.X, backgroundOriginalPosition.Y + (animationTimeRun / animationTime) * (background.Size.Y + 50));
|
||||
if (animationTimeRun <= 0)
|
||||
{
|
||||
text.Position = labelOriginalPosition;
|
||||
background.Position = backgroundOriginalPosition;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
text.Position = new Vector2(text.Position.X, labelOriginalPosition.Y - (1 - animationTimeRun / animationTime) * text.Size.Y);
|
||||
background.Position = new Vector2(background.Position.X, backgroundOriginalPosition.Y + (1 - animationTimeRun / animationTime) * (background.Size.Y + 50));
|
||||
if (animationTimeRun <= 0)
|
||||
{
|
||||
Visible = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public void OpenBag()
|
||||
{
|
||||
Visible = true;
|
||||
isOpen = true;
|
||||
UpdateUI();
|
||||
animationTimeRun = animationTime - animationTimeRun;
|
||||
isOpen = true;
|
||||
}
|
||||
public void CloseBag()
|
||||
{
|
||||
isOpen = false;
|
||||
animationTimeRun = animationTime - animationTimeRun;
|
||||
isOpen = false;
|
||||
}
|
||||
|
||||
public void UpdateUI()
|
||||
{
|
||||
// 清除容器中现有的所有子节点
|
||||
foreach (Node child in itemContainer.GetChildren())
|
||||
{
|
||||
child.QueueFree();
|
||||
}
|
||||
|
||||
var bagItems = player.bagItem.Items;
|
||||
|
||||
foreach (BagItem item in bagItems)
|
||||
{
|
||||
// 实例化新的物品UI
|
||||
BagItemUI itemInstance = (BagItemUI)itemUI.Instantiate();
|
||||
|
||||
// 设置物品数据
|
||||
itemInstance.SetItem(item);
|
||||
|
||||
// 将实例添加到容器中
|
||||
itemContainer.AddChild(itemInstance);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
1
Script/UI/BagUI.cs.uid
Normal file
1
Script/UI/BagUI.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://dry2x5otnbedt
|
73
Script/UI/DevUi.cs
Normal file
73
Script/UI/DevUi.cs
Normal file
@ -0,0 +1,73 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
1
Script/UI/DevUi.cs.uid
Normal file
1
Script/UI/DevUi.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bpaoua3tyv5ju
|
Reference in New Issue
Block a user