LookAtComponent.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/Math/Transform.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/EntityBus.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzCore/Component/TransformBus.h>
  14. namespace LmbrCentral
  15. {
  16. class LookAtComponentRequests
  17. : public AZ::ComponentBus
  18. {
  19. public:
  20. //! Set the target entity to look at
  21. virtual void SetTarget([[maybe_unused]] AZ::EntityId targetEntity) {}
  22. //! Set the target position to look at
  23. virtual void SetTargetPosition([[maybe_unused]] const AZ::Vector3& position) {}
  24. //! Set the reference forward axis
  25. virtual void SetAxis([[maybe_unused]] AZ::Transform::Axis axis = AZ::Transform::Axis::ZPositive) {}
  26. };
  27. using LookAtComponentRequestBus = AZ::EBus<LookAtComponentRequests>;
  28. class LookAtComponentNotifications
  29. : public AZ::ComponentBus
  30. {
  31. public:
  32. //! Notifies you that the target has changed
  33. virtual void OnTargetChanged(AZ::EntityId) { }
  34. };
  35. using LookAtComponentNotificationBus = AZ::EBus<LookAtComponentNotifications>;
  36. //=========================================================================
  37. // LookAtComponent
  38. //=========================================================================
  39. class LookAtComponent
  40. : public AZ::Component
  41. , private AZ::TransformNotificationBus::MultiHandler
  42. , private AZ::TickBus::Handler
  43. , private AZ::EntityBus::Handler
  44. , private LookAtComponentRequestBus::Handler
  45. {
  46. public:
  47. friend class EditorLookAtComponent;
  48. AZ_COMPONENT(LookAtComponent, "{11CDC627-25A9-4760-A61F-576CDB189B38}");
  49. //=====================================================================
  50. // AZ::Component
  51. void Activate() override;
  52. void Deactivate() override;
  53. //=====================================================================
  54. //=====================================================================
  55. // TransformBus
  56. void OnTransformChanged(const AZ::Transform& /*local*/, const AZ::Transform& /*world*/) override;
  57. //=====================================================================
  58. //=====================================================================
  59. // TickBus
  60. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  61. //=====================================================================
  62. //=====================================================================
  63. // EntityBus
  64. void OnEntityActivated(const AZ::EntityId& entityId) override;
  65. void OnEntityDeactivated(const AZ::EntityId& entityId) override;
  66. //=====================================================================
  67. //=====================================================================
  68. // LookAtComponentRequestBus
  69. void SetTarget(AZ::EntityId targetEntity) override;
  70. void SetTargetPosition(const AZ::Vector3& targetPosition) override;
  71. void SetAxis(AZ::Transform::Axis axis) override;
  72. //=====================================================================
  73. protected:
  74. static void Reflect(AZ::ReflectContext* context);
  75. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  76. {
  77. provided.push_back(AZ_CRC_CE("LookAtService"));
  78. }
  79. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  80. {
  81. required.push_back(AZ_CRC_CE("TransformService"));
  82. }
  83. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  84. {
  85. incompatible.push_back(AZ_CRC_CE("LookAtService"));
  86. }
  87. private:
  88. void RecalculateTransform();
  89. // Serialized data
  90. AZ::EntityId m_targetId;
  91. AZ::Vector3 m_targetPosition;
  92. AZ::Transform::Axis m_forwardAxis;
  93. };
  94. }//namespace LmbrCentral