GatheringRowComponent.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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(3)
  21. ->Field("Start", &GatheringRowComponent::m_start)
  22. ->Field("End", &GatheringRowComponent::m_end)
  23. ->Field("PoseOffset", &GatheringRowComponent::m_poseOffset)
  24. ->Field("TreeCount", &GatheringRowComponent::m_appleTreeCount);
  25. if (AZ::EditContext* ec = serialize->GetEditContext())
  26. {
  27. ec->Class<GatheringRowComponent>("Gathering Row Component", "Poses (points with orientation) suitable for apple gathering")
  28. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  29. ->Attribute(AZ::Edit::Attributes::Category, "AppleKraken")
  30. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  31. ->DataElement(AZ::Edit::UIHandlers::Default, &GatheringRowComponent::m_start, "Start", "Entity with the start pose")
  32. ->DataElement(AZ::Edit::UIHandlers::Default, &GatheringRowComponent::m_end, "End", "Entity with the end pose")
  33. ->DataElement(
  34. AZ::Edit::UIHandlers::Default,
  35. &GatheringRowComponent::m_poseOffset,
  36. "Offset",
  37. "Pose offset for each point (depends on robot)")
  38. ->DataElement(
  39. AZ::Edit::UIHandlers::Default,
  40. &GatheringRowComponent::m_appleTreeCount,
  41. "Tree count",
  42. "How many trees are in the row")
  43. ->Attribute(AZ::Edit::Attributes::Min, 1);
  44. }
  45. }
  46. }
  47. void GatheringRowComponent::Activate()
  48. {
  49. GatheringRowRequestBus::Handler::BusConnect();
  50. }
  51. void GatheringRowComponent::Deactivate()
  52. {
  53. GatheringRowRequestBus::Handler::BusDisconnect();
  54. m_gatheringPoses.clear();
  55. }
  56. void GatheringRowComponent::ComputeGatheringPoses()
  57. {
  58. if (!m_start.IsValid() || !m_end.IsValid())
  59. {
  60. AZ_Error("GatheringRowComponent", false, "ComputeGatheringPoses: unable to proceed without both start and end entity set");
  61. return;
  62. }
  63. if (m_appleTreeCount < 1)
  64. {
  65. AZ_Error("GatheringRowComponent", false, "ComputeGatheringPoses: unable to proceed with apple tree count less than 1");
  66. return;
  67. }
  68. GatheringPoses allPoses;
  69. AZ::Transform startPose;
  70. AZ::Transform endPose;
  71. AZ::TransformBus::EventResult(startPose, m_start, &AZ::TransformBus::Events::GetWorldTM);
  72. AZ::TransformBus::EventResult(endPose, m_end, &AZ::TransformBus::Events::GetWorldTM);
  73. // Simplification - we assume same orientations along the way
  74. const auto rowVector = endPose.GetTranslation() - startPose.GetTranslation();
  75. for (int i = 0; i < m_appleTreeCount; ++i)
  76. {
  77. AZ::Transform gatheringPoint = startPose;
  78. const float scale = static_cast<float>(i) / (m_appleTreeCount - 1);
  79. gatheringPoint.SetTranslation(gatheringPoint.GetTranslation() + m_poseOffset + rowVector * scale);
  80. m_gatheringPoses.emplace_back(gatheringPoint);
  81. }
  82. }
  83. } // namespace AppleKraken