using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using OHM.UnityToolkit; using OHM.UnityToolkit.Inventory; public class AppController : MonoBehaviour { public class TransformEvent : UnityEvent { } public const string EDITOR_NAME = "GameSqueleton"; public CoreController coreGame; public SavedDataController savedDataController; public MetaDef metaDef; [HideInInspector] public UnityEvent onGameExited; [HideInInspector] public UnityEvent onCurrenciesChanged; [HideInInspector] public UnityEvent onLevelChanged; [HideInInspector] public UnityEvent onLevelCleared; [HideInInspector] public UnityEvent onBestScoreChanged; [NonSerialized] public bool needRestart; public System.Random random = new System.Random(); private CustomizableController _playerSkinController; private CustomizableController _pickupsSkinsController; public CustomizableController PlayerSkinController => _playerSkinController ?? (_playerSkinController = new PlayerSkinController(this)); public CustomizableController PickupsSkinsController => _pickupsSkinsController ?? (_pickupsSkinsController = new PickupsSkinsController(this)); public void Awake() { Application.targetFrameRate = 60; Physics.autoSyncTransforms = false; coreGame.onGameStarted.AddListener(OnGameStarted); coreGame.onGameOver.AddListener(OnGameOver); coreGame.onGameWon.AddListener(OnGameEnded); coreGame.currenciesController.onWinCurrencies += OnWinCurrency; } public void Start() { AppSettings.Instance.Apply(); Quit(); } internal void StartWaitForReadyThen(Action p) { StartCoroutine(WaitForReadyThen(p)); } private IEnumerator WaitForReadyThen(Action action) { while (!coreGame.IsReadyToPlay()) { yield return null; } action(); } private void Update() { if (Application.isEditor && Input.GetKeyDown(KeyCode.R)) { needRestart = true; } if (needRestart) { Restart(); needRestart = false; } if (Application.isEditor && Input.GetKeyDown(KeyCode.Q)) { Quit(); } if (Application.isEditor && Input.GetKeyDown(KeyCode.U)) { DebugNextLevel(); } } public void Restart() { Quit(); coreGame.Go(); } public void Quit() { onGameExited.Invoke(); PlayerSkinController.ApplyCurrent(); PickupsSkinsController.ApplyCurrent(); coreGame.Warmup(GetPlayerLevel()); } public void Go() { coreGame.Go(); } private void OnGameStarted() { } private void OnGameEnded() { savedDataController.SavedData.inventory.AddReward(SavedDataTUType.LEVEL_IDX, 1L); RegisterBestScore(); savedDataController.SaveData(); onLevelCleared.Invoke(); } private void OnGameOver() { RegisterBestScore(); } private void RegisterBestScore() { long score = coreGame.scoreController.Score; if (score <= GetBestScore()) { return; } savedDataController.SavedData.inventory.SetCount(SavedDataTUType.BEST_SCORE_DEFAULT, score); savedDataController.SaveData(); onBestScoreChanged.Invoke(); } public long GetBestScore() { return savedDataController.SavedData.inventory.GetCount(SavedDataTUType.BEST_SCORE_DEFAULT); } public long GetPlayerLevel() { return savedDataController.SavedData.inventory.GetCount(SavedDataTUType.LEVEL_IDX); } private void OnWinCurrency(int count, Transform fromObj) { WinCurrencies(count); } public void WinCurrencies(int count) { savedDataController.SavedData.inventory.AddReward(SavedDataTUType.CURRENCY, count); savedDataController.SaveData(); onCurrenciesChanged.Invoke(); } public long GetCurrencies() { return savedDataController.SavedData.inventory.GetCount(SavedDataTUType.CURRENCY); } public bool CanPayCost(TransactionUnit cost) { return savedDataController.SavedData.inventory.HasCost(cost); } public bool TryPayCost(TransactionUnit cost) { if (!savedDataController.SavedData.inventory.PayCost(cost)) { return false; } onCurrenciesChanged.Invoke(); return true; } public void ResetData(bool exit) { PlayerPrefs.DeleteAll(); savedDataController.ClearData(); savedDataController.ResetData(); AppSettings.Instance.Apply(); if (exit) { Application.Quit(); } } public void DebugNextLevel() { savedDataController.SavedData.inventory.AddReward(SavedDataTUType.LEVEL_IDX, 1L); savedDataController.SaveData(); onLevelChanged.Invoke(); } public void DebugRich() { savedDataController.SavedData.inventory.AddReward(SavedDataTUType.CURRENCY, 1000L); savedDataController.SaveData(); onCurrenciesChanged.Invoke(); } public void DebugPoor() { savedDataController.SavedData.inventory.SetCount(SavedDataTUType.CURRENCY, 0L); savedDataController.SaveData(); onCurrenciesChanged.Invoke(); } }