101 lines
3.4 KiB
C#
101 lines
3.4 KiB
C#
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
} |