Guest User

Untitled

a guest
Jul 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5.  
  6. public class HPBar : MonoBehaviour {
  7.  
  8.     public static bool Enabled = true;
  9.     static bool TextEnabled = false;
  10.  
  11.     public GUITexture BackTexture;
  12.     public GUITexture ForeTexture;
  13.     public GUIText StatusText;
  14.  
  15.     Entity entity;
  16.     public float height = 10.0f;
  17.  
  18.     float halfWidth = 1.0f;
  19.     float width;
  20.  
  21.     Entity m_Entity;
  22.  
  23.     /// <summary>
  24.     /// Current HP Pct of the attached entity.
  25.     /// 1.0 = 100%
  26.     /// </summary>
  27.     float HpPct
  28.     {
  29.         get
  30.         {
  31.             if (m_Entity == null) return 1.0f;
  32.             return m_Entity.HpPct;
  33.         }
  34.     }
  35.  
  36.     public string Status
  37.     {
  38.         set
  39.         {
  40.             if (!HPBar.Enabled || !HPBar.TextEnabled) return;
  41.  
  42.             //if (StatusText == null && StatusText != null) StatusText = GUIText.Instantiate(StatusText) as GUIText;
  43.             //if (StatusText != null) StatusText.text = value;
  44.         }
  45.     }
  46.  
  47.     // Use this for initialization
  48.     void Start ()
  49.     {
  50.         if (!HPBar.Enabled) return;
  51.  
  52.         if (false && transform.parent != null)
  53.         {
  54.             if (transform.parent.GetComponent<Entity>() != null)
  55.             {
  56.                 m_Entity = transform.parent.GetComponent<Entity>();
  57.             }
  58.         }
  59.  
  60.         //BackTexture = GUITexture.Instantiate(BackTexture, Vector3.zero, Quaternion.identity) as GUITexture;
  61.         //ForeTexture = GUITexture.Instantiate(ForeTexture, new Vector3(0, 0, 1f), Quaternion.identity) as GUITexture;
  62.         entity = gameObject.GetComponent<Entity>();
  63.  
  64.         width = ForeTexture.pixelInset.width;
  65.         halfWidth = BackTexture.pixelInset.width / 2f;
  66.  
  67.         if (TextEnabled && StatusText != null)
  68.         {
  69.             //StatusText = GUIText.Instantiate(StatusText) as GUIText;
  70.             StatusText.material.color = Color.red;
  71.         }
  72.        
  73.     }
  74.    
  75.     // Update is called once per frame
  76.     void Update()
  77.     {
  78.         if (!HPBar.Enabled) return;
  79.        
  80.         if (m_Entity == null || m_Entity.transform == null)
  81.         {
  82.             Destroy (this);
  83.             return;
  84.         }
  85.         //transform.position = Vector3.zero;
  86.         //Vector2 wantedPos = Camera.main.transform.InverseTransformPoint(gameObject.transform.position);
  87.         Vector2 wantedPos = Camera.main.WorldToScreenPoint(m_Entity.transform.position + (m_Entity.transform.up * height));
  88.  
  89.         BackTexture.pixelInset = new Rect(wantedPos.x - halfWidth, wantedPos.y, BackTexture.pixelInset.width, BackTexture.pixelInset.height);
  90.         BackTexture.transform.position = new Vector3(0, 0, m_Entity.transform.position.z - .01f);
  91.         ForeTexture.transform.position = new Vector3(0, 0, m_Entity.transform.position.z);
  92.         //float hpPct = (float)entity.GetHP / (float)entity.MaxHP;
  93.         int iWidth = Convert.ToInt32(width * HpPct);
  94.         ForeTexture.pixelInset = new Rect(wantedPos.x - halfWidth + 1, wantedPos.y + 1, iWidth, ForeTexture.pixelInset.height);
  95.  
  96.         Vector2 wantedStatusPos = Camera.main.WorldToViewportPoint(gameObject.transform.position);
  97.  
  98.         if (TextEnabled && StatusText != null)
  99.         {
  100.             StatusText.transform.position = new Vector2(wantedStatusPos.x, wantedStatusPos.y);
  101.         }
  102.     }
  103.  
  104.     void OnDestroy()
  105.     {
  106.         if (!HPBar.Enabled) return;
  107.  
  108.         Destroy(BackTexture);
  109.         Destroy(ForeTexture);
  110.         if (TextEnabled) Destroy(StatusText);
  111.     }
  112.  
  113.     internal void SetEntity(Entity entity)
  114.     {
  115.         m_Entity = entity;
  116.     }
  117. }
Add Comment
Please, Sign In to add comment