TeamSideBarContainer.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using UnityEngine;
  2. using System.Collections;
  3. public class TeamSideBarContainer : MonoBehaviour {
  4. public enum State
  5. {
  6. Showing,
  7. Shown,
  8. Hiding,
  9. Hiden
  10. }
  11. public GameObject bar;
  12. public GameObject flag;
  13. private CrystalBase crystal;
  14. public State state = State.Hiding;
  15. public void setCrystal(CrystalBase crystal)
  16. {
  17. this.crystal = crystal;
  18. if(crystal != null)
  19. {
  20. bar.GetComponent<GUITexture>().enabled = flag.GetComponent<GUITexture>().enabled = true;
  21. }
  22. else
  23. {
  24. bar.GetComponent<GUITexture>().enabled = flag.GetComponent<GUITexture>().enabled = false;
  25. }
  26. UpdateBar();
  27. }
  28. // Update is called once per frame
  29. public void UpdateBar ()
  30. {
  31. if(crystal != null)
  32. {
  33. float powerLevel = (float)crystal.powerLevel;
  34. TeamUtil.Team team = crystal.team;
  35. Rect rectBar = bar.GetComponent<GUITexture>().pixelInset;
  36. Rect rectFlag = flag.GetComponent<GUITexture>().pixelInset;
  37. if(team == TeamUtil.Team.Blue)
  38. {
  39. rectFlag.x = rectBar.x + rectBar.width/2f - (rectBar.width-6f)/10f*powerLevel - rectFlag.width/2f;
  40. }
  41. else if(team == TeamUtil.Team.Red)
  42. {
  43. rectFlag.x = rectBar.x + rectBar.width/2f + (rectBar.width-6f)/10f*powerLevel - rectFlag.width/2f;
  44. }
  45. else
  46. {
  47. rectFlag.x = rectBar.x + rectBar.width/2f - rectFlag.width/2f;
  48. }
  49. flag.GetComponent<GUITexture>().pixelInset = rectFlag;
  50. }
  51. }
  52. void Update()
  53. {
  54. if(state == State.Showing)
  55. {
  56. Color color = flag.GetComponent<GUITexture>().color;
  57. color.a += GameTime.deltaTime;
  58. if(color.a >= 1f)
  59. {
  60. state = State.Shown;
  61. return;
  62. }
  63. if(color.a >= 1f)
  64. {
  65. color.a = 1f;
  66. state = State.Shown;
  67. }
  68. flag.GetComponent<GUITexture>().color = bar.GetComponent<GUITexture>().color = color;
  69. }
  70. else if(state == State.Hiding)
  71. {
  72. Color color = flag.GetComponent<GUITexture>().color;
  73. if(color.a <= 0)
  74. {
  75. state = State.Hiden;
  76. return;
  77. }
  78. color.a -= GameTime.deltaTime;
  79. if(color.a <= 0)
  80. {
  81. color.a = 0;
  82. state = State.Hiden;
  83. }
  84. flag.GetComponent<GUITexture>().color = bar.GetComponent<GUITexture>().color = color;
  85. }
  86. updatePosition();
  87. }
  88. private void updatePosition()
  89. {
  90. if(crystal == null)
  91. {
  92. return;
  93. }
  94. Vector3 position = crystal.position;
  95. position.y += 5f;
  96. Vector3 screenPosition = Camera.main.WorldToScreenPoint(position);
  97. position.x = screenPosition.x/Screen.width;
  98. position.y = screenPosition.y/Screen.height;
  99. position.z = -1f;
  100. this.transform.position = position;
  101. }
  102. }