LevelSettingsComponent.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "LevelSettingsComponent.h"
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <Vegetation/Ebuses/SystemConfigurationBus.h>
  13. namespace Vegetation
  14. {
  15. //////////////////////////////////////////////////////////////////////////
  16. // LevelSettingsConfig
  17. void LevelSettingsConfig::Reflect(AZ::ReflectContext* context)
  18. {
  19. InstanceSystemConfig::Reflect(context);
  20. AreaSystemConfig::Reflect(context);
  21. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<LevelSettingsConfig, AZ::ComponentConfig>()
  24. ->Version(0)
  25. ->Field("AreaSystemConfig", &LevelSettingsConfig::m_areaSystemConfig)
  26. ->Field("InstanceSystemConfig", &LevelSettingsConfig::m_instanceSystemConfig)
  27. ;
  28. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  29. {
  30. editContext->Class<LevelSettingsConfig>(
  31. "Vegetation System Settings", "The vegetation system settings for this level/map.")
  32. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  33. ->Attribute(AZ::Edit::Attributes::Category, "Vegetation")
  34. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  35. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  36. ->DataElement(0, &LevelSettingsConfig::m_areaSystemConfig, "Area System Settings", "Area management settings.")
  37. ->DataElement(0, &LevelSettingsConfig::m_instanceSystemConfig, "Instance System Settings", "Instance management settings.")
  38. ;
  39. }
  40. }
  41. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  42. {
  43. behaviorContext->Class<LevelSettingsConfig>()
  44. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  45. ->Constructor()
  46. ->Property("areaSystemConfig", BehaviorValueProperty(&LevelSettingsConfig::m_areaSystemConfig))
  47. ->Property("instanceSystemConfig", BehaviorValueProperty(&LevelSettingsConfig::m_instanceSystemConfig))
  48. ;
  49. }
  50. }
  51. //////////////////////////////////////////////////////////////////////////
  52. // LevelSettingsComponent
  53. void LevelSettingsComponent::Reflect(AZ::ReflectContext* context)
  54. {
  55. LevelSettingsConfig::Reflect(context);
  56. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  57. if (serializeContext)
  58. {
  59. serializeContext->Class<LevelSettingsComponent, AZ::Component>()
  60. ->Version(0)
  61. ->Field("Configuration", &LevelSettingsComponent::m_configuration)
  62. ;
  63. }
  64. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  65. {
  66. behaviorContext->Constant("LevelSettingsComponentTypeId", BehaviorConstant(LevelSettingsComponentTypeId));
  67. behaviorContext->Class<LevelSettingsComponent>()->RequestBus("LevelSettingsRequestBus");
  68. behaviorContext->EBus<LevelSettingsRequestBus>("LevelSettingsRequestBus")
  69. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  70. ->Event("GetAreaSystemConfig", &LevelSettingsRequestBus::Events::GetAreaSystemConfig)
  71. ->Event("GetInstanceSystemConfig", &LevelSettingsRequestBus::Events::GetInstanceSystemConfig)
  72. ;
  73. }
  74. }
  75. void LevelSettingsComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  76. {
  77. services.push_back(AZ_CRC_CE("VegetationLevelSettingsService"));
  78. }
  79. void LevelSettingsComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  80. {
  81. services.push_back(AZ_CRC_CE("VegetationLevelSettingsService"));
  82. }
  83. LevelSettingsComponent::LevelSettingsComponent(const LevelSettingsConfig& configuration)
  84. : m_configuration(configuration)
  85. {
  86. }
  87. LevelSettingsComponent::~LevelSettingsComponent()
  88. {
  89. }
  90. void LevelSettingsComponent::Init()
  91. {
  92. }
  93. void LevelSettingsComponent::Activate()
  94. {
  95. SystemConfigurationRequestBus::Broadcast(&SystemConfigurationRequestBus::Events::GetSystemConfig, &m_previousAreaSystemConfig);
  96. SystemConfigurationRequestBus::Broadcast(&SystemConfigurationRequestBus::Events::GetSystemConfig, &m_previousInstanceSystemConfig);
  97. LevelSettingsRequestBus::Handler::BusConnect(GetEntityId());
  98. m_active = true;
  99. UpdateSystemConfig();
  100. }
  101. void LevelSettingsComponent::Deactivate()
  102. {
  103. SystemConfigurationRequestBus::Broadcast(&SystemConfigurationRequestBus::Events::UpdateSystemConfig, &m_previousAreaSystemConfig);
  104. SystemConfigurationRequestBus::Broadcast(&SystemConfigurationRequestBus::Events::UpdateSystemConfig, &m_previousInstanceSystemConfig);
  105. LevelSettingsRequestBus::Handler::BusDisconnect();
  106. m_active = false;
  107. }
  108. bool LevelSettingsComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  109. {
  110. if (auto config = azrtti_cast<const LevelSettingsConfig*>(baseConfig))
  111. {
  112. m_configuration = *config;
  113. UpdateSystemConfig();
  114. return true;
  115. }
  116. return false;
  117. }
  118. bool LevelSettingsComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  119. {
  120. if (auto config = azrtti_cast<LevelSettingsConfig*>(outBaseConfig))
  121. {
  122. *config = m_configuration;
  123. return true;
  124. }
  125. return false;
  126. }
  127. void LevelSettingsComponent::UpdateSystemConfig()
  128. {
  129. // Need to check if active to avoid writing our data to the component while inactive, which will cause our save of previous
  130. // configs to be incorrect in Activate
  131. if (m_active)
  132. {
  133. SystemConfigurationRequestBus::Broadcast(&SystemConfigurationRequestBus::Events::UpdateSystemConfig, &m_configuration.m_areaSystemConfig);
  134. SystemConfigurationRequestBus::Broadcast(&SystemConfigurationRequestBus::Events::UpdateSystemConfig, &m_configuration.m_instanceSystemConfig);
  135. }
  136. }
  137. AreaSystemConfig* LevelSettingsComponent::GetAreaSystemConfig()
  138. {
  139. return &(m_configuration.m_areaSystemConfig);
  140. }
  141. InstanceSystemConfig* LevelSettingsComponent::GetInstanceSystemConfig()
  142. {
  143. return &(m_configuration.m_instanceSystemConfig);
  144. }
  145. } // namespace Vegetation