using System; using D2D.Common; using D2D.Utilities; using D2D.Core; using D2D.Gameplay; using NaughtyAttributes; using UnityEngine; using static D2D.Utilities.SettingsFacade; using static D2D.Utilities.CommonLazyFacade; using static D2D.Utilities.CommonGameplayFacade; using System.Collections.Generic; namespace D2D.Databases { public class GameProgressionDatabase : GameStateMachineUser, ILazy { #region For analytics public readonly DataContainer Reloads = new DataContainer("ReloadsCount", 0); public readonly DataContainer CompletedLevelsPerSession = new DataContainer("CompletedLevelsPerSession", 0); #endregion // Stickman Squad Specified Vars public readonly DataContainer PowerIncreasePercent = new DataContainer("PowerIncreasePercent", 0); public readonly DataContainer FireRateDecreasePercent = new DataContainer("FireRateDecreasePercent", 0); public readonly DataContainer PowerIncreaseLevel = new DataContainer("PowerIncreaseLevel", 0); public readonly DataContainer FireRateDecreaseLevel = new DataContainer("FireRateDecreaseLevel", 0); public readonly DataContainer UnlockableItem = new DataContainer("UnlockableItem", ""); public readonly DataContainer UnlockableItemProgress = new DataContainer("UnlockableItemProgress", 0f); public readonly DataContainer LastUnlockedMember = new DataContainer("LastUnlockedMember", ""); public List UnlockedMembers { get { if (ES3.KeyExists(UnlockedMembersKey)) { _UnlockedMembers = ES3.Load>(UnlockedMembersKey); } else { _UnlockedMembers = new List(); ES3.Save(UnlockedMembersKey, _UnlockedMembers); } return _UnlockedMembers; } } private List _UnlockedMembers; public void SaveMembers() => ES3.Save(UnlockedMembersKey, _UnlockedMembers); private const string UnlockedMembersKey = "UnlockedMembers"; // Common Vars public readonly DataContainer PassedLevels = new DataContainer("PassedLevels", 0); public readonly TrackableValue Money = new TrackableValue(value: 0, firstGet: () => PlayerPrefs.GetInt("Money")); public readonly DataContainer LastSceneNumber = new DataContainer("LastSceneNumber", 1); public float TimeOfSceneLoad { get; private set; } public float TimeElapsedFromSceneLoad => Time.time - TimeOfSceneLoad; [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] private static void ResetLevelsPerSession() { LazySugar.FindLazy().CompletedLevelsPerSession.Value = 0; } protected override void OnEnable() { base.OnEnable(); Money.Changed += SaveMoney; TimeOfSceneLoad = Time.time; } protected override void OnDisable() { base.OnDisable(); Money.Changed -= SaveMoney; } private void SaveMoney(float val) { PlayerPrefs.SetInt("Money", Money.Value.Round()); } private void OnApplicationQuit() { SaveMoney(Money.Value.Round()); } private void Start() { var level = this.Find(); if (level != null) LastSceneNumber.Value = level.SceneNumber; } protected override void OnGameWin() { Debug.Log("PassedLevels: " + PassedLevels.Value); CompletedLevelsPerSession.Value++; PassedLevels.Value++; } public static void Clear() { ES3.Save("ReloadsCount", 0); ES3.Save("CompletedLevelsPerSession", 0); ES3.Save("PassedLevels", 0); ES3.Save("LastSceneNumber", 1); ES3.Save("PassedLevels", 0); ES3.Save("LastSceneNumber", 1); ES3.Save("PowerIncreaseLevel", 0f); ES3.Save("FireRateDecreaseLevel", 0f); ES3.Save("UnlockableItemProgress", 0f); ES3.Save("UnlockableItem", ""); ES3.Save("LastUnlockedMember", ""); ES3.Save(UnlockedMembersKey, new List()); PlayerPrefs.SetInt("Money", 0); } } }