HUDControllerCS.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * FUNCTION:
  3. * - Controls the HUD display which includes the score, currency and distance notifications.
  4. *
  5. * USED BY: This script is a part of the "Player" prefab.
  6. *
  7. */
  8. using UnityEngine;
  9. using System.Collections;
  10. public class HUDControllerCS : MonoBehaviour {
  11. private GameObject goHUDGroup;
  12. //script references
  13. private InGameScriptCS hInGameScriptCS;
  14. private MissionsControllerCS hMissionsControllerCS;
  15. private GlobalAchievementControllerCS hGlobalAchievementControllerCS;
  16. private PowerupsMainControllerCS hPowerupsMainControllerCS;
  17. private ControllerScriptCS hControllerScriptCS;
  18. //HUD componenets for custom menu
  19. private TextMesh tmHUDCurrencyText;
  20. private TextMesh tmHUDScoreText;
  21. private Transform tHUDScoreContainerMid;
  22. private Transform tHUDCurrencyContainerMid;
  23. //HUD components for NGUI menu
  24. private UILabel uilScoreText;
  25. private UILabel uilCurrencyText;
  26. private UILabel uilDistanceNotif;
  27. private UILabel uilMissionDescription;
  28. //Calculate Score
  29. private float fPreviousDistance = 0.0f; //mileage in the last frame
  30. private float fCurrentDistance = 0.0f; //mileage in the current frame
  31. private float fCurrentTime = 0.0f;
  32. private float fPreviousTime = 0.0f;
  33. //Distance covered notification
  34. private float fDistanceNotification = 500;//distance after which notification will be shown
  35. private int iDistanceNotifState = 0;
  36. private Transform tDistanceNotification;
  37. private TextMesh tmDistanceNotif;
  38. //HUD element Container sizes
  39. private int iDivisorScore;
  40. private int iDivisorCurrency;
  41. private int iDivisorMultiplier;
  42. //HUD mission description drop down
  43. private Transform tMissionDropDown;
  44. private TextMesh missionDescription;
  45. void Start()
  46. {
  47. hInGameScriptCS = (InGameScriptCS)GameObject.Find("Player").GetComponent(typeof(InGameScriptCS));
  48. hControllerScriptCS = (ControllerScriptCS)GameObject.Find("Player").GetComponent(typeof(ControllerScriptCS));
  49. hMissionsControllerCS = (MissionsControllerCS)GameObject.Find("Player").GetComponent(typeof(MissionsControllerCS));
  50. hGlobalAchievementControllerCS = (GlobalAchievementControllerCS)GameObject.Find("Player").GetComponent(typeof(GlobalAchievementControllerCS));
  51. hPowerupsMainControllerCS = (PowerupsMainControllerCS)GameObject.Find("Player").GetComponent(typeof(PowerupsMainControllerCS));
  52. tMissionDropDown = this.transform.Find("HUDGroup/MissionNotifier");
  53. missionDescription = tMissionDropDown.Find("Text_MissionDescription").GetComponent("TextMesh") as TextMesh;
  54. tmHUDCurrencyText = GameObject.Find("HUDMainGroup/HUDGroup/HUDCurrencyGroup/HUD_Currency_Text").GetComponent("TextMesh") as TextMesh;
  55. tmHUDScoreText = GameObject.Find("HUDMainGroup/HUDGroup/HUDScoreGroup/HUD_Score_Text").GetComponent("TextMesh") as TextMesh;
  56. tHUDScoreContainerMid = (Transform)GameObject.Find("HUDMainGroup/HUDGroup/HUDScoreGroup/HUD_Score_BG").GetComponent(typeof(Transform)); // HUD Score Container
  57. tHUDCurrencyContainerMid = (Transform)GameObject.Find("HUDMainGroup/HUDGroup/HUDCurrencyGroup/HUD_Currency_BG").GetComponent(typeof(Transform)); // HUD Currency Container
  58. tHUDScoreContainerMid.localScale = new Vector3(tHUDScoreContainerMid.localScale.x, tHUDScoreContainerMid.localScale.y, 0.45f);
  59. tHUDCurrencyContainerMid.localScale = new Vector3(tHUDCurrencyContainerMid.localScale.x, tHUDCurrencyContainerMid.localScale.y, 0.45f);
  60. //Distance Notification
  61. tmDistanceNotif = GameObject.Find("HUDMainGroup/HUDGroup/DistanceNotifier/Text_Distance").GetComponent("TextMesh") as TextMesh;
  62. tDistanceNotification = (Transform)GameObject.Find("HUDMainGroup/HUDGroup/DistanceNotifier").GetComponent(typeof(Transform));
  63. tDistanceNotification.gameObject.SetActive(false);
  64. //get time difference to calculate score
  65. fCurrentTime = Time.time;
  66. fPreviousTime = Time.time;
  67. fPreviousDistance = 0;
  68. fCurrentDistance = 0;
  69. fCurrentTime = 0;
  70. fPreviousTime = 0;
  71. iDivisorScore = 10;
  72. iDivisorCurrency = 10;
  73. iDivisorMultiplier = 10;
  74. //call the resize Dight Container function every .5 seconds
  75. InvokeRepeating("resizeDigitContainer", 1, 0.5f);
  76. resizeDigitContainer();
  77. }
  78. void FixedUpdate()
  79. {
  80. if(hInGameScriptCS.isGamePaused()==true)
  81. return;
  82. UpdateHUDStats();
  83. //show distance notification after covering 500 meters
  84. if (iDistanceNotifState == 0
  85. && (Mathf.Round(fCurrentDistance) % fDistanceNotification) == 0
  86. && fCurrentDistance != 0)
  87. {
  88. StartCoroutine("displayDistanceNotificaiton");
  89. }
  90. }//end of Update
  91. /*
  92. * FUNCTION: The score is calculated and added up in Level_Score variable
  93. * CALLED BY: FixedUpdate()
  94. */
  95. private void UpdateHUDStats()
  96. {
  97. //skip time and check the difference in milage in the duration
  98. if ( (fCurrentTime - fPreviousTime) >= 0.1f )
  99. {
  100. //calculate the score
  101. float iCurrentFrameScore = (fCurrentDistance - fPreviousDistance);
  102. //hInGameScriptCS.incrementLevelScore((int)iCurrentFrameScore);
  103. //hMissionsControllerCS.incrementMissionCount(MissionsControllerCS.MissionTypes.Score, (int)iCurrentFrameScore);//mission score counter
  104. //hGlobalAchievementControllerCS.incrementAchievementCount(GlobalAchievementControllerCS.GlobalAchievementTypes.Score, (int)iCurrentFrameScore);//global achievements counter
  105. fPreviousDistance = fCurrentDistance;
  106. fCurrentDistance = hControllerScriptCS.getCurrentMileage();
  107. hMissionsControllerCS.incrementMissionCount(MissionsControllerCS.MissionTypes.Distance);//mission milage counter
  108. hGlobalAchievementControllerCS.incrementAchievementCount(GlobalAchievementControllerCS.GlobalAchievementTypes.Distance);//global achievements counter
  109. fPreviousTime = fCurrentTime;
  110. fCurrentTime = Time.time;
  111. }
  112. else
  113. {
  114. fCurrentDistance = hControllerScriptCS.getCurrentMileage(); //get the current mileage
  115. fCurrentTime = Time.time;
  116. }
  117. //display the currency and score on the HUD
  118. tmHUDCurrencyText.text = hPowerupsMainControllerCS.getCurrencyUnits().ToString(); //update Currency on HUD
  119. tmHUDScoreText.text = hInGameScriptCS.getLevelScore().ToString(); //update Score on HUD
  120. }//end of Update HUD Stats function
  121. /*
  122. * FUNCTION: Show the distance covered in meters after every 'x' meters
  123. * defined by fNotificationDistance.
  124. * CALLED BY: FixedUpdate()
  125. */
  126. private IEnumerator displayDistanceNotificaiton()
  127. {
  128. while (true)
  129. {
  130. yield return new WaitForFixedUpdate();
  131. if (iDistanceNotifState == 0)//enable and update component
  132. {
  133. NGUITools.SetActive(tDistanceNotification.gameObject, true);
  134. tmDistanceNotif.text = Mathf.Round(fCurrentDistance).ToString();
  135. tDistanceNotification.localScale = new Vector3(0,0,0);
  136. iDistanceNotifState = 1;
  137. }
  138. else if (iDistanceNotifState == 1)//display the component in front of camera
  139. {
  140. tDistanceNotification.localScale = Vector3.Lerp(tDistanceNotification.localScale, new Vector3(1.79f,1.79f,1), Time.deltaTime*2.5f);
  141. if (tDistanceNotification.localScale.x >= 1.65f)
  142. iDistanceNotifState = 2;
  143. }
  144. else if (iDistanceNotifState == 2)//hide the component
  145. {
  146. NGUITools.SetActive(tDistanceNotification.gameObject, false);
  147. iDistanceNotifState = 3;
  148. break;
  149. }
  150. else if (iDistanceNotifState == 3)//stop the coroutine
  151. {
  152. StopCoroutine("displayDistanceNotificaiton");
  153. iDistanceNotifState = 0;
  154. break;
  155. }
  156. }//end of while
  157. }
  158. /*
  159. * FUNCTION: Display the drop down with the completed mission description
  160. */
  161. private int missionDropDownState = -1;
  162. public IEnumerator displayMissionDescriptionDropDown(string description)
  163. {
  164. missionDropDownState = 0;
  165. missionDescription.text = description;
  166. while (true)
  167. {
  168. yield return new WaitForFixedUpdate();
  169. if (missionDropDownState == 0)//show the drop down
  170. {
  171. tMissionDropDown.position = new Vector3(tMissionDropDown.position.x,
  172. Mathf.Lerp(tMissionDropDown.position.y, -1, Time.deltaTime*2.5f), tMissionDropDown.position.z);
  173. if (tMissionDropDown.position.y <= -0.9f)
  174. missionDropDownState = 1;
  175. }
  176. else if (missionDropDownState == 1)//hide the drop down
  177. {
  178. tMissionDropDown.position = new Vector3(tMissionDropDown.position.x,
  179. Mathf.Lerp(tMissionDropDown.position.y, 23, Time.deltaTime*4.5f), tMissionDropDown.position.z);
  180. if (tMissionDropDown.position.y >= 22)
  181. missionDropDownState = 2;
  182. }
  183. else if (missionDropDownState == 2)//stop the coroutine
  184. {
  185. missionDropDownState = -1;
  186. StopCoroutine("displayMissionDescriptionDropDown");
  187. break;
  188. }
  189. }//end of while
  190. }
  191. /*
  192. * FUNCTION: Resize HUD Score and Currency containers according to digit count
  193. * CALLED BY: Start() (invoke repeating)
  194. */
  195. private void resizeDigitContainer()
  196. {
  197. int fScore = hInGameScriptCS.getLevelScore();
  198. int fCurrency = hPowerupsMainControllerCS.getCurrencyUnits();
  199. if ( (fScore / iDivisorScore) >= 1 )
  200. {
  201. tHUDScoreContainerMid.localScale += new Vector3(0,0,0.4f);//expand the Score Container Mid
  202. iDivisorScore *= 10;
  203. }
  204. if ( (fCurrency / iDivisorCurrency) >= 1 )
  205. {
  206. tHUDCurrencyContainerMid.localScale += new Vector3(0,0,0.4f);//expand the Currency Container Mid
  207. iDivisorCurrency *= 10;
  208. }
  209. }
  210. }