Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
@@ -33,58 +33,70 @@ public partial class Robot : Node3D
|
||||
|
||||
if (isExecuting)
|
||||
{
|
||||
if (CanExecute(delta))
|
||||
{
|
||||
switch (currentNode.Execute(this, delta))
|
||||
{
|
||||
case NodeResult.SUCCESS:
|
||||
currentNode = currentNode.nextNode;
|
||||
if (currentNode == null)
|
||||
{
|
||||
isExecuting = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case NodeResult.FAILURE:
|
||||
isExecuting = false;
|
||||
currentMessage = "(FAILED)" + currentNode.lastExecutionMessage;
|
||||
break;
|
||||
|
||||
case NodeResult.RUNNING:
|
||||
currentMessage = "";
|
||||
break;
|
||||
case NodeResult.CONDITIONFALSE:
|
||||
currentNode = currentNode.NegativeNode;
|
||||
if (currentNode == null)
|
||||
{
|
||||
isExecuting = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (currentMessage.Length <= 0)
|
||||
{
|
||||
CoolDown(
|
||||
delta,
|
||||
GameData.robotStats.GetCoolingRate(IdleHeatLossPerSecond)
|
||||
* TypeStats.CoolingMultiplier
|
||||
);
|
||||
|
||||
currentMessage = "No script executing";
|
||||
UpdateExecution(delta);
|
||||
}
|
||||
else
|
||||
{
|
||||
CoolDown(
|
||||
delta,
|
||||
GameData.robotStats.GetCoolingRate(IdleHeatLossPerSecond)
|
||||
* TypeStats.CoolingMultiplier
|
||||
);
|
||||
UpdateIdle(delta);
|
||||
}
|
||||
|
||||
Visible = Math.Round(Math.Abs(Position.Y / GameData.tileHeight), 0) == GameData.visibleLayer;
|
||||
}
|
||||
|
||||
private void UpdateExecution(double delta)
|
||||
{
|
||||
if (!CanExecute(delta)) return;
|
||||
|
||||
NodeResult result = currentNode.Execute(this, delta);
|
||||
ApplyNodeResult(result);
|
||||
}
|
||||
|
||||
private void ApplyNodeResult(NodeResult result)
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
case NodeResult.SUCCESS:
|
||||
MoveToNextNode(currentNode.nextNode);
|
||||
break;
|
||||
|
||||
case NodeResult.CONDITIONFALSE:
|
||||
MoveToNextNode(currentNode.NegativeNode);
|
||||
break;
|
||||
|
||||
case NodeResult.FAILURE:
|
||||
isExecuting = false;
|
||||
currentMessage = "(FAILED)" + currentNode.lastExecutionMessage;
|
||||
break;
|
||||
|
||||
case NodeResult.RUNNING:
|
||||
currentMessage = "";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveToNextNode(ProgramNode nextNode)
|
||||
{
|
||||
currentNode = nextNode;
|
||||
if (currentNode == null)
|
||||
{
|
||||
isExecuting = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateIdle(double delta)
|
||||
{
|
||||
CoolDown(
|
||||
delta,
|
||||
GameData.robotStats.GetCoolingRate(IdleHeatLossPerSecond)
|
||||
* TypeStats.CoolingMultiplier
|
||||
);
|
||||
|
||||
if (currentMessage.Length <= 0)
|
||||
{
|
||||
currentMessage = "No script executing";
|
||||
}
|
||||
}
|
||||
|
||||
public void SetupExecution(List<ProgramNode> nodes)
|
||||
{
|
||||
if (nodes.Count <= 0) return;
|
||||
@@ -200,32 +212,13 @@ public partial class Robot : Node3D
|
||||
return false;
|
||||
}
|
||||
|
||||
float energyUse =
|
||||
GameData.robotStats.GetEnergyUse(EnergyUsePerSecond)
|
||||
* TypeStats.EnergyUseMultiplier
|
||||
* (float)delta;
|
||||
|
||||
if (!GameData.survival.TryConsumeEnergy(energyUse))
|
||||
if (!TryConsumeEnergy(delta))
|
||||
{
|
||||
currentMessage = "Not enough energy";
|
||||
return false;
|
||||
}
|
||||
|
||||
heat = Math.Clamp(
|
||||
heat + GameData.robotStats.GetHeatGain(HeatGainPerSecond)
|
||||
* TypeStats.HeatGainMultiplier
|
||||
* (float)delta,
|
||||
0f,
|
||||
100f
|
||||
);
|
||||
|
||||
maintenance = Math.Clamp(
|
||||
maintenance - GameData.robotStats.GetMaintenanceLoss(MaintenanceLossPerSecond)
|
||||
* TypeStats.MaintenanceLossMultiplier
|
||||
* (float)delta,
|
||||
0f,
|
||||
100f
|
||||
);
|
||||
ApplyWear(delta);
|
||||
|
||||
if (heat >= 100f)
|
||||
{
|
||||
@@ -244,6 +237,35 @@ public partial class Robot : Node3D
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool TryConsumeEnergy(double delta)
|
||||
{
|
||||
float energyUse =
|
||||
GameData.robotStats.GetEnergyUse(EnergyUsePerSecond)
|
||||
* TypeStats.EnergyUseMultiplier
|
||||
* (float)delta;
|
||||
|
||||
return GameData.survival.TryConsumeEnergy(energyUse);
|
||||
}
|
||||
|
||||
private void ApplyWear(double delta)
|
||||
{
|
||||
heat = Math.Clamp(
|
||||
heat + GameData.robotStats.GetHeatGain(HeatGainPerSecond)
|
||||
* TypeStats.HeatGainMultiplier
|
||||
* (float)delta,
|
||||
0f,
|
||||
100f
|
||||
);
|
||||
|
||||
maintenance = Math.Clamp(
|
||||
maintenance - GameData.robotStats.GetMaintenanceLoss(MaintenanceLossPerSecond)
|
||||
* TypeStats.MaintenanceLossMultiplier
|
||||
* (float)delta,
|
||||
0f,
|
||||
100f
|
||||
);
|
||||
}
|
||||
|
||||
private void CoolDown(double delta, float heatLossPerSecond)
|
||||
{
|
||||
heat = Math.Clamp(
|
||||
|
||||
Reference in New Issue
Block a user