临时提交
This commit is contained in:
190
scripts/production/PowerManager.cs
Normal file
190
scripts/production/PowerManager.cs
Normal file
@ -0,0 +1,190 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
public partial class PowerManager : Node
|
||||
{
|
||||
public static PowerManager Instance { get; private set; }
|
||||
|
||||
// 电力系统状态
|
||||
private float totalGeneration = 0.0f; // 总发电量 (W)
|
||||
private float totalConsumption = 0.0f; // 总耗电量 (W)
|
||||
private float currentPowerRatio = 1.0f; // 当前电力满足率 0-1
|
||||
|
||||
// 发电设备列表 - 记录每个发电设备的发电量
|
||||
private Dictionary<string, float> generators = new Dictionary<string, float>();
|
||||
|
||||
// 信号事件
|
||||
[Signal] public delegate void PowerRatioChangedEventHandler(float ratio);
|
||||
[Signal] public delegate void PowerStatsChangedEventHandler(float generation, float consumption);
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
GD.Print("PowerManager 初始化完成");
|
||||
}
|
||||
else
|
||||
{
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
// 添加发电设备
|
||||
public void AddGenerator(string generatorId, float powerOutput)
|
||||
{
|
||||
if (powerOutput <= 0) return;
|
||||
|
||||
// 如果发电设备已存在,移除旧的再添加新的
|
||||
if (generators.ContainsKey(generatorId))
|
||||
{
|
||||
float oldPower = generators[generatorId];
|
||||
generators[generatorId] = powerOutput;
|
||||
GD.Print($"更新发电设备 {generatorId}: {oldPower}W -> {powerOutput}W");
|
||||
}
|
||||
else
|
||||
{
|
||||
generators[generatorId] = powerOutput;
|
||||
GD.Print($"添加发电设备 {generatorId}: +{powerOutput}W");
|
||||
}
|
||||
|
||||
UpdatePowerBalance();
|
||||
}
|
||||
|
||||
// 移除发电设备
|
||||
public void RemoveGenerator(string generatorId)
|
||||
{
|
||||
if (generators.ContainsKey(generatorId))
|
||||
{
|
||||
float removedPower = generators[generatorId];
|
||||
generators.Remove(generatorId);
|
||||
UpdatePowerBalance();
|
||||
|
||||
GD.Print($"移除发电设备 {generatorId}: -{removedPower}W");
|
||||
}
|
||||
}
|
||||
|
||||
// 更新发电设备状态(发电机异常或没有原料时调用)
|
||||
public void UpdateGeneratorStatus(string generatorId, bool isActive)
|
||||
{
|
||||
// 这里可以实现发电设备状态管理
|
||||
// 暂时简化,直接通过AddGenerator/RemoveGenerator来管理
|
||||
GD.Print($"发电设备 {generatorId} 状态更新: {(isActive ? "激活" : "停止")}");
|
||||
}
|
||||
|
||||
// 更新电力平衡
|
||||
private void UpdatePowerBalance()
|
||||
{
|
||||
// 计算总发电量
|
||||
totalGeneration = 0;
|
||||
foreach (var generator in generators.Values)
|
||||
{
|
||||
totalGeneration += generator;
|
||||
}
|
||||
|
||||
// 获取总耗电量(从生产线管理器获取)
|
||||
UpdateTotalConsumption();
|
||||
|
||||
// 计算电力满足率
|
||||
float newPowerRatio;
|
||||
if (totalConsumption > 0)
|
||||
{
|
||||
newPowerRatio = Mathf.Min(1.0f, totalGeneration / totalConsumption);
|
||||
}
|
||||
else
|
||||
{
|
||||
newPowerRatio = 1.0f; // 没有耗电设备时电力充足
|
||||
}
|
||||
|
||||
// 只有当电力状态发生显著变化时才打印日志和发送信号
|
||||
if (Math.Abs(newPowerRatio - currentPowerRatio) > 0.01f ||
|
||||
Math.Abs(totalGeneration - GetPreviousGeneration()) > 1.0f ||
|
||||
Math.Abs(totalConsumption - GetPreviousConsumption()) > 1.0f)
|
||||
{
|
||||
currentPowerRatio = newPowerRatio;
|
||||
|
||||
// 发送信号通知其他系统
|
||||
EmitSignal(SignalName.PowerRatioChanged, currentPowerRatio);
|
||||
EmitSignal(SignalName.PowerStatsChanged, totalGeneration, totalConsumption);
|
||||
|
||||
GD.Print($"电力平衡更新: 发电 {totalGeneration}W, 耗电 {totalConsumption}W, 满足率 {currentPowerRatio * 100:F1}%");
|
||||
}
|
||||
else
|
||||
{
|
||||
currentPowerRatio = newPowerRatio;
|
||||
}
|
||||
}
|
||||
|
||||
private float previousGeneration = 0;
|
||||
private float previousConsumption = 0;
|
||||
|
||||
private float GetPreviousGeneration()
|
||||
{
|
||||
float prev = previousGeneration;
|
||||
previousGeneration = totalGeneration;
|
||||
return prev;
|
||||
}
|
||||
|
||||
private float GetPreviousConsumption()
|
||||
{
|
||||
float prev = previousConsumption;
|
||||
previousConsumption = totalConsumption;
|
||||
return prev;
|
||||
}
|
||||
|
||||
// 更新总耗电量
|
||||
private void UpdateTotalConsumption()
|
||||
{
|
||||
totalConsumption = 0;
|
||||
|
||||
var productionLineManager = ProductionLineManager.Instance;
|
||||
if (productionLineManager != null)
|
||||
{
|
||||
var activeLines = productionLineManager.GetAllActiveProductionLines();
|
||||
foreach (var activeLine in activeLines.Values)
|
||||
{
|
||||
if (activeLine.IsActive)
|
||||
{
|
||||
totalConsumption += activeLine.TotalPowerConsumption;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前电力满足率
|
||||
public float GetPowerRatio()
|
||||
{
|
||||
return currentPowerRatio;
|
||||
}
|
||||
|
||||
// 获取电力统计信息
|
||||
public (float generation, float consumption, float ratio) GetPowerStats()
|
||||
{
|
||||
return (totalGeneration, totalConsumption, currentPowerRatio);
|
||||
}
|
||||
|
||||
// 定期更新电力系统(通过_Process调用)
|
||||
private float updateTimer = 0.0f;
|
||||
private const float UPDATE_INTERVAL = 0.5f; // 每0.5秒更新一次
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
updateTimer += (float)delta;
|
||||
|
||||
// 每0.5秒更新一次电力平衡,而不是每帧都更新
|
||||
if (updateTimer >= UPDATE_INTERVAL)
|
||||
{
|
||||
updateTimer = 0.0f;
|
||||
UpdatePowerBalance();
|
||||
}
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (Instance == this)
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user