HeadBar.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. public class HeadBar : MonoBehaviour {
  5. public Image bar;
  6. public Text text;
  7. public Animator radioAnim;
  8. public Sprite[] bgList;
  9. public GameObject spellBar;
  10. public Image spellProgress;
  11. private BattleObject owner;
  12. private RectTransform rectTrans;
  13. private RectTransform canvasTrans;
  14. public void Init(BattleObject owner)
  15. {
  16. if(Session.GetInstance().GetBattleSession() == null)
  17. {
  18. Destroy(this.gameObject);
  19. }
  20. this.owner = owner;
  21. rectTrans = GetComponent<RectTransform> ();
  22. rectTrans.SetSiblingIndex(0);
  23. canvasTrans = GameObject.FindObjectOfType<Canvas> ().GetComponent<RectTransform>();
  24. HideSpell();
  25. UpdateHpBar();
  26. LateUpdate();
  27. }
  28. public void UpdateColor()
  29. {
  30. Player myPlayer = Session.GetInstance().GetBattleSession().myPlayer;
  31. if(owner is Craft && (owner as Craft).IsCtrl())
  32. {
  33. bar.sprite = bgList[0];
  34. }
  35. else if(myPlayer.team == owner.team)
  36. {
  37. bar.sprite = bgList[1];
  38. }
  39. else
  40. {
  41. bar.sprite = bgList[2];
  42. }
  43. }
  44. public void RadioAlert()
  45. {
  46. radioAnim.Play("RadioAlertShow", 0, 0);
  47. }
  48. // Update is called once per frame
  49. void LateUpdate ()
  50. {
  51. if(owner != null)
  52. {
  53. if(owner.IsDead())
  54. {
  55. Destroy(this.gameObject);
  56. }
  57. else
  58. {
  59. UpdatePosition();
  60. }
  61. }
  62. }
  63. private void UpdatePosition()
  64. {
  65. Vector3 position = owner.position;
  66. position.y += owner.headBarHeight;
  67. position.z += owner.headBarHeight;
  68. Vector3 screenPosition = Camera.main.WorldToScreenPoint(position);
  69. position.x = screenPosition.x/(float)Screen.width;
  70. position.y = screenPosition.y/(float)Screen.height;
  71. position.x = position.x*canvasTrans.sizeDelta.x - canvasTrans.sizeDelta.x/2f;
  72. position.y = position.y*canvasTrans.sizeDelta.y - canvasTrans.sizeDelta.y/2f;
  73. position.z = 0;
  74. rectTrans.anchoredPosition = position;
  75. }
  76. public void UpdateHpBar()
  77. {
  78. float percent = owner.hp/owner.maxHp;
  79. bar.fillAmount = percent;
  80. }
  81. public void SetSpellProgress(float value, float total)
  82. {
  83. if(!spellBar.activeInHierarchy)
  84. spellBar.SetActive(true);
  85. float percent = value/total;
  86. spellProgress.fillAmount = Mathf.Min(percent, 1f);
  87. }
  88. public void HideSpell()
  89. {
  90. spellBar.SetActive(false);
  91. }
  92. }