EditorLookAtComponent.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "EditorLookAtComponent.h"
  9. #include "LookAtComponent.h"
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Math/Transform.h>
  12. namespace LmbrCentral
  13. {
  14. //=========================================================================
  15. void EditorLookAtComponent::Reflect(AZ::ReflectContext* context)
  16. {
  17. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serializeContext->Class<EditorLookAtComponent, AZ::Component>()
  20. ->Version(1)
  21. ->Field("Target", &EditorLookAtComponent::m_targetId)
  22. ->Field("ForwardAxis", &EditorLookAtComponent::m_forwardAxis)
  23. ;
  24. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  25. {
  26. editContext->Class<EditorLookAtComponent>("Look At", "Force an entity to always look at a given target")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::Category, "Gameplay")
  29. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/LookAt.svg")
  30. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/LookAt.svg")
  31. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/gameplay/look-at/")
  32. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  33. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  34. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorLookAtComponent::m_targetId, "Target", "The entity to look at")
  35. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorLookAtComponent::OnTargetChanged)
  36. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &EditorLookAtComponent::m_forwardAxis, "Forward Axis", "The local axis that should point at the target")
  37. ->EnumAttribute(AZ::Transform::Axis::YPositive, "Y+")
  38. ->EnumAttribute(AZ::Transform::Axis::YNegative, "Y-")
  39. ->EnumAttribute(AZ::Transform::Axis::XPositive, "X+")
  40. ->EnumAttribute(AZ::Transform::Axis::XNegative, "X-")
  41. ->EnumAttribute(AZ::Transform::Axis::ZPositive, "Z+")
  42. ->EnumAttribute(AZ::Transform::Axis::ZNegative, "Z-")
  43. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorLookAtComponent::RecalculateTransform)
  44. ;
  45. }
  46. }
  47. }
  48. //=========================================================================
  49. void EditorLookAtComponent::Activate()
  50. {
  51. if (m_targetId.IsValid())
  52. {
  53. AZ::EntityBus::Handler::BusConnect(m_targetId);
  54. }
  55. }
  56. //=========================================================================
  57. void EditorLookAtComponent::Deactivate()
  58. {
  59. AZ::TransformNotificationBus::MultiHandler::BusDisconnect();
  60. AZ::EntityBus::Handler::BusDisconnect();
  61. }
  62. //=========================================================================
  63. void EditorLookAtComponent::OnEntityActivated(const AZ::EntityId& /*entity*/)
  64. {
  65. AZ::TransformNotificationBus::MultiHandler::BusConnect(GetEntityId());
  66. AZ::TransformNotificationBus::MultiHandler::BusConnect(m_targetId);
  67. }
  68. //=========================================================================
  69. void EditorLookAtComponent::OnEntityDeactivated(const AZ::EntityId& /*entity*/)
  70. {
  71. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(GetEntityId());
  72. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(m_targetId);
  73. }
  74. //=========================================================================
  75. void EditorLookAtComponent::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, [[maybe_unused]] const AZ::Transform& world)
  76. {
  77. // We need to defer the Look-At transform change. Can't flush it through here because
  78. // that will cause a feedback loop and the originator of the transform change might
  79. // not be finished broadcasting out to listeners. If we set look-at here, the look-at
  80. // transform can be stomped later by the original data.
  81. // Method 1: Connect to the Tick bus for a frame. In the next OnTick we set the
  82. // Look-At transform and disconnect.
  83. AZ::TickBus::Handler::BusConnect();
  84. // Method 2: We may want to stay connected to the TickBus if the transform is constantly
  85. // changing. Without a good heuristic to detect this case, we'll stick with Method 1.
  86. // Here's a gist of this method:
  87. // connect/disconnect to TickBus in OnEntityActivated/OnEntityDeactivated.
  88. // set a 'shouldRecalc' flag here in OnTransformChanged to true;
  89. // in OnTick do this:
  90. // if (shouldRecalc)
  91. // {
  92. // RecalculateTransform();
  93. // shouldRecalc = false;
  94. // }
  95. }
  96. //=========================================================================
  97. void EditorLookAtComponent::BuildGameEntity(AZ::Entity* gameEntity)
  98. {
  99. LookAtComponent* lookAtComponent = gameEntity->CreateComponent<LookAtComponent>();
  100. if (lookAtComponent)
  101. {
  102. lookAtComponent->m_targetId = m_targetId;
  103. lookAtComponent->m_forwardAxis = m_forwardAxis;
  104. }
  105. }
  106. //=========================================================================
  107. void EditorLookAtComponent::OnTargetChanged()
  108. {
  109. if (m_oldTargetId.IsValid())
  110. {
  111. // Disconnect from the old target entity
  112. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(m_oldTargetId);
  113. AZ::EntityBus::Handler::BusDisconnect(m_oldTargetId);
  114. m_oldTargetId = AZ::EntityId();
  115. }
  116. if (m_targetId.IsValid())
  117. {
  118. // Connect to the new target entity
  119. // Won't connect to the new target's transform bus until we receive notification
  120. // that target is activated via the EntityBus.
  121. AZ::EntityBus::Handler::BusConnect(m_targetId);
  122. m_oldTargetId = m_targetId;
  123. RecalculateTransform();
  124. }
  125. else
  126. {
  127. // If the target is invalid (nothing to look at), stop listening to everything
  128. AZ::TransformNotificationBus::MultiHandler::BusDisconnect();
  129. AZ::EntityBus::Handler::BusDisconnect();
  130. }
  131. }
  132. //=========================================================================
  133. void EditorLookAtComponent::OnTick(float /*deltaTime*/, AZ::ScriptTimePoint /*time*/)
  134. {
  135. RecalculateTransform();
  136. AZ::TickBus::Handler::BusDisconnect();
  137. }
  138. //=========================================================================
  139. void EditorLookAtComponent::RecalculateTransform()
  140. {
  141. if (m_targetId.IsValid())
  142. {
  143. AZ::TransformNotificationBus::MultiHandler::BusDisconnect(GetEntityId());
  144. {
  145. AZ::Transform sourceTM = AZ::Transform::CreateIdentity();
  146. AZ::TransformBus::EventResult(sourceTM, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  147. AZ::Transform targetTM = AZ::Transform::CreateIdentity();
  148. AZ::TransformBus::EventResult(targetTM, m_targetId, &AZ::TransformBus::Events::GetWorldTM);
  149. AZ::Transform lookAtTransform = AZ::Transform::CreateLookAt(
  150. sourceTM.GetTranslation(),
  151. targetTM.GetTranslation(),
  152. m_forwardAxis
  153. );
  154. // update the rotation and translation for sourceTM based on lookAtTransform, but leave scale unchanged
  155. sourceTM.SetRotation(lookAtTransform.GetRotation());
  156. sourceTM.SetTranslation(lookAtTransform.GetTranslation());
  157. AZ::TransformBus::Event(GetEntityId(), &AZ::TransformBus::Events::SetWorldTM, lookAtTransform);
  158. AZ::TransformBus::Event(GetEntityId(), &AZ::TransformBus::Events::SetWorldTM, sourceTM);
  159. }
  160. AZ::TransformNotificationBus::MultiHandler::BusConnect(GetEntityId());
  161. }
  162. }
  163. }//namespace LmbrCentral