forked from Unity-Technologies/Megacity-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHUD.cs
More file actions
224 lines (195 loc) · 7.26 KB
/
Copy pathHUD.cs
File metadata and controls
224 lines (195 loc) · 7.26 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System.Collections;
using JetBrains.Annotations;
using Unity.Mathematics;
using Unity.Megacity.Gameplay;
using Unity.NetCode;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UIElements;
using UnityEngine.UIElements.Experimental;
namespace Unity.Megacity.UI
{
/// <summary>
/// Manages the HUD UI elements.
/// </summary>
[RequireComponent(typeof(Crosshair))]
[RequireComponent(typeof(LaserBar))]
[RequireComponent(typeof(Notification))]
[RequireComponent(typeof(UIDocument))]
public class HUD : MonoBehaviour
{
[SerializeField] private UILeaderboard m_Leaderboard;
[SerializeField] private PlayerInfoItemSettings m_PlayerInfoSettings;
[SerializeField] private GameObject m_VirtualJoystickPrefab;
public VirtualJoystick JoystickLeft { get; private set; }
public VirtualJoystick JoystickRight { get; private set; }
private ProgressBar m_LifeBar;
private Crosshair m_Crosshair;
private Notification m_Notification;
private LaserBar m_LaserBar;
private VisualElement m_MessageScreen;
private VisualElement m_LifeBarContainer;
private Button m_SettingsButton;
private Label m_BottomMessageLabel;
private Label m_MessageLabel;
private Label m_ControllerLabel;
private float m_TypeDelay = 0.01f;
private float m_DeathCooldown = 5f;
private bool m_CompletedShowMessage;
public static HUD Instance { get; private set; }
public UILeaderboard Leaderboard => m_Leaderboard;
public Crosshair Crosshair => m_Crosshair;
public LaserBar Laser => m_LaserBar;
public Notification Notification => m_Notification;
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
return;
}
m_Crosshair = GetComponent<Crosshair>();
m_Notification = GetComponent<Notification>();
m_LaserBar = GetComponent<LaserBar>();
}
private void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
m_LifeBar = root.Q<ProgressBar>("life-bar");
m_LifeBarContainer = root.Q<VisualElement>("lifebar-container");
m_MessageScreen = root.Q<VisualElement>("message-screen");
m_MessageLabel = m_MessageScreen.Q<Label>("message-label");
m_BottomMessageLabel = m_MessageScreen.Q<Label>("bottom-message-label");
m_SettingsButton = root.Q<Button>("settings-button");
m_MessageScreen.style.display = DisplayStyle.None;
CursorUtils.HideCursor();
JoystickLeft = CreateJoystick(VirtualJoystick.Align.Left);
JoystickRight = CreateJoystick(VirtualJoystick.Align.Right);
m_SettingsButton.clicked += () =>
{
GameSettingsOptionsMenu.Instance.ShowSettingsOptions(true);
};
if (PlayerInfoController.Instance.IsSinglePlayer)
{
Hide();
}
else
{
Show();
}
}
private VirtualJoystick CreateJoystick(VirtualJoystick.Align align)
{
var prefab = Instantiate(m_VirtualJoystickPrefab, transform.parent, true);
var joystick = prefab.GetComponent<VirtualJoystick>();
joystick.SetPosition(align);
return joystick;
}
private void Hide()
{
m_LifeBarContainer.style.display = DisplayStyle.None;
m_Crosshair.Hide();
m_LaserBar.Hide();
NetcodePanelStats.Instance.Disable();
}
private void Show()
{
m_LifeBarContainer.style.display = DisplayStyle.Flex;
m_Crosshair.Show();
m_LaserBar.Show();
NetcodePanelStats.Instance.Disable();
}
public void UpdateLife(float life)
{
if (m_LifeBar.value >= m_PlayerInfoSettings.MinLifeBar &&
life < m_PlayerInfoSettings.MinLifeBar &&
!m_LifeBar.ClassListContains("magenta"))
{
m_LifeBar.AddToClassList("magenta");
}
else if (m_LifeBar.value <= m_PlayerInfoSettings.MinLifeBar &&
life > m_PlayerInfoSettings.MinLifeBar &&
m_LifeBar.ClassListContains("magenta"))
{
m_LifeBar.RemoveFromClassList("magenta");
}
m_LifeBar.value = life;
m_LifeBar.title = ((int) life).ToString();
}
public void ShowDeathMessage(string killerName)
{
FadeMessageScreen(true);
StartCoroutine(Type($"You have been destroyed by\n{killerName}!"));
StartCoroutine(DeathCooldown());
m_Crosshair.Hide();
}
public void ShowBoundsMessage()
{
FadeMessageScreen(true);
StartCoroutine(Type("You still have unfinished business here..."));
m_BottomMessageLabel.text = "";
m_Crosshair.Hide();
}
private void FadeMessageScreen(bool value)
{
if (value)
{
m_MessageScreen.style.display = DisplayStyle.Flex;
m_MessageScreen.experimental.animation
.Start(new StyleValues {opacity = 0f}, new StyleValues {opacity = 1f}, 1000);
}
else
{
m_MessageScreen.experimental.animation
.Start(new StyleValues {opacity = 1f}, new StyleValues {opacity = 0f}, 1000).OnCompleted(() =>
{
m_MessageScreen.style.display = DisplayStyle.None;
});
}
}
private IEnumerator DeathCooldown()
{
var timer = m_DeathCooldown;
while (timer >= 0)
{
var bottomMessage = (timer > 1)
? $"Respawning in: {math.trunc(timer)}s"
: "Returning...";
m_BottomMessageLabel.text = bottomMessage;
timer -= Time.deltaTime;
yield return null;
}
}
public void HideMessageScreen()
{
if(m_MessageScreen.style.display == DisplayStyle.Flex)
StartCoroutine(HideWhenCompletedMessage());
}
private IEnumerator HideWhenCompletedMessage()
{
while (!m_CompletedShowMessage)
{
yield return null;
}
FadeMessageScreen(false);
if (!PlayerInfoController.Instance.IsSinglePlayer)
m_Crosshair.Show();
}
private IEnumerator Type(string message)
{
m_CompletedShowMessage = false;
m_MessageLabel.text = "";
foreach (var c in message.ToCharArray())
{
m_MessageLabel.text += c;
yield return new WaitForSeconds(m_TypeDelay);
}
yield return new WaitForSeconds(1.5f);
m_CompletedShowMessage = true;
}
}
}