LevelManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using Cysharp.Threading.Tasks;
  3. using Eiko.YaSDK;
  4. using JTSystems;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using UnityEngine.AddressableAssets;
  9. using UnityEngine.ResourceManagement.AsyncOperations;
  10. using Random = UnityEngine.Random;
  11. public class LevelManager : MonoBehaviour
  12. {
  13. public const int SCALE_EARNNED_COINS_WITH_AD_VALUE = 3;
  14. public delegate void OnPaperInstantiated(Paper paper);
  15. public static OnPaperInstantiated onPaperInstantiated;
  16. public delegate void OnThemeUnlocked(ThemeData themeData, int themeUnlockLevelStep = 0);
  17. public static OnThemeUnlocked themeUnlocked;
  18. public delegate void ThemeUnlockProgressUpdated(int themeUnlockProgress, int themeUnlcokLevleStep);
  19. public static ThemeUnlockProgressUpdated themeUnlockProgressUpdated;
  20. [Header(" Settings ")] [SerializeField]
  21. private int adCoinsCount = 45;
  22. [SerializeField] private int maxCoinsCount = 15;
  23. [SerializeField] private int minCoinsCount = 5;
  24. [Min(1)] [SerializeField] private int takenCoinsCount = 5;
  25. [Min(1)] [SerializeField] private int unlockThemeLevelStep;
  26. [SerializeField] private AssetReference[] papers;
  27. [SerializeField] private ThemeData[] unlockableThemes;
  28. private Paper currentPaper;
  29. private int level;
  30. private int earnedCoins;
  31. private readonly Queue<ThemeData> unlockableThemesQueue = new Queue<ThemeData>();
  32. private int currentLoadedLevelIndex = -1;
  33. private int nextLoadedLevelIndex = -1;
  34. private void Awake()
  35. {
  36. level = YanGamesSaveManager.GetLevel();
  37. if (YandexSDK.instance != null)
  38. YandexSDK.instance.onInterstitialShown += OnOtherAdShown;
  39. UIManager.onNextLevelButtonPressed += SpawnNextLevel;
  40. UIManager.onNextLevelButtonPressedWithAd += SpawnNextLevelWithAdditionalCoins;
  41. UIManager.wrongPaperFolded += DecreaseEarnedCoins;
  42. UIManager.onLevelCompleteSet += IncrementThemeUnlockProgress;
  43. foreach (ThemeData unlockableTheme in unlockableThemes)
  44. {
  45. if (YanGamesSaveManager.HasUnlokedTheme(unlockableTheme.Id))
  46. continue;
  47. unlockableThemesQueue.Enqueue(unlockableTheme);
  48. }
  49. }
  50. private void OnDestroy()
  51. {
  52. UIManager.onNextLevelButtonPressed -= SpawnNextLevel;
  53. UIManager.onNextLevelButtonPressedWithAd -= SpawnNextLevelWithAdditionalCoins;
  54. UIManager.wrongPaperFolded -= DecreaseEarnedCoins;
  55. UIManager.onLevelCompleteSet -= IncrementThemeUnlockProgress;
  56. }
  57. private async void Start()
  58. {
  59. UpdateEarnedCoins();
  60. await SpawnLevel();
  61. StartUp.EndLoad();
  62. try
  63. {
  64. UIManager.instance.SetMenu();
  65. }
  66. catch (Exception ex)
  67. {
  68. Debug.LogError(ex.Message);
  69. }
  70. YandexSDK.instance.ShowInterstitial();
  71. }
  72. #if UNITY_EDITOR
  73. private void Update()
  74. {
  75. if (Input.GetKeyDown(KeyCode.C))
  76. SpawnNextLevel().Forget();
  77. }
  78. #endif
  79. private async UniTask SpawnNextLevelWithAdditionalCoins()
  80. {
  81. earnedCoins = adCoinsCount;
  82. await SpawnNextLevel();
  83. AppMetricaWeb.Event("45coinsAd");
  84. }
  85. private void IncrementThemeUnlockProgress(int starsCount)
  86. {
  87. if (unlockableThemesQueue.Count <= 0)
  88. return;
  89. if (YanGamesSaveManager.HasUnlokedTheme(unlockableThemesQueue.Peek().Id))
  90. {
  91. ChangeUnlockableTheme();
  92. IncrementThemeUnlockProgress(starsCount);
  93. return;
  94. }
  95. int unlockThemeProgress = YanGamesSaveManager.GetUnlockThemeProgress();
  96. unlockThemeProgress++;
  97. if (unlockThemeProgress >= unlockThemeLevelStep)
  98. {
  99. themeUnlocked?.Invoke(unlockableThemesQueue.Peek(), unlockThemeLevelStep);
  100. YanGamesSaveManager.AddUnlockedTheme(unlockableThemesQueue.Peek().Id);
  101. UIManager.instance?.THEMES.ChangeTheme(unlockableThemesQueue.Peek());
  102. ChangeUnlockableTheme();
  103. YanGamesSaveManager.SetUnlockThemeProgress(0);
  104. }
  105. else
  106. {
  107. themeUnlockProgressUpdated?.Invoke(unlockThemeProgress, unlockThemeLevelStep);
  108. YanGamesSaveManager.SetUnlockThemeProgress(unlockThemeProgress);
  109. }
  110. }
  111. private void ChangeUnlockableTheme()
  112. {
  113. unlockableThemesQueue.Dequeue();
  114. }
  115. public async UniTask SpawnLevel()
  116. {
  117. transform.Clear();
  118. // Просчитали индекс уровня
  119. int correctedLevelIndex = level;
  120. if (level > papers.Length - 1)
  121. correctedLevelIndex = Random.Range(0, papers.Length - 1);
  122. else
  123. AppMetricaWeb.Event($"lvl{level + 1}");
  124. // Выгружаем текущий левел если он есть и не равен
  125. if (currentLoadedLevelIndex > -1 && nextLoadedLevelIndex > -1 &&
  126. currentLoadedLevelIndex != nextLoadedLevelIndex)
  127. {
  128. UnloadLevelPaper(currentLoadedLevelIndex);
  129. currentLoadedLevelIndex = nextLoadedLevelIndex;
  130. }
  131. // Загружаем текущий если его нету
  132. if (currentLoadedLevelIndex <= -1)
  133. {
  134. currentLoadedLevelIndex = correctedLevelIndex;
  135. }
  136. Paper levelPrefab = await LoadPaperLevel(currentLoadedLevelIndex);
  137. if (levelPrefab == null)
  138. {
  139. throw new InvalidOperationException($"Loading of level with index {currentLoadedLevelIndex}");
  140. }
  141. currentPaper = Instantiate(levelPrefab, transform);
  142. onPaperInstantiated?.Invoke(currentPaper);
  143. // Загружаем в фоне следующий
  144. LoadNextLevel(level + 1).Forget();
  145. }
  146. public async UniTask LoadNextLevel(int level)
  147. {
  148. int correctedLevelIndex = level;
  149. if (level > papers.Length - 1)
  150. correctedLevelIndex = Random.Range(0, papers.Length - 1);
  151. if (correctedLevelIndex == currentLoadedLevelIndex)
  152. {
  153. nextLoadedLevelIndex = currentLoadedLevelIndex;
  154. }
  155. else
  156. {
  157. await LoadPaperLevel(correctedLevelIndex);
  158. nextLoadedLevelIndex = correctedLevelIndex;
  159. }
  160. }
  161. private async UniTask<Paper> LoadPaperLevel(int level)
  162. {
  163. GameObject paperGameObject = null;
  164. if (papers[level].Asset is GameObject)
  165. paperGameObject = papers[level].Asset as GameObject;
  166. if (paperGameObject == null)
  167. {
  168. AsyncOperationHandle<GameObject> paperHandler = papers[level].LoadAssetAsync<GameObject>();
  169. paperGameObject = await paperHandler.Task;
  170. }
  171. return paperGameObject.GetComponent<Paper>();
  172. }
  173. private void UnloadLevelPaper(int level)
  174. {
  175. papers[level].ReleaseAsset();
  176. }
  177. private async UniTask SpawnNextLevel()
  178. {
  179. UIManager.AddCoins(earnedCoins);
  180. level++;
  181. YanGamesSaveManager.SaveLevel(level);
  182. UpdateEarnedCoins();
  183. await SpawnLevel();
  184. try
  185. {
  186. UIManager.instance.SetGame();
  187. }
  188. catch (System.Exception ex)
  189. {
  190. Debug.LogError(ex.Message);
  191. }
  192. }
  193. private void UpdateEarnedCoins()
  194. {
  195. earnedCoins = maxCoinsCount;
  196. UIManager.instance.UpdateEarnedCoins(earnedCoins, adCoinsCount);
  197. }
  198. public void RetryLevel()
  199. {
  200. currentPaper.UnfoldAllFoldings();
  201. }
  202. public async void SkipLevel()
  203. {
  204. level++;
  205. YanGamesSaveManager.SaveLevel(level);
  206. earnedCoins = maxCoinsCount;
  207. UIManager.instance.UpdateEarnedCoins(earnedCoins, adCoinsCount);
  208. await SpawnLevel();
  209. try
  210. {
  211. UIManager.instance.SetGame();
  212. }
  213. catch (System.Exception ex)
  214. {
  215. Debug.LogError(ex.Message);
  216. }
  217. YandexSDK.instance.ShowRewarded("SkipLevel");
  218. AppMetricaWeb.Event("skipAd");
  219. }
  220. private void DecreaseEarnedCoins()
  221. {
  222. if (earnedCoins <= minCoinsCount)
  223. return;
  224. earnedCoins -= takenCoinsCount;
  225. if (earnedCoins < minCoinsCount)
  226. earnedCoins = minCoinsCount;
  227. UIManager.instance?.UpdateEarnedCoins(earnedCoins, adCoinsCount);
  228. }
  229. private void OnOtherAdShown()
  230. {
  231. AppMetricaWeb.Event("otherAd");
  232. }
  233. }
  234. public class LoadedLevel
  235. {
  236. public int LevelIndex { get; private set; }
  237. public Paper PaperLevel { get; private set; }
  238. public LoadedLevel(int levelIndex, Paper paperLevel)
  239. {
  240. LevelIndex = levelIndex;
  241. PaperLevel = paperLevel;
  242. }
  243. }