Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class bl_HudManager : MonoBehaviour {
  6.  
  7. public List<bl_HudInfo> Huds = new List<bl_HudInfo>();
  8. public Transform LocalPlayer = null;
  9. [Space(5)]
  10. public bool DecalMode = true;
  11. public float clampBorder = 12;
  12. [Space(5)]
  13. public GUIStyle TextStyle;
  14. private bl_GameManager GManager;
  15.  
  16. private static bl_HudManager _instance;
  17.  
  18. //Get singleton
  19. public static bl_HudManager instance
  20. {
  21. get
  22. {
  23. if (_instance == null)
  24. {
  25. _instance = GameObject.FindObjectOfType<bl_HudManager>();
  26. }
  27. return _instance;
  28. }
  29. }
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. void Awake()
  34. {
  35.  
  36. GManager = GameObject.FindWithTag("GameManager").GetComponent<bl_GameManager>();
  37. }
  38.  
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. void Update()
  43. {
  44. if (GManager == null)
  45. {
  46. GManager = GameObject.FindWithTag("GameManager").GetComponent<bl_GameManager>();
  47. return;
  48. }
  49.  
  50. if (GManager.OurPlayer != null)
  51. {
  52. LocalPlayer = GManager.OurPlayer.transform;
  53. }
  54. }
  55.  
  56. /// <summary>
  57. /// Draw a icon foreach in list
  58. /// </summary>
  59. void OnGUI()
  60. {
  61. if (!Screen.lockCursor)
  62. return;
  63. if (Camera.main == null)
  64. return;
  65. if (LocalPlayer == null)
  66. return;
  67. //pass test :)
  68. for (int i = 0; i < Huds.Count; i++)
  69. {
  70. //if transform destroy, then remove from list
  71. if (Huds[i].m_Target == null)
  72. {
  73. Huds.Remove(Huds[i]);
  74. return;
  75. }
  76. //Calculate Position of target
  77. Vector3 RelativePosition = Huds[i].m_Target.position + Huds[i].Offset;
  78. if (!DecalMode)
  79. {
  80. // only if is front of view draw
  81. if ((Vector3.Dot(this.LocalPlayer.forward, RelativePosition - this.LocalPlayer.position) > 0f))
  82. {
  83. //Calculate the 2D position of the position where the icon should be drawn
  84. Vector3 point = Camera.main.GetComponent<Camera>().WorldToScreenPoint(RelativePosition);
  85. Rect rect = new Rect(0f, 0f, Screen.width, Screen.height);
  86. //position is in screen?
  87. if (rect.Contains(point))
  88. {
  89. float Distance = Vector3.Distance(this.LocalPlayer.position, RelativePosition);
  90. float CompleteDistance = Distance;
  91. if (Distance > 50)
  92. {
  93. Distance = 50;
  94. }
  95. float n;
  96. if (Huds[i].m_TypeHud == TypeHud.Decreasing)
  97. {
  98. n = (((50 + Distance) / (50 * 2f)) * 0.9f) + 0.1f;
  99. if (n > Huds[i].m_MaxSize)
  100. {
  101. n = Huds[i].m_MaxSize;
  102. }
  103. }
  104. else
  105. {
  106. n = (((50 - Distance) / (50 * 2f)) * 0.9f) + 0.1f;
  107. }
  108. //palpiting
  109. if (Huds[i].isPalpitin)
  110. {
  111. if (Huds[i].m_Color.a <= 0)
  112. {
  113. Huds[i].tip = false;
  114. }
  115. else if (Huds[i].m_Color.a >= 1)
  116. {
  117. Huds[i].tip = true;
  118. }
  119.  
  120. if (Huds[i].tip == false)
  121. {
  122. Huds[i].m_Color.a += Time.deltaTime * 0.5f;
  123. }
  124. else
  125. {
  126. Huds[i].m_Color.a -= Time.deltaTime * 0.5f;
  127. }
  128. }
  129.  
  130. //Draw Icon
  131. GUI.color = Huds[i].m_Color;
  132. GUI.DrawTexture(new Rect(point.x - ((Huds[i].m_Icon.width * n) / 2f), (Screen.height - point.y) - ((Huds[i].m_Icon.height * n) / 2f), Huds[i].m_Icon.width * n, Huds[i].m_Icon.height * n), Huds[i].m_Icon);
  133. if (!Huds[i].ShowDistance)
  134. {
  135. GUI.Label(new Rect(point.x - (Huds[i].m_Text.Length), (Screen.height - point.y) + 5f, 300, 50), Huds[i].m_Text, TextStyle);
  136. }
  137. else
  138. {
  139. GUI.Label(new Rect(point.x - (Huds[i].m_Text.Length), (Screen.height - point.y) + 5f, 300, 50), Huds[i].m_Text + "\n <color=whitte>[" + bl_UtilityHelper.GetThreefoldChar(CompleteDistance) + "m]</color>", TextStyle);
  140. }
  141. GUI.color = Color.white;
  142. }
  143. }
  144. }
  145. else
  146. {
  147. if ((Vector3.Dot(this.LocalPlayer.forward, RelativePosition - this.LocalPlayer.position) > 0f))
  148. {
  149. //Calculate the 2D position of the position where the icon should be drawn
  150. Vector3 point = Camera.main.WorldToViewportPoint(RelativePosition);
  151.  
  152. //The viewportPoint coordinates are between 0 and 1, so we have to convert them into screen space here
  153. Vector2 drawPosition = new Vector2(point.x * Screen.width, Screen.height * (1 - point.y));
  154.  
  155. //Clamp the position to the edge of the screen in case the icon would be drawn outside the screen
  156. drawPosition.x = Mathf.Clamp(drawPosition.x, clampBorder, Screen.width - clampBorder);
  157. drawPosition.y = Mathf.Clamp(drawPosition.y, clampBorder, Screen.height - clampBorder);
  158.  
  159. float Distance = Vector3.Distance(this.LocalPlayer.position, RelativePosition);
  160. float CompleteDistance = Distance;
  161. if (Distance > 50)
  162. {
  163. Distance = 50;
  164. }
  165. float n;
  166.  
  167. if (Huds[i].m_TypeHud == TypeHud.Decreasing)
  168. {
  169. n = (((50 + Distance) / (50 * 2f)) * 0.9f) + 0.1f;
  170. }
  171. else
  172. {
  173. n = (((50 - Distance) / (50 * 2f)) * 0.9f) + 0.1f;
  174. }
  175. //Calculate Size of Hud
  176. float sizeX = Huds[i].m_Icon.width * n;
  177. if (sizeX >= Huds[i].m_MaxSize)
  178. {
  179. sizeX = Huds[i].m_MaxSize;
  180. }
  181. float sizeY = Huds[i].m_Icon.height * n;
  182. if (sizeY >= Huds[i].m_MaxSize)
  183. {
  184. sizeY = Huds[i].m_MaxSize;
  185. }
  186. //palpiting
  187. if (Huds[i].isPalpitin)
  188. {
  189. if (Huds[i].m_Color.a <= 0)
  190. {
  191. Huds[i].tip = false;
  192. }
  193. else if(Huds[i].m_Color.a >= 1)
  194. {
  195. Huds[i].tip = true;
  196. }
  197.  
  198. if (Huds[i].tip == false)
  199. {
  200. Huds[i].m_Color.a += Time.deltaTime * 0.5f;
  201. }
  202. else
  203. {
  204. Huds[i].m_Color.a -= Time.deltaTime * 0.5f;
  205. }
  206. }
  207.  
  208. GUI.color = Huds[i].m_Color;
  209. GUI.DrawTexture(new Rect(drawPosition.x - ((sizeX) / 2), drawPosition.y - ((sizeY) / 2), sizeX, sizeY), Huds[i].m_Icon);
  210. if (!Huds[i].ShowDistance)
  211. {
  212. GUI.Label(new Rect((drawPosition.x - Huds[i].m_Text.Length), (drawPosition.y - n) - 35f, 300, 50), Huds[i].m_Text, TextStyle);
  213. }
  214. else
  215. {
  216. GUI.Label(new Rect((drawPosition.x - Huds[i].m_Text.Length), (drawPosition.y - n) - 35f, 300, 50), Huds[i].m_Text + "\n <color=whitte>["+bl_UtilityHelper.GetThreefoldChar(CompleteDistance)+"m]</color>", TextStyle);
  217. }
  218. GUI.color = Color.white;
  219. }
  220. }
  221. }
  222. }
  223.  
  224. //Add a new Huds from instance
  225. public void CreateHud(bl_HudInfo info)
  226. {
  227. Huds.Add(info);
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement