LookAtComponent.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "LookAtComponent.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. namespace LmbrCentral
  13. {
  14. //////////////////////////////////////////////////////////////////////////
  15. class BehaviorLookAtComponentNotificationBusHandler : public LookAtComponentNotificationBus::Handler, public AZ::BehaviorEBusHandler
  16. {
  17. public:
  18. AZ_EBUS_BEHAVIOR_BINDER(BehaviorLookAtComponentNotificationBusHandler, "{2C171B89-CE6A-4C53-A286-0E1236A61FA0}", AZ::SystemAllocator,
  19. OnTargetChanged);
  20. // Sent when the light is turned on.
  21. void OnTargetChanged(AZ::EntityId entityId) override
  22. {
  23. Call(FN_OnTargetChanged, entityId);
  24. }
  25. };
  26. //=========================================================================
  27. void LookAtComponent::Reflect(AZ::ReflectContext* context)
  28. {
  29. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  30. {
  31. serializeContext->Class<LookAtComponent, AZ::Component>()
  32. ->Version(1)
  33. ->Field("Target", &LookAtComponent::m_targetId)
  34. ->Field("ForwardAxis", &LookAtComponent::m_forwardAxis)
  35. ;
  36. }
  37. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  38. {
  39. behaviorContext->EBus<LookAtComponentRequestBus>("LookAt", "LookAtRequestBus")
  40. ->Attribute(AZ::Script::Attributes::Category, "Gameplay")
  41. ->Event("SetTarget", &LookAtComponentRequestBus::Events::SetTarget, "Set Target", { { { "Target", "The entity to look at" } } })
  42. ->Attribute(AZ::Script::Attributes::ToolTip, "Set the entity to look at")
  43. ->Event("SetTargetPosition", &LookAtComponentRequestBus::Events::SetTargetPosition, "Set Target Position", { { { "Position", "The position to look at" } } })
  44. ->Attribute(AZ::Script::Attributes::ToolTip, "Sets the target position to look at.")
  45. ->Event("SetAxis", &LookAtComponentRequestBus::Events::SetAxis, "Set Axis", { { { "Axis", "The forward axis to use as reference" } } })
  46. ->Attribute(AZ::Script::Attributes::ToolTip, "Specify the forward axis to use as reference for the look at")
  47. ;
  48. behaviorContext->EBus<LookAtComponentNotificationBus>("LookAtNotification", "LookAtComponentNotificationBus", "Notifications for the Look At Component")
  49. ->Attribute(AZ::Script::Attributes::Category, "Gameplay")
  50. ->Handler<BehaviorLookAtComponentNotificationBusHandler>();
  51. }
  52. }
  53. //=========================================================================
  54. void LookAtComponent::Activate()
  55. {
  56. LookAtComponentRequestBus::Handler::BusConnect(GetEntityId());
  57. if (m_targetId.IsValid())
  58. {
  59. AZ::EntityBus::Handler::BusConnect(m_targetId);
  60. }
  61. }
  62. //=========================================================================
  63. void LookAtComponent::Deactivate()
  64. {
  65. AZ::TransformNotificationBus::MultiHandler::BusDisconnect();
  66. AZ::EntityBus::Handler::BusDisconnect();
  67. LookAtComponentRequestBus::Handler::BusDisconnect();
  68. }
  69. //=========================================================================
  70. void LookAtComponent::OnEntityActivated(const AZ::EntityId& /*entityId*/)
  71. {
  72. AZ::TransformNotificationBus::MultiHandler::BusConnect(GetEntityId());
  73. AZ::TransformNotificationBus::MultiHandler::BusConnect(m_targetId);
  74. }
  75. //=========================================================================
  76. void LookAtComponent::OnEntityDeactivated(const AZ::EntityId& /*entityId*/)
  77. {
  78. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(GetEntityId());
  79. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(m_targetId);
  80. }
  81. void LookAtComponent::SetTarget(AZ::EntityId targetEntity)
  82. {
  83. if (m_targetId.IsValid())
  84. {
  85. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(m_targetId);
  86. }
  87. m_targetPosition = AZ::Vector3(0, 0, 0);
  88. m_targetId = targetEntity;
  89. AZ::TransformNotificationBus::MultiHandler::BusConnect(m_targetId);
  90. RecalculateTransform();
  91. LookAtComponentNotificationBus::Broadcast(&LookAtComponentNotifications::OnTargetChanged, m_targetId);
  92. }
  93. void LookAtComponent::SetTargetPosition(const AZ::Vector3& targetPosition)
  94. {
  95. if (m_targetId.IsValid())
  96. {
  97. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(m_targetId);
  98. }
  99. m_targetId.SetInvalid();
  100. m_targetPosition = targetPosition;
  101. RecalculateTransform();
  102. LookAtComponentNotificationBus::Broadcast(&LookAtComponentNotifications::OnTargetChanged, m_targetId);
  103. }
  104. void LookAtComponent::SetAxis(AZ::Transform::Axis axis)
  105. {
  106. m_forwardAxis = axis;
  107. RecalculateTransform();
  108. }
  109. //=========================================================================
  110. void LookAtComponent::OnTransformChanged(const AZ::Transform& /*local*/, const AZ::Transform& /*world*/)
  111. {
  112. // See corresponding function in EditorLookAtComponent for comment.
  113. AZ::TickBus::Handler::BusConnect();
  114. }
  115. //=========================================================================
  116. void LookAtComponent::OnTick(float /*deltaTime*/, AZ::ScriptTimePoint /*time*/)
  117. {
  118. RecalculateTransform();
  119. AZ::TickBus::Handler::BusDisconnect();
  120. }
  121. //=========================================================================
  122. void LookAtComponent::RecalculateTransform()
  123. {
  124. AZ::Vector3 targetPosition = m_targetPosition;
  125. if (m_targetId.IsValid())
  126. {
  127. AZ::Transform targetTM = AZ::Transform::CreateIdentity();
  128. AZ::TransformBus::EventResult(targetTM, m_targetId, &AZ::TransformBus::Events::GetWorldTM);
  129. targetPosition = targetTM.GetTranslation();
  130. }
  131. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(GetEntityId());
  132. {
  133. AZ::Transform currentTM = AZ::Transform::CreateIdentity();
  134. AZ::TransformBus::EventResult(currentTM, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  135. AZ::Transform lookAtTransform = AZ::Transform::CreateLookAt(
  136. currentTM.GetTranslation(),
  137. targetPosition,
  138. m_forwardAxis
  139. );
  140. lookAtTransform.SetUniformScale(currentTM.GetUniformScale());
  141. AZ::TransformBus::Event(GetEntityId(), &AZ::TransformInterface::SetWorldTM, lookAtTransform);
  142. }
  143. AZ::TransformNotificationBus::MultiHandler::BusConnect(GetEntityId());
  144. }
  145. } // namespace LmbrCentral