| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class FighterHPBar
- {
- public const string HUD_HERO_PREFAB = "Battle/HPBar";
- private GameObject mHud;
- private RectTransform mItemRt = null;
- private GameObject mBloodGo;
- private Image mBloodImage;
- private Vector3 m_bloodPos;
- private Fighter mFighter = null;
- private bool m_bloodDirty = false;
- private bool m_hudLogicVisible = false;
- public void Born(Fighter owner)
- {
- mFighter = owner;
- mHud = null;
- mBloodImage = null;
- mItemRt = null;
- m_bloodPos = Vector3.zero;
- }
- public void Dispose()
- {
- if (mHud != null)
- {
- ResourceMgr.Instance.RecycleGO(Constants.UIPath, HUD_HERO_PREFAB, mHud);
- }
- mItemRt = null;
- mHud = null;
- mBloodImage = null;
- m_bloodPos = Vector3.zero;
- }
- public void Destroy()
- {
- Dispose();
- }
- public void Prepare()
- {
- InitHudUI();
- mHud.SetActive(false);
- }
- public void Fight()
- {
- #if UNITY_EDITOR
- if (mHud)
- {
- mHud.name = mFighter.Ctrl.name + "_hpbar";
- }
- #endif
- ResetHudUI();
- }
- public void Deactive()
- {
- setHudLogicVisible(false);
- }
- public void OnActorDead()
- {
- if (null == mHud)
- return;
- mHud.SetActive(false);
- setHudLogicVisible(false);
- }
- public void OnActorRevive()
- {
- if (mFighter == null) return;
- setHudLogicVisible(false);
- }
- public void LateUpdate(float deltaTime)
- {
- if (mHud == null) return;
- if (mFighter == null)
- {
- ResourceMgr.Instance.RecycleGO(Constants.UIPath, HUD_HERO_PREFAB, mHud);
- mHud = null;
- return;
- }
- // 这几种情况不需要更新下面的东西,太费了
- if (!mFighter.IsVisible)
- {
- if (m_bloodDirty)
- {
- m_bloodDirty = false;
- mHud.SetActive(false);
- }
- return;
- }
- if (!BattleCamera.Instance.BattleCameraEnabled) return;
-
- Vector3 curUIPointPos = mFighter.Ctrl.GetUIPointPos();
- // 血条可见性需要修改
- if (m_bloodDirty)
- {
- m_bloodDirty = false;
- bool result = m_hudLogicVisible && mFighter.IsVisible && mFighter.IsAlive && mFighter.IsFighting;
- mHud.SetActive(result);
- }
- // Update Hud
- //if (mHud.activeSelf)
- {
- Vector3 hudPosition = curUIPointPos + mFighter.Actor.AvatarData.bloodBarPos;
- Vector3 curBloodPos = Camera.main.WorldToViewportPoint(hudPosition);
- bool isNeedUpdateUIHud = (m_bloodPos != curBloodPos);
- m_bloodPos = curBloodPos;
- if (isNeedUpdateUIHud)
- {
- UpdateUIHud(ref curBloodPos);
- }
- }
- }
- public void OnLifeChanged(long life, long maxLife)
- {
- if (life < maxLife)
- setHudLogicVisible(true);
- UpdateBloodBar(life, maxLife);
- }
- private void setHudLogicVisible(bool isVisible)
- {
- m_hudLogicVisible = isVisible;
- refreshHudVisible();
- }
- private void refreshHudVisible()
- {
- if (null != mHud)
- {
- bool result = m_hudLogicVisible && mFighter.IsVisible && mFighter.IsFighting;
- if (result != mHud.activeSelf)
- {
- m_bloodDirty = true;
- }
- }
- }
- ///-------------------------------------------------
- /// 初始化Hud UI
- ///-------------------------------------------------
- private void InitHudUI()
- {
- if (mFighter == null)
- return;
- if (mHud == null)
- {
- mHud = ResourceMgr.Instance.GetGoFromPool(Constants.UIPath, HUD_HERO_PREFAB);
- DebugHelper.Assert(mHud != null, "wtf?");
- if (mHud == null)
- {
- return;
- }
- Transform hudPanel = BattleFlyWordMgr.Instance.HudRootTrans;
- if (hudPanel != null)
- {
- mHud.transform.SetParent(hudPanel, true);
- mHud.transform.localScale = Vector3.one;
- mHud.transform.localRotation = Quaternion.identity;
- }
- RectTransform rt = mHud.GetComponent<RectTransform>();
- rt.anchorMin = Vector2.zero;
- rt.anchorMax = Vector2.one;
- rt.anchoredPosition3D = Vector3.zero;
- rt.offsetMin = rt.offsetMax = Vector2.zero;
- Transform item = mHud.transform.Find("Item");
- mItemRt = item.GetComponent<RectTransform>();
- mBloodGo = item.Find("HP").gameObject;
- mBloodImage = item.Find("HP/bloodImg").GetComponent<Image>();
- DebugHelper.Assert(mBloodImage != null);
- if (mBloodImage != null)
- {
- if (mFighter.Life > 0)
- {
- mBloodImage.fillAmount = ((float)mFighter.Life / (float)mFighter.MaxLife);
- }
- else
- {
- mBloodImage.fillAmount = 0;
- }
- }
- }
- setHudLogicVisible(false);
- }
- private void ResetHudUI()
- {
- if (mHud == null || mFighter == null || mBloodImage == null)
- {
- return;
- }
- if (mFighter.Life > 0)
- {
- mBloodImage.fillAmount = ((float)mFighter.Life / (float)mFighter.MaxLife);
- }
- else
- {
- mBloodImage.fillAmount = 0;
- }
- mBloodGo.SetActive(true);
- //添加到对应的Panel
- Transform hudPanel = BattleFlyWordMgr.Instance.HudRootTrans;
- if (hudPanel != null)
- {
- mHud.transform.SetParent(hudPanel, true);
- mHud.transform.localScale = Vector3.one;
- mHud.transform.localRotation = Quaternion.identity;
- }
- setHudLogicVisible(true);
- }
- ///-------------------------------------------------
- /// 更新UI Hud
- ///-------------------------------------------------
- private void UpdateUIHud(ref Vector3 bloodPosition)
- {
- if (mItemRt != null)
- {
- bloodPosition.Set(bloodPosition.x, bloodPosition.y, bloodPosition.z);
- if (CameraMgr.Instance.UICamera == null)
- {
- return;
- }
- mItemRt.anchoredPosition3D = new Vector3((bloodPosition.x - 0.5f) * UIMgr.SCREEN_WIDTH, (bloodPosition.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0);
- }
- }
- ///-------------------------------------------------
- /// 更新血条值
- ///-------------------------------------------------
- private void UpdateBloodBar(long curValue, long maxValue)
- {
- if (mHud != null && maxValue != 0)
- {
- if ((curValue == 0 || curValue >= maxValue) && mFighter.IsDisposed)
- {
- setHudLogicVisible(false);
- }
- else
- {
- setHudLogicVisible(true);
- if (mBloodImage != null)
- {
- float lastVal = mBloodImage.fillAmount;
- mBloodImage.fillAmount = ((float)curValue / (float)maxValue);
- }
- }
- }
- }
- }
|