Added basic upgrade menu
This commit is contained in:
128
Assets/Scripts/Game/UpgradeManager.cs
Normal file
128
Assets/Scripts/Game/UpgradeManager.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace Fruitomation.Game
|
||||
{
|
||||
[Serializable] public enum BasicUpgrade
|
||||
{
|
||||
Grapes,
|
||||
Bananas,
|
||||
Kiwi,
|
||||
Mangoes,
|
||||
Durian,
|
||||
BuddhasHand,
|
||||
Pitayas,
|
||||
|
||||
AppleSlices,
|
||||
DriedAppleSlices,
|
||||
AppleJuice,
|
||||
GrapeJuice,
|
||||
Wine,
|
||||
Raisins,
|
||||
DriedFruitSelection,
|
||||
BananaSlices,
|
||||
DriedBananaSlices,
|
||||
BananaBacon,
|
||||
BananaPeeler,
|
||||
KiwiPresser,
|
||||
KiwiSeedOil,
|
||||
KiwiVinegar,
|
||||
AppleMangoJuice,
|
||||
SlicedKiwi,
|
||||
BananaIceCream,
|
||||
MangoJuice,
|
||||
MangoSlices,
|
||||
SpicedBananaIceCream,
|
||||
DurianPowder,
|
||||
DurianSlices,
|
||||
ExoticFruitSelection,
|
||||
BuddhasHandSlices,
|
||||
PitayaPeeler,
|
||||
PitayaFoodDye,
|
||||
PitayaIceCream,
|
||||
SpicedPitayaIceCream,
|
||||
}
|
||||
|
||||
public class UnlockedUpgrades
|
||||
{
|
||||
[Serializable] public class Serialized
|
||||
{
|
||||
[SerializeField] private BasicUpgrade[] BasicUpgrades;
|
||||
|
||||
private Serialized()
|
||||
{
|
||||
// All logic done in Serialize
|
||||
}
|
||||
|
||||
public UnlockedUpgrades Unserialize()
|
||||
{
|
||||
UnlockedUpgrades upgrades = new();
|
||||
foreach (BasicUpgrade upgrade in BasicUpgrades)
|
||||
{
|
||||
Debug.Log($"Restored [{upgrade}] upgrade");
|
||||
upgrades.Unlocks.Add(upgrade);
|
||||
}
|
||||
|
||||
return upgrades;
|
||||
}
|
||||
|
||||
public static Serialized Serialize(UnlockedUpgrades upgrades)
|
||||
{
|
||||
Serialized serialized = new()
|
||||
{
|
||||
BasicUpgrades = upgrades.Unlocks.ToArray()
|
||||
};
|
||||
|
||||
return serialized;
|
||||
}
|
||||
}
|
||||
|
||||
private HashSet<BasicUpgrade> Unlocks = new();
|
||||
|
||||
public void Unlock(BasicUpgrade upgrade)
|
||||
{
|
||||
Unlocks.Add(upgrade);
|
||||
}
|
||||
}
|
||||
|
||||
public class UpgradeManager : MonoBehaviour
|
||||
{
|
||||
private static string Filepath => Path.Combine(Application.persistentDataPath, "upgrades.json");
|
||||
private static UnlockedUpgrades CurrentUpgrades;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void LoadSavedUpgrades()
|
||||
{
|
||||
if (File.Exists(Filepath))
|
||||
{
|
||||
string json = File.ReadAllText(Filepath);
|
||||
UnlockedUpgrades.Serialized serialized = JsonUtility.FromJson<UnlockedUpgrades.Serialized>(json);
|
||||
|
||||
if (serialized is not null)
|
||||
{
|
||||
CurrentUpgrades = JsonUtility.FromJson<UnlockedUpgrades.Serialized>(json).Unserialize();
|
||||
}
|
||||
}
|
||||
|
||||
CurrentUpgrades ??= new UnlockedUpgrades();
|
||||
|
||||
Application.quitting += SaveUpgradesToDisk;
|
||||
}
|
||||
|
||||
private static void SaveUpgradesToDisk()
|
||||
{
|
||||
UnlockedUpgrades.Serialized serialized = UnlockedUpgrades.Serialized.Serialize(CurrentUpgrades);
|
||||
string json = JsonUtility.ToJson(serialized, true);
|
||||
File.WriteAllText(Filepath, json);
|
||||
}
|
||||
|
||||
public void UnlockBasicUpgrade(string id)
|
||||
{
|
||||
BasicUpgrade upgrade = Enum.Parse<BasicUpgrade>(id);
|
||||
CurrentUpgrades.Unlock(upgrade);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Game/UpgradeManager.cs.meta
Normal file
3
Assets/Scripts/Game/UpgradeManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38c26644b322484cb5ca345985d03dd9
|
||||
timeCreated: 1776174265
|
||||
@@ -20,7 +20,7 @@ namespace Fruitomation.UI
|
||||
UpgradesButton.onClick.AddListener(() =>
|
||||
{
|
||||
GameStateController.State = GameState.UpgradeMenu;
|
||||
SceneController.StartLoadOf("SampleScene");
|
||||
SceneController.StartLoadOf("UpgradesScene");
|
||||
});
|
||||
|
||||
SimulateButton.onClick.AddListener(() =>
|
||||
|
||||
Reference in New Issue
Block a user