Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user