BlockerComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/Component/Component.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/Math/Aabb.h>
  12. #include <AzCore/Math/Transform.h>
  13. #include <LmbrCentral/Dependency/DependencyNotificationBus.h>
  14. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  15. #include <Vegetation/Ebuses/AreaNotificationBus.h>
  16. #include <Vegetation/Ebuses/BlockerRequestBus.h>
  17. #include <Vegetation/AreaComponentBase.h>
  18. #define VEG_BLOCKER_ENABLE_CACHING 0
  19. namespace LmbrCentral
  20. {
  21. template<typename, typename>
  22. class EditorWrappedComponentBase;
  23. }
  24. namespace Vegetation
  25. {
  26. class BlockerConfig
  27. : public AreaConfig
  28. {
  29. public:
  30. AZ_CLASS_ALLOCATOR(BlockerConfig, AZ::SystemAllocator);
  31. AZ_RTTI(BlockerConfig, "{01F6E6C5-707E-42EC-91BB-F674B9F51A40}", AreaConfig);
  32. static void Reflect(AZ::ReflectContext* context);
  33. BlockerConfig() : AreaConfig() { m_priority = AreaConstants::s_priorityMax; m_layer = AreaConstants::s_foregroundLayer; }
  34. bool m_inheritBehavior = true;
  35. };
  36. inline constexpr AZ::TypeId BlockerComponentTypeId{ "{C8A7AAEB-C315-44CE-919D-F304B53ACA4A}" };
  37. /**
  38. * Blocking claim logic for vegetation in an area
  39. */
  40. class BlockerComponent
  41. : public AreaComponentBase
  42. , private BlockerRequestBus::Handler
  43. {
  44. public:
  45. friend class EditorBlockerComponent;
  46. template<typename, typename> friend class LmbrCentral::EditorWrappedComponentBase;
  47. AZ_COMPONENT(BlockerComponent, BlockerComponentTypeId, AreaComponentBase);
  48. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  49. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  50. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  51. static void Reflect(AZ::ReflectContext* context);
  52. BlockerComponent(const BlockerConfig& configuration);
  53. BlockerComponent() = default;
  54. ~BlockerComponent() = default;
  55. //////////////////////////////////////////////////////////////////////////
  56. // AZ::Component interface implementation
  57. void Activate() override;
  58. void Deactivate() override;
  59. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  60. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override;
  61. //////////////////////////////////////////////////////////////////////////
  62. // AreaRequestBus
  63. bool PrepareToClaim(EntityIdStack& stackIds) override;
  64. void ClaimPositions(EntityIdStack& stackIds, ClaimContext& context) override;
  65. void UnclaimPosition(const ClaimHandle handle) override;
  66. AZ::Aabb GetEncompassingAabb() const override;
  67. AZ::u32 GetProductCount() const override;
  68. //////////////////////////////////////////////////////////////////////////
  69. // AreaNotificationBus
  70. void OnCompositionChanged() override;
  71. protected:
  72. //////////////////////////////////////////////////////////////////////////
  73. // BlockerRequestBus
  74. AZ::u32 GetAreaPriority() const override;
  75. void SetAreaPriority(AZ::u32 priority) override;
  76. AZ::u32 GetAreaLayer() const override;
  77. void SetAreaLayer(AZ::u32 layer) override;
  78. AZ::u32 GetAreaProductCount() const override;
  79. bool GetInheritBehavior() const override;
  80. void SetInheritBehavior(bool value) override;
  81. private:
  82. bool ClaimPosition(EntityIdStack& processedIds, const ClaimPoint& point, InstanceData& instanceData);
  83. BlockerConfig m_configuration;
  84. #if VEG_BLOCKER_ENABLE_CACHING
  85. // cached data
  86. AZStd::recursive_mutex m_cacheMutex;
  87. using ClaimInstanceMapping = AZStd::unordered_map<ClaimHandle, bool>;
  88. ClaimInstanceMapping m_claimCacheMapping;
  89. #endif
  90. };
  91. }