-
Notifications
You must be signed in to change notification settings - Fork 324
/
HUDVitals.cs
365 lines (314 loc) · 14.8 KB
/
HUDVitals.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
using UnityEngine;
using DaggerfallWorkshop.Game.Entity;
namespace DaggerfallWorkshop.Game.UserInterface
{
/// <summary>
/// Player vital signs for HUD.
/// </summary>
public class HUDVitals : Panel
{
const string healthBarFilename = "MAIN03I0.IMG";
const string fatigueBarFilename = "MAIN04I0.IMG";
const string magickaBarFilename = "MAIN05I0.IMG";
public const int nativeBarWidth = 4;
const int nativeBarHeight = 32;
public const int borderSize = 10;
VitalsChangeDetector vitalsDetector = GameManager.Instance.VitalsChangeDetector;
VerticalProgressSmoother healthBar = new VerticalProgressSmoother();
VerticalProgressSmoother fatigueBar = new VerticalProgressSmoother();
VerticalProgressSmoother magickaBar = new VerticalProgressSmoother();
VerticalProgressSmoother healthBarLoss = new VerticalProgressSmoother();
VerticalProgressSmoother fatigueBarLoss = new VerticalProgressSmoother();
VerticalProgressSmoother magickaBarLoss = new VerticalProgressSmoother();
VerticalProgress healthBarGain = new VerticalProgress();
VerticalProgress fatigueBarGain = new VerticalProgress();
VerticalProgress magickaBarGain = new VerticalProgress();
PlayerEntity playerEntity;
Color healthLossColor = new Color(0, 0.22f, 0);
Color fatigueLossColor = new Color(0.44f, 0, 0);
Color magickaLossColor = new Color(0, 0, 0.44f);
Color healthGainColor = new Color(0.60f, 1f, 0.60f);
Color fatigueGainColor = new Color(1f, 0.50f, 0.50f);
Color magickaGainColor = new Color(0.70f, 0.70f, 1f);
/// <summary>
/// Gets or sets current health as value between 0 and 1.
/// </summary>
public float Health
{
get { return healthBarGain.Amount; }
set { healthBarGain.Amount = value; }
}
/// <summary>
/// Gets or sets current fatigue as value between 0 and 1.
/// </summary>
public float Fatigue
{
get { return fatigueBarGain.Amount; }
set { fatigueBarGain.Amount = value; }
}
/// <summary>
/// Gets or sets current magicka as value between 0 and 1.
/// </summary>
public float Magicka
{
get { return magickaBarGain.Amount; }
set { magickaBarGain.Amount = value; }
}
public Vector2? CustomHealthBarPosition { get; set; }
public Vector2? CustomHealthBarSize { get; set; }
public Vector2? CustomFatigueBarPosition { get; set; }
public Vector2? CustomFatigueBarSize { get; set; }
public Vector2? CustomMagickaBarPosition { get; set; }
public Vector2? CustomMagickaBarSize { get; set; }
public HUDVitals()
:base()
{
playerEntity = GameManager.Instance.PlayerEntity;
LoadAssets();
BackgroundColor = Color.clear;
HorizontalAlignment = HorizontalAlignment.Left;
VerticalAlignment = VerticalAlignment.Bottom;
SetMargins(Margins.All, borderSize);
healthBar.VerticalAlignment = VerticalAlignment.Bottom;
fatigueBar.VerticalAlignment = VerticalAlignment.Bottom;
magickaBar.VerticalAlignment = VerticalAlignment.Bottom;
if (DaggerfallUnity.Settings.EnableVitalsIndicators)
{
healthBarLoss.VerticalAlignment = VerticalAlignment.Bottom;
fatigueBarLoss.VerticalAlignment = VerticalAlignment.Bottom;
magickaBarLoss.VerticalAlignment = VerticalAlignment.Bottom;
healthBarGain.VerticalAlignment = VerticalAlignment.Bottom;
fatigueBarGain.VerticalAlignment = VerticalAlignment.Bottom;
magickaBarGain.VerticalAlignment = VerticalAlignment.Bottom;
// to make bar appear behind other bars, add it first.
Components.Add(healthBarLoss);
Components.Add(fatigueBarLoss);
Components.Add(magickaBarLoss);
Components.Add(healthBarGain);
Components.Add(fatigueBarGain);
Components.Add(magickaBarGain);
}
Components.Add(healthBar);
Components.Add(fatigueBar);
Components.Add(magickaBar);
VitalsChangeDetector.OnReset += VitalChangeDetector_OnReset;
vitalsDetector.HealthChanged += VitalsDetector_HealthChanged;
vitalsDetector.FatigueChanged += VitalsDetector_FatigueChanged;
vitalsDetector.MagickaChanged += VitalsDetector_MagickaChanged;
}
public void SetAllHorizontalAlignment(HorizontalAlignment alignment)
{
healthBar.HorizontalAlignment = alignment;
fatigueBar.HorizontalAlignment = alignment;
magickaBar.HorizontalAlignment = alignment;
healthBarLoss.HorizontalAlignment = alignment;
fatigueBarLoss.HorizontalAlignment = alignment;
magickaBarLoss.HorizontalAlignment = alignment;
healthBarGain.HorizontalAlignment = alignment;
fatigueBarGain.HorizontalAlignment = alignment;
magickaBarGain.HorizontalAlignment = alignment;
}
public void SetAllVerticalAlignment(VerticalAlignment alignment)
{
healthBar.VerticalAlignment = alignment;
fatigueBar.VerticalAlignment = alignment;
magickaBar.VerticalAlignment = alignment;
healthBarLoss.VerticalAlignment = alignment;
fatigueBarLoss.VerticalAlignment = alignment;
magickaBarLoss.VerticalAlignment = alignment;
healthBarGain.VerticalAlignment = alignment;
fatigueBarGain.VerticalAlignment = alignment;
magickaBarGain.VerticalAlignment = alignment;
}
public void SetAllAutoSize(AutoSizeModes mode)
{
healthBar.AutoSize = mode;
fatigueBar.AutoSize = mode;
magickaBar.AutoSize = mode;
healthBarLoss.AutoSize = mode;
fatigueBarLoss.AutoSize = mode;
magickaBarLoss.AutoSize = mode;
healthBarGain.AutoSize = mode;
fatigueBarGain.AutoSize = mode;
magickaBarGain.AutoSize = mode;
}
public void SetAllParent(Panel parent)
{
healthBar.Parent = parent;
fatigueBar.Parent = parent;
magickaBar.Parent = parent;
healthBarLoss.Parent = parent;
fatigueBarLoss.Parent = parent;
magickaBarLoss.Parent = parent;
healthBarGain.Parent = parent;
fatigueBarGain.Parent = parent;
magickaBarGain.Parent = parent;
}
void LoadAssets()
{
if (DaggerfallUnity.Settings.SwapHealthAndFatigueColors)
{
healthBar.ProgressTexture = DaggerfallUI.GetTextureFromImg(fatigueBarFilename);
fatigueBar.ProgressTexture = DaggerfallUI.GetTextureFromImg(healthBarFilename);
healthBarLoss.Color = fatigueLossColor;
fatigueBarLoss.Color = healthLossColor;
healthBarGain.Color = fatigueGainColor;
fatigueBarGain.Color = healthGainColor;
}
else
{
healthBar.ProgressTexture = DaggerfallUI.GetTextureFromImg(healthBarFilename);
fatigueBar.ProgressTexture = DaggerfallUI.GetTextureFromImg(fatigueBarFilename);
healthBarLoss.Color = healthLossColor;
fatigueBarLoss.Color = fatigueLossColor;
healthBarGain.Color = healthGainColor;
fatigueBarGain.Color = fatigueGainColor;
}
magickaBar.ProgressTexture = DaggerfallUI.GetTextureFromImg(magickaBarFilename);
magickaBarLoss.Color = magickaLossColor;
magickaBarGain.Color = magickaGainColor;
}
public override void Update()
{
if (Enabled)
{
base.Update();
PositionIndicators();
if (DaggerfallUnity.Settings.EnableVitalsIndicators)
UpdateAllVitals();
else
SynchronizeImmediately();
}
}
void PositionIndicators()
{
// Most of this depends on Scale and LiveEndurance only,
// so does not change from frame to frame; Is it worth optimizing?
float barWidth = nativeBarWidth * Scale.x;
float barHeight = nativeBarHeight * Scale.y;
Size = new Vector2(barWidth * 5, barHeight);
healthBar.Position = (CustomHealthBarPosition != null) ? CustomHealthBarPosition.Value : Position + new Vector2(0, 0);
healthBar.Size = (CustomHealthBarSize != null) ? CustomHealthBarSize.Value : new Vector2(barWidth, barHeight);
fatigueBar.Position = (CustomFatigueBarPosition != null) ? CustomFatigueBarPosition.Value : Position + new Vector2(barWidth * 2, 0);
fatigueBar.Size = (CustomFatigueBarSize != null) ? CustomFatigueBarSize.Value : new Vector2(barWidth, barHeight);
magickaBar.Position = (CustomMagickaBarPosition != null) ? CustomMagickaBarPosition.Value : Position + new Vector2(barWidth * 4, 0);
magickaBar.Size = (CustomMagickaBarSize != null) ? CustomMagickaBarSize.Value : new Vector2(barWidth, barHeight);
if (DaggerfallUnity.Settings.EnableVitalsIndicators)
{
healthBarLoss.Position = healthBar.Position;
healthBarLoss.Size = healthBar.Size;
fatigueBarLoss.Position = fatigueBar.Position;
fatigueBarLoss.Size = fatigueBar.Size;
magickaBarLoss.Position = magickaBar.Position;
magickaBarLoss.Size = magickaBar.Size;
healthBarGain.Position = healthBar.Position;
healthBarGain.Size = healthBar.Size;
fatigueBarGain.Position = fatigueBar.Position;
fatigueBarGain.Size = fatigueBar.Size;
magickaBarGain.Position = magickaBar.Position;
magickaBarGain.Size = magickaBar.Size;
}
}
private void SynchronizeImmediately()
{
// Adjust vitals based on current player state
healthBar.Amount = playerEntity.CurrentHealth / (float)playerEntity.MaxHealth;
fatigueBar.Amount = playerEntity.CurrentFatigue / (float)playerEntity.MaxFatigue;
magickaBar.Amount = playerEntity.CurrentMagicka / (float)playerEntity.MaxMagicka;
if (DaggerfallUnity.Settings.EnableVitalsIndicators)
{
healthBarGain.Amount = healthBar.Amount;
healthBarLoss.Amount = healthBar.Amount;
fatigueBarGain.Amount = fatigueBar.Amount;
fatigueBarLoss.Amount = fatigueBar.Amount;
magickaBarGain.Amount = magickaBar.Amount;
magickaBarLoss.Amount = magickaBar.Amount;
}
}
void UpdateAllVitals()
{
// these progress bars never smooth-change.
healthBarGain.Amount = playerEntity.CurrentHealth / (float)playerEntity.MaxHealth;
fatigueBarGain.Amount = playerEntity.CurrentFatigue / (float)playerEntity.MaxFatigue;
magickaBarGain.Amount = playerEntity.CurrentMagicka / (float)playerEntity.MaxMagicka;
healthBarLoss.Cycle();
fatigueBarLoss.Cycle();
magickaBarLoss.Cycle();
healthBar.Cycle();
fatigueBar.Cycle();
magickaBar.Cycle();
}
private void VitalsDetector_HealthChanged(object sender, System.EventArgs e)
{
if (!DaggerfallUnity.Settings.EnableVitalsIndicators)
{
return;
}
healthBarGain.Amount = playerEntity.CurrentHealth / (float)playerEntity.MaxHealth;
// if there's any change in health... Smooth update the Loss bar, and
// decide if should smooth update or instant update the progress bar
if (vitalsDetector.HealthLost != 0)
{
if (vitalsDetector.HealthLost > 0)
healthBar.Amount -= vitalsDetector.HealthLostPercent;
else // assumed gaining health
healthBarLoss.Amount += vitalsDetector.HealthGainPercent;
var target = healthBarGain.Amount;
healthBar.BeginSmoothChange(target);
healthBarLoss.BeginSmoothChange(target);
}
}
private void VitalsDetector_FatigueChanged(object sender, System.EventArgs e)
{
if (!DaggerfallUnity.Settings.EnableVitalsIndicators)
{
return;
}
fatigueBar.Amount = playerEntity.CurrentFatigue / (float)playerEntity.MaxFatigue;
// if there's any change in fatigue...
if (vitalsDetector.FatigueLost != 0)
{
if (vitalsDetector.FatigueLost > 0)
fatigueBar.Amount -= vitalsDetector.FatigueLostPercent;
else // assumed gaining health
fatigueBarLoss.Amount += vitalsDetector.FatigueGainPercent;
var target = fatigueBarGain.Amount;
fatigueBar.BeginSmoothChange(target);
fatigueBarLoss.BeginSmoothChange(target);
}
}
private void VitalsDetector_MagickaChanged(object sender, System.EventArgs e)
{
if (!DaggerfallUnity.Settings.EnableVitalsIndicators)
{
return;
}
magickaBarGain.Amount = playerEntity.CurrentMagicka / (float)playerEntity.MaxMagicka;
// if there's any change in magicka...
if (vitalsDetector.MagickaLost != 0)
{
if (vitalsDetector.MagickaLost > 0)
magickaBar.Amount -= vitalsDetector.MagickaLostPercent;
else // assumed gaining health
magickaBarLoss.Amount += vitalsDetector.MagickaGainPercent;
var target = magickaBarGain.Amount;
magickaBar.BeginSmoothChange(target);
magickaBarLoss.BeginSmoothChange(target);
}
}
private void VitalChangeDetector_OnReset()
{
SynchronizeImmediately();
}
}
}