-
Notifications
You must be signed in to change notification settings - Fork 0
/
StaminaBar
179 lines (121 loc) · 4.09 KB
/
StaminaBar
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class StaminaBar : MonoBehaviour
{
public bool flag = false;
public bool isWalking = false;
public GameObject EnergyGreen;
public GameObject EnergyYellow;
public GameObject StaminaOnHealth1;
public GameObject StaminaOnHealth2;
public GameObject StaminaOnHealth3;
public GameObject StaminaOnHealth4;
public GameObject BlackHealthBar1;
public GameObject BlackHealthBar2;
public GameObject BlackHealthBar3;
public GameObject BlackHealthBar4;
public GameObject HideUi_Energy_Green;
public GameObject HideUi_Energy_Yellow;
public PlayerMovement playerSprint;
public float stamina ;
float maxStamina = 10 ;
private WaitForSeconds regenTick = new WaitForSeconds(0.1f);
private Coroutine regen;
public Slider staminaBar;
public float dValue ;
void Start()
{
HideUi_Energy_Green.SetActive(false);
HideUi_Energy_Yellow.SetActive(false);
StaminaOnHealth1.SetActive(false);
StaminaOnHealth2.SetActive(false);
StaminaOnHealth3.SetActive(false);
StaminaOnHealth4.SetActive(false);
BlackHealthBar1.SetActive(false);
BlackHealthBar2.SetActive(false);
BlackHealthBar3.SetActive(false);
BlackHealthBar4.SetActive(false);
maxStamina = stamina;
staminaBar.maxValue = maxStamina;
staminaBar.value = maxStamina;
playerSprint = FindObjectOfType<PlayerMovement>();
}
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.tag == "FlashLight")
{
if(Input.GetKeyDown(KeyCode.E))
{
HideUi_Energy_Green.SetActive(true);
HideUi_Energy_Yellow.SetActive(true);
BlackHealthBar1.SetActive(true);
BlackHealthBar2.SetActive(true);
BlackHealthBar3.SetActive(true);
BlackHealthBar4.SetActive(true);
flag = true;
}
}
}
void Update()
{
if (isWalking == true)
{
if (Input.GetKey(KeyCode.LeftShift))
//if( playerSprint.this.animator.GetCurrentAnimatorStateInfo(0).IsName("New_Idle"))
// {
DecreaseEnergy();
//}
else if (stamina != maxStamina)
IncreaseEnergy();
staminaBar.value = stamina;
}
if (flag == true)
{
if (stamina <= 1 )
{
EnergyGreen.SetActive(false);
EnergyYellow.SetActive(true);
StaminaOnHealth1.SetActive(true);
StaminaOnHealth2.SetActive(true);
StaminaOnHealth3.SetActive(true);
StaminaOnHealth4.SetActive(true);
}
else
{
EnergyGreen.SetActive(true);
EnergyYellow.SetActive(false);
StaminaOnHealth1.SetActive(false);
StaminaOnHealth2.SetActive(false);
StaminaOnHealth3.SetActive(false);
StaminaOnHealth4.SetActive(false);
}
}
}
private void DecreaseEnergy()
{
if (stamina >= 0)
stamina -= dValue * Time.deltaTime;
}
private void IncreaseEnergy()
{
regen = StartCoroutine(RegenStamina());
}
private IEnumerator RegenStamina()
{
yield return new WaitForSeconds(2);
if (stamina < maxStamina)
{
stamina += dValue * Time.deltaTime / 2;
staminaBar.value = stamina;
yield return regenTick;
}
regen = null;
if(regen != null)
{
StopCoroutine(regen);
}
}
}