using UnityEngine; using System.Collections; public class TeamSideBarContainer : MonoBehaviour { public enum State { Showing, Shown, Hiding, Hiden } public GameObject bar; public GameObject flag; private CrystalBase crystal; public State state = State.Hiding; public void setCrystal(CrystalBase crystal) { this.crystal = crystal; if(crystal != null) { bar.GetComponent().enabled = flag.GetComponent().enabled = true; } else { bar.GetComponent().enabled = flag.GetComponent().enabled = false; } UpdateBar(); } // Update is called once per frame public void UpdateBar () { if(crystal != null) { float powerLevel = (float)crystal.powerLevel; TeamUtil.Team team = crystal.team; Rect rectBar = bar.GetComponent().pixelInset; Rect rectFlag = flag.GetComponent().pixelInset; if(team == TeamUtil.Team.Blue) { rectFlag.x = rectBar.x + rectBar.width/2f - (rectBar.width-6f)/10f*powerLevel - rectFlag.width/2f; } else if(team == TeamUtil.Team.Red) { rectFlag.x = rectBar.x + rectBar.width/2f + (rectBar.width-6f)/10f*powerLevel - rectFlag.width/2f; } else { rectFlag.x = rectBar.x + rectBar.width/2f - rectFlag.width/2f; } flag.GetComponent().pixelInset = rectFlag; } } void Update() { if(state == State.Showing) { Color color = flag.GetComponent().color; color.a += GameTime.deltaTime; if(color.a >= 1f) { state = State.Shown; return; } if(color.a >= 1f) { color.a = 1f; state = State.Shown; } flag.GetComponent().color = bar.GetComponent().color = color; } else if(state == State.Hiding) { Color color = flag.GetComponent().color; if(color.a <= 0) { state = State.Hiden; return; } color.a -= GameTime.deltaTime; if(color.a <= 0) { color.a = 0; state = State.Hiden; } flag.GetComponent().color = bar.GetComponent().color = color; } updatePosition(); } private void updatePosition() { if(crystal == null) { return; } Vector3 position = crystal.position; position.y += 5f; Vector3 screenPosition = Camera.main.WorldToScreenPoint(position); position.x = screenPosition.x/Screen.width; position.y = screenPosition.y/Screen.height; position.z = -1f; this.transform.position = position; } }