GameStateSamplesModule.cpp 9.5 KB

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