添加基础的生产线内容-不少bug

This commit is contained in:
2025-06-17 00:05:47 +08:00
parent 7a6cd423fc
commit 50dc434ed9
11 changed files with 1435 additions and 17 deletions

View File

@ -0,0 +1,250 @@
using Godot;
public partial class ProductionLineItem : Panel
{
private Label nameLabel;
private Label productionLabel;
private Label timeLabel;
private Label powerLabel;
private Label buildingLabel;
private Label deviceLabel;
private Button addButton;
private Button removeButton;
private ColorRect progressFill;
private TextureRect iconTexture;
private ProductionLineManager.ProductionLine productionLine;
private string productionLineId;
public override void _Ready()
{
// 获取UI组件引用
nameLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/MiddleContainer/TopInfoRow/NameLabel");
productionLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/MiddleContainer/TopInfoRow/ProductionLabel");
timeLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/MiddleContainer/InfoRow/TimeLabel");
powerLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/MiddleContainer/InfoRow/PowerLabel");
buildingLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/MiddleContainer/InfoRow/BuildingLabel");
deviceLabel = GetNode<Label>("MarginContainer/VBoxContainer/TopRow/RightContainer/BuildingCountRow/DeviceLabel");
addButton = GetNode<Button>("MarginContainer/VBoxContainer/TopRow/RightContainer/ButtonsRow/AddButton");
removeButton = GetNode<Button>("MarginContainer/VBoxContainer/TopRow/RightContainer/ButtonsRow/RemoveButton");
progressFill = GetNode<ColorRect>("MarginContainer/VBoxContainer/ProgressContainer/ProgressFill");
iconTexture = GetNode<TextureRect>("MarginContainer/VBoxContainer/TopRow/IconTexture");
// 连接按钮信号
addButton.Pressed += OnAddButtonPressed;
removeButton.Pressed += OnRemoveButtonPressed;
// 如果生产线数据已经设置,现在应用它
if (productionLine != null)
{
ApplyProductionLineData();
}
}
public void SetupProductionLine(ProductionLineManager.ProductionLine line)
{
productionLine = line;
productionLineId = line.Id;
// 如果UI组件还没有初始化延迟设置
if (nameLabel == null)
{
GD.Print("UI组件还未初始化延迟设置生产线数据");
CallDeferred(nameof(ApplyProductionLineData));
return;
}
ApplyProductionLineData();
}
private void ApplyProductionLineData()
{
if (productionLine == null)
{
GD.PrintErr("productionLine为null无法应用数据");
return;
}
// 确保UI组件已初始化
if (nameLabel == null)
{
GD.PrintErr("nameLabel仍然为nullUI初始化可能失败");
return;
}
// 设置基本信息
nameLabel.Text = productionLine.Name;
timeLabel.Text = $"{productionLine.ProductionTime:F1}s";
powerLabel.Text = $"{productionLine.PowerConsumption}W";
// 设置建筑需求信息
if (productionLine.BuildingRequirements != null && productionLine.BuildingRequirements.Count > 0)
{
var firstRequirement = productionLine.BuildingRequirements[0];
var buildingItem = GameData.Instance?.GetItem(firstRequirement.ItemId);
string buildingName = buildingItem?.Name ?? firstRequirement.ItemId;
if (productionLine.BuildingRequirements.Count == 1)
{
buildingLabel.Text = $"需要: {firstRequirement.Quantity}x{buildingName}";
}
else
{
buildingLabel.Text = $"需要: {firstRequirement.Quantity}x{buildingName}...";
}
}
else
{
buildingLabel.Text = "无建筑需求";
}
// 尝试加载图标
LoadProductionLineIcon();
// 初始更新显示
UpdateDisplay();
GD.Print($"成功设置生产线数据: {productionLine.Name}");
}
private void LoadProductionLineIcon()
{
if (productionLine == null) return;
// 尝试加载产线专用图标,如果没有则使用建筑图标
string iconPath = $"res://assets/textures/production_lines/{productionLineId}.png";
if (!ResourceLoader.Exists(iconPath))
{
// 回退到建筑图标(使用第一个建筑需求的图标)
if (productionLine.BuildingRequirements != null && productionLine.BuildingRequirements.Count > 0)
{
var firstRequirement = productionLine.BuildingRequirements[0];
var buildingItem = GameData.Instance?.GetItem(firstRequirement.ItemId);
if (buildingItem != null && !string.IsNullOrEmpty(buildingItem.IconPath))
{
iconPath = buildingItem.IconPath;
}
}
}
if (ResourceLoader.Exists(iconPath))
{
var texture = GD.Load<Texture2D>(iconPath);
if (texture != null)
{
iconTexture.Texture = texture;
}
}
}
public override void _Process(double delta)
{
// 只有当UI组件准备好且有生产线数据时才更新显示
if (productionLine != null && nameLabel != null)
{
UpdateDisplay();
}
}
private void UpdateDisplay()
{
if (productionLine == null) return;
// 确保UI组件已初始化
if (deviceLabel == null || productionLabel == null || progressFill == null)
{
return; // UI组件还没准备好跳过更新
}
var activeLine = ProductionLineManager.Instance?.GetActiveProductionLine(productionLineId);
var status = ProductionProcessor.Instance?.GetProductionLineStatus(productionLineId);
if (activeLine != null && status != null)
{
// 更新设备数量
deviceLabel.Text = $"设备: {activeLine.BuildingCount}";
// 更新产出率
if (activeLine.IsActive && activeLine.ProductionRate > 0)
{
productionLabel.Text = $"{activeLine.ProductionRate:F1}/s";
}
else
{
productionLabel.Text = "0/s";
}
// 更新进度条
progressFill.AnchorRight = status.Progress;
// 更新按钮状态
UpdateButtonStates();
}
else
{
// 没有激活的产线
deviceLabel.Text = "设备: 0";
productionLabel.Text = "0/s";
progressFill.AnchorRight = 0;
UpdateButtonStates();
}
}
private void UpdateButtonStates()
{
if (productionLine == null) return;
// 确保按钮组件已初始化
if (addButton == null || removeButton == null)
{
return; // 按钮还没准备好,跳过更新
}
// 检查是否有足够的建筑材料来添加设备
bool canAdd = CanAddBuilding();
addButton.Disabled = !canAdd;
// 检查是否有设备可以移除
var activeLine = ProductionLineManager.Instance?.GetActiveProductionLine(productionLineId);
removeButton.Disabled = activeLine == null || activeLine.BuildingCount <= 0;
}
private bool CanAddBuilding()
{
if (productionLine?.BuildingRequirements == null) return true;
var inventoryManager = InventoryManager.Instance;
if (inventoryManager == null) return false;
// 检查是否有足够的建筑材料
foreach (var requirement in productionLine.BuildingRequirements)
{
int available = inventoryManager.GetItemQuantity(requirement.ItemId);
if (available < requirement.Quantity)
{
return false;
}
}
return true;
}
private void OnAddButtonPressed()
{
if (productionLine == null) return;
// 直接尝试添加建筑到产线材料检查和扣除在ProductionLineManager中处理
ProductionLineManager.Instance?.AddBuilding(productionLineId, 1);
}
private void OnRemoveButtonPressed()
{
if (productionLine == null) return;
// 从产线移除建筑(建筑会自动返回库存)
ProductionLineManager.Instance?.RemoveBuilding(productionLineId, 1);
GD.Print($"移除了 {productionLine.Name} 产线的一个设备");
}
}