临时提交

This commit is contained in:
2025-06-22 20:39:47 +08:00
parent b3d1135e23
commit d274ae0fec
24 changed files with 603 additions and 50 deletions

View 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;
}
}
}

View File

@ -26,6 +26,7 @@ public partial class ProductionLineManager : Node
public string Description { get; set; }
public float ProductionTime { get; set; }
public int PowerConsumption { get; set; }
public int PowerGeneration { get; set; } = 0; // 新增:发电量
public List<ProductionRecipeItem> BuildingRequirements { get; set; } = new List<ProductionRecipeItem>();
public ProductionRecipe Recipe { get; set; } = new ProductionRecipe();
}
@ -44,6 +45,7 @@ public partial class ProductionLineManager : Node
public bool IsActive { get; set; } = false; // 是否激活
public float ProductionRate { get; set; } = 0; // 每秒产出率
public int TotalPowerConsumption { get; set; } = 0; // 总功耗
public int TotalPowerGeneration { get; set; } = 0; // 总发电量
}
private ProductionLineData productionLineData;
@ -191,19 +193,59 @@ public partial class ProductionLineManager : Node
activeProductionLines[productionLineId] = new ActiveProductionLine
{
ProductionLineId = productionLineId,
RemainingTime = productionLine.ProductionTime // 从完整的生产时间开始
RemainingTime = productionLine.PowerGeneration > 0 && productionLine.ProductionTime <= 0
? 0 // 发电设备不需要生产时间
: productionLine.ProductionTime // 普通生产设备从完整的生产时间开始
};
GD.Print($"创建新产线 {productionLine.Name},初始剩余时间: {productionLine.ProductionTime}s");
if (productionLine.PowerGeneration > 0 && productionLine.ProductionTime <= 0)
{
GD.Print($"创建新发电设备 {productionLine.Name},无需生产周期");
}
else
{
GD.Print($"创建新产线 {productionLine.Name},初始剩余时间: {productionLine.ProductionTime}s");
}
}
var activeLine = activeProductionLines[productionLineId];
activeLine.BuildingCount += count;
activeLine.IsActive = true;
activeLine.ProductionRate = activeLine.BuildingCount / productionLine.ProductionTime; // 每秒产出率
// 重新计算产出率:发电设备特殊处理
if (productionLine.PowerGeneration > 0 && productionLine.ProductionTime <= 0)
{
// 发电设备不产出物品产出率设为0
activeLine.ProductionRate = 0;
}
else if (productionLine.ProductionTime > 0)
{
activeLine.ProductionRate = activeLine.BuildingCount / productionLine.ProductionTime;
}
else
{
activeLine.ProductionRate = 0; // 避免除零
}
activeLine.TotalPowerConsumption = activeLine.BuildingCount * productionLine.PowerConsumption;
activeLine.TotalPowerGeneration = activeLine.BuildingCount * productionLine.PowerGeneration;
// 如果是发电设备通知PowerManager
if (productionLine.PowerGeneration > 0)
{
var powerManager = PowerManager.Instance;
if (powerManager != null)
{
// 使用更简单的generatorId避免重复问题
string generatorId = $"{productionLineId}";
float totalGeneration = productionLine.PowerGeneration * activeLine.BuildingCount;
powerManager.AddGenerator(generatorId, totalGeneration);
GD.Print($"发电设备 {productionLine.Name} 已添加到电网: +{totalGeneration}W");
}
}
GD.Print($"成功添加 {count} 个设备到产线 {productionLine.Name}");
GD.Print($"当前产线状态: {activeLine.BuildingCount} 个建筑, 产出率: {activeLine.ProductionRate:F2}/s, 功耗: {activeLine.TotalPowerConsumption}W");
GD.Print($"当前产线状态: {activeLine.BuildingCount} 个建筑, 产出率: {activeLine.ProductionRate:F2}/s, 功耗: {activeLine.TotalPowerConsumption}W, 发电: {activeLine.TotalPowerGeneration}W");
}
public void RemoveBuilding(string productionLineId, int count = 1)
@ -220,6 +262,18 @@ public partial class ProductionLineManager : Node
count = Mathf.Min(count, activeLine.BuildingCount);
if (count <= 0) return;
// 如果是发电设备先通知PowerManager移除发电量
if (productionLine.PowerGeneration > 0)
{
var powerManager = PowerManager.Instance;
if (powerManager != null)
{
string generatorId = $"{productionLineId}";
float totalGeneration = productionLine.PowerGeneration * activeLine.BuildingCount;
powerManager.RemoveGenerator(generatorId);
}
}
activeLine.BuildingCount -= count;
if (activeLine.BuildingCount <= 0)
@ -227,11 +281,40 @@ public partial class ProductionLineManager : Node
activeLine.IsActive = false;
activeLine.ProductionRate = 0;
activeLine.TotalPowerConsumption = 0;
activeLine.TotalPowerGeneration = 0;
}
else
{
activeLine.ProductionRate = activeLine.BuildingCount / productionLine.ProductionTime;
// 重新计算产出率:发电设备特殊处理
if (productionLine.PowerGeneration > 0 && productionLine.ProductionTime <= 0)
{
// 发电设备不产出物品产出率设为0
activeLine.ProductionRate = 0;
}
else if (productionLine.ProductionTime > 0)
{
activeLine.ProductionRate = activeLine.BuildingCount / productionLine.ProductionTime;
}
else
{
activeLine.ProductionRate = 0; // 避免除零
}
activeLine.TotalPowerConsumption = activeLine.BuildingCount * productionLine.PowerConsumption;
activeLine.TotalPowerGeneration = activeLine.BuildingCount * productionLine.PowerGeneration;
// 如果是发电设备且还有剩余建筑重新添加到PowerManager
if (productionLine.PowerGeneration > 0)
{
var powerManager = PowerManager.Instance;
if (powerManager != null)
{
string newGeneratorId = $"{productionLineId}";
float totalGeneration = productionLine.PowerGeneration * activeLine.BuildingCount;
powerManager.AddGenerator(newGeneratorId, totalGeneration);
GD.Print($"发电设备 {productionLine.Name} 已添加到电网: +{totalGeneration}W");
}
}
}
// 归还建筑材料到库存
@ -246,7 +329,7 @@ public partial class ProductionLineManager : Node
}
GD.Print($"移除 {count} 个设备从产线 {productionLine.Name}");
GD.Print($"当前产线状态: {activeLine.BuildingCount} 个建筑, 产出率: {activeLine.ProductionRate:F2}/s, 功耗: {activeLine.TotalPowerConsumption}W");
GD.Print($"当前产线状态: {activeLine.BuildingCount} 个建筑, 产出率: {activeLine.ProductionRate:F2}/s, 功耗: {activeLine.TotalPowerConsumption}W, 发电: {activeLine.TotalPowerGeneration}W");
}
public ActiveProductionLine GetActiveProductionLine(string productionLineId)

