SimpleStateComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <AzCore/std/containers/vector.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/EntityBus.h>
  12. #include <LmbrCentral/Scripting/SimpleStateComponentBus.h>
  13. namespace LmbrCentral
  14. {
  15. /**
  16. * State
  17. *
  18. * Structure describing a single state
  19. */
  20. class State
  21. : private AZ::EntityBus::MultiHandler
  22. {
  23. public:
  24. AZ_TYPE_INFO(State, "{97BCF9D8-A76D-456F-A4B8-98EFF6897CE7}");
  25. State();
  26. void Init();
  27. void Activate();
  28. void Deactivate();
  29. const char* GetNameCStr() const
  30. {
  31. return m_name.c_str();
  32. }
  33. const AZStd::string& GetName() const
  34. {
  35. return m_name;
  36. }
  37. static State* FindWithName(AZStd::vector<State>& states, const char* stateName);
  38. static void Reflect(AZ::ReflectContext* context);
  39. private:
  40. //////////////////////////////////////////////////////////////////////////
  41. // EntityBus::Handler
  42. void OnEntityExists(const AZ::EntityId& entityId) override;
  43. //////////////////////////////////////////////////////////////////////////
  44. AZ::Crc32 OnStateNameChanged();
  45. void UpdateNameCrc();
  46. // runtime value, not serialized
  47. AZ::Crc32 m_nameCrc;
  48. // serialized values
  49. AZStd::string m_name;
  50. AZStd::vector<AZ::EntityId> m_entityIds;
  51. };
  52. /**
  53. * SimpleStateComponent
  54. *
  55. * SimpleState provides a simple state machine.
  56. *
  57. * Each state is represented by a name and zero or more entities to activate when entered and deactivate when the state is left.
  58. */
  59. class SimpleStateComponent
  60. : public AZ::Component
  61. , public SimpleStateComponentRequestBus::Handler
  62. {
  63. public:
  64. AZ_COMPONENT(SimpleStateComponent, "{242D4707-BC72-4245-AC96-BCEE38BBC1B7}");
  65. SimpleStateComponent();
  66. ~SimpleStateComponent() override {}
  67. //////////////////////////////////////////////////////////////////////////
  68. // AZ::Component
  69. void Init() override;
  70. void Activate() override;
  71. void Deactivate() override;
  72. //////////////////////////////////////////////////////////////////////////
  73. //////////////////////////////////////////////////////////////////////////
  74. // SimpleStateComponentRequestBus::Handler
  75. //////////////////////////////////////////////////////////////////////////
  76. void SetState(const char* stateName) override;
  77. void SetStateByIndex(AZ::u32 stateIndex) override;
  78. void SetToNextState() override;
  79. void SetToPreviousState() override;
  80. void SetToFirstState() override;
  81. void SetToLastState() override;
  82. AZ::u32 GetNumStates() override;
  83. const char* GetCurrentState() override;
  84. private:
  85. //////////////////////////////////////////////////////////////////////////
  86. // Component descriptor
  87. static void Reflect(AZ::ReflectContext* context);
  88. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  89. {
  90. provided.push_back(AZ_CRC_CE("SimpleStateService"));
  91. }
  92. //////////////////////////////////////////////////////////////////////////
  93. // Helpers
  94. AZStd::vector<AZStd::string> GetStateNames() const;
  95. void SetStateInternal(State * newState);
  96. void SetStateToOffset(AZ::s32 offset, State& fromNullState);
  97. //////////////////////////////////////////////////////////////////////////
  98. // Runtime state, not serialized
  99. State* m_initialState = nullptr;
  100. State* m_currentState = nullptr;
  101. // Serialized
  102. AZStd::string m_initialStateName;
  103. AZStd::vector<State> m_states;
  104. bool m_resetStateOnActivate = true;
  105. };
  106. } // namespace LmbrCentral