Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
public class Building
|
||||
{
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://cl2yvllo35qbb
|
||||
@@ -1,92 +1,95 @@
|
||||
public class GameResource
|
||||
{
|
||||
private const float NormalExtractionSpeed = 1f;
|
||||
private const float EndlessExtractionSpeed = 4f;
|
||||
private const float NormalExtractionSpeed = 1f;
|
||||
private const float EndlessExtractionSpeed = 4f;
|
||||
|
||||
public string name;
|
||||
public ItemData item;
|
||||
public string name;
|
||||
public ItemData item;
|
||||
|
||||
private int currentAmount;
|
||||
private int maxAmount;
|
||||
private bool isEndless;
|
||||
private float extractionSpeed;
|
||||
private double timeSinceLastExtraction;
|
||||
private int currentAmount;
|
||||
private int maxAmount;
|
||||
private bool isEndless;
|
||||
private float extractionSpeed;
|
||||
private double timeSinceLastExtraction;
|
||||
|
||||
public GameResource(string name)
|
||||
{
|
||||
this.name = name;
|
||||
maxAmount = GameData.rand.Next(1000, 10000);
|
||||
currentAmount = maxAmount;
|
||||
isEndless = false;
|
||||
extractionSpeed = NormalExtractionSpeed;
|
||||
item = GameData.availableItems[name];
|
||||
}
|
||||
public GameResource(string name)
|
||||
{
|
||||
this.name = name;
|
||||
maxAmount = GameData.rand.Next(1000, 10000);
|
||||
currentAmount = maxAmount;
|
||||
isEndless = false;
|
||||
extractionSpeed = NormalExtractionSpeed;
|
||||
item = GameData.availableItems[name];
|
||||
}
|
||||
|
||||
public static GameResource FromSaveData(ResourceSaveData saveData)
|
||||
{
|
||||
GameResource resource = new GameResource(saveData.Name)
|
||||
{
|
||||
currentAmount = saveData.CurrentAmount,
|
||||
maxAmount = saveData.MaxAmount,
|
||||
isEndless = saveData.IsEndless,
|
||||
extractionSpeed = saveData.ExtractionSpeed
|
||||
};
|
||||
if (resource.isEndless && resource.extractionSpeed <= NormalExtractionSpeed)
|
||||
{
|
||||
resource.extractionSpeed = EndlessExtractionSpeed;
|
||||
}
|
||||
resource.timeSinceLastExtraction = saveData.TimeSinceLastExtraction;
|
||||
return resource;
|
||||
}
|
||||
public static GameResource FromSaveData(ResourceSaveData saveData)
|
||||
{
|
||||
GameResource resource = new GameResource(saveData.Name)
|
||||
{
|
||||
currentAmount = saveData.CurrentAmount,
|
||||
maxAmount = saveData.MaxAmount,
|
||||
isEndless = saveData.IsEndless,
|
||||
extractionSpeed = saveData.ExtractionSpeed,
|
||||
timeSinceLastExtraction = saveData.TimeSinceLastExtraction
|
||||
};
|
||||
resource.NormalizeExtractionSpeed();
|
||||
return resource;
|
||||
}
|
||||
|
||||
public ResourceSaveData CreateSaveData()
|
||||
{
|
||||
return new ResourceSaveData
|
||||
{
|
||||
Name = name,
|
||||
CurrentAmount = currentAmount,
|
||||
MaxAmount = maxAmount,
|
||||
IsEndless = isEndless,
|
||||
ExtractionSpeed = extractionSpeed,
|
||||
TimeSinceLastExtraction = timeSinceLastExtraction
|
||||
};
|
||||
}
|
||||
public ResourceSaveData CreateSaveData()
|
||||
{
|
||||
return new ResourceSaveData
|
||||
{
|
||||
Name = name,
|
||||
CurrentAmount = currentAmount,
|
||||
MaxAmount = maxAmount,
|
||||
IsEndless = isEndless,
|
||||
ExtractionSpeed = extractionSpeed,
|
||||
TimeSinceLastExtraction = timeSinceLastExtraction
|
||||
};
|
||||
}
|
||||
|
||||
public bool Extract(double delta)
|
||||
{
|
||||
timeSinceLastExtraction += delta;
|
||||
if (timeSinceLastExtraction < extractionSpeed) return false;
|
||||
public bool Extract(double delta)
|
||||
{
|
||||
timeSinceLastExtraction += delta;
|
||||
if (timeSinceLastExtraction < extractionSpeed) return false;
|
||||
|
||||
timeSinceLastExtraction = 0;
|
||||
if (isEndless) return true;
|
||||
timeSinceLastExtraction = 0;
|
||||
if (isEndless) return true;
|
||||
|
||||
if (currentAmount > 0)
|
||||
{
|
||||
currentAmount--;
|
||||
return true;
|
||||
}
|
||||
if (currentAmount <= 0) return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
currentAmount--;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool CanExtract()
|
||||
{
|
||||
return (isEndless || currentAmount > 0) && GameData.availableResearch[item.Research].state == ResearchState.RESEARCHED;
|
||||
}
|
||||
public bool CanExtract()
|
||||
{
|
||||
return (isEndless || currentAmount > 0)
|
||||
&& GameData.availableResearch[item.Research].state == ResearchState.RESEARCHED;
|
||||
}
|
||||
|
||||
public void MakeEndless()
|
||||
{
|
||||
isEndless = true;
|
||||
extractionSpeed = EndlessExtractionSpeed;
|
||||
}
|
||||
public void MakeEndless()
|
||||
{
|
||||
isEndless = true;
|
||||
extractionSpeed = EndlessExtractionSpeed;
|
||||
}
|
||||
|
||||
public bool IsEndless()
|
||||
{
|
||||
return isEndless;
|
||||
}
|
||||
public bool IsEndless()
|
||||
{
|
||||
return isEndless;
|
||||
}
|
||||
|
||||
public float GetExtractionSpeed()
|
||||
{
|
||||
return extractionSpeed;
|
||||
}
|
||||
public float GetExtractionSpeed()
|
||||
{
|
||||
return extractionSpeed;
|
||||
}
|
||||
|
||||
private void NormalizeExtractionSpeed()
|
||||
{
|
||||
if (!isEndless) return;
|
||||
if (extractionSpeed > NormalExtractionSpeed) return;
|
||||
|
||||
extractionSpeed = EndlessExtractionSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,12 +29,13 @@ public class ItemData
|
||||
|
||||
public string GetReadableName()
|
||||
{
|
||||
string noUnderscore = Id.Replace("_", " ").ToLower();
|
||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
||||
return GetReadableName(Id);
|
||||
}
|
||||
|
||||
public static string GetReadableName(string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input)) return "";
|
||||
|
||||
string noUnderscore = input.Replace("_", " ").ToLower();
|
||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
||||
}
|
||||
@@ -46,6 +47,8 @@ public class ItemData
|
||||
|
||||
public string GetCraftingDisplay()
|
||||
{
|
||||
if (Inputs.Count <= 0) return GetReadableName() + ": \r";
|
||||
|
||||
string result = GetReadableName() + ": \r";
|
||||
|
||||
foreach (Ingredient ingredient in Inputs)
|
||||
@@ -53,8 +56,6 @@ public class ItemData
|
||||
result += $"{GetReadableName(ingredient.Item)} ({ingredient.Amount}),\r";
|
||||
}
|
||||
|
||||
if (Inputs.Count <= 0) return result;
|
||||
|
||||
result = result.Remove(result.Length - 2);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@ public class Research
|
||||
|
||||
public static string GetReadableName(string input)
|
||||
{
|
||||
string noUnderscore = input.Replace("_", " ").ToLower();
|
||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
||||
return ItemData.GetReadableName(input);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +23,7 @@ public class ResearchData
|
||||
|
||||
public string GetReadableName()
|
||||
{
|
||||
string noUnderscore = Id.Replace("_", " ").ToLower();
|
||||
return char.ToUpper(noUnderscore[0]) + noUnderscore.Substring(1);
|
||||
return ItemData.GetReadableName(Id);
|
||||
}
|
||||
|
||||
public static string GetIndex(string readable)
|
||||
|
||||
@@ -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