View File

@ -44,8 +44,33 @@ public partial class ProductionProcessor : Node
var productionLine = ProductionLineManager.Instance?.GetProductionLine(activeLine.ProductionLineId);
if (productionLine == null) return;
// 特殊处理如果是发电设备powerGeneration > 0 且 productionTime <= 0跳过生产处理
if (productionLine.PowerGeneration > 0 && productionLine.ProductionTime <= 0)
{
// 发电设备不需要生产周期,直接返回
// 发电量已经在添加建筑时注册到PowerManager了
return;
}
// 获取电力满足率
float powerRatio = 1.0f;
var powerManager = PowerManager.Instance;
if (powerManager != null)
{
powerRatio = powerManager.GetPowerRatio();
}
// 根据电力满足率调整生产速度
float adjustedDeltaTime = deltaTime * powerRatio;
// 如果电力不足,减慢生产速度
if (powerRatio < 1.0f)
{
GD.Print($"产线 {productionLine.Name} 电力不足,生产速度调整为 {powerRatio * 100:F1}%");
}
// 更新生产时间
activeLine.RemainingTime -= deltaTime;
activeLine.RemainingTime -= adjustedDeltaTime;
if (activeLine.RemainingTime <= 0)
{
@ -62,12 +87,32 @@ public partial class ProductionProcessor : Node
activeLine.RemainingTime = productionLine.ProductionTime;
GD.Print($"产线 {productionLine.Name} 完成一轮生产,{activeLine.BuildingCount} 个建筑同时运行");
// 如果是发电设备通知PowerManager发电机运行
if (productionLine.PowerGeneration > 0)
{
if (powerManager != null)
{
string generatorId = $"{activeLine.ProductionLineId}_{activeLine.BuildingCount}";
powerManager.UpdateGeneratorStatus(generatorId, true);
}
}
}
else
{
// 原料不足,暂停生产
activeLine.RemainingTime = 0.1f; // 短暂等待后再检查
GD.Print($"产线 {productionLine.Name} 原料不足,暂停生产");
// 如果是发电设备通知PowerManager发电机异常
if (productionLine.PowerGeneration > 0)
{
if (powerManager != null)
{
string generatorId = $"{activeLine.ProductionLineId}_{activeLine.BuildingCount}";
powerManager.UpdateGeneratorStatus(generatorId, false);
}
}
}
}
}
@ -77,6 +122,12 @@ public partial class ProductionProcessor : Node
var inventoryManager = InventoryManager.Instance;
if (inventoryManager == null) return false;
// 如果没有输入材料需求如发电设备直接返回true
if (productionLine.Recipe.Inputs == null || productionLine.Recipe.Inputs.Count == 0)
{
return true;
}
// 检查所有输入材料
foreach (var input in productionLine.Recipe.Inputs)
{
@ -143,7 +194,12 @@ public partial class ProductionProcessor : Node
}
float progress = 0;
if (activeLine.IsActive && productionLine.ProductionTime > 0)
// 发电设备的进度条始终显示100%
if (productionLine.PowerGeneration > 0 && productionLine.ProductionTime <= 0)
{
progress = 1.0f; // 发电设备进度始终为100%
}
else if (activeLine.IsActive && productionLine.ProductionTime > 0)
{
progress = 1.0f - (activeLine.RemainingTime / productionLine.ProductionTime);
}