GameStateSamplesModule.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Component/ComponentApplicationBus.h>
  9. #include <AzCore/Component/TickBus.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <AzCore/Module/Module.h>
  12. #include <AzCore/std/smart_ptr/make_shared.h>
  13. #include <GameStateSamples/GameOptionRequestBus.h>
  14. #include <GameStateSamples/GameStateLevelRunning.h>
  15. #include <GameStateSamples/GameStateMainMenu.h>
  16. #include <GameStateSamples/GameStatePrimaryUserSelection.h>
  17. #include <GameStateSamples/GameStateSamples_Traits_Platform.h>
  18. #include <IConsole.h>
  19. #include <IGem.h>
  20. namespace GameStateSamples
  21. {
  22. using namespace GameState;
  23. ////////////////////////////////////////////////////////////////////////////////////////////////
  24. //! This Gem provides a set of sample game states that can be overridden (or replaced entirely)
  25. //! in order to customize the functionality as needed for your game. To circumvent this default
  26. //! set of game states, push a custom game state before GameStateModule::OnCrySystemInitialized
  27. //! is called, or just don't enable this Gem for your project (only the GameState Gem is needed
  28. //! if you plan on creating entirely custom game states). The flow of the sample game states in
  29. //! this Gem is roughly as follows:
  30. //!
  31. //! GameStatePrimaryUserSelection
  32. //! |
  33. //! V
  34. //! GameStatePrimaryUserMonitor____
  35. //! | |
  36. //! V |
  37. //! GameStateMainMenu |
  38. //! | |
  39. //! V |
  40. //! GameStateLevelLoading |
  41. //! | |
  42. //! V |
  43. //! GameStateLevelRunning |
  44. //! | |
  45. //! V |
  46. //! GameStateLevelPaused |
  47. //! |
  48. //! GameStatePrimaryUserSignedOut<--|
  49. //! |
  50. //! PrimaryControllerDisconnected<--|
  51. //!
  52. class GameStateSamplesModule
  53. : public CryHooksModule
  54. , public AZ::TickBus::Handler
  55. , public GameOptionRequestBus::Handler
  56. {
  57. public:
  58. AZ_RTTI(GameStateSamplesModule, "{FC206260-D188-45A5-8B23-1D7A1DA6E82F}", AZ::Module);
  59. AZ_CLASS_ALLOCATOR(GameStateSamplesModule, AZ::SystemAllocator, 0);
  60. GameStateSamplesModule()
  61. : CryHooksModule()
  62. {
  63. m_gameOptions = AZStd::make_shared<GameOptions>();
  64. AZ::SerializeContext* serializeContext = nullptr;
  65. AZ::ComponentApplicationBus::BroadcastResult(serializeContext,
  66. &AZ::ComponentApplicationRequests::GetSerializeContext);
  67. if (serializeContext)
  68. {
  69. m_gameOptions->Reflect(*serializeContext);
  70. }
  71. GameOptionRequestBus::Handler::BusConnect();
  72. }
  73. ~GameStateSamplesModule()
  74. {
  75. GameOptionRequestBus::Handler::BusDisconnect();
  76. m_gameOptions.reset();
  77. }
  78. protected:
  79. void OnCrySystemInitialized(ISystem& system, const SSystemInitParams& systemInitParams)
  80. {
  81. CryHooksModule::OnCrySystemInitialized(system, systemInitParams);
  82. AZ::TickBus::Handler::BusConnect();
  83. }
  84. void OnTick([[maybe_unused]]float deltaTime, [[maybe_unused]]AZ::ScriptTimePoint scriptTimePoint) override
  85. {
  86. // Ideally this would be called at startup (either above in OnCrySystemInitialized, or better during AZ system component
  87. // initialisation), but because the initial game state depends on loading a UI canvas using LYShine we need to wait until
  88. // the first tick, because LyShine in turn is not properly initialized until UiRenderer::OnBootstrapSceneReady has been
  89. // called, which doesn't happen until a queued tick event that gets called right at the end of initialisation before we
  90. // enter the main game loop.
  91. CreateAndPushInitialGameState();
  92. AZ::TickBus::Handler::BusDisconnect();
  93. }
  94. void CreateAndPushInitialGameState()
  95. {
  96. REGISTER_INT("sys_primaryUserSelectionEnabled", 2, VF_NULL,
  97. "Controls whether the game forces selection of a primary user at startup.\n"
  98. "0 : Skip selection of a primary user at startup on all platform.\n"
  99. "1 : Force selection of a primary user at startup on all platforms.\n"
  100. "2 : Force selection of a primary user at startup on console platforms (default).\n");
  101. REGISTER_INT("sys_pauseOnApplicationConstrained", 2, VF_NULL,
  102. "Controls whether the game should pause when the application is constrained.\n"
  103. "0 : Don't pause the game when the application is constrained on any platform.\n"
  104. "1 : Pause the game when the application is constrained on all platforms.\n"
  105. "2 : Pause the game when the application is constrained on console platforms (default).\n");
  106. REGISTER_INT("sys_localUserLobbyEnabled", 2, VF_NULL,
  107. "Controls whether the local user lobby should be enabled.\n"
  108. "0 : Don't enable the local user lobby on any platform.\n"
  109. "1 : Enable the local user lobby on all platforms.\n"
  110. "2 : Enable the local user lobby on console platforms (default).\n");
  111. if (gEnv && gEnv->IsEditor())
  112. {
  113. // Don't push any game states when running in the editor
  114. return;
  115. }
  116. AZStd::shared_ptr<IGameState> activeGameState;
  117. GameStateRequestBus::BroadcastResult(activeGameState, &GameStateRequests::GetActiveGameState);
  118. if (activeGameState)
  119. {
  120. // The game has pushed a custom initial game state
  121. return;
  122. }
  123. bool primaryUserSelectionEnabled = false;
  124. #if AZ_TRAIT_GAMESTATESAMPLES_PRIMARY_USER_SELECTION_ENABLED
  125. primaryUserSelectionEnabled = true;
  126. #else
  127. primaryUserSelectionEnabled = false;
  128. #endif // AZ_TRAIT_GAMESTATESAMPLES_PRIMARY_USER_SELECTION_ENABLED
  129. if (gEnv && gEnv->pConsole && gEnv->pConsole->GetCVar("sys_primaryUserSelectionEnabled"))
  130. {
  131. switch (gEnv->pConsole->GetCVar("sys_primaryUserSelectionEnabled")->GetIVal())
  132. {
  133. case 0: { primaryUserSelectionEnabled = false; } break;
  134. case 1: { primaryUserSelectionEnabled = true; } break;
  135. default: break; // Use the default value that was set above
  136. }
  137. }
  138. if (primaryUserSelectionEnabled)
  139. {
  140. GameStateRequests::CreateAndPushNewOverridableGameStateOfType<GameStatePrimaryUserSelection>();
  141. }
  142. else
  143. {
  144. GameStateRequests::CreateAndPushNewOverridableGameStateOfType<GameStateMainMenu>();
  145. }
  146. }
  147. void OnSystemEvent(ESystemEvent systemEvent, UINT_PTR wparam, UINT_PTR /*lparam*/) override
  148. {
  149. // This logic is a little confusing, but we need to check for both...
  150. // ...the END of a switch INTO game mode...
  151. if (systemEvent == ESYSTEM_EVENT_GAME_MODE_SWITCH_END && wparam)
  152. {
  153. OnEditorGameModeEntered();
  154. }
  155. // ...and the START of a switch OUT OF game mode.
  156. else if (systemEvent == ESYSTEM_EVENT_GAME_MODE_SWITCH_START && !wparam)
  157. {
  158. OnEditorGameModeExiting();
  159. }
  160. }
  161. AZStd::shared_ptr<GameOptions> GetGameOptions() override
  162. {
  163. return m_gameOptions;
  164. }
  165. private:
  166. void OnEditorGameModeEntered()
  167. {
  168. AZStd::shared_ptr<IGameState> activeGameState;
  169. GameStateRequestBus::BroadcastResult(activeGameState, &GameStateRequests::GetActiveGameState);
  170. AZ_Assert(!activeGameState, "OnEditorGameModeStart: The game state stack is not empty.");
  171. // After entering game mode from the editor, transition straight into the level running state
  172. GameStateRequests::CreateAndPushNewOverridableGameStateOfType<GameStateLevelRunning>();
  173. }
  174. void OnEditorGameModeExiting()
  175. {
  176. // Before exiting game mode from the editor, clear all active game states
  177. GameStateRequestBus::Broadcast(&GameStateRequests::PopAllGameStates);
  178. }
  179. private:
  180. AZStd::shared_ptr<GameOptions> m_gameOptions;
  181. };
  182. }
  183. // DO NOT MODIFY THIS LINE UNLESS YOU RENAME THE GEM
  184. // The first parameter should be GemName_GemIdLower
  185. // The second should be the fully qualified name of the class above
  186. AZ_DECLARE_MODULE_CLASS(Gem_GameStateSamples, GameStateSamples::GameStateSamplesModule)