123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using System;
- using Cysharp.Threading.Tasks;
- using Eiko.YaSDK;
- using JTSystems;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using UnityEngine;
- using UnityEngine.AddressableAssets;
- using UnityEngine.ResourceManagement.AsyncOperations;
- using Random = UnityEngine.Random;
- public class LevelManager : MonoBehaviour
- {
- public const int SCALE_EARNNED_COINS_WITH_AD_VALUE = 3;
- public delegate void OnPaperInstantiated(Paper paper);
- public static OnPaperInstantiated onPaperInstantiated;
- public delegate void OnThemeUnlocked(ThemeData themeData, int themeUnlockLevelStep = 0);
- public static OnThemeUnlocked themeUnlocked;
- public delegate void ThemeUnlockProgressUpdated(int themeUnlockProgress, int themeUnlcokLevleStep);
- public static ThemeUnlockProgressUpdated themeUnlockProgressUpdated;
- [Header(" Settings ")] [SerializeField]
- private int adCoinsCount = 45;
- [SerializeField] private int maxCoinsCount = 15;
- [SerializeField] private int minCoinsCount = 5;
- [Min(1)] [SerializeField] private int takenCoinsCount = 5;
- [Min(1)] [SerializeField] private int unlockThemeLevelStep;
- [SerializeField] private AssetReference[] papers;
- [SerializeField] private ThemeData[] unlockableThemes;
- private Paper currentPaper;
- private int level;
- private int earnedCoins;
- private readonly Queue<ThemeData> unlockableThemesQueue = new Queue<ThemeData>();
- private int currentLoadedLevelIndex = -1;
- private int nextLoadedLevelIndex = -1;
- private void Awake()
- {
- level = YanGamesSaveManager.GetLevel();
- if (YandexSDK.instance != null)
- YandexSDK.instance.onInterstitialShown += OnOtherAdShown;
- UIManager.onNextLevelButtonPressed += SpawnNextLevel;
- UIManager.onNextLevelButtonPressedWithAd += SpawnNextLevelWithAdditionalCoins;
- UIManager.wrongPaperFolded += DecreaseEarnedCoins;
- UIManager.onLevelCompleteSet += IncrementThemeUnlockProgress;
- foreach (ThemeData unlockableTheme in unlockableThemes)
- {
- if (YanGamesSaveManager.HasUnlokedTheme(unlockableTheme.Id))
- continue;
- unlockableThemesQueue.Enqueue(unlockableTheme);
- }
- }
- private void OnDestroy()
- {
- UIManager.onNextLevelButtonPressed -= SpawnNextLevel;
- UIManager.onNextLevelButtonPressedWithAd -= SpawnNextLevelWithAdditionalCoins;
- UIManager.wrongPaperFolded -= DecreaseEarnedCoins;
- UIManager.onLevelCompleteSet -= IncrementThemeUnlockProgress;
- }
- private async void Start()
- {
- UpdateEarnedCoins();
- await SpawnLevel();
- StartUp.EndLoad();
- try
- {
- UIManager.instance.SetMenu();
- }
- catch (Exception ex)
- {
- Debug.LogError(ex.Message);
- }
- YandexSDK.instance.ShowInterstitial();
- }
-
- #if UNITY_EDITOR
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.C))
- SpawnNextLevel().Forget();
- }
- #endif
-
- private async UniTask SpawnNextLevelWithAdditionalCoins()
- {
- earnedCoins = adCoinsCount;
- await SpawnNextLevel();
- AppMetricaWeb.Event("45coinsAd");
- }
- private void IncrementThemeUnlockProgress(int starsCount)
- {
- if (unlockableThemesQueue.Count <= 0)
- return;
- if (YanGamesSaveManager.HasUnlokedTheme(unlockableThemesQueue.Peek().Id))
- {
- ChangeUnlockableTheme();
- IncrementThemeUnlockProgress(starsCount);
- return;
- }
- int unlockThemeProgress = YanGamesSaveManager.GetUnlockThemeProgress();
- unlockThemeProgress++;
- if (unlockThemeProgress >= unlockThemeLevelStep)
- {
- themeUnlocked?.Invoke(unlockableThemesQueue.Peek(), unlockThemeLevelStep);
- YanGamesSaveManager.AddUnlockedTheme(unlockableThemesQueue.Peek().Id);
- UIManager.instance?.THEMES.ChangeTheme(unlockableThemesQueue.Peek());
- ChangeUnlockableTheme();
- YanGamesSaveManager.SetUnlockThemeProgress(0);
- }
- else
- {
- themeUnlockProgressUpdated?.Invoke(unlockThemeProgress, unlockThemeLevelStep);
- YanGamesSaveManager.SetUnlockThemeProgress(unlockThemeProgress);
- }
- }
- private void ChangeUnlockableTheme()
- {
- unlockableThemesQueue.Dequeue();
- }
- public async UniTask SpawnLevel()
- {
- transform.Clear();
- // Просчитали индекс уровня
- int correctedLevelIndex = level;
- if (level > papers.Length - 1)
- correctedLevelIndex = Random.Range(0, papers.Length - 1);
- else
- AppMetricaWeb.Event($"lvl{level + 1}");
- // Выгружаем текущий левел если он есть и не равен
- if (currentLoadedLevelIndex > -1 && nextLoadedLevelIndex > -1 &&
- currentLoadedLevelIndex != nextLoadedLevelIndex)
- {
- UnloadLevelPaper(currentLoadedLevelIndex);
- currentLoadedLevelIndex = nextLoadedLevelIndex;
- }
- // Загружаем текущий если его нету
- if (currentLoadedLevelIndex <= -1)
- {
- currentLoadedLevelIndex = correctedLevelIndex;
- }
- Paper levelPrefab = await LoadPaperLevel(currentLoadedLevelIndex);
- if (levelPrefab == null)
- {
- throw new InvalidOperationException($"Loading of level with index {currentLoadedLevelIndex}");
- }
- currentPaper = Instantiate(levelPrefab, transform);
- onPaperInstantiated?.Invoke(currentPaper);
- // Загружаем в фоне следующий
- LoadNextLevel(level + 1).Forget();
- }
- public async UniTask LoadNextLevel(int level)
- {
- int correctedLevelIndex = level;
- if (level > papers.Length - 1)
- correctedLevelIndex = Random.Range(0, papers.Length - 1);
- if (correctedLevelIndex == currentLoadedLevelIndex)
- {
- nextLoadedLevelIndex = currentLoadedLevelIndex;
- }
- else
- {
- await LoadPaperLevel(correctedLevelIndex);
- nextLoadedLevelIndex = correctedLevelIndex;
- }
- }
- private async UniTask<Paper> LoadPaperLevel(int level)
- {
- GameObject paperGameObject = null;
- if (papers[level].Asset is GameObject)
- paperGameObject = papers[level].Asset as GameObject;
- if (paperGameObject == null)
- {
- AsyncOperationHandle<GameObject> paperHandler = papers[level].LoadAssetAsync<GameObject>();
- paperGameObject = await paperHandler.Task;
- }
- return paperGameObject.GetComponent<Paper>();
- }
- private void UnloadLevelPaper(int level)
- {
- papers[level].ReleaseAsset();
- }
- private async UniTask SpawnNextLevel()
- {
- UIManager.AddCoins(earnedCoins);
- level++;
- YanGamesSaveManager.SaveLevel(level);
- UpdateEarnedCoins();
- await SpawnLevel();
- try
- {
- UIManager.instance.SetGame();
- }
- catch (System.Exception ex)
- {
- Debug.LogError(ex.Message);
- }
- }
- private void UpdateEarnedCoins()
- {
- earnedCoins = maxCoinsCount;
- UIManager.instance.UpdateEarnedCoins(earnedCoins, adCoinsCount);
- }
- public void RetryLevel()
- {
- currentPaper.UnfoldAllFoldings();
- }
- public async void SkipLevel()
- {
- level++;
- YanGamesSaveManager.SaveLevel(level);
- earnedCoins = maxCoinsCount;
- UIManager.instance.UpdateEarnedCoins(earnedCoins, adCoinsCount);
- await SpawnLevel();
- try
- {
- UIManager.instance.SetGame();
- }
- catch (System.Exception ex)
- {
- Debug.LogError(ex.Message);
- }
- YandexSDK.instance.ShowRewarded("SkipLevel");
- AppMetricaWeb.Event("skipAd");
- }
- private void DecreaseEarnedCoins()
- {
- if (earnedCoins <= minCoinsCount)
- return;
- earnedCoins -= takenCoinsCount;
- if (earnedCoins < minCoinsCount)
- earnedCoins = minCoinsCount;
- UIManager.instance?.UpdateEarnedCoins(earnedCoins, adCoinsCount);
- }
- private void OnOtherAdShown()
- {
- AppMetricaWeb.Event("otherAd");
- }
- }
- public class LoadedLevel
- {
- public int LevelIndex { get; private set; }
- public Paper PaperLevel { get; private set; }
- public LoadedLevel(int levelIndex, Paper paperLevel)
- {
- LevelIndex = levelIndex;
- PaperLevel = paperLevel;
- }
- }
|