GatheringRowComponent.cpp 3.5 KB

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