Files
Godot-test/scripts/production/ManualCollectionPanel.cs
2025-06-16 07:59:50 +08:00

192 lines
5.0 KiB
C#

using Godot;
public partial class ManualCollectionPanel : Control
{
// 采集相关参数
private const float COLLECTION_TIME = 1.0f; // 采集时间1秒
// 状态变量
private bool isCollecting = false;
private float collectionProgress = 0.0f;
private string itemId;
// UI引用
private ColorRect progressFill;
private Color originalProgressColor;
private Color collectingProgressColor = new Color(1.0f, 0.8f, 0.3f, 1.0f); // 采集时的橙色
public override void _Ready()
{
GD.Print("ManualCollectionPanel _Ready 开始");
// 获取进度条引用 - 现在需要从父节点获取
var parent = GetParent<Control>();
if (parent != null)
{
progressFill = parent.GetNode<ColorRect>("MarginContainer/VBoxContainer/ProgressContainer/ProgressFill");
if (progressFill != null)
{
originalProgressColor = progressFill.Color;
GD.Print("成功获取进度条引用");
}
else
{
GD.PrintErr("无法获取进度条引用");
}
}
else
{
GD.PrintErr("无法获取父节点");
}
// 连接鼠标事件
GuiInput += OnGuiInput;
GD.Print("已连接鼠标事件");
GD.Print("ManualCollectionPanel _Ready 完成");
}
// 设置物品ID
public void SetItemId(string id)
{
itemId = id;
GD.Print($"设置手动采集面板物品ID: {itemId}");
}
// 处理输入事件
private void OnGuiInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseEvent)
{
if (mouseEvent.ButtonIndex == MouseButton.Left)
{
//打印鼠标事件
GD.Print($"鼠标事件: {mouseEvent}");
if (mouseEvent.Pressed)
{
// 开始采集
StartCollection();
}
else
{
// 停止采集
StopCollection();
}
}
}
}
// 开始采集
private void StartCollection()
{
if (string.IsNullOrEmpty(itemId)) return;
isCollecting = true;
collectionProgress = 0.0f;
// 改变进度条颜色表示正在采集
if (progressFill != null)
{
progressFill.Color = collectingProgressColor;
}
GD.Print($"开始采集: {itemId}");
}
// 停止采集
private void StopCollection()
{
if (!isCollecting) return;
isCollecting = false;
collectionProgress = 0.0f;
// 恢复进度条
UpdateProgressBar();
// 恢复原始颜色
if (progressFill != null)
{
progressFill.Color = originalProgressColor;
}
GD.Print($"停止采集: {itemId}");
}
// 完成采集
private void CompleteCollection()
{
if (string.IsNullOrEmpty(itemId)) return;
// 添加物品到库存
if (InventoryManager.Instance != null)
{
InventoryManager.Instance.AddItem(itemId, 1);
GD.Print($"采集完成,获得: {itemId} x1");
}
// 重置采集状态
collectionProgress = 0.0f;
// 如果还在按住,继续下一轮采集
if (isCollecting)
{
GD.Print($"继续采集: {itemId}");
}
else
{
// 恢复进度条显示
UpdateProgressBar();
if (progressFill != null)
{
progressFill.Color = originalProgressColor;
}
}
}
// 更新进度条显示
private void UpdateProgressBar()
{
if (progressFill != null)
{
if (isCollecting)
{
// 采集中显示当前进度
progressFill.AnchorRight = collectionProgress;
}
else
{
// 非采集状态显示满进度(表示可采集)
progressFill.AnchorRight = 0f;
}
}
}
public override void _Process(double delta)
{
if (isCollecting)
{
// 更新采集进度
collectionProgress += (float)delta / COLLECTION_TIME;
// 限制进度在0-1之间
collectionProgress = Mathf.Clamp(collectionProgress, 0.0f, 1.0f);
// 更新进度条显示
UpdateProgressBar();
// 检查是否完成采集
if (collectionProgress >= 1.0f)
{
CompleteCollection();
}
}
}
// 清理
public override void _ExitTree()
{
GuiInput -= OnGuiInput;
}
}