-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBonineEnergy.cs
68 lines (57 loc) · 2.12 KB
/
BonineEnergy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
public class BonineEnergy : MonoBehaviour
{
public Slider energyBar;
public Image energyIconObject;
public TextMeshProUGUI energyText;
public Sprite[] energyIcons;
public int energy = 100;
public float energyRate = 0.05f;
public float energyCooldown = 2f;
Coroutine runningCoroutine;
float energyTimer;
void Update()
{
if (energy < 100)
{
if (energyTimer < energyCooldown) energyTimer += Time.deltaTime;
else IncreaseEnergy(100, energyRate);
}
}
public void DecreaseEnergy(int change, float rate = 0.03f) => SetEnergy(energy - change >= 0 ? energy - change : 0, false, rate);
public void IncreaseEnergy(int change, float rate = 0.03f) => SetEnergy(energy + change <= 100 ? energy + change : 100, true, rate);
// Decreases/Increases Bonine's energy at a specified rate
IEnumerator UseEnergyCoroutine(int energyVal, bool increase, float rate)
{
bool condition = increase ? energy < energyVal : energy > energyVal;
while (condition)
{
if (!increase)
{
energy = energy - 1 <= 0 ? 0 : energy - 1;
energyTimer = 0;
}
else energy = energy + 1 >= 100 ? 100 : energy + 1;
energyBar.value = energy;
energyText.text = energy.ToString() + "EG";
energyIconObject.sprite = energyIcons[Convert.ToInt32(Math.Ceiling((float)energy / 10))];
condition = increase ? energy < energyVal : energy > energyVal;
yield return new WaitForSeconds(rate);
}
}
IEnumerator UseEnergy(int energyVal, bool increase, float rate)
{
if (runningCoroutine != null) StopCoroutine(runningCoroutine);
runningCoroutine = StartCoroutine(UseEnergyCoroutine(energyVal, increase, rate));
yield return null;
}
void SetEnergy(int energyVal, bool increase, float rate)
{
if (energyVal == energy) return;
StartCoroutine(UseEnergy(energyVal, increase, rate));
}
}