GatheringRowComponent.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "GatheringRowComponent.h"
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/EditContextConstants.inl>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. namespace AppleKraken
  14. {
  15. void GatheringRowComponent::Reflect(AZ::ReflectContext* context)
  16. {
  17. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serialize->Class<GatheringRowComponent, AZ::Component>()
  20. ->Version(4)
  21. ->Field("PoseOffset", &GatheringRowComponent::m_poseOffset);
  22. if (AZ::EditContext* ec = serialize->GetEditContext())
  23. {
  24. ec->Class<GatheringRowComponent>("Gathering Row Component", "Poses (points with orientation) suitable for apple gathering."
  25. "Component will return as navigation plan all its children "
  26. "with name containing \'GatherPoint\'. "
  27. "Points are sorted with entity name.")
  28. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  29. ->Attribute(AZ::Edit::Attributes::Category, "AppleKraken")
  30. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  31. ->DataElement(
  32. AZ::Edit::UIHandlers::Default,
  33. &GatheringRowComponent::m_poseOffset,
  34. "Offset",
  35. "Pose offset for each point (depends on robot)")
  36. ->Attribute(AZ::Edit::Attributes::Min, 1);
  37. }
  38. }
  39. }
  40. void GatheringRowComponent::Activate()
  41. {
  42. GatheringRowRequestBus::Handler::BusConnect();
  43. }
  44. void GatheringRowComponent::Deactivate()
  45. {
  46. GatheringRowRequestBus::Handler::BusDisconnect();
  47. m_gatheringPoses.clear();
  48. }
  49. void GatheringRowComponent::ComputeGatheringPoses()
  50. {
  51. // find all children
  52. AZStd::vector<AZ::EntityId> descendants;
  53. AZ::TransformBus::EventResult(descendants, GetEntityId(), &AZ::TransformBus::Events::GetAllDescendants);
  54. if (descendants.empty())
  55. {
  56. AZ_Error("GatheringRowComponent", false, "ComputeGatheringPoses: unable to proceed with empty set");
  57. return;
  58. }
  59. // Simplification - we assume same orientations along the way
  60. AZStd::map<AZStd::string, AZ::EntityId> sorted_names;
  61. for (const auto& entity_id:descendants)
  62. {
  63. AZStd::string entity_name;
  64. AZ::ComponentApplicationBus::BroadcastResult(entity_name, &AZ::ComponentApplicationRequests::GetEntityName, entity_id);
  65. if (entity_id.IsValid()) {
  66. if (entity_name.contains("GatherPoint")) {
  67. sorted_names[entity_name] = entity_id;
  68. }
  69. }
  70. }
  71. for (const auto &[_, entity_id]:sorted_names)
  72. {
  73. AZ::Transform pose;
  74. AZ::TransformBus::EventResult(pose, entity_id, &AZ::TransformBus::Events::GetWorldTM);
  75. pose = pose * AZ::Transform::CreateFromQuaternionAndTranslation(AZ::Quaternion::CreateIdentity(),
  76. m_poseOffset);
  77. m_gatheringPoses.emplace_back(pose);
  78. }
  79. }
  80. } // namespace AppleKraken