Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class bl_HudManager : MonoBehaviour {
- public List<bl_HudInfo> Huds = new List<bl_HudInfo>();
- public Transform LocalPlayer = null;
- [Space(5)]
- public bool DecalMode = true;
- public float clampBorder = 12;
- [Space(5)]
- public GUIStyle TextStyle;
- private bl_GameManager GManager;
- private static bl_HudManager _instance;
- //Get singleton
- public static bl_HudManager instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = GameObject.FindObjectOfType<bl_HudManager>();
- }
- return _instance;
- }
- }
- /// <summary>
- ///
- /// </summary>
- void Awake()
- {
- GManager = GameObject.FindWithTag("GameManager").GetComponent<bl_GameManager>();
- }
- /// <summary>
- ///
- /// </summary>
- void Update()
- {
- if (GManager == null)
- {
- GManager = GameObject.FindWithTag("GameManager").GetComponent<bl_GameManager>();
- return;
- }
- if (GManager.OurPlayer != null)
- {
- LocalPlayer = GManager.OurPlayer.transform;
- }
- }
- /// <summary>
- /// Draw a icon foreach in list
- /// </summary>
- void OnGUI()
- {
- if (!Screen.lockCursor)
- return;
- if (Camera.main == null)
- return;
- if (LocalPlayer == null)
- return;
- //pass test :)
- for (int i = 0; i < Huds.Count; i++)
- {
- //if transform destroy, then remove from list
- if (Huds[i].m_Target == null)
- {
- Huds.Remove(Huds[i]);
- return;
- }
- //Calculate Position of target
- Vector3 RelativePosition = Huds[i].m_Target.position + Huds[i].Offset;
- if (!DecalMode)
- {
- // only if is front of view draw
- if ((Vector3.Dot(this.LocalPlayer.forward, RelativePosition - this.LocalPlayer.position) > 0f))
- {
- //Calculate the 2D position of the position where the icon should be drawn
- Vector3 point = Camera.main.GetComponent<Camera>().WorldToScreenPoint(RelativePosition);
- Rect rect = new Rect(0f, 0f, Screen.width, Screen.height);
- //position is in screen?
- if (rect.Contains(point))
- {
- float Distance = Vector3.Distance(this.LocalPlayer.position, RelativePosition);
- float CompleteDistance = Distance;
- if (Distance > 50)
- {
- Distance = 50;
- }
- float n;
- if (Huds[i].m_TypeHud == TypeHud.Decreasing)
- {
- n = (((50 + Distance) / (50 * 2f)) * 0.9f) + 0.1f;
- if (n > Huds[i].m_MaxSize)
- {
- n = Huds[i].m_MaxSize;
- }
- }
- else
- {
- n = (((50 - Distance) / (50 * 2f)) * 0.9f) + 0.1f;
- }
- //palpiting
- if (Huds[i].isPalpitin)
- {
- if (Huds[i].m_Color.a <= 0)
- {
- Huds[i].tip = false;
- }
- else if (Huds[i].m_Color.a >= 1)
- {
- Huds[i].tip = true;
- }
- if (Huds[i].tip == false)
- {
- Huds[i].m_Color.a += Time.deltaTime * 0.5f;
- }
- else
- {
- Huds[i].m_Color.a -= Time.deltaTime * 0.5f;
- }
- }
- //Draw Icon
- GUI.color = Huds[i].m_Color;
- 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);
- if (!Huds[i].ShowDistance)
- {
- GUI.Label(new Rect(point.x - (Huds[i].m_Text.Length), (Screen.height - point.y) + 5f, 300, 50), Huds[i].m_Text, TextStyle);
- }
- else
- {
- 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);
- }
- GUI.color = Color.white;
- }
- }
- }
- else
- {
- if ((Vector3.Dot(this.LocalPlayer.forward, RelativePosition - this.LocalPlayer.position) > 0f))
- {
- //Calculate the 2D position of the position where the icon should be drawn
- Vector3 point = Camera.main.WorldToViewportPoint(RelativePosition);
- //The viewportPoint coordinates are between 0 and 1, so we have to convert them into screen space here
- Vector2 drawPosition = new Vector2(point.x * Screen.width, Screen.height * (1 - point.y));
- //Clamp the position to the edge of the screen in case the icon would be drawn outside the screen
- drawPosition.x = Mathf.Clamp(drawPosition.x, clampBorder, Screen.width - clampBorder);
- drawPosition.y = Mathf.Clamp(drawPosition.y, clampBorder, Screen.height - clampBorder);
- float Distance = Vector3.Distance(this.LocalPlayer.position, RelativePosition);
- float CompleteDistance = Distance;
- if (Distance > 50)
- {
- Distance = 50;
- }
- float n;
- if (Huds[i].m_TypeHud == TypeHud.Decreasing)
- {
- n = (((50 + Distance) / (50 * 2f)) * 0.9f) + 0.1f;
- }
- else
- {
- n = (((50 - Distance) / (50 * 2f)) * 0.9f) + 0.1f;
- }
- //Calculate Size of Hud
- float sizeX = Huds[i].m_Icon.width * n;
- if (sizeX >= Huds[i].m_MaxSize)
- {
- sizeX = Huds[i].m_MaxSize;
- }
- float sizeY = Huds[i].m_Icon.height * n;
- if (sizeY >= Huds[i].m_MaxSize)
- {
- sizeY = Huds[i].m_MaxSize;
- }
- //palpiting
- if (Huds[i].isPalpitin)
- {
- if (Huds[i].m_Color.a <= 0)
- {
- Huds[i].tip = false;
- }
- else if(Huds[i].m_Color.a >= 1)
- {
- Huds[i].tip = true;
- }
- if (Huds[i].tip == false)
- {
- Huds[i].m_Color.a += Time.deltaTime * 0.5f;
- }
- else
- {
- Huds[i].m_Color.a -= Time.deltaTime * 0.5f;
- }
- }
- GUI.color = Huds[i].m_Color;
- GUI.DrawTexture(new Rect(drawPosition.x - ((sizeX) / 2), drawPosition.y - ((sizeY) / 2), sizeX, sizeY), Huds[i].m_Icon);
- if (!Huds[i].ShowDistance)
- {
- GUI.Label(new Rect((drawPosition.x - Huds[i].m_Text.Length), (drawPosition.y - n) - 35f, 300, 50), Huds[i].m_Text, TextStyle);
- }
- else
- {
- 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);
- }
- GUI.color = Color.white;
- }
- }
- }
- }
- //Add a new Huds from instance
- public void CreateHud(bl_HudInfo info)
- {
- Huds.Add(info);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement