Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class Test : MonoBehaviour {
  7.  
  8. public float currentEnergy { get; set; }
  9. public float maxEnergy { get; set; }
  10.  
  11. public float abilityCost;
  12. //These trigger buttons will be set in the ability scripts
  13. //public string p1TriggerButton1;
  14. //public string p1TriggerButton2;
  15.  
  16. private bool stopECost = true;
  17.  
  18. public float nextFire;
  19. public float cooldown;
  20.  
  21. private Text insufficientSP;
  22. private Text SPAmount;
  23.  
  24. [SerializeField]
  25. private Slider energyBar;
  26.  
  27. // Use this for initialization
  28. void Start () {
  29. //Load this gameobjects basis cooldown script so we can use them to check if we can use more energy
  30. cooldown = 2f;
  31. nextFire = 2f;
  32.  
  33. maxEnergy = 10f;
  34. //Resets to value on game load
  35. currentEnergy = 7f;
  36.  
  37. StartCoroutine(AddEnergy());
  38. }
  39.  
  40. // Update is called once per frame
  41. void Update () {
  42. energyBar = GameObject.Find("Slider").GetComponent<Slider>();
  43. energyBar.value = CurrentEnergy();
  44.  
  45. SPAmount = GameObject.Find("Text").GetComponent<Text>();
  46. SPAmount.text = CurrentEnergy() * 10 + "SP";
  47.  
  48. PlayerTriggers();
  49. }
  50.  
  51. public void EnergyCost(float aCostValue)
  52. {
  53. if (currentEnergy >= abilityCost && Time.time > nextFire)
  54. {
  55. nextFire = Time.time + cooldown; //Using basis cooldown script to check if the ability is ready, so we can use more energy
  56.  
  57. //Deduct the energy spent from the current energy
  58. currentEnergy -= aCostValue;
  59. energyBar.value = CurrentEnergy();
  60. }
  61. //If there is insufficient energy alert and stop use
  62. else if(currentEnergy < abilityCost){
  63. StartCoroutine(NotEnoughEnergy());
  64. } else {
  65. Debug.Log("Cooldown!");
  66. }
  67.  
  68. //If the current energy is less or 0 set it to 0 so we wont get negative energy
  69. if (currentEnergy < 0f)
  70. {
  71. currentEnergy = 0f;
  72. }
  73. }
  74.  
  75. public void PlayerTriggers()
  76. {
  77. //If player presses one of their ability buttons we subtract the abilities cost thorugh the EnergyCost() script
  78. if (Input.GetKeyDown(KeyCode.Space))
  79. {
  80. EnergyCost(abilityCost);
  81. }
  82. }
  83.  
  84. float CurrentEnergy()
  85. {
  86. return currentEnergy / maxEnergy;
  87. }
  88.  
  89. public IEnumerator NotEnoughEnergy()
  90. {
  91. insufficientSP = GameObject.Find("NoText").GetComponent<Text>();
  92. insufficientSP.text = "Not enough Energy...";
  93.  
  94. yield return new WaitForSeconds(2);
  95.  
  96. insufficientSP.text = "";
  97. }
  98.  
  99. public IEnumerator AddEnergy()
  100. {
  101. while (true)// loops forever
  102. {
  103. //if current energy is less than maxenergy
  104. if(currentEnergy < maxEnergy)
  105. {
  106. currentEnergy += 1; //increase energy with 1
  107. yield return new WaitForSeconds(3);
  108. }
  109.  
  110. else //if currentenergy is 10>=
  111. {
  112. yield return null;
  113. }
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement