LevelObjectiveUI.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using TMPro;
  5. using UnityEngine.UI;
  6. using ICTS.Localization;
  7. using DG.Tweening;
  8. using System.Collections.Generic;
  9. public class LevelObjectiveUI : MonoBehaviour
  10. {
  11. public TextMeshProUGUI title;
  12. public TextMeshProUGUI label;
  13. public Image[] icons;
  14. public Image objectiveMarkMet;
  15. public Image objectiveMarkNotMet;
  16. public SoundEvent objectiveMetSound;
  17. public SoundEvent objectiveNotMetSound;
  18. Vector3 _originalScaleMarkMet;
  19. Vector3 _originalScaleMarkNotMet;
  20. LevelObjective _levelObjective;
  21. const float VALUE_ANIM_TIME = 0.5f;
  22. void Awake()
  23. {
  24. _originalScaleMarkMet = objectiveMarkMet.transform.localScale;
  25. _originalScaleMarkNotMet = objectiveMarkNotMet.transform.localScale;
  26. }
  27. void OnEnable()
  28. {
  29. objectiveMarkMet.gameObject.SetActive(false);
  30. objectiveMarkNotMet.gameObject.SetActive(false);
  31. label.text = "";
  32. }
  33. public void SetObjective(LevelObjective objective)
  34. {
  35. _levelObjective = objective;
  36. int nextIcon = 0;
  37. for (int i = 0; i < icons.Length; i++)
  38. {
  39. icons[i].gameObject.SetActive(false);
  40. }
  41. //label.text = objective.target.ToString();
  42. switch (objective.type)
  43. {
  44. case LevelObjective.ObjectiveType.SurviveForXTime:
  45. title.text = Localization.instance.Get("objectives.surviveForXTime.title");
  46. objectiveMetSound = SoundEvent.scoreSummary_surviveMet;
  47. objectiveNotMetSound = SoundEvent.scoreSummary_surviveNotMet;
  48. break;
  49. case LevelObjective.ObjectiveType.CaptureXEnemies:
  50. title.text = Localization.instance.Get("objectives.captureXEnemies.title");
  51. //AVDebug.Assert(objective.enemyType != EnemyType.anyVirus, "We don't have an icon for this yet: "+objective.enemyType);
  52. //AVDebug.Assert(objective.enemyType != EnemyType.bronzeSperm, "We don't have an icon for this yet: "+objective.enemyType);
  53. //AVDebug.Assert(objective.enemyType != EnemyType.goldSperm, "We don't have an icon for this yet: "+objective.enemyType);
  54. //AVDebug.Assert(objective.enemyType != EnemyType.silverSperm, "We don't have an icon for this yet: "+objective.enemyType);
  55. SetNextIcon(ref nextIcon, "icon_"+objective.enemyType.ToString());
  56. if (objective.weaponType != PickupType.AnyWeapon)
  57. {
  58. SetNextIcon(ref nextIcon, "pickup"+objective.weaponType.ToString());
  59. }
  60. objectiveMetSound = SoundEvent.scoreSummary_captureMet;
  61. objectiveNotMetSound = SoundEvent.scoreSummary_captureNotMet;
  62. break;
  63. case LevelObjective.ObjectiveType.LimitedAmmo:
  64. title.text = Localization.instance.Get("objectives.limitedAmmo.title");
  65. //Not really required for limited ammo, since it's more of a restriction
  66. objectiveMetSound = SoundEvent.scoreSummary_targetMet;
  67. objectiveNotMetSound = SoundEvent.scoreSummary_targetNotMet;
  68. break;
  69. default:
  70. AVDebug.LogError("Objective type not being handled in UI: "+objective.type);
  71. break;
  72. }
  73. }
  74. void SetNextIcon(ref int nextIcon, string spriteName)
  75. {
  76. Image icon = icons[nextIcon];
  77. var list = MenuManager._instance.IconSprites;
  78. Debug.Log(spriteName);
  79. for (int i = 0; i < list.Count; i++)
  80. {
  81. if(spriteName == list[i].Texture.name)
  82. {
  83. icon.sprite = list[i].Texture;
  84. }
  85. }
  86. icon.gameObject.SetActive(true);
  87. icon.SetNativeSize();
  88. //Limit the size (especially done for pickups, since we are re-using the same graphics, which are too big for this
  89. int maxWidth = 67;
  90. if (icon.transform.localScale.x > maxWidth)
  91. {
  92. float factor = maxWidth / icon.transform.localScale.x;
  93. icon.transform.localScale *= factor;
  94. }
  95. ++nextIcon;
  96. }
  97. public void SetComplete(bool playSound)
  98. {
  99. if (playSound)
  100. {
  101. SoundManager.Play(objectiveMetSound);
  102. }
  103. objectiveMarkMet.gameObject.SetActive(true);
  104. objectiveMarkMet.transform.localScale = _originalScaleMarkMet*2;
  105. objectiveMarkMet.color = new Color(objectiveMarkMet.color.r, objectiveMarkMet.color.g, objectiveMarkMet.color.b, 0);
  106. objectiveMarkMet.DOFade(1,1);
  107. objectiveMarkMet.ScaleTo(_originalScaleMarkMet, 0.5f, ease:MyTween.Ease.easeOutBack);
  108. }
  109. public void SetNotComplete(bool playSound)
  110. {
  111. if (playSound)
  112. {
  113. SoundManager.Play(objectiveNotMetSound);
  114. }
  115. objectiveMarkNotMet.gameObject.SetActive(true);
  116. objectiveMarkNotMet.transform.localScale = _originalScaleMarkNotMet*2;
  117. objectiveMarkMet.color = new Color(objectiveMarkMet.color.r, objectiveMarkMet.color.g, objectiveMarkMet.color.b, 0);
  118. objectiveMarkMet.DOFade(1, 1);
  119. objectiveMarkNotMet.ScaleTo(_originalScaleMarkNotMet, 0.5f, ease:MyTween.Ease.easeOutBack);
  120. }
  121. public void UpdateStateForPauseScreen()
  122. {
  123. if (_levelObjective != null)
  124. {
  125. if (_levelObjective.IsMet)
  126. {
  127. SetComplete(playSound:false);
  128. } else
  129. {
  130. SetNotComplete(playSound:false);
  131. }
  132. }
  133. }
  134. public void UpdateStateForGameOverScreen()
  135. {
  136. if (_levelObjective == null)
  137. {
  138. return;
  139. }
  140. //don't do anything for limited ammo, as such it is just a restriction rather an objective
  141. if (_levelObjective.type == LevelObjective.ObjectiveType.LimitedAmmo)
  142. {
  143. return;
  144. }
  145. if (_levelObjective.IsMet)
  146. {
  147. SetComplete(playSound:true);
  148. } else
  149. {
  150. SetNotComplete(playSound:true);
  151. }
  152. }
  153. public int Target
  154. {
  155. get
  156. {
  157. if (_levelObjective != null)
  158. {
  159. return _levelObjective.target;
  160. }
  161. else
  162. {
  163. return 0;
  164. }
  165. }
  166. }
  167. public int Actual
  168. {
  169. get
  170. {
  171. if (_levelObjective != null)
  172. {
  173. return _levelObjective.Actual;
  174. }
  175. else
  176. {
  177. return 0;
  178. }
  179. }
  180. }
  181. public void SetTargetValue(int targetValue)
  182. {
  183. //Debug.Log("SetTargetValue");
  184. gameObject.SetActive(true);
  185. if (title.text == "Timi" || title.text == "Time")
  186. {
  187. UpdateLabelTimi(targetValue.ToString());
  188. }
  189. else
  190. {
  191. UpdateLabel(targetValue);
  192. }
  193. }
  194. public void SetProgressValue(int actualValue, string prefix, string suffix)
  195. {
  196. //Debug.Log("SetProgressValue");
  197. gameObject.SetActive(true);
  198. //Debug.Log(suffix);
  199. UpdateLabel(actualValue, prefix, suffix);
  200. }
  201. public void SetProgressValue(string actualValue, string prefix, string suffix)
  202. {
  203. //Debug.Log("SetProgressValue");
  204. gameObject.SetActive(true);
  205. //Debug.Log(suffix);
  206. UpdateLabel(actualValue, prefix, suffix);
  207. }
  208. public void AnimateValue(int actualValue, string prefix, string suffix, Action onComplete)
  209. {
  210. //Debug.Log("AnimateValue");
  211. gameObject.SetActive(true);
  212. if (title!= null)
  213. {
  214. if (title.text == "Timi" || title.text == "Time")
  215. {
  216. UpdateLabelTimi(suffix);
  217. }
  218. else
  219. {
  220. UpdateLabel(0, prefix, suffix);
  221. }
  222. }
  223. else
  224. {
  225. UpdateLabel(0, prefix, suffix);
  226. }
  227. AVDebug.LogWarning("Sound Active" + gameObject.name + " " + gameObject.transform.parent.parent.gameObject.name);
  228. if (gameObject.transform.parent.parent.gameObject.activeSelf)
  229. {
  230. SoundManager.Play(SoundEvent.scoreSummary_pointCounter);
  231. //MenuManager._instance.audioLoop.loop = false;
  232. }
  233. this.ValueTo(0, actualValue, VALUE_ANIM_TIME, (f) =>
  234. {
  235. if(prefix.Contains("Stig"))
  236. {
  237. char[] chars = ((int)f).ToString().ToCharArray();
  238. Array.Reverse(chars);
  239. string newValue = "";
  240. var count = 1;
  241. for (int i = 0; i < chars.Length; i++)
  242. {
  243. if (count % 3 == 0 && chars.Length>3)
  244. {
  245. newValue = newValue + chars[i] + ".";
  246. }
  247. else
  248. {
  249. newValue = newValue + chars[i];
  250. }
  251. count++;
  252. }
  253. chars = newValue.ToCharArray();
  254. Array.Reverse(chars);
  255. newValue = "";
  256. for (int i = 0; i < chars.Length; i++)
  257. {
  258. newValue = newValue + chars[i];
  259. }
  260. UpdateLabel(newValue, prefix, suffix);
  261. }
  262. else
  263. {
  264. if (title != null)
  265. {
  266. if (title.text == "Timi" || title.text == "Time")
  267. {
  268. UpdateLabelTimi(suffix);
  269. }
  270. else
  271. {
  272. UpdateLabel((int)f, prefix, suffix);
  273. }
  274. }
  275. else
  276. {
  277. UpdateLabel((int)f, prefix, suffix);
  278. }
  279. }
  280. }, OnComplete: () =>
  281. {
  282. AVDebug.LogWarning("Sound OFF");
  283. //SoundManager.StopLoop(SoundEvent.scoreSummary_pointCounter);
  284. //MenuManager._instance.audioLoop.loop = false;
  285. if (onComplete != null)
  286. {
  287. onComplete();
  288. }
  289. });
  290. }
  291. void UpdateLabel(int value, string prefix = "", string suffix = "")
  292. {
  293. label.text = prefix + " " +value + " " +suffix;
  294. //Debug.Log("suffix " + suffix + " " + value);
  295. }
  296. void UpdateLabel(string value, string prefix = "", string suffix = "")
  297. {
  298. //Debug.Log(value + " " + prefix + " " + suffix);
  299. label.text = prefix + " " + value + " " + suffix;
  300. //Debug.Log("suffix " + suffix + " " + value);
  301. }
  302. void UpdateLabelTimi(string suffix = "")
  303. {
  304. //Debug.Log(suffix + " sekúndur");
  305. label.text = string.Format(Localization.instance.Get("time.text"), suffix);
  306. //Debug.Log("suffix " + suffix + " " + value);
  307. }
  308. }