Files
Godot-test/scripts/core/GameScene.cs

419 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Godot;
using System;
using System.Collections.Generic;
public partial class GameScene : Control
{
// 资源类型枚举
public enum ResourceType
{
IronOre,
CopperOre,
IronIngot,
CopperIngot
}
// 资源数据结构
public class ResourceData
{
public string Name { get; set; }
public int Amount { get; set; }
public ResourceType Type { get; set; }
public bool IsProcessed { get; set; }
}
// 电力系统数据
private float powerGeneration = 0;
private float powerConsumption = 0;
private float powerStorage = 0;
private float powerDischarge = 0;
// 库存数据保留用于兼容性但主要由InventoryManager管理
private Dictionary<ResourceType, ResourceData> inventory = new Dictionary<ResourceType, ResourceData>();
// UI引用
private Label powerGenerationLabel;
private Label powerConsumptionLabel;
private Label powerStorageLabel;
private Label powerDischargeLabel;
private TabContainer categoryTabs;
private CraftingQueueManager craftingQueue;
public override void _Ready()
{
GD.Print("GameScene _Ready 开始");
// 先初始化管理器系统确保在UI创建之前完成
InitializeManagers();
// 等待一帧确保管理器完全初始化
CallDeferred(nameof(InitializeUI));
}
private void InitializeManagers()
{
// 创建ProductionLineManager
var productionLineManager = new ProductionLineManager();
productionLineManager.Name = "ProductionLineManager";
AddChild(productionLineManager);
// 创建ProductionProcessor
var productionProcessor = new ProductionProcessor();
productionProcessor.Name = "ProductionProcessor";
AddChild(productionProcessor);
GD.Print("自动产线管理器初始化完成");
}
private void InitializeUI()
{
GD.Print("开始初始化UI");
// 测试ProductionLineManager是否正常
var productionLineManager = ProductionLineManager.Instance;
if (productionLineManager != null)
{
var allLines = productionLineManager.GetAllProductionLines();
GD.Print($"ProductionLineManager已初始化共有 {allLines.Count} 个生产线");
var categories = productionLineManager.GetAllCategories();
GD.Print($"生产线分类数: {categories.Count}");
foreach (var category in categories)
{
GD.Print($"- 分类: {category}");
}
}
else
{
GD.PrintErr("ProductionLineManager 仍然为null");
}
// 获取UI引用
powerGenerationLabel = GetNode<Label>("HSplitContainer/LeftPanel/VBoxContainer/PowerInfo/MarginContainer/VBoxContainer/PowerRow1/PowerGeneration");
powerConsumptionLabel = GetNode<Label>("HSplitContainer/LeftPanel/VBoxContainer/PowerInfo/MarginContainer/VBoxContainer/PowerRow1/PowerConsumption");
powerStorageLabel = GetNode<Label>("HSplitContainer/LeftPanel/VBoxContainer/PowerInfo/MarginContainer/VBoxContainer/PowerRow2/PowerStorage");
powerDischargeLabel = GetNode<Label>("HSplitContainer/LeftPanel/VBoxContainer/PowerInfo/MarginContainer/VBoxContainer/PowerRow2/PowerDischarge");
categoryTabs = GetNode<TabContainer>("HSplitContainer/RightPanel/VBoxContainer/CategoryTabs");
craftingQueue = GetNode<CraftingQueueManager>("HSplitContainer/LeftPanel/VBoxContainer/CraftingQueue");
// 初始化库存(保留用于兼容性)
InitializeInventory();
// 更新UI
UpdatePowerUI();
GD.Print("GameScene 初始化完成");
}
private void InitializeInventory()
{
inventory[ResourceType.IronOre] = new ResourceData { Name = "铁矿", Amount = 0, Type = ResourceType.IronOre, IsProcessed = false };
inventory[ResourceType.CopperOre] = new ResourceData { Name = "铜矿", Amount = 0, Type = ResourceType.CopperOre, IsProcessed = false };
inventory[ResourceType.IronIngot] = new ResourceData { Name = "铁块", Amount = 0, Type = ResourceType.IronIngot, IsProcessed = true };
inventory[ResourceType.CopperIngot] = new ResourceData { Name = "铜块", Amount = 0, Type = ResourceType.CopperIngot, IsProcessed = true };
}
private void UpdatePowerUI()
{
powerGenerationLabel.Text = $"发电: {powerGeneration:F1} KW";
powerConsumptionLabel.Text = $"耗电: {powerConsumption:F1} KW";
powerStorageLabel.Text = $"蓄电: {powerStorage:F1} KWh";
powerDischargeLabel.Text = $"放电: {powerDischarge:F1} KW";
}
// 添加资源到库存(保留用于兼容性)
public void AddResource(ResourceType type, int amount)
{
if (inventory.ContainsKey(type))
{
inventory[type].Amount += amount;
}
}
// 更新电力系统
public void UpdatePowerSystem(float generation, float consumption, float storage, float discharge)
{
powerGeneration = generation;
powerConsumption = consumption;
powerStorage = storage;
powerDischarge = discharge;
UpdatePowerUI();
}
public override void _Process(double delta)
{
// 更新电力信息显示
UpdatePowerInfo();
}
private void UpdatePowerInfo()
{
// 这里可以从电力管理器获取实际数据
// 目前使用占位符数据
if (powerGenerationLabel != null)
powerGenerationLabel.Text = "发电: 0 KW";
if (powerConsumptionLabel != null)
powerConsumptionLabel.Text = "耗电: 0 KW";
if (powerStorageLabel != null)
powerStorageLabel.Text = "蓄电: 0 KWh";
if (powerDischargeLabel != null)
powerDischargeLabel.Text = "放电: 0 KW";
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
{
// 按C键测试合成铁块
if (keyEvent.Keycode == Key.C)
{
if (craftingQueue != null)
{
craftingQueue.StartIronIngotCrafting();
GD.Print("按下C键尝试开始铁块合成");
}
}
// 按P键测试自动产线
if (keyEvent.Keycode == Key.P)
{
TestProductionLine();
}
// 按L键列出所有产线
if (keyEvent.Keycode == Key.L)
{
ListAllProductionLines();
}
// 按B键测试生产线UI
if (keyEvent.Keycode == Key.B)
{
TestProductionLineUI();
}
// 按R键刷新标签页
if (keyEvent.Keycode == Key.R)
{
RefreshProductionLineTabs();
}
// 按I键查看库存状态
if (keyEvent.Keycode == Key.I)
{
ShowInventoryStatus();
}
}
}
private void TestProductionLine()
{
var productionLineManager = ProductionLineManager.Instance;
if (productionLineManager == null)
{
GD.PrintErr("ProductionLineManager not found");
return;
}
// 给库存添加一些建筑和材料用于测试
var inventoryManager = InventoryManager.Instance;
if (inventoryManager != null)
{
inventoryManager.AddItem("furnace", 1); // 添加1个熔炉
inventoryManager.AddItem("iron_ore", 10); // 添加10个铁矿用于冶炼测试
GD.Print("已添加测试物品到库存");
}
// 测试添加铁矿采集产线
productionLineManager.AddBuilding("iron_ore_extraction", 1);
// 测试添加自动冶炼产线
productionLineManager.AddBuilding("iron_smelting_auto", 1);
GD.Print("已启动测试产线");
}
private void ListAllProductionLines()
{
var productionLineManager = ProductionLineManager.Instance;
if (productionLineManager == null)
{
GD.PrintErr("ProductionLineManager not found");
return;
}
var allLines = productionLineManager.GetAllProductionLines();
GD.Print($"总共有 {allLines.Count} 个产线:");
foreach (var line in allLines)
{
string requirements = "";
if (line.BuildingRequirements != null && line.BuildingRequirements.Count > 0)
{
var req = line.BuildingRequirements[0];
var item = GameData.Instance?.GetItem(req.ItemId);
requirements = $"需要: {req.Quantity}x{item?.Name ?? req.ItemId}";
if (line.BuildingRequirements.Count > 1)
{
requirements += "...";
}
}
else
{
requirements = "无需求";
}
GD.Print($"- {line.Name} ({line.Category}): {line.ProductionTime}s, {line.PowerConsumption}W, {requirements}");
}
var activeLines = productionLineManager.GetAllActiveProductionLines();
GD.Print($"活跃产线: {activeLines.Count} 个");
foreach (var kvp in activeLines)
{
var activeLine = kvp.Value;
if (activeLine.IsActive)
{
var line = productionLineManager.GetProductionLine(activeLine.ProductionLineId);
GD.Print($" 活跃: {line?.Name} - {activeLine.BuildingCount} 个建筑, {activeLine.ProductionRate:F2}/s");
}
}
}
private void TestProductionLineUI()
{
// 给库存添加一些建筑用于测试
var inventoryManager = InventoryManager.Instance;
if (inventoryManager != null)
{
// 建筑设施
inventoryManager.AddItem("miner", 10); // 添加10个钻机
inventoryManager.AddItem("furnace", 8); // 添加8个熔炉
inventoryManager.AddItem("miner", 6); // 添加6个采矿机
inventoryManager.AddItem("assembler", 3); // 添加3个组装机
inventoryManager.AddItem("chemical_plant", 2); // 添加2个化工厂
// 原材料
inventoryManager.AddItem("iron_ore", 50); // 添加50个铁矿
inventoryManager.AddItem("copper_ore", 40); // 添加40个铜矿
inventoryManager.AddItem("coal_ore", 60); // 添加60个煤矿修正ID
inventoryManager.AddItem("stone_ore", 80); // 添加80个石矿修正ID
inventoryManager.AddItem("water", 100); // 添加100个水
inventoryManager.AddItem("crude_oil", 30); // 添加30个原油
// 加工材料
inventoryManager.AddItem("iron_ingot", 25); // 添加25个铁块
inventoryManager.AddItem("copper_ingot", 20); // 添加20个铜块
GD.Print("已添加测试建筑设施和材料到库存:");
GD.Print("建筑设施: 钻机x10, 熔炉x8, 冶炼厂x5, 采矿机x6, 组装机x3, 化工厂x2");
GD.Print("原材料: 铁矿x50, 铜矿x40, 煤矿x60, 石矿x80, 水x100, 原油x30");
GD.Print("加工材料: 铁块x25, 铜块x20");
GD.Print("现在可以在生产线和合成标签中测试各种功能了!");
}
}
private void RefreshProductionLineTabs()
{
GD.Print("刷新生产线标签页");
if (categoryTabs != null)
{
var dynamicTabManager = categoryTabs as DynamicTabManager;
if (dynamicTabManager != null)
{
dynamicTabManager.RefreshTabs();
GD.Print("已刷新标签页");
}
else
{
GD.PrintErr("categoryTabs 不是 DynamicTabManager 类型");
}
}
else
{
GD.PrintErr("categoryTabs 为null");
}
}
private void ShowInventoryStatus()
{
GD.Print("=== 当前库存状态 ===");
var inventoryManager = InventoryManager.Instance;
if (inventoryManager == null)
{
GD.PrintErr("InventoryManager 实例为null");
return;
}
// 获取所有物品类别并显示库存
var gameData = GameData.Instance;
if (gameData == null)
{
GD.PrintErr("GameData 实例为null");
return;
}
var allItems = gameData.GetAllItems();
bool hasItems = false;
GD.Print("建筑设施:");
foreach (var kvp in allItems)
{
var itemId = kvp.Key;
var itemData = kvp.Value;
if (itemData.Category == GameData.ItemCategory.Building || itemData.Category == GameData.ItemCategory.ProductionDevice)
{
int quantity = inventoryManager.GetItemQuantity(itemId);
if (quantity > 0)
{
GD.Print($" {itemData.Name}: {quantity} 个");
hasItems = true;
}
}
}
GD.Print("原材料:");
foreach (var kvp in allItems)
{
var itemId = kvp.Key;
var itemData = kvp.Value;
if (itemData.Category == GameData.ItemCategory.RawMaterial)
{
int quantity = inventoryManager.GetItemQuantity(itemId);
if (quantity > 0)
{
GD.Print($" {itemData.Name}: {quantity} 个");
hasItems = true;
}
}
}
GD.Print("加工材料:");
foreach (var kvp in allItems)
{
var itemId = kvp.Key;
var itemData = kvp.Value;
if (itemData.Category == GameData.ItemCategory.ProcessedMaterial)
{
int quantity = inventoryManager.GetItemQuantity(itemId);
if (quantity > 0)
{
GD.Print($" {itemData.Name}: {quantity} 个");
hasItems = true;
}
}
}
if (!hasItems)
{
GD.Print("库存为空按B键添加测试物品");
}
GD.Print("=================");
}
}