Skip to content

Commit

Permalink
Fixed restarting changing powercell count
Browse files Browse the repository at this point in the history
  • Loading branch information
Greenfoot5 committed May 24, 2024
1 parent d460676 commit 093bf46
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
20 changes: 10 additions & 10 deletions Tower Defence/Assets/Abstract/Saving/SaveLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ public struct NodeData
public List<ModuleChainHandler> moduleChainHandlers;
public Quaternion turretRotation;
}
public List<NodeData> nodes;
public List<NodeData> Nodes;

public int energy;
public int cells;
public int lives;
public int waveIndex;
public Random.State random;
public int shopCost;
public int Energy;
public int Powercells;
public int Lives;
public int WaveIndex;
public Random.State RandomState;
public int ShopCost;

public List<TurretBlueprint> turretInventory;
public List<ModuleChainHandler> moduleInventory;
public List<TurretBlueprint> TurretInventory;
public List<ModuleChainHandler> ModuleInventory;

public readonly string version = Application.version;
public readonly string Version = Application.version;

/// <summary>
/// Translates the class into json format
Expand Down
2 changes: 1 addition & 1 deletion Tower Defence/Assets/Abstract/Saving/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static bool SaveExists(string sceneName)
var sd = new SaveLevel();
sd.LoadFromJson(json);

return sd.version == Application.version;
return sd.Version == Application.version;
}

public static void ClearSave(string sceneName)
Expand Down
43 changes: 23 additions & 20 deletions Tower Defence/Assets/Gameplay/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ private void EndGame()

public void PopulateSaveData(SaveLevel saveData)
{
saveData.lives = GameStats.Lives;
saveData.energy = GameStats.Energy;
saveData.waveIndex = GameStats.Rounds - 1;
saveData.random = Random.state;
saveData.shopCost = shop.GetComponent<Shop>().nextCost;
saveData.nodes = new List<SaveLevel.NodeData>();
saveData.turretInventory = new List<TurretBlueprint>();
saveData.moduleInventory = new List<ModuleChainHandler>();
saveData.Lives = GameStats.Lives;
saveData.Energy = GameStats.Energy;
saveData.Powercells = GameStats.Powercells;
saveData.WaveIndex = GameStats.Rounds - 1;
saveData.RandomState = Random.state;
Debug.Log("Getting nextCost from Save Data");
saveData.ShopCost = shop.GetComponent<Shop>().nextCost;
saveData.Nodes = new List<SaveLevel.NodeData>();
saveData.TurretInventory = new List<TurretBlueprint>();
saveData.ModuleInventory = new List<ModuleChainHandler>();

// Node Data
foreach (Node node in nodeParent.GetComponentsInChildren<Node>())
Expand All @@ -125,19 +127,19 @@ public void PopulateSaveData(SaveLevel saveData)
turretRotation = node.turret.transform.rotation,
moduleChainHandlers = node.turret.GetComponent<Turret>().moduleHandlers
};
saveData.nodes.Add(nodeData);
saveData.Nodes.Add(nodeData);
}

// Inventory
// Turrets
foreach (TurretBlueprint turret in TurretInventory)
{
saveData.turretInventory.Add(turret);
saveData.TurretInventory.Add(turret);
}
// Modules
foreach (ModuleChainHandler module in ModuleInventory)
{
saveData.moduleInventory.Add(module);
saveData.ModuleInventory.Add(module);
}
}

Expand All @@ -152,15 +154,16 @@ private static void LoadJsonData(ISaveableLevel level)
public void LoadFromSaveData(SaveLevel saveData)
{
_startLives = GameStats.Lives;
GameStats.Lives = saveData.lives;
GameStats.PopulateRounds(saveData.waveIndex + 1);
Random.state = saveData.random;
GameStats.Lives = saveData.Lives;
GameStats.PopulateRounds(saveData.WaveIndex + 1);
Random.state = saveData.RandomState;
Debug.Log("Setting nextCost from Save Data");
var shopComponent = shop.GetComponent<Shop>();
shopComponent.nextCost = saveData.shopCost;
GameStats.Powercells = 0;
GameStats.Energy = saveData.energy;
shopComponent.nextCost = saveData.ShopCost;
GameStats.Powercells = saveData.Powercells;
GameStats.Energy = saveData.Energy;

foreach (SaveLevel.NodeData nodeData in saveData.nodes)
foreach (SaveLevel.NodeData nodeData in saveData.Nodes)
{
foreach (Node node in nodeParent.GetComponentsInChildren<Node>())
{
Expand All @@ -175,11 +178,11 @@ public void LoadFromSaveData(SaveLevel saveData)
}
}

foreach (TurretBlueprint turret in saveData.turretInventory)
foreach (TurretBlueprint turret in saveData.TurretInventory)
{
shopComponent.SpawnNewTurret(turret);
}
foreach (ModuleChainHandler module in saveData.moduleInventory)
foreach (ModuleChainHandler module in saveData.ModuleInventory)
{
shopComponent.SpawnNewModule(module);
}
Expand Down
7 changes: 4 additions & 3 deletions Tower Defence/Assets/Gameplay/GameStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static int Energy
set
{
_energy = value;
OnGainMoney?.Invoke();
OnGainEnergy?.Invoke();
}
}

Expand Down Expand Up @@ -78,7 +78,7 @@ public static void PopulateLives(int lives)
public static event RoundProgressEvent OnRoundProgress;
public static event GameOverEvent OnGameOver;
public static event LoseLife OnLoseLife;
public static event GainMoney OnGainMoney;
public static event GainMoney OnGainEnergy;
public static event GainCell OnGainPowercell;

/// <summary>
Expand All @@ -88,7 +88,8 @@ private void Awake()
{
if (!_active)
{
Energy = startEnergy;
_energy = startEnergy;
_powercells = 0;
_lives = startLives;
_rounds = 0;
}
Expand Down
16 changes: 8 additions & 8 deletions Tower Defence/Assets/Gameplay/Shop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,19 @@ private void Start()
{
_buildManager = BuildManager.instance;
_levelData = _buildManager.GetComponent<GameManager>().levelData;

// It should only be greater than 0 if we've loaded a save
if (nextCost == 0)
{
nextCost = _levelData.initialSelectionCost;
}
else
{
_hasPlayerMadePurchase = true;
}

_defaultButtonTop = energyProgress.outColorA;
_defaultButtonBottom = energyProgress.outColorB;

// Update button text
powercellsText.text = "<sprite=\"UI-Powercell\" name=\"full\"> " + nextCost;
GameStats.OnGainMoney += CalculateCells;
GameStats.OnGainEnergy += CalculateCells;
GameStats.OnGainPowercell += UpdateEnergyButton;
CalculateCells();
}
Expand Down Expand Up @@ -161,12 +158,15 @@ public int GetSellAmount()

private void CalculateCells()
{
while (GameStats.Energy > nextCost)
var energyToSubtract = 0;
while (GameStats.Energy - energyToSubtract > nextCost)
{
nextCost += _levelData.selectionCostIncrement;
GameStats.Energy -= nextCost;
energyToSubtract += nextCost;
GameStats.Powercells++;
}
if (energyToSubtract > 0)
GameStats.Energy -= energyToSubtract;
UpdateEnergyButton();
}

Expand Down

0 comments on commit 093bf46

Please sign in to comment.