26
26
27
27
#include " Library/FileSystem/Memory/MemoryFileSystem.h"
28
28
#include " Library/Platform/Application/PlatformApplication.h"
29
+ #include " Library/Platform/Interface/PlatformEnumFunctions.h"
29
30
#include " Library/Platform/Interface/PlatformEvents.h"
30
31
31
32
#include " Utility/Exception.h"
@@ -149,38 +150,23 @@ void EngineController::pressGuiButton(std::string_view buttonId) {
149
150
}
150
151
151
152
void EngineController::goToGame () {
152
- ThrowingTicker ticker (this , " Couldn't return to game" );
153
-
154
- // Skip movies.
155
- while (current_screen_type == SCREEN_VIDEO) {
156
- pressAndReleaseKey (PlatformKey::KEY_ESCAPE);
157
- ticker.tick ();
158
- }
159
-
160
- // Can't always leave key settings menu by pressing ESC, so need custom handling.
161
- if (current_screen_type == SCREEN_KEYBOARD_OPTIONS) {
162
- pressGuiButton (" KeyBinding_Default" );
163
- ticker.tick ();
164
- }
165
-
166
- // Leave to game screen if we're in the game, or to main menu if we're in menus.
167
- while (current_screen_type != SCREEN_GAME && GetCurrentMenuID () != MENU_MAIN) {
168
- pressAndReleaseKey (PlatformKey::KEY_ESCAPE);
169
- ticker.tick (2 ); // Somehow tick(1) is not enough when we're trying to leave the game loading menu.
170
- }
171
-
172
- // If game is starting up - wait for main menu to appear.
173
- while (GetCurrentMenuID () == MENU_MAIN && lWindowList.empty ())
174
- ticker.tick ();
153
+ goToGameOrMainMenu ();
154
+ if (GetCurrentMenuID () == MENU_MAIN)
155
+ throw Exception (" Can't go to game from the main menu" );
175
156
}
176
157
177
158
void EngineController::goToInventory (int characterIndex) {
159
+ assert (characterIndex >= 1 && characterIndex <= 4 );
160
+
178
161
goToGame ();
179
162
180
- if (GetCurrentMenuID () != MENU_NONE)
181
- throw Exception (" Can't go to inventory from the main menu" );
163
+ if (pParty->activeCharacterIndex () != characterIndex) {
164
+ pressAndReleaseKey (platformKeyForDigit (characterIndex));
165
+ tick (1 );
166
+ if (pParty->activeCharacterIndex () != characterIndex)
167
+ throw Exception (" Couldn't activate character #{}" , characterIndex);
168
+ }
182
169
183
- pParty->setActiveCharacterIndex (characterIndex);
184
170
pressAndReleaseKey (PlatformKey::KEY_I);
185
171
tick (2 ); // Need two ticks for inventory to be shown.
186
172
@@ -189,14 +175,13 @@ void EngineController::goToInventory(int characterIndex) {
189
175
}
190
176
191
177
void EngineController::goToMainMenu () {
192
- ThrowingTicker ticker (this , " Couldn't return to main menu" );
193
-
194
- goToGame ();
195
-
178
+ goToGameOrMainMenu ();
196
179
if (GetCurrentMenuID () == MENU_MAIN)
197
180
return ;
198
181
assert (GetCurrentMenuID () == MENU_NONE);
199
182
183
+ ThrowingTicker ticker (this , " Couldn't return to main menu" );
184
+
200
185
// Go to in-game menu.
201
186
while (current_screen_type != SCREEN_MENU) {
202
187
pressAndReleaseKey (PlatformKey::KEY_ESCAPE);
@@ -343,15 +328,22 @@ void EngineController::teleportTo(MapId map, Vec3f position, int viewYaw) {
343
328
}
344
329
345
330
void EngineController::castSpell (int characterIndex, SpellId spell) {
346
- goToGame ( );
331
+ assert (characterIndex >= 1 && characterIndex <= 4 );
347
332
333
+ goToGame ();
348
334
if (GetCurrentMenuID () != MENU_NONE)
349
335
throw Exception (" Can't cast a spell from the main menu" );
350
336
337
+ if (pParty->activeCharacterIndex () != characterIndex) {
338
+ pressAndReleaseKey (platformKeyForDigit (characterIndex));
339
+ tick (1 );
340
+ if (pParty->activeCharacterIndex () != characterIndex)
341
+ throw Exception (" Couldn't activate character #{}" , characterIndex);
342
+ }
343
+
351
344
MagicSchool school = magicSchoolForSpell (spell);
352
345
int index = spellIndexInMagicSchool (spell);
353
346
354
- pParty->setActiveCharacterIndex (characterIndex);
355
347
pressGuiButton (" Game_CastSpell" );
356
348
tick (1 );
357
349
pressGuiButton (fmt::format (" SpellBook_School{}" , std::to_underlying (school)));
@@ -362,6 +354,32 @@ void EngineController::castSpell(int characterIndex, SpellId spell) {
362
354
tick (1 );
363
355
}
364
356
357
+ void EngineController::goToGameOrMainMenu () {
358
+ ThrowingTicker ticker (this , " Couldn't return to game" );
359
+
360
+ // Skip movies.
361
+ while (current_screen_type == SCREEN_VIDEO) {
362
+ pressAndReleaseKey (PlatformKey::KEY_ESCAPE);
363
+ ticker.tick ();
364
+ }
365
+
366
+ // Can't always leave key settings menu by pressing ESC, so need custom handling.
367
+ if (current_screen_type == SCREEN_KEYBOARD_OPTIONS) {
368
+ pressGuiButton (" KeyBinding_Default" );
369
+ ticker.tick ();
370
+ }
371
+
372
+ // Leave to game screen if we're in the game, or to main menu if we're in menus.
373
+ while (current_screen_type != SCREEN_GAME && GetCurrentMenuID () != MENU_MAIN) {
374
+ pressAndReleaseKey (PlatformKey::KEY_ESCAPE);
375
+ ticker.tick (2 ); // Somehow tick(1) is not enough when we're trying to leave the game loading menu.
376
+ }
377
+
378
+ // If game is starting up - wait for main menu to appear.
379
+ while (GetCurrentMenuID () == MENU_MAIN && lWindowList.empty ())
380
+ ticker.tick ();
381
+ }
382
+
365
383
GUIButton *EngineController::existingButton (std::string_view buttonId) {
366
384
auto findButton = [](std::string_view buttonId) -> GUIButton * {
367
385
for (GUIWindow *window : lWindowList)
0 commit comments