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

136 lines
4.8 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引用
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键尝试开始铁块合成");
}
}
}
}
}