Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Test : MonoBehaviour {
- public float currentEnergy { get; set; }
- public float maxEnergy { get; set; }
- public float abilityCost;
- //These trigger buttons will be set in the ability scripts
- //public string p1TriggerButton1;
- //public string p1TriggerButton2;
- private bool stopECost = true;
- public float nextFire;
- public float cooldown;
- private Text insufficientSP;
- private Text SPAmount;
- [SerializeField]
- private Slider energyBar;
- // Use this for initialization
- void Start () {
- //Load this gameobjects basis cooldown script so we can use them to check if we can use more energy
- cooldown = 2f;
- nextFire = 2f;
- maxEnergy = 10f;
- //Resets to value on game load
- currentEnergy = 7f;
- StartCoroutine(AddEnergy());
- }
- // Update is called once per frame
- void Update () {
- energyBar = GameObject.Find("Slider").GetComponent<Slider>();
- energyBar.value = CurrentEnergy();
- SPAmount = GameObject.Find("Text").GetComponent<Text>();
- SPAmount.text = CurrentEnergy() * 10 + "SP";
- PlayerTriggers();
- }
- public void EnergyCost(float aCostValue)
- {
- if (currentEnergy >= abilityCost && Time.time > nextFire)
- {
- nextFire = Time.time + cooldown; //Using basis cooldown script to check if the ability is ready, so we can use more energy
- //Deduct the energy spent from the current energy
- currentEnergy -= aCostValue;
- energyBar.value = CurrentEnergy();
- }
- //If there is insufficient energy alert and stop use
- else if(currentEnergy < abilityCost){
- StartCoroutine(NotEnoughEnergy());
- } else {
- Debug.Log("Cooldown!");
- }
- //If the current energy is less or 0 set it to 0 so we wont get negative energy
- if (currentEnergy < 0f)
- {
- currentEnergy = 0f;
- }
- }
- public void PlayerTriggers()
- {
- //If player presses one of their ability buttons we subtract the abilities cost thorugh the EnergyCost() script
- if (Input.GetKeyDown(KeyCode.Space))
- {
- EnergyCost(abilityCost);
- }
- }
- float CurrentEnergy()
- {
- return currentEnergy / maxEnergy;
- }
- public IEnumerator NotEnoughEnergy()
- {
- insufficientSP = GameObject.Find("NoText").GetComponent<Text>();
- insufficientSP.text = "Not enough Energy...";
- yield return new WaitForSeconds(2);
- insufficientSP.text = "";
- }
- public IEnumerator AddEnergy()
- {
- while (true)// loops forever
- {
- //if current energy is less than maxenergy
- if(currentEnergy < maxEnergy)
- {
- currentEnergy += 1; //increase energy with 1
- yield return new WaitForSeconds(3);
- }
- else //if currentenergy is 10>=
- {
- yield return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement