GameStateSystemComponent.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #pragma once
  9. #include <GameState/GameStateRequestBus.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/TickBus.h>
  12. #include <AzCore/std/containers/deque.h>
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. namespace GameState
  15. {
  16. ////////////////////////////////////////////////////////////////////////////////////////////////
  17. //! This system component manages game state instances and the transitions between them. A few
  18. //! default game states are implemented in the GameStateSamples Gem, and these can be extended
  19. //! as needed in order to provide a custom experience for each game, but it's also possible to
  20. //! create completely new states by inheriting from the abstract GameState::IGameState class.
  21. //! States are managed using a stack (pushdown automaton) in order to maintain their history.
  22. class GameStateSystemComponent : public AZ::Component
  23. , public AZ::TickBus::Handler
  24. , public GameStateRequestBus::Handler
  25. {
  26. public:
  27. ////////////////////////////////////////////////////////////////////////////////////////////
  28. // AZ::Component Setup
  29. AZ_COMPONENT(GameStateSystemComponent, "{03A10E41-3339-42C1-A6C8-A81327CB034B}");
  30. ////////////////////////////////////////////////////////////////////////////////////////////
  31. //! \ref AZ::ComponentDescriptor::Reflect
  32. static void Reflect(AZ::ReflectContext* context);
  33. ////////////////////////////////////////////////////////////////////////////////////////////
  34. //! \ref AZ::ComponentDescriptor::GetProvidedServices
  35. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  36. ////////////////////////////////////////////////////////////////////////////////////////////
  37. //! \ref AZ::ComponentDescriptor::GetIncompatibleServices
  38. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  39. protected:
  40. ////////////////////////////////////////////////////////////////////////////////////////////
  41. //! \ref AZ::Component::Activate
  42. void Activate() override;
  43. ////////////////////////////////////////////////////////////////////////////////////////////
  44. //! \ref AZ::Component::Deactivate
  45. void Deactivate() override;
  46. ////////////////////////////////////////////////////////////////////////////////////////////
  47. //! \ref AZ::TickEvents::GetTickOrder
  48. int GetTickOrder() override;
  49. ////////////////////////////////////////////////////////////////////////////////////////////
  50. //! \ref AZ::TickEvents::OnTick
  51. void OnTick(float deltaTime, AZ::ScriptTimePoint scriptTimePoint) override;
  52. ////////////////////////////////////////////////////////////////////////////////////////////
  53. //! \ref GameState::GameStateRequests::UpdateActiveGameState
  54. void UpdateActiveGameState() override;
  55. ////////////////////////////////////////////////////////////////////////////////////////////
  56. //! \ref GameState::GameStateRequests::GetActiveGameState
  57. AZStd::shared_ptr<IGameState> GetActiveGameState() override;
  58. ////////////////////////////////////////////////////////////////////////////////////////////
  59. //! \ref GameState::GameStateRequests::PushGameState
  60. bool PushGameState(AZStd::shared_ptr<IGameState> newGameState) override;
  61. ////////////////////////////////////////////////////////////////////////////////////////////
  62. //! \ref GameState::GameStateRequests::PopActiveGameState
  63. bool PopActiveGameState() override;
  64. ////////////////////////////////////////////////////////////////////////////////////////////
  65. //! \ref GameState::GameStateRequests::PopAllGameStates
  66. void PopAllGameStates() override;
  67. ////////////////////////////////////////////////////////////////////////////////////////////
  68. //! \ref GameState::GameStateRequests::ReplaceActiveGameState
  69. bool ReplaceActiveGameState(AZStd::shared_ptr<IGameState> newGameState) override;
  70. ////////////////////////////////////////////////////////////////////////////////////////////
  71. //! \ref GameState::GameStateRequests::DoesStackContainGameStateOfTypeId
  72. bool DoesStackContainGameStateOfTypeId(const AZ::TypeId& gameStateTypeId) override;
  73. ////////////////////////////////////////////////////////////////////////////////////////////
  74. //! \ref GameState::GameStateRequests::AddGameStateFactoryOverrideForTypeId
  75. bool AddGameStateFactoryOverrideForTypeId(const AZ::TypeId& gameStateTypeId,
  76. GameStateFactory factory) override;
  77. ////////////////////////////////////////////////////////////////////////////////////////////
  78. //! \ref GameState::GameStateRequests::RemoveGameStateFactoryOverrideForTypeId
  79. bool RemoveGameStateFactoryOverrideForTypeId(const AZ::TypeId& gameStateTypeId) override;
  80. ////////////////////////////////////////////////////////////////////////////////////////////
  81. //! \ref GameState::GameStateRequests::GetGameStateFactoryOverrideForTypeId
  82. GameStateFactory GetGameStateFactoryOverrideForTypeId(const AZ::TypeId& gameStateTypeId) override;
  83. private:
  84. ////////////////////////////////////////////////////////////////////////////////////////////
  85. //! The game state stack, where the top element is considered to be the active game state
  86. AZStd::deque<AZStd::shared_ptr<IGameState>> m_gameStateStack;
  87. ////////////////////////////////////////////////////////////////////////////////////////////
  88. //! A map of game state factory functions indexed by the game state type id to override
  89. AZStd::unordered_map<AZ::TypeId, GameStateFactory> m_gameStateFactoryOverrides;
  90. };
  91. }