156 lines
4.2 KiB
C#
156 lines
4.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif // UNITY_EDITOR
|
|
|
|
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,
|
|
}
|
|
|
|
[Serializable] public enum IncrementalUpgrade
|
|
{
|
|
}
|
|
|
|
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 bool IsUnlocked(BasicUpgrade upgrade)
|
|
{
|
|
return Unlocks.Contains(upgrade);
|
|
}
|
|
}
|
|
|
|
public static class UpgradeManager
|
|
{
|
|
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 static void Unlock(BasicUpgrade upgrade) => CurrentUpgrades.Unlock(upgrade);
|
|
|
|
public static bool Is(BasicUpgrade upgrade) => CurrentUpgrades.IsUnlocked(upgrade);
|
|
|
|
#if UNITY_EDITOR
|
|
[MenuItem("Fruitomation/Reset Upgrades")]
|
|
public static void ResetUpgrades() => CurrentUpgrades = new UnlockedUpgrades();
|
|
|
|
[MenuItem("Fruitomation/Unlock All Upgrades")]
|
|
public static void UnlockAll()
|
|
{
|
|
BasicUpgrade[] upgrades = Enum.GetValues(typeof(BasicUpgrade)) as BasicUpgrade[];
|
|
System.Diagnostics.Debug.Assert(upgrades != null, nameof(upgrades) + " != null");
|
|
|
|
foreach (BasicUpgrade upgrade in upgrades)
|
|
{
|
|
Unlock(upgrade);
|
|
}
|
|
}
|
|
#endif // UNITY_EDITOR
|
|
}
|
|
} |