/
DaggerfallDaedraSummonedWindow.cs
173 lines (152 loc) · 6.22 KB
/
DaggerfallDaedraSummonedWindow.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
// 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: Hazelnut
// Contributors:
//
// Notes:
//
using UnityEngine;
using DaggerfallWorkshop.Game.UserInterface;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop.Utility;
using System;
using DaggerfallWorkshop.Game.Questing;
namespace DaggerfallWorkshop.Game.UserInterfaceWindows
{
/// <summary>
/// Class for handling Daedra summoning.
/// </summary>
public class DaggerfallDaedraSummonedWindow : DaggerfallBaseWindow
{
protected const int TextLinesPerChunk = 4;
protected const int TokensPerChunk = TextLinesPerChunk * 2;
FLCPlayer playerPanel;
MultiFormatTextLabel messageLabel;
TextCursor textCursor;
DaggerfallQuestPopupWindow.DaedraData daedraSummoned;
Quest daedraQuest;
int textId;
IMacroContextProvider mcp;
TextFile.Token[] messageTokens;
bool lastChunk = false;
bool answerGiven = false;
int idx = 0;
public DaggerfallDaedraSummonedWindow(IUserInterfaceManager uiManager, DaggerfallQuestPopupWindow.DaedraData daedraSummoned, Quest daedraQuest)
: base(uiManager)
{
this.daedraSummoned = daedraSummoned;
this.daedraQuest = daedraQuest;
}
public DaggerfallDaedraSummonedWindow(IUserInterfaceManager uiManager, DaggerfallQuestPopupWindow.DaedraData daedraSummoned, int textId, IMacroContextProvider mcp)
: base(uiManager)
{
this.daedraSummoned = daedraSummoned;
this.textId = textId;
this.mcp = mcp;
answerGiven = true;
}
protected override void Setup()
{
if (IsSetup)
return;
// Initialise the video panel.
playerPanel = new FLCPlayer()
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Middle,
Size = new Vector2(320, 200),
BottomMargin = 13
};
NativePanel.Components.Add(playerPanel);
// Start video playing.
playerPanel.Load(daedraSummoned.vidFile);
if (playerPanel.FLCFile.ReadyToPlay)
playerPanel.Start();
// Add text message area.
messageLabel = new MultiFormatTextLabel()
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Bottom,
ExtraLeading = 3,
};
playerPanel.Components.Add(messageLabel);
playerPanel.OnMouseClick += PlayerPanel_OnMouseClick;
textCursor = new TextCursor();
textCursor.Enabled = false;
playerPanel.Components.Add(textCursor);
// Initialise message to display,
if (daedraQuest != null)
{ // with the quest offer message.
Message message = daedraQuest.GetMessage((int)QuestMachine.QuestMessages.QuestorOffer);
messageTokens = message.GetTextTokens();
}
else
{ // with the textId message evaluated with mcp provided.
messageTokens = DaggerfallUnity.Instance.TextProvider.GetRSCTokens(textId);
MacroHelper.ExpandMacros(ref messageTokens, mcp);
}
idx = 0;
DisplayNextTextChunk();
}
public override void Update()
{
base.Update();
if (lastChunk && !answerGiven)
{
HotkeySequence.KeyModifiers keyModifiers = HotkeySequence.GetKeyboardKeyModifiers();
if (DaggerfallShortcut.GetBinding(DaggerfallShortcut.Buttons.Yes).IsUpWith(keyModifiers))
{
HandleAnswer(QuestMachine.QuestMessages.AcceptQuest);
QuestMachine.Instance.StartQuest(daedraQuest);
}
else if (DaggerfallShortcut.GetBinding(DaggerfallShortcut.Buttons.No).IsUpWith(keyModifiers))
{
HandleAnswer(QuestMachine.QuestMessages.RefuseQuest);
GameObjectHelper.CreateFoeSpawner(true, DaggerfallQuestPopupWindow.daedricFoes[UnityEngine.Random.Range(0, 5)], UnityEngine.Random.Range(3, 6), 8, 64);
}
}
}
private void HandleAnswer(QuestMachine.QuestMessages qMessage)
{
lastChunk = false;
textCursor.Enabled = false;
answerGiven = true;
idx = 0;
Message message = daedraQuest.GetMessage((int) qMessage);
messageTokens = message.GetTextTokens();
playerPanel.OnMouseClick += PlayerPanel_OnMouseClick;
DisplayNextTextChunk();
}
private void DisplayNextTextChunk()
{
TextFile.Token[] chunk = new TextFile.Token[TokensPerChunk];
int len = Math.Min(TokensPerChunk, messageTokens.Length - idx);
Array.Copy(messageTokens, idx, chunk, 0, len);
messageLabel.SetText(chunk);
// Is this the last chunk?
if (len < TokensPerChunk || idx + len == messageTokens.Length)
{
lastChunk = true;
if (!answerGiven)
{ // Disable click and listen for Y/N keypress.
playerPanel.OnMouseClick -= PlayerPanel_OnMouseClick;
textCursor.Position = new Vector2(310, 190);
textCursor.Enabled = true;
textCursor.BlinkOn();
}
}
idx += TokensPerChunk;
}
private void PlayerPanel_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
// If done, close window on click after last chunk, else display next chunk of text.
if (lastChunk && answerGiven)
CloseWindow();
else if (!lastChunk)
DisplayNextTextChunk();
}
}
}