Station.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Station : MonoBehaviour
  5. {
  6. public float maxPower;
  7. public Transform gauge;
  8. public float powerSuckDelay = 1;
  9. public float powerLevel;
  10. private IEnumerator drainCoroutine;
  11. // Use this for initialization
  12. void Start ()
  13. {
  14. powerLevel = 0;
  15. }
  16. // Update is called once per frame
  17. void Update () {
  18. UpdatePowerBar();
  19. }
  20. void PowerUp(float delta)
  21. {
  22. powerLevel += delta;
  23. if (powerLevel >= maxPower)
  24. Debug.Log("Station at max power!");
  25. UpdatePowerBar();
  26. }
  27. private void UpdatePowerBar()
  28. {
  29. var level = powerLevel / maxPower;
  30. gauge.localScale = new Vector3(1, level, 1);
  31. }
  32. void OnTriggerEnter(Collider other)
  33. {
  34. if (other.CompareTag("Player"))
  35. {
  36. drainCoroutine = DrainPower(other.GetComponent<Player>());
  37. StartCoroutine(drainCoroutine);
  38. }
  39. }
  40. void OnTriggerExit(Collider other)
  41. {
  42. if (other.CompareTag("Player"))
  43. {
  44. StopCoroutine(drainCoroutine);
  45. }
  46. }
  47. private IEnumerator DrainPower(Player player)
  48. {
  49. while (player.PowerLevel >= 1f)
  50. {
  51. if (powerLevel >= maxPower)
  52. {
  53. yield break;
  54. }
  55. yield return new WaitForSeconds(powerSuckDelay);
  56. PowerUp(1);
  57. player.PowerLevel -= 1f;
  58. UpdatePowerBar();
  59. }
  60. }
  61. }