/
DaggerfallCharacterSheetWindow.cs
509 lines (433 loc) · 20.9 KB
/
DaggerfallCharacterSheetWindow.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// Project: Daggerfall Tools For Unity
// Copyright: Copyright (C) 2009-2018 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: Hazelnut
//
// Notes:
//
using UnityEngine;
using System;
using System.Collections.Generic;
using DaggerfallConnect;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop.Game.UserInterface;
using DaggerfallWorkshop.Game.Entity;
using DaggerfallWorkshop.Game.Formulas;
using DaggerfallWorkshop.Game.Guilds;
namespace DaggerfallWorkshop.Game.UserInterfaceWindows
{
/// <summary>
/// Implements character sheet window.
/// </summary>
public class DaggerfallCharacterSheetWindow : DaggerfallPopupWindow
{
const string nativeImgName = "INFO00I0.IMG";
private const int noAffiliationsMsgId = 19;
Texture2D nativeTexture;
PlayerEntity playerEntity;
TextLabel nameLabel = new TextLabel();
TextLabel raceLabel = new TextLabel();
TextLabel classLabel = new TextLabel();
TextLabel levelLabel = new TextLabel();
TextLabel goldLabel = new TextLabel();
TextLabel fatigueLabel = new TextLabel();
TextLabel healthLabel = new TextLabel();
TextLabel encumbranceLabel = new TextLabel();
Panel[] statPanels = new Panel[DaggerfallStats.Count];
TextLabel[] statLabels = new TextLabel[DaggerfallStats.Count];
PaperDoll characterPortrait = new PaperDoll();
StatsRollout statsRollout;
bool leveling = false;
const int minBonusPool = 4; // The minimum number of free points to allocate on level up
const int maxBonusPool = 6; // The maximum number of free points to allocate on level up
SoundClips levelUpSound = SoundClips.LevelUp;
PlayerEntity PlayerEntity
{
get { return (playerEntity != null) ? playerEntity : playerEntity = GameManager.Instance.PlayerEntity; }
}
public DaggerfallCharacterSheetWindow(IUserInterfaceManager uiManager)
: base(uiManager)
{
}
protected override void Setup()
{
// Load native texture
nativeTexture = DaggerfallUI.GetTextureFromImg(nativeImgName);
if (!nativeTexture)
throw new Exception("DaggerfallCharacterSheetWindow: Could not load native texture.");
// Always dim background
ParentPanel.BackgroundColor = ScreenDimColor;
// Setup native panel background
NativePanel.BackgroundTexture = nativeTexture;
// Character portrait
NativePanel.Components.Add(characterPortrait);
characterPortrait.Position = new Vector2(200, 8);
// Setup labels
nameLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(41, 4), NativePanel);
raceLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(41, 14), NativePanel);
classLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(46, 24), NativePanel);
levelLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(45, 34), NativePanel);
goldLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(39, 44), NativePanel);
fatigueLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(57, 54), NativePanel);
healthLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(52, 64), NativePanel);
encumbranceLabel = DaggerfallUI.AddDefaultShadowedTextLabel(new Vector2(90, 74), NativePanel);
// Setup stat labels
Vector2 panelPos = new Vector2(141, 17);
for (int i = 0; i < DaggerfallStats.Count; i++)
{
statPanels[i] = DaggerfallUI.AddPanel(new Rect(panelPos.x, panelPos.y, 28, 6), NativePanel);
statLabels[i] = DaggerfallUI.AddDefaultShadowedTextLabel(Vector2.zero, statPanels[i]);
statLabels[i].HorizontalAlignment = HorizontalAlignment.Center;
panelPos.y += 24f;
}
// Health button
Button healthButton = DaggerfallUI.AddButton(new Rect(4, 63, 128, 8), NativePanel);
healthButton.OnMouseClick += HealthButton_OnMouseClick;
// Affiliations button
Button affiliationsButton = DaggerfallUI.AddButton(new Rect(3, 84, 130, 8), NativePanel);
affiliationsButton.OnMouseClick += AffiliationsButton_OnMouseClick;
// Primary skills button
Button primarySkillsButton = DaggerfallUI.AddButton(new Rect(11, 106, 115, 8), NativePanel);
primarySkillsButton.OnMouseClick += PrimarySkillsButton_OnMouseClick;
// Major skills button
Button majorSkillsButton = DaggerfallUI.AddButton(new Rect(11, 116, 115, 8), NativePanel);
majorSkillsButton.OnMouseClick += MajorSkillsButton_OnMouseClick;
// Minor skills button
Button minorSkillsButton = DaggerfallUI.AddButton(new Rect(11, 126, 115, 8), NativePanel);
minorSkillsButton.OnMouseClick += MinorSkillsButton_OnMouseClick;
// Miscellaneous skills button
Button miscSkillsButton = DaggerfallUI.AddButton(new Rect(11, 136, 115, 8), NativePanel);
miscSkillsButton.OnMouseClick += MiscSkillsButton_OnMouseClick;
// Inventory button
Button inventoryButton = DaggerfallUI.AddButton(new Rect(3, 151, 65, 12), NativePanel);
inventoryButton.OnMouseClick += InventoryButton_OnMouseClick;
//inventoryButton.BackgroundColor = DaggerfallUI.DaggerfallUnityNotImplementedColor;
// Spellbook button
Button spellBookButton = DaggerfallUI.AddButton(new Rect(69, 151, 65, 12), NativePanel);
spellBookButton.OnMouseClick += SpellBookButton_OnMouseClick;
//spellBookButton.BackgroundColor = DaggerfallUI.DaggerfallUnityNotImplementedColor;
// Logbook button
Button logBookButton = DaggerfallUI.AddButton(new Rect(3, 165, 65, 12), NativePanel);
logBookButton.OnMouseClick += LogBookButton_OnMouseClick;
//logBookButton.BackgroundColor = DaggerfallUI.DaggerfallUnityNotImplementedColor;
// History button
Button historyButton = DaggerfallUI.AddButton(new Rect(69, 165, 65, 12), NativePanel);
historyButton.BackgroundColor = DaggerfallUI.DaggerfallUnityNotImplementedColor;
// Exit button
Button exitButton = DaggerfallUI.AddButton(new Rect(50, 179, 39, 19), NativePanel);
exitButton.OnMouseClick += ExitButton_OnMouseClick;
// Attribute popup text
Vector2 pos = new Vector2(141, 6);
for (int i = 0; i < DaggerfallStats.Count; i++)
{
Rect rect = new Rect(pos.x, pos.y, 28, 20);
AddAttributePopupButton((DFCareer.Stats)i, rect);
pos.y += 24f;
}
statsRollout = new StatsRollout(true);
statsRollout.OnStatChanged += StatsRollout_OnStatChanged;
// Update player paper doll for first time
UpdatePlayerValues();
characterPortrait.Refresh();
}
public override void OnPush()
{
Refresh();
}
public override void OnReturn()
{
Refresh();
}
public override void CancelWindow()
{
if (leveling)
{
if (!CheckIfDoneLeveling())
return;
}
base.CancelWindow();
}
#region Private Methods
// Adds button for attribute popup text
// NOTE: This has only partial functionality until %vars and all formatting tokens are supported
void AddAttributePopupButton(DFCareer.Stats stat, Rect rect)
{
Button button = DaggerfallUI.AddButton(rect, NativePanel);
button.Tag = DaggerfallUnity.TextProvider.GetStatDescriptionTextID(stat);
button.OnMouseClick += StatButton_OnMouseClick;
}
// Creates formatting tokens for skill popups
TextFile.Token[] CreateSkillTokens(DFCareer.Skills skill, bool twoColumn = false, int startPosition = 0)
{
List<TextFile.Token> tokens = new List<TextFile.Token>();
TextFile.Token skillNameToken = new TextFile.Token();
skillNameToken.text = DaggerfallUnity.Instance.TextProvider.GetSkillName(skill);
skillNameToken.formatting = TextFile.Formatting.Text;
TextFile.Token skillValueToken = new TextFile.Token();
skillValueToken.text = string.Format("{0}%", playerEntity.Skills.GetLiveSkillValue(skill));
skillValueToken.formatting = TextFile.Formatting.Text;
DFCareer.Stats primaryStat = DaggerfallSkills.GetPrimaryStat(skill);
TextFile.Token skillPrimaryStatToken = new TextFile.Token();
skillPrimaryStatToken.text = DaggerfallUnity.Instance.TextProvider.GetAbbreviatedStatName(primaryStat);
skillPrimaryStatToken.formatting = TextFile.Formatting.Text;
TextFile.Token positioningToken = new TextFile.Token();
positioningToken.formatting = TextFile.Formatting.PositionPrefix;
TextFile.Token tabToken = new TextFile.Token();
tabToken.formatting = TextFile.Formatting.PositionPrefix;
// Add tokens in order
if (!twoColumn)
{
tokens.Add(skillNameToken);
tokens.Add(tabToken);
tokens.Add(tabToken);
tokens.Add(tabToken);
tokens.Add(skillValueToken);
tokens.Add(tabToken);
tokens.Add(skillPrimaryStatToken);
}
else // miscellaneous skills
{
if (startPosition != 0) // if this is the second column
{
positioningToken.x = startPosition;
tokens.Add(positioningToken);
}
tokens.Add(skillNameToken);
positioningToken.x = startPosition + 85;
tokens.Add(positioningToken);
tokens.Add(skillValueToken);
positioningToken.x = startPosition + 112;
tokens.Add(positioningToken);
tokens.Add(skillPrimaryStatToken);
}
return tokens.ToArray();
}
void ShowSkillsDialog(List<DFCareer.Skills> skills, bool twoColumn = false)
{
bool secondColumn = false;
bool showHandToHandDamage = false;
List<TextFile.Token> tokens = new List<TextFile.Token>();
for (int i = 0; i < skills.Count; i++)
{
if (!showHandToHandDamage && (skills[i] == DFCareer.Skills.HandToHand))
showHandToHandDamage = true;
if (!twoColumn)
{
tokens.AddRange(CreateSkillTokens(skills[i]));
if (i < skills.Count - 1)
tokens.Add(TextFile.NewLineToken);
}
else
{
if (!secondColumn)
{
tokens.AddRange(CreateSkillTokens(skills[i], true));
secondColumn = !secondColumn;
}
else
{
tokens.AddRange(CreateSkillTokens(skills[i], true, 136));
secondColumn = !secondColumn;
if (i < skills.Count - 1)
tokens.Add(TextFile.NewLineToken);
}
}
}
if (showHandToHandDamage)
{
tokens.Add(TextFile.NewLineToken);
TextFile.Token HandToHandDamageToken = new TextFile.Token();
int minDamage = FormulaHelper.CalculateHandToHandMinDamage(playerEntity.Skills.GetLiveSkillValue(DFCareer.Skills.HandToHand));
int maxDamage = FormulaHelper.CalculateHandToHandMaxDamage(playerEntity.Skills.GetLiveSkillValue(DFCareer.Skills.HandToHand));
HandToHandDamageToken.text = DaggerfallUnity.Instance.TextProvider.GetSkillName(DFCareer.Skills.HandToHand) + " dmg: " + minDamage + "-" + maxDamage;
HandToHandDamageToken.formatting = TextFile.Formatting.Text;
tokens.Add(HandToHandDamageToken);
}
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this);
messageBox.SetTextTokens(tokens.ToArray(), null, false);
messageBox.ClickAnywhereToClose = true;
messageBox.Show();
}
void ShowAffiliationsDialog()
{
List<TextFile.Token> tokens = new List<TextFile.Token>();
List<Guild> guildMemberships = GameManager.Instance.GuildManager.GetMemberships();
if (guildMemberships.Count == 0)
DaggerfallUI.MessageBox(noAffiliationsMsgId);
else
{
TextFile.Token tab = TextFile.TabToken;
tab.x = 125;
tokens.Add(new TextFile.Token() {
text = HardStrings.affiliation,
formatting = TextFile.Formatting.TextHighlight
});
tokens.Add(tab);
tokens.Add(new TextFile.Token()
{
text = HardStrings.rank,
formatting = TextFile.Formatting.TextHighlight
});
tokens.Add(TextFile.NewLineToken);
foreach (Guild guild in guildMemberships)
{
tokens.Add(TextFile.CreateTextToken(guild.GetAffiliation()));
tokens.Add(tab);
tokens.Add(TextFile.CreateTextToken(guild.GetTitle() //)); DEBUG rep:
+ " (rep:" + guild.GetReputation(playerEntity).ToString() + ")"));
tokens.Add(TextFile.NewLineToken);
}
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this);
messageBox.SetTextTokens(tokens.ToArray(), null, false);
messageBox.ClickAnywhereToClose = true;
messageBox.Show();
}
}
void UpdatePlayerValues()
{
// Handle leveling up
if (PlayerEntity.ReadyToLevelUp)
{
leveling = true;
PlayerEntity.Level++;
PlayerEntity.MaxHealth += FormulaHelper.CalculateHitPointsPerLevelUp(PlayerEntity);
DaggerfallUI.Instance.PlayOneShot(levelUpSound);
// Roll bonus pool for player to distribute
// Using maxBonusPool + 1 for inclusive range
int bonusPool = UnityEngine.Random.Range(minBonusPool, maxBonusPool + 1);
// Add stats rollout for leveling up
NativePanel.Components.Add(statsRollout);
this.statsRollout.StartingStats = PlayerEntity.Stats.Clone();
this.statsRollout.WorkingStats = PlayerEntity.Stats.Clone();
this.statsRollout.BonusPool = bonusPool;
PlayerEntity.ReadyToLevelUp = false;
}
// Update main labels
nameLabel.Text = PlayerEntity.Name;
raceLabel.Text = PlayerEntity.RaceTemplate.Name;
classLabel.Text = PlayerEntity.Career.Name;
levelLabel.Text = PlayerEntity.Level.ToString();
goldLabel.Text = PlayerEntity.GetGoldAmount().ToString();
fatigueLabel.Text = string.Format("{0}/{1}", PlayerEntity.CurrentFatigue / DaggerfallEntity.FatigueMultiplier, PlayerEntity.MaxFatigue / DaggerfallEntity.FatigueMultiplier);
healthLabel.Text = string.Format("{0}/{1}", PlayerEntity.CurrentHealth, PlayerEntity.MaxHealth);
encumbranceLabel.Text = string.Format("{0}/{1}", (int)PlayerEntity.CarriedWeight, PlayerEntity.MaxEncumbrance);
// Update stat labels
for (int i = 0; i < DaggerfallStats.Count; i++)
{
if (!leveling)
statLabels[i].Text = PlayerEntity.Stats.GetLiveStatValue(i).ToString();
else
statLabels[i].Text = ""; // If leveling, statsRollout will fill in the stat labels.
// Handle stat colour changes
if (PlayerEntity.Stats.GetLiveStatValue(i) < PlayerEntity.Stats.GetPermanentStatValue(i))
statLabels[i].TextColor = DaggerfallUI.DaggerfallUnityStatDrainedTextColor;
else if (PlayerEntity.Stats.GetLiveStatValue(i) > PlayerEntity.Stats.GetPermanentStatValue(i))
statLabels[i].TextColor = DaggerfallUI.DaggerfallUnityStatIncreasedTextColor;
else
statLabels[i].TextColor = DaggerfallUI.DaggerfallDefaultTextColor;
}
}
void Refresh()
{
if (IsSetup)
{
UpdatePlayerValues();
characterPortrait.Refresh();
}
}
bool CheckIfDoneLeveling()
{
if (statsRollout.BonusPool > 0)
{
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this);
messageBox.SetText(HardStrings.mustDistributeBonusPoints);
messageBox.ClickAnywhereToClose = true;
messageBox.Show();
return false;
}
else
{
leveling = false;
PlayerEntity.Stats = statsRollout.WorkingStats;
NativePanel.Components.Remove(statsRollout);
return true;
}
}
#endregion
#region Event Handlers
private void HealthButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
DaggerfallMessageBox healthBox = DaggerfallUI.Instance.CreateHealthStatusBox(this);
healthBox.Show();
}
private void AffiliationsButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
ShowAffiliationsDialog();
}
private void PrimarySkillsButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
ShowSkillsDialog(PlayerEntity.GetPrimarySkills());
}
private void MajorSkillsButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
ShowSkillsDialog(PlayerEntity.GetMajorSkills());
}
private void MinorSkillsButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
ShowSkillsDialog(PlayerEntity.GetMinorSkills());
}
private void MiscSkillsButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
ShowSkillsDialog(PlayerEntity.GetMiscSkills(), true);
}
private void InventoryButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
uiManager.PostMessage(DaggerfallUIMessages.dfuiOpenInventoryWindow);
}
private void SpellBookButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
uiManager.PostMessage(DaggerfallUIMessages.dfuiOpenSpellBookWindow);
}
private void LogBookButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
uiManager.PostMessage(DaggerfallUIMessages.dfuiOpenQuestJournalWindow);
}
private void StatButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
if (!leveling)
{
DaggerfallMessageBox messageBox = new DaggerfallMessageBox(uiManager, this);
messageBox.SetTextTokens((int)sender.Tag, playerEntity.Stats);
messageBox.ClickAnywhereToClose = true;
messageBox.Show();
}
else
{
// If leveling, let the statsRollOut use the stat buttons
}
}
private void ExitButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
if (leveling)
{
if (!CheckIfDoneLeveling())
return;
}
CloseWindow();
}
private void StatsRollout_OnStatChanged()
{
UpdateSecondaryStatLabels();
}
private void UpdateSecondaryStatLabels()
{
DaggerfallStats workingStats = statsRollout.WorkingStats;
fatigueLabel.Text = string.Format("{0}/{1}", PlayerEntity.CurrentFatigue / DaggerfallEntity.FatigueMultiplier, workingStats.LiveStrength + workingStats.LiveEndurance);
encumbranceLabel.Text = string.Format("{0}/{1}", (int)PlayerEntity.CarriedWeight, FormulaHelper.MaxEncumbrance(workingStats.LiveStrength));
}
#endregion
}
}