| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class SkillName2D
- {
- public const string HUD_SKILLNAME_PREFAB = "Battle/SkillNameText";
- private Text mSkillName;
- private GameObject mHud;
- private RectTransform mItemRt = null;
- //引导条
- private GameObject mCastingGo;
- private Image mCastingAdd;
- private Image mCastingImage;
- private GameObject mBreakGo;
- private GameObject mSingGo;
- private Image mSingImage;
- private Animator mAnimator = null;
- private Fighter mFighter = null;
- private int mHideBreakTimer = 0;
- private int mAutoTimer = 0;
- private Vector3 m_skillNamePos;
- const float c_miniShowTime = 1.0f;
- public void Born(Fighter owner)
- {
- mFighter = owner;
- mHud = null;
- mItemRt = null;
- }
- bool registered = false;
- void RegisterEvents()
- {
- if (registered) return;
- registered = true;
- //EventMgr.AddEventListener<Fighter>(ECoreEventType.EID_FIGHTER_DO_SKILL, OnShowSkillName);
- }
- void UnRegisterEvents()
- {
- registered = false;
- //EventMgr.RemoveEventListener<Fighter>(ECoreEventType.EID_FIGHTER_DO_SKILL, OnShowSkillName);
- }
- public void Dispose()
- {
- if (mHud != null)
- {
- ResourceMgr.Instance.RecycleGO(Constants.UIPath, HUD_SKILLNAME_PREFAB, mHud);
- }
- m_skillNamePos = Vector3.zero;
- mHud = null;
- mItemRt = null;
- if (mHideBreakTimer > 0)
- {
- TimerManager.Instance.RemoveTimer(mHideBreakTimer);
- mHideBreakTimer = 0;
- }
- RemoveAutoTimer();
- mItemRt = null;
- mHud = null;
- mSingImage = null;
- mCastingGo = null;
- mCastingAdd = null;
- mCastingImage = null;
- mAnimator = null;
- }
- public void Destroy()
- {
- UnRegisterEvents();
- Dispose();
- }
- public void Prepare()
- {
- InitHudUI();
- }
- public void Fight()
- {
- #if UNITY_EDITOR
- if (mHud)
- {
- mHud.name = mFighter.Ctrl.name + "_skillName";
- }
- #endif
- ResetHudUI();
- RegisterEvents();
- }
- public void Deactive()
- {
- SetVisible(false);
- }
- public void OnActorDead()
- {
- if (mFighter == null) return;
- UnRegisterEvents();
- SetVisible(false);
- }
- public void OnActorRevive()
- {
- if (mFighter == null) return;
- RegisterEvents();
- SetVisible(false);
- }
- public void SetVisible(bool vis)
- {
- if (mHud == null) return;
- mHud.SetActive(vis);
- }
- public void LateUpdate(float deltaTime)
- {
- if (mHud == null) return;
- if (mFighter == null)
- {
- Dispose();
- return;
- }
- // 这几种情况不需要更新下面的东西,太费了
- if (!mFighter.IsVisible)
- {
- return;
- }
- // Update Hud
- if (mHud.activeSelf && Camera.main != null && Camera.main.enabled)
- {
- Vector3 curSkillNamePos = mFighter.Ctrl.GetUIPointPos() + mFighter.Actor.AvatarData.skillNamePos;
- Vector3 pos = Camera.main.WorldToViewportPoint(curSkillNamePos);
- bool isNeedUpdateUIHud = (m_skillNamePos != pos);
- if (isNeedUpdateUIHud && mItemRt != null)
- {
- mItemRt.anchoredPosition3D = new Vector3((pos.x - 0.5f) * UIMgr.SCREEN_WIDTH, (pos.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0); ;
- m_skillNamePos = pos;
- }
- if (curentCastingTime > 0 && mCastingImage != null && BattleMgr.Instance.Battle.IsFighting)
- {
- mCastingImage.fillAmount = curentCastingTime / totalCastingTime;
- if (mCastingAdd != null)
- mCastingAdd.fillAmount = mCastingImage.fillAmount;
- curentCastingTime -= deltaTime;
- }
- }
- }
- ///-------------------------------------------------
- /// 初始化Hud UI
- ///-------------------------------------------------
- private void InitHudUI()
- {
- if (mFighter == null)
- return;
- if (mHud == null)
- {
- mHud = ResourceMgr.Instance.GetGoFromPool(Constants.UIPath, HUD_SKILLNAME_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");
- mAnimator = item.GetComponent<Animator>();
- mAnimator.logWarnings = false;
- mItemRt = item.GetComponent<RectTransform>();
- mSkillName = item.Find("name").GetComponent<Text>();
- mSingGo = item.Find("Sing").gameObject;
- mSingImage = item.getGameObject("Sing/singImg").GetComponent<Image>();
- mSingGo.SetActive(false);
- mCastingGo = item.Find("Working").gameObject;
- mCastingImage = item.getGameObject("Working/workingImg").GetComponent<Image>();
- mCastingAdd = item.getGameObject("Working/workingAdd").GetComponent<Image>();
- mCastingGo.SetActive(false);
- mBreakGo = item.Find("Broken").gameObject;
- mBreakGo.SetActive(false);
- }
- SetVisible(false);
- }
- private void ResetHudUI()
- {
- if (mHud == null || mFighter == 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;
- }
- SetVisible(false);
- }
- private void OnShowSkillName(CoreEvent<Fighter> ce)
- {
- Fighter fighter = ce.Data;
- if (fighter != mFighter) return;
- OnShowSkillName(ce.Param.ToString());
- }
- public void OnShowSkillName(string skillName)
- {
- RemoveAutoTimer();
- mSkillName.text = skillName;
- SetVisible(true);
- mAutoTimer = TimerManager.Instance.AddTimer((int)(c_miniShowTime * 1000), 1, OnHideSkillNameTimer);
- LateUpdate(0);
- }
- private void OnHideSkillNameTimer(int seq)
- {
- SetVisible(false);
- mAutoTimer = 0;
- }
- public void OnBeginSing(float totalTime)
- {
- if (totalTime > c_miniShowTime )
- RemoveAutoTimer();
- mSingGo.SetActive(true);
- mCastingGo.SetActive(false);
- mBreakGo.SetActive(false);
- }
- public void OnUpdateSing(float totalTime, float leftTime)
- {
- if (mSingImage != null)
- {
- mSingImage.fillAmount = (totalTime - leftTime) / totalTime;
- }
- }
- public void OnEndSing()
- {
- mSingGo.SetActive(false);
- if (mAutoTimer == 0)
- SetVisible(false);
- }
- float totalCastingTime = 0;
- float curentCastingTime = 0;
- public void OnCasting(float totalTime, bool vis)
- {
- if(totalTime > c_miniShowTime)
- RemoveAutoTimer();
- if (mAnimator != null)
- {
- if (vis)
- {
- mCastingImage.fillAmount = 1;
- mCastingAdd.fillAmount = 1;
- totalCastingTime = totalTime;
- curentCastingTime = totalCastingTime;
- mAnimator.Play("Working");
- }
- else
- {
- if(mAutoTimer == 0)
- SetVisible(false);
- }
- }
- mCastingGo.SetActive(vis);
- }
- public void OnSkillBreak()
- {
- if (mAnimator == null) return;
- if (mHideBreakTimer == 0) return;//已经打断
- if (mCastingGo.activeSelf || mSingGo.activeSelf)
- {
- mBreakGo.SetActive(true);
- mAnimator.Play("Broken");
- mHideBreakTimer = TimerManager.Instance.AddTimer(1000, 1, OnHideBreakGo);
- }
- }
- private void OnHideBreakGo(int timerSequence)
- {
- mHideBreakTimer = 0;
- if (null != mBreakGo)
- mBreakGo.SetActive(false);
- }
- private void RemoveAutoTimer()
- {
- if (mAutoTimer > 0) {
- TimerManager.Instance.RemoveTimer(mAutoTimer);
- mAutoTimer = 0;
- }
- }
- }
|