GameStateSystemComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <GameStateSystemComponent.h>
  9. #include <GameState/GameStateNotificationBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/EditContextConstants.inl>
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. namespace GameState
  15. {
  16. ////////////////////////////////////////////////////////////////////////////////////////////////
  17. void GameStateSystemComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  20. {
  21. serialize->Class<GameStateSystemComponent, AZ::Component>()
  22. ->Version(0);
  23. if (AZ::EditContext* ec = serialize->GetEditContext())
  24. {
  25. ec->Class<GameStateSystemComponent>("GameState", "A generic framework for managing game states and the transitions between them.")
  26. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  27. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  28. ;
  29. }
  30. }
  31. }
  32. ////////////////////////////////////////////////////////////////////////////////////////////////
  33. void GameStateSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  34. {
  35. provided.push_back(AZ_CRC_CE("GameStateService"));
  36. }
  37. ////////////////////////////////////////////////////////////////////////////////////////////////
  38. void GameStateSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  39. {
  40. incompatible.push_back(AZ_CRC_CE("GameStateService"));
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////////////////////
  43. void GameStateSystemComponent::Activate()
  44. {
  45. AZ::TickBus::Handler::BusConnect();
  46. GameStateRequestBus::Handler::BusConnect();
  47. }
  48. ////////////////////////////////////////////////////////////////////////////////////////////////
  49. void GameStateSystemComponent::Deactivate()
  50. {
  51. GameStateRequestBus::Handler::BusDisconnect();
  52. AZ::TickBus::Handler::BusDisconnect();
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////////////////////
  55. int GameStateSystemComponent::GetTickOrder()
  56. {
  57. return AZ::ComponentTickBus::TICK_GAME;
  58. }
  59. ////////////////////////////////////////////////////////////////////////////////////////////////
  60. void GameStateSystemComponent::OnTick(float /*deltaTime*/, AZ::ScriptTimePoint /*scriptTimePoint*/)
  61. {
  62. UpdateActiveGameState();
  63. }
  64. ////////////////////////////////////////////////////////////////////////////////////////////////
  65. void GameStateSystemComponent::UpdateActiveGameState()
  66. {
  67. AZStd::shared_ptr<IGameState> activeGameState = GetActiveGameState();
  68. if (activeGameState)
  69. {
  70. activeGameState->OnUpdate();
  71. }
  72. }
  73. ////////////////////////////////////////////////////////////////////////////////////////////////
  74. AZStd::shared_ptr<IGameState> GameStateSystemComponent::GetActiveGameState()
  75. {
  76. return m_gameStateStack.empty() ? nullptr : m_gameStateStack.back();
  77. }
  78. ////////////////////////////////////////////////////////////////////////////////////////////////
  79. bool GameStateSystemComponent::PushGameState(AZStd::shared_ptr<IGameState> newGameState)
  80. {
  81. // Error checking
  82. if (!newGameState)
  83. {
  84. AZ_Warning("GameStateSystemComponent", false,
  85. "Trying to push a null game state.");
  86. return false;
  87. }
  88. // Error checking
  89. const bool isAlreadyInStack = AZStd::find(m_gameStateStack.begin(),
  90. m_gameStateStack.end(),
  91. newGameState) != m_gameStateStack.end();
  92. if (isAlreadyInStack)
  93. {
  94. AZ_Warning("GameStateSystemComponent", false,
  95. "Trying to push a new game state that is already in the stack.");
  96. return false;
  97. }
  98. // Deactivate the currently active game state
  99. AZStd::shared_ptr<IGameState> oldGameState = GetActiveGameState();
  100. if (oldGameState)
  101. {
  102. oldGameState->OnExit();
  103. }
  104. // Push the new game state onto the stack to make it the active state
  105. m_gameStateStack.push_back(newGameState);
  106. newGameState->OnPushed();
  107. newGameState->OnEnter();
  108. // Inform any interested parties that the active game state has been changed
  109. GameStateNotificationBus::Broadcast(&GameStateNotifications::OnActiveGameStateChanged,
  110. oldGameState,
  111. newGameState);
  112. return true;
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////////
  115. bool GameStateSystemComponent::PopActiveGameState()
  116. {
  117. // Error checking
  118. if (m_gameStateStack.empty())
  119. {
  120. AZ_Warning("GameStateSystemComponent", false,
  121. "Trying to pop the active game state but the stack is empty.");
  122. return false;
  123. }
  124. // Deactivate the currently active game state before popping it from the stack
  125. AZStd::shared_ptr<IGameState> oldGameState = m_gameStateStack.back();
  126. oldGameState->OnExit();
  127. m_gameStateStack.pop_back();
  128. oldGameState->OnPopped();
  129. // Reactivate the next game state in the stack
  130. AZStd::shared_ptr<IGameState> newGameState = GetActiveGameState();
  131. if (newGameState)
  132. {
  133. newGameState->OnEnter();
  134. }
  135. // Inform any interested parties that the active game state has been changed
  136. GameStateNotificationBus::Broadcast(&GameStateNotifications::OnActiveGameStateChanged,
  137. oldGameState,
  138. newGameState);
  139. return true;
  140. }
  141. ////////////////////////////////////////////////////////////////////////////////////////////////
  142. void GameStateSystemComponent::PopAllGameStates()
  143. {
  144. AZStd::shared_ptr<IGameState> activeGameState = GetActiveGameState();
  145. while (activeGameState)
  146. {
  147. PopActiveGameState();
  148. activeGameState = GetActiveGameState();
  149. }
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////////////////////
  152. bool GameStateSystemComponent::ReplaceActiveGameState(AZStd::shared_ptr<IGameState> newGameState)
  153. {
  154. // Error checking
  155. if (!newGameState)
  156. {
  157. AZ_Warning("GameStateSystemComponent", false,
  158. "Trying to replace the active game state with a null game state.");
  159. return false;
  160. }
  161. // If no game state is currently active just push the new game state
  162. AZStd::shared_ptr<IGameState> oldGameState = GetActiveGameState();
  163. if (!oldGameState)
  164. {
  165. return PushGameState(newGameState);
  166. }
  167. // Deactivate the currently active game state before popping it from the stack
  168. oldGameState->OnExit();
  169. m_gameStateStack.pop_back();
  170. oldGameState->OnPopped();
  171. // Push the new game state onto the stack to make it the active state
  172. m_gameStateStack.push_back(newGameState);
  173. newGameState->OnPushed();
  174. newGameState->OnEnter();
  175. // Inform any interested parties that the active game state has been changed
  176. GameStateNotificationBus::Broadcast(&GameStateNotifications::OnActiveGameStateChanged,
  177. oldGameState,
  178. newGameState);
  179. return true;
  180. }
  181. ////////////////////////////////////////////////////////////////////////////////////////////////
  182. bool GameStateSystemComponent::DoesStackContainGameStateOfTypeId(const AZ::TypeId& gameStateTypeId)
  183. {
  184. // Reverse iterate over the stack until we find a game state of the specified type
  185. for (auto it = m_gameStateStack.rbegin(); it != m_gameStateStack.rend(); ++it)
  186. {
  187. const IGameState* gameState = (*it).get();
  188. if (azrtti_istypeof(gameStateTypeId, gameState))
  189. {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. ////////////////////////////////////////////////////////////////////////////////////////////////
  196. bool GameStateSystemComponent::AddGameStateFactoryOverrideForTypeId(const AZ::TypeId& gameStateTypeId,
  197. GameStateFactory factory)
  198. {
  199. if (!azrtti_istypeof(gameStateTypeId, factory().get()))
  200. {
  201. AZ_Warning("GameStateSystemComponent", false,
  202. "Trying to override a game state type with one that doesn't derive from it.");
  203. return false;
  204. }
  205. if (m_gameStateFactoryOverrides.find(gameStateTypeId) != m_gameStateFactoryOverrides.end())
  206. {
  207. AZ_Warning("GameStateSystemComponent", false,
  208. "Trying to override a game state type that has already been overriden.");
  209. return false;
  210. }
  211. m_gameStateFactoryOverrides[gameStateTypeId] = factory;
  212. return true;
  213. }
  214. ////////////////////////////////////////////////////////////////////////////////////////////////
  215. bool GameStateSystemComponent::RemoveGameStateFactoryOverrideForTypeId(const AZ::TypeId& gameStateTypeId)
  216. {
  217. return m_gameStateFactoryOverrides.erase(gameStateTypeId) > 0;
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////////////////////
  220. GameStateFactory GameStateSystemComponent::GetGameStateFactoryOverrideForTypeId(const AZ::TypeId& gameStateTypeId)
  221. {
  222. const auto& it = m_gameStateFactoryOverrides.find(gameStateTypeId);
  223. return it != m_gameStateFactoryOverrides.end() ? it->second : GameStateFactory();
  224. }
  225. }