添加基础的生产线内容-不少bug
This commit is contained in:
@ -5,6 +5,7 @@ public partial class DynamicTabManager : TabContainer
|
||||
{
|
||||
private PackedScene itemPanelScene;
|
||||
private PackedScene craftingItemScene;
|
||||
private PackedScene productionLineItemScene;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@ -29,7 +30,22 @@ public partial class DynamicTabManager : TabContainer
|
||||
return;
|
||||
}
|
||||
|
||||
// 加载ProductionLineItem场景(用于生产线)
|
||||
productionLineItemScene = GD.Load<PackedScene>("res://scenes/ProductionLineItem.tscn");
|
||||
if (productionLineItemScene == null)
|
||||
{
|
||||
GD.PrintErr("无法加载ProductionLineItem场景");
|
||||
return;
|
||||
}
|
||||
|
||||
GD.Print("开始初始化标签页");
|
||||
// 延迟初始化以确保所有管理器都已初始化
|
||||
CallDeferred(nameof(InitializeTabs));
|
||||
}
|
||||
|
||||
public void RefreshTabs()
|
||||
{
|
||||
GD.Print("刷新标签页");
|
||||
InitializeTabs();
|
||||
}
|
||||
|
||||
@ -113,29 +129,146 @@ public partial class DynamicTabManager : TabContainer
|
||||
vboxContainer.SizeFlagsVertical = Control.SizeFlags.ExpandFill;
|
||||
scrollContainer.AddChild(vboxContainer);
|
||||
|
||||
// 获取所有分类
|
||||
var categoryManager = ResourceCategoryManager.Instance;
|
||||
if (categoryManager == null)
|
||||
// 获取生产线管理器
|
||||
var productionLineManager = ProductionLineManager.Instance;
|
||||
if (productionLineManager == null)
|
||||
{
|
||||
GD.PrintErr("ResourceCategoryManager 实例为null");
|
||||
GD.PrintErr("ProductionLineManager 实例为null,创建空的生产线标签");
|
||||
|
||||
// 创建一个提示标签
|
||||
var noDataLabel = new Label();
|
||||
noDataLabel.Text = "生产线管理器未初始化";
|
||||
noDataLabel.HorizontalAlignment = HorizontalAlignment.Center;
|
||||
noDataLabel.VerticalAlignment = VerticalAlignment.Center;
|
||||
vboxContainer.AddChild(noDataLabel);
|
||||
|
||||
// 添加到TabContainer
|
||||
AddChild(scrollContainer);
|
||||
SetTabTitle(GetTabCount() - 1, "生产线");
|
||||
return;
|
||||
}
|
||||
|
||||
var allCategories = categoryManager.GetAllCategories();
|
||||
// 获取所有生产线分类
|
||||
var categories = productionLineManager.GetAllCategories();
|
||||
GD.Print($"获取到 {categories.Count} 个生产线分类");
|
||||
|
||||
// 为生产线相关的分类创建块
|
||||
foreach (var category in allCategories)
|
||||
if (categories.Count == 0)
|
||||
{
|
||||
// 生产线标签包含:生产设备
|
||||
if (category.CategoryName == "生产设备")
|
||||
GD.Print("没有找到生产线分类,创建提示标签");
|
||||
|
||||
// 创建一个提示标签
|
||||
var noDataLabel = new Label();
|
||||
noDataLabel.Text = "暂无生产线配置";
|
||||
noDataLabel.HorizontalAlignment = HorizontalAlignment.Center;
|
||||
noDataLabel.VerticalAlignment = VerticalAlignment.Center;
|
||||
vboxContainer.AddChild(noDataLabel);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 为每个分类创建块
|
||||
foreach (var category in categories)
|
||||
{
|
||||
CreateCategoryBlock(vboxContainer, category, "生产线");
|
||||
GD.Print($"处理生产线分类: {category}");
|
||||
CreateProductionCategoryBlock(vboxContainer, category);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加到TabContainer
|
||||
AddChild(scrollContainer);
|
||||
SetTabTitle(GetTabCount() - 1, "生产线");
|
||||
|
||||
GD.Print("生产线标签创建完成");
|
||||
}
|
||||
|
||||
private void CreateProductionCategoryBlock(VBoxContainer parentContainer, string category)
|
||||
{
|
||||
GD.Print($"创建生产线分类块: {category}");
|
||||
|
||||
// 创建分类块的容器
|
||||
var categoryContainer = new VBoxContainer();
|
||||
categoryContainer.Name = $"{category}Block";
|
||||
|
||||
// 添加间距
|
||||
var topSpacer = new Control();
|
||||
topSpacer.CustomMinimumSize = new Vector2(0, 10);
|
||||
categoryContainer.AddChild(topSpacer);
|
||||
|
||||
// 创建标题行(分类名称 + 横线)
|
||||
var titleContainer = new HBoxContainer();
|
||||
|
||||
// 分类名称标签
|
||||
var titleLabel = new Label();
|
||||
titleLabel.Text = category;
|
||||
titleLabel.HorizontalAlignment = HorizontalAlignment.Left;
|
||||
titleContainer.AddChild(titleLabel);
|
||||
|
||||
// 添加小间距
|
||||
var labelSpacer = new Control();
|
||||
labelSpacer.CustomMinimumSize = new Vector2(10, 0);
|
||||
titleContainer.AddChild(labelSpacer);
|
||||
|
||||
// HSeparator 横线分隔符
|
||||
var separator = new HSeparator();
|
||||
separator.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
|
||||
separator.SizeFlagsVertical = Control.SizeFlags.ShrinkCenter;
|
||||
titleContainer.AddChild(separator);
|
||||
|
||||
categoryContainer.AddChild(titleContainer);
|
||||
|
||||
// 添加小间距
|
||||
var spacer = new Control();
|
||||
spacer.CustomMinimumSize = new Vector2(0, 5);
|
||||
categoryContainer.AddChild(spacer);
|
||||
|
||||
// 创建生产线网格 - 使用VBoxContainer实现垂直堆叠
|
||||
var productionContainer = new VBoxContainer();
|
||||
productionContainer.Name = $"{category}ProductionLines";
|
||||
productionContainer.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
|
||||
productionContainer.AddThemeConstantOverride("separation", 8); // 垂直间距
|
||||
|
||||
// 添加该分类的所有生产线
|
||||
AddProductionLinesToContainer(productionContainer, category);
|
||||
|
||||
categoryContainer.AddChild(productionContainer);
|
||||
|
||||
// 添加到父容器
|
||||
parentContainer.AddChild(categoryContainer);
|
||||
}
|
||||
|
||||
private void AddProductionLinesToContainer(VBoxContainer container, string category)
|
||||
{
|
||||
GD.Print($"为分类 '{category}' 添加生产线");
|
||||
|
||||
var productionLineManager = ProductionLineManager.Instance;
|
||||
if (productionLineManager == null)
|
||||
{
|
||||
GD.PrintErr("ProductionLineManager 实例为null");
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取该分类的所有生产线
|
||||
var productionLines = productionLineManager.GetProductionLinesByCategory(category);
|
||||
|
||||
GD.Print($"获取到 {productionLines.Count} 条生产线");
|
||||
|
||||
foreach (var productionLine in productionLines)
|
||||
{
|
||||
// 创建ProductionLineItem实例
|
||||
var productionLineItem = productionLineItemScene.Instantiate<ProductionLineItem>();
|
||||
if (productionLineItem == null)
|
||||
{
|
||||
GD.PrintErr($"无法创建ProductionLineItem实例: {productionLine.Name}");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 设置生产线数据
|
||||
productionLineItem.SetupProductionLine(productionLine);
|
||||
|
||||
// 添加到容器
|
||||
container.AddChild(productionLineItem);
|
||||
|
||||
GD.Print($"成功添加生产线: {productionLine.Name}");
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateCategoryBlock(VBoxContainer parentContainer, ResourceCategoryManager.ResourceCategory category, string tabType)
|
||||
|
||||
250
scripts/ui/ProductionLineItem.cs
Normal file
250
scripts/ui/ProductionLineItem.cs
Normal 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仍然为null,UI初始化可能失败");
|
||||
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} 产线的一个设备");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user