ObjectivesDialog.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System;
  2. using Prime31;
  3. using UnityEngine;
  4. using System.Collections;
  5. using TMPro;
  6. using ICTS.Localization;
  7. public class ObjectivesDialog : ScreenBase
  8. {
  9. public TextMeshProUGUI targetScore;
  10. public bool ScreenPaused;
  11. public LevelObjectiveUI targetObjective;
  12. public LevelObjectiveUI[] objectives;
  13. public TextMeshProUGUI Title;
  14. public StarsSummary starsSummary;
  15. private GameOverScreenTitle _gameOverScreenTitle;
  16. LevelMapping _level;
  17. Vector3[] _originalObjectivePositions;
  18. protected override void Awake()
  19. {
  20. base.Awake();
  21. _originalObjectivePositions = new Vector3[objectives.Length];
  22. for (int i = 0; i < objectives.Length; i++)
  23. {
  24. _originalObjectivePositions[i] = objectives[i].transform.localPosition;
  25. }
  26. }
  27. void OnEnable()
  28. {
  29. for (int i = 0; i < objectives.Length; i++)
  30. {
  31. objectives[i].transform.localPosition = _originalObjectivePositions[i];
  32. }
  33. if(!ScreenPaused)
  34. {
  35. Title.text = string.Format(Localization.instance.Get("scores.levelGlobalTitle"),
  36. LevelsManager.Instance.CurrentLevelIndex+1);
  37. }
  38. }
  39. void completionHandler(string error, object result)
  40. {
  41. if (error != null)
  42. Debug.LogError(error);
  43. else
  44. Prime31.Utils.logObject(result);
  45. }
  46. public void SetObjectives(LevelMapping level, bool showAmmoConstraint)
  47. {
  48. //position according to number of objectives, so they appear centered
  49. AVDebug.Assert(objectives.Length == 3, "Centering of objectives is assuming there will be max of 3 objectives");
  50. int numOfLevelObjectives = level.objectives.Length;
  51. //If we don't show the ammo constraint, then just reduce the number of level objectives being shown to the player, as that ammo is not really an objective.
  52. if (!showAmmoConstraint)
  53. {
  54. for (int i = 0; i < numOfLevelObjectives; i++)
  55. {
  56. if (level.objectives[i].type == LevelObjective.ObjectiveType.LimitedAmmo)
  57. {
  58. --numOfLevelObjectives;
  59. }
  60. }
  61. }
  62. if (numOfLevelObjectives == 1)
  63. {
  64. //put it in the center
  65. objectives[0].transform.localPosition = _originalObjectivePositions[1];
  66. } else
  67. if (numOfLevelObjectives == 2)
  68. {
  69. //find mid points, and move the first 2 objectives
  70. float x = (_originalObjectivePositions[0].x + _originalObjectivePositions[1].x)/2;
  71. objectives[0].transform.localPosition = new Vector3(x, _originalObjectivePositions[0].y, _originalObjectivePositions[0].z);
  72. x = (_originalObjectivePositions[1].x + _originalObjectivePositions[2].x)/2;
  73. objectives[1].transform.localPosition = new Vector3(x, _originalObjectivePositions[1].y, _originalObjectivePositions[1].z);
  74. }
  75. _level = level;
  76. char[] chars = level.star1Threshold.ToString().ToCharArray();
  77. Array.Reverse(chars);
  78. string newValue = "";
  79. var count = 1;
  80. for (int i = 0; i < chars.Length; i++)
  81. {
  82. if (count % 3 == 0 && chars.Length > 3)
  83. {
  84. //Debug.Log("DOT " + i + " " + chars.Length);
  85. newValue = newValue + chars[i] + ".";
  86. }
  87. else
  88. {
  89. newValue = newValue + chars[i];
  90. }
  91. count++;
  92. }
  93. chars = newValue.ToCharArray();
  94. Array.Reverse(chars);
  95. newValue = "";
  96. for (int i = 0; i < chars.Length; i++)
  97. {
  98. newValue = newValue + chars[i];
  99. }
  100. targetScore.text = Localization.instance.Get("scoreSummary.target.prefix") + " " + newValue + " " + Localization.instance.Get("scoreSummary.target.points");
  101. AVDebug.Assert(numOfLevelObjectives <= objectives.Length, "We don't have enough UI elements to show the level objects set for this level "+level.levelName);
  102. int objectiveUIIndex = 0;
  103. for (int i = 0; i < level.objectives.Length; i++)
  104. {
  105. //Skip limited ammo
  106. if (!showAmmoConstraint &&
  107. level.objectives[i].type == LevelObjective.ObjectiveType.LimitedAmmo)
  108. {
  109. continue;
  110. }
  111. objectives[objectiveUIIndex].gameObject.SetActive(true);
  112. objectives[objectiveUIIndex].SetObjective(level.objectives[i]);
  113. objectiveUIIndex++;
  114. }
  115. //disable the others
  116. for (int i = numOfLevelObjectives; i < objectives.Length; i++)
  117. {
  118. objectives[i].gameObject.SetActive(false);
  119. }
  120. }
  121. public void SetupForPreLevelObjectivesSummary()
  122. {
  123. for (int i = 0; i < _level.objectives.Length; i++)
  124. {
  125. objectives[i].SetTargetValue(objectives[i].Target);
  126. }
  127. }
  128. IEnumerator ResultSave()
  129. {
  130. yield return new WaitForSeconds(0.1f);
  131. CurrentResult();
  132. yield return new WaitForSeconds(4);
  133. }
  134. public bool CurrentResult()
  135. {
  136. int score = GameDataManager.Instance.Score;
  137. int curTargetFinish = 0;
  138. if (score >= _level.star1Threshold)
  139. {
  140. for (int i = 0; i < _level.objectives.Length; i++)
  141. {
  142. var currentTarget1 = _level.objectives[i].Actual;
  143. var target1 = _level.objectives[i].target;
  144. if (currentTarget1 == target1 )
  145. {
  146. Debug.Log(currentTarget1 == target1);
  147. curTargetFinish++;
  148. }
  149. }
  150. Debug.Log(PlayerPrefs.GetInt("Level"));
  151. if (curTargetFinish == _level.objectives.Length)
  152. {
  153. var curLEvel = PlayerPrefs.GetInt("Level");
  154. if (LevelsManager.Instance.CurrentLevelIndex + 1 == curLEvel)
  155. {
  156. if (curLEvel<=6)
  157. {
  158. Debug.Log("!!! curLEvel " + curLEvel);
  159. curLEvel = curLEvel + 1;
  160. var post = PlayerPrefs.GetInt("Post");
  161. if (curLEvel == 7 && post == 0)
  162. {
  163. //Debug.Log("Facebook.instance.postMessageWithLinkAndLinkToImage");
  164. //MenuManager._instance.FBShareDialog.Show();
  165. }
  166. PlayerPrefs.SetInt("Level", curLEvel);
  167. PlayerPrefs.Save();
  168. Debug.LogError(curLEvel);
  169. //if (curLEvel == 3)
  170. //{
  171. // MenuManager._instance.RescueMomentMessage.Show();
  172. //}
  173. if (curLEvel == 1 || curLEvel == 3 || curLEvel == 6 )
  174. {
  175. //Vungle.playAd();
  176. MenuManager._instance.CurState = MenuManager.StateAd.GameOver;
  177. }
  178. if (curLEvel == 2 || curLEvel == 5)
  179. {
  180. //MoPub.showBanner(true);
  181. }
  182. //if (curLEvel == 6)
  183. //{
  184. // MenuManager._instance.OnActionLevelSixStart();
  185. //}
  186. if (curLEvel == 6)
  187. {
  188. MenuManager._instance.OnActionLevelNineStart();
  189. }
  190. }
  191. }
  192. var curLevel1 = PlayerPrefs.GetInt("Level");
  193. //if (/*LevelsManager.Instance.CurrentLevelIndex <= curLevel1 - 1 &&*/ LevelsManager.Instance.CurrentLevelIndex + 1 <= 9)
  194. //{
  195. // LevelsManager.Instance.CurrentLevelIndex++;
  196. //}
  197. //if (LevelsManager.Instance.CurrentLevelIndex <= curLevel1 - 1 && LevelsManager.Instance.CurrentLevelIndex+1<=5)
  198. //{
  199. // LevelsManager.Instance.CurrentLevelIndex++;
  200. //}
  201. MenuManager._instance.Finish = true;
  202. GetComponent<GameOverScreenTitle>().OnActive(true);
  203. return true;
  204. }
  205. else
  206. {
  207. MenuManager._instance.Finish = false;
  208. GetComponent<GameOverScreenTitle>().OnActive(true);
  209. return false;
  210. }
  211. }
  212. else
  213. {
  214. MenuManager._instance.Finish = false;
  215. GetComponent<GameOverScreenTitle>().OnActive(true);
  216. return false;
  217. }
  218. }
  219. public void UpdateResult()
  220. {
  221. if(targetObjective.objectiveMarkMet.gameObject.activeSelf)
  222. {
  223. for (int i = 0; i < _level.objectives.Length; i++)
  224. {
  225. if(objectives[i].objectiveMarkMet.gameObject.activeSelf)
  226. {
  227. }
  228. else
  229. {
  230. //var curLevel = PlayerPrefs.GetInt("Level");
  231. //if (LevelsManager.Instance.CurrentLevelIndex <= curLevel - 1)
  232. //{
  233. // LevelsManager.Instance.CurrentLevelIndex++;
  234. //}
  235. return;
  236. }
  237. }
  238. }
  239. else
  240. {
  241. return;
  242. }
  243. var curLEvel = PlayerPrefs.GetInt("Level");
  244. if (LevelsManager.Instance.CurrentLevelIndex + 1 == curLEvel)
  245. {
  246. curLEvel = curLEvel + 1;
  247. Debug.Log(curLEvel);
  248. //PlayerPrefs.SetInt("Level", curLEvel);
  249. if (curLEvel == 1 || curLEvel == 3 || curLEvel == 6)
  250. {
  251. //Vungle.playAd();
  252. MenuManager._instance.CurState = MenuManager.StateAd.GameOver;
  253. }
  254. if (curLEvel == 2 || curLEvel == 5 || curLEvel == 4 )
  255. {
  256. //MoPub.showBanner(true);
  257. }
  258. }
  259. var curLevel1 = PlayerPrefs.GetInt("Level");
  260. //if (LevelsManager.Instance.CurrentLevelIndex <= curLevel1 - 1 && LevelsManager.Instance.CurrentLevelIndex<5)
  261. //{
  262. // LevelsManager.Instance.CurrentLevelIndex++;
  263. //
  264. //}
  265. }
  266. public void UpdateObjectivesStateForPauseScreen()
  267. {
  268. int score = GameDataManager.Instance.Score;
  269. int target = _level.star1Threshold;
  270. string targetPrefix = Localization.instance.Get("scoreSummary.target.prefix");
  271. string targetSuffix = Localization.instance.Get("scoreSummary.target.points");
  272. char[] chars =target.ToString().ToCharArray();
  273. Array.Reverse(chars);
  274. string newValue = "";
  275. var count = 1;
  276. for (int i = 0; i < chars.Length; i++)
  277. {
  278. if (count % 3 == 0 && chars.Length > 3)
  279. {
  280. newValue = newValue + chars[i] + ".";
  281. }
  282. else
  283. {
  284. newValue = newValue + chars[i];
  285. }
  286. count++;
  287. }
  288. chars = newValue.ToCharArray();
  289. Array.Reverse(chars);
  290. newValue = "";
  291. for (int i = 0; i < chars.Length; i++)
  292. {
  293. newValue = newValue + chars[i];
  294. }
  295. targetObjective.SetProgressValue(newValue, targetPrefix, targetSuffix);
  296. if (score >= target)
  297. {
  298. targetObjective.SetComplete(playSound:false);
  299. } else
  300. {
  301. targetObjective.SetNotComplete(playSound:false);
  302. }
  303. int objectiveUIIndex = 0;
  304. for (int i = 0; i < _level.objectives.Length; i++)
  305. {
  306. //Skip limited ammo for pause
  307. if (_level.objectives[i].type == LevelObjective.ObjectiveType.LimitedAmmo)
  308. {
  309. continue;
  310. }
  311. LevelObjectiveUI objective = objectives[objectiveUIIndex++];
  312. if (objective.title.text == "Timi" || objective.title.text == "Time")
  313. {
  314. objective.SetTargetValue(objective.Target);
  315. objective.UpdateStateForPauseScreen();
  316. }
  317. else
  318. {
  319. string objectivePrefix = string.Empty;
  320. string objectiveSuffix = "/" + objective.Target;
  321. objective.SetProgressValue(objective.Actual, objectivePrefix, objectiveSuffix);
  322. objective.UpdateStateForPauseScreen();
  323. }
  324. }
  325. }
  326. public void UpdateObjectivesStateForGameOverScreen()
  327. {
  328. StartCoroutine(UpdateObjectivesStateForGameOverScreenCoroutine());
  329. StartCoroutine(ResultSave());
  330. }
  331. IEnumerator UpdateObjectivesStateForGameOverScreenCoroutine()
  332. {
  333. yield return new WaitForSeconds(0.5f);
  334. //Start with introducing target score and objectives
  335. int score = GameDataManager.Instance.Score;
  336. string scorePrefix = Localization.instance.Get("scoreSummary.score.prefix");
  337. string scoreSuffix = string.Empty;
  338. targetObjective.AnimateValue(score, scorePrefix, scoreSuffix, () =>
  339. {
  340. IntroduceNextObjective(0);
  341. });
  342. }
  343. void IntroduceNextObjective(int i)
  344. {
  345. if (i < _level.objectives.Length)
  346. {
  347. //Skip limited ammo for game over
  348. if (_level.objectives[i].type == LevelObjective.ObjectiveType.LimitedAmmo)
  349. {
  350. IntroduceNextObjective(i+1);
  351. return;
  352. }
  353. LevelObjectiveUI objective = objectives[i];
  354. string objectivePrefix = "";
  355. string objectiveSuffix = "";
  356. if(objective.title.text == "Timi" || objective.title.text == "Time")
  357. {
  358. objectivePrefix = string.Empty;
  359. objectiveSuffix = objective.Target.ToString();
  360. }
  361. else
  362. {
  363. objectivePrefix = string.Empty;
  364. objectiveSuffix = "/" + objective.Target;
  365. }
  366. objective.AnimateValue(objective.Actual, objectivePrefix, objectiveSuffix, () =>
  367. {
  368. IntroduceNextObjective(i+1);
  369. });
  370. } else
  371. {
  372. StartCoroutine(ShowMarkingsCoroutine());
  373. }
  374. }
  375. IEnumerator ShowMarkingsCoroutine()
  376. {
  377. yield return new WaitForSeconds(0.3f);
  378. int score = GameDataManager.Instance.Score;
  379. if (score >= _level.star1Threshold)
  380. {
  381. targetObjective.SetComplete(playSound:true);
  382. } else
  383. {
  384. targetObjective.SetNotComplete(playSound:true);
  385. }
  386. yield return new WaitForSeconds(0.5f);
  387. for (int i = 0; i < _level.objectives.Length; i++)
  388. {
  389. LevelObjectiveUI objective = objectives[i];
  390. objective.UpdateStateForGameOverScreen();
  391. yield return new WaitForSeconds(0.5f);
  392. }
  393. yield return new WaitForSeconds(0.5f);
  394. starsSummary.AnimateStars();
  395. }
  396. }