Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- public class HPBar : MonoBehaviour {
- public static bool Enabled = true;
- static bool TextEnabled = false;
- public GUITexture BackTexture;
- public GUITexture ForeTexture;
- public GUIText StatusText;
- Entity entity;
- public float height = 10.0f;
- float halfWidth = 1.0f;
- float width;
- Entity m_Entity;
- /// <summary>
- /// Current HP Pct of the attached entity.
- /// 1.0 = 100%
- /// </summary>
- float HpPct
- {
- get
- {
- if (m_Entity == null) return 1.0f;
- return m_Entity.HpPct;
- }
- }
- public string Status
- {
- set
- {
- if (!HPBar.Enabled || !HPBar.TextEnabled) return;
- //if (StatusText == null && StatusText != null) StatusText = GUIText.Instantiate(StatusText) as GUIText;
- //if (StatusText != null) StatusText.text = value;
- }
- }
- // Use this for initialization
- void Start ()
- {
- if (!HPBar.Enabled) return;
- if (false && transform.parent != null)
- {
- if (transform.parent.GetComponent<Entity>() != null)
- {
- m_Entity = transform.parent.GetComponent<Entity>();
- }
- }
- //BackTexture = GUITexture.Instantiate(BackTexture, Vector3.zero, Quaternion.identity) as GUITexture;
- //ForeTexture = GUITexture.Instantiate(ForeTexture, new Vector3(0, 0, 1f), Quaternion.identity) as GUITexture;
- entity = gameObject.GetComponent<Entity>();
- width = ForeTexture.pixelInset.width;
- halfWidth = BackTexture.pixelInset.width / 2f;
- if (TextEnabled && StatusText != null)
- {
- //StatusText = GUIText.Instantiate(StatusText) as GUIText;
- StatusText.material.color = Color.red;
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (!HPBar.Enabled) return;
- if (m_Entity == null || m_Entity.transform == null)
- {
- Destroy (this);
- return;
- }
- //transform.position = Vector3.zero;
- //Vector2 wantedPos = Camera.main.transform.InverseTransformPoint(gameObject.transform.position);
- Vector2 wantedPos = Camera.main.WorldToScreenPoint(m_Entity.transform.position + (m_Entity.transform.up * height));
- BackTexture.pixelInset = new Rect(wantedPos.x - halfWidth, wantedPos.y, BackTexture.pixelInset.width, BackTexture.pixelInset.height);
- BackTexture.transform.position = new Vector3(0, 0, m_Entity.transform.position.z - .01f);
- ForeTexture.transform.position = new Vector3(0, 0, m_Entity.transform.position.z);
- //float hpPct = (float)entity.GetHP / (float)entity.MaxHP;
- int iWidth = Convert.ToInt32(width * HpPct);
- ForeTexture.pixelInset = new Rect(wantedPos.x - halfWidth + 1, wantedPos.y + 1, iWidth, ForeTexture.pixelInset.height);
- Vector2 wantedStatusPos = Camera.main.WorldToViewportPoint(gameObject.transform.position);
- if (TextEnabled && StatusText != null)
- {
- StatusText.transform.position = new Vector2(wantedStatusPos.x, wantedStatusPos.y);
- }
- }
- void OnDestroy()
- {
- if (!HPBar.Enabled) return;
- Destroy(BackTexture);
- Destroy(ForeTexture);
- if (TextEnabled) Destroy(StatusText);
- }
- internal void SetEntity(Entity entity)
- {
- m_Entity = entity;
- }
- }
Add Comment
Please, Sign In to add comment