using System.Collections; using System.Collections.Generic; using UnityEngine; public class Station : MonoBehaviour { public float maxPower; public Transform gauge; public float powerSuckDelay = 1; public float powerLevel; private IEnumerator drainCoroutine; // Use this for initialization void Start () { powerLevel = 0; } // Update is called once per frame void Update () { UpdatePowerBar(); } void PowerUp(float delta) { powerLevel += delta; if (powerLevel >= maxPower) Debug.Log("Station at max power!"); UpdatePowerBar(); } private void UpdatePowerBar() { var level = powerLevel / maxPower; gauge.localScale = new Vector3(1, level, 1); } void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) { drainCoroutine = DrainPower(other.GetComponent()); StartCoroutine(drainCoroutine); } } void OnTriggerExit(Collider other) { if (other.CompareTag("Player")) { StopCoroutine(drainCoroutine); } } private IEnumerator DrainPower(Player player) { while (player.PowerLevel >= 1f) { if (powerLevel >= maxPower) { yield break; } yield return new WaitForSeconds(powerSuckDelay); PowerUp(1); player.PowerLevel -= 1f; UpdatePowerBar(); } } }