FruitStorageComponent.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "FruitStorageComponent.h"
  9. #include "FruitStorageBus.h"
  10. #include <AzCore/Asset/AssetSerializer.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/EditContextConstants.inl>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzFramework/Components/TransformComponent.h>
  15. namespace AppleKraken
  16. {
  17. void FruitStorageComponent::Activate()
  18. {
  19. FruitStorageRequestsBus::Handler::BusConnect(GetEntityId());
  20. }
  21. void FruitStorageComponent::Deactivate()
  22. {
  23. FruitStorageRequestsBus::Handler::BusDisconnect(GetEntityId());
  24. }
  25. void FruitStorageComponent::Reflect(AZ::ReflectContext* context)
  26. {
  27. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  28. {
  29. serialize->Class<FruitStorageComponent, AZ::Component>()
  30. ->Version(3)
  31. ->Field("Crate", &FruitStorageComponent::m_crateSpawnable)
  32. ->Field("Capacity", &FruitStorageComponent::m_crateCapacity)
  33. ->Field("CrateDropPoint", &FruitStorageComponent::m_crateDropPoint);
  34. if (AZ::EditContext* ec = serialize->GetEditContext())
  35. {
  36. ec->Class<FruitStorageComponent>("Fruit Storage", "Fruit storage component")
  37. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Manages Kraken capacity and spawns a crate when full")
  38. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game"))
  39. ->Attribute(AZ::Edit::Attributes::Category, "AppleKraken")
  40. ->DataElement(AZ::Edit::UIHandlers::EntityId, &FruitStorageComponent::m_crateSpawnable, "Crate", "Crate spawnable")
  41. ->DataElement(AZ::Edit::UIHandlers::EntityId, &FruitStorageComponent::m_crateCapacity, "Capacity", "Capacity")
  42. ->DataElement(
  43. AZ::Edit::UIHandlers::EntityId,
  44. &FruitStorageComponent::m_crateDropPoint,
  45. "Crate drop point",
  46. "Place this entity behind the robot");
  47. }
  48. }
  49. }
  50. void FruitStorageComponent::SpawnCrate()
  51. {
  52. if (!m_crateTicket.IsValid())
  53. {
  54. m_crateTicket = AzFramework::EntitySpawnTicket(m_crateSpawnable);
  55. }
  56. auto spawner = AZ::Interface<AzFramework::SpawnableEntitiesDefinition>::Get();
  57. AzFramework::SpawnAllEntitiesOptionalArgs optionalArgs;
  58. optionalArgs.m_preInsertionCallback = [this](auto id, auto view)
  59. {
  60. this->PreSpawn(id, view);
  61. };
  62. AZ_TracePrintf("FruitStorageComponent", "Spawning a crate\n");
  63. spawner->SpawnAllEntities(m_crateTicket, optionalArgs);
  64. }
  65. void FruitStorageComponent::PreSpawn(
  66. AzFramework::EntitySpawnTicket::Id id [[maybe_unused]], AzFramework::SpawnableEntityContainerView view)
  67. {
  68. if (view.empty())
  69. {
  70. AZ_Warning("FruitStorageComponent", false, "Can not spawn a crate - no entities\n");
  71. return;
  72. }
  73. AZ::Entity* dropPointEntity = nullptr;
  74. if (m_crateDropPoint.IsValid())
  75. {
  76. AZ::ComponentApplicationBus::BroadcastResult(dropPointEntity, &AZ::ComponentApplicationRequests::FindEntity, m_crateDropPoint);
  77. }
  78. if (dropPointEntity == nullptr)
  79. {
  80. AZ_Warning("FruitStorageComponent", false, "Crate drop point not set or invalid, spawning a crate within this entity\n");
  81. dropPointEntity = GetEntity();
  82. }
  83. AZ::Entity* root = *view.begin();
  84. auto* crateTransformInterface = root->FindComponent<AzFramework::TransformComponent>();
  85. auto entityTransform = dropPointEntity->FindComponent<AzFramework::TransformComponent>();
  86. crateTransformInterface->SetWorldTM(entityTransform->GetWorldTM());
  87. }
  88. ApplesGatheredByTag FruitStorageComponent::GetTotalGatheredAppleCount(const Tags& tags) const
  89. {
  90. ApplesGatheredByTag result;
  91. if (tags.empty())
  92. {
  93. result["all"] = m_applesGathered;
  94. }
  95. else
  96. {
  97. for (const auto& tag : tags)
  98. {
  99. if (m_tagsStored.contains(tag))
  100. {
  101. result[tag] = m_tagsStored.at(tag);
  102. }
  103. }
  104. }
  105. return result;
  106. }
  107. uint32_t FruitStorageComponent::GetCurrentStorageAppleCount() const
  108. {
  109. return m_applesInStorage;
  110. }
  111. void FruitStorageComponent::AddApple(const Tags& tags)
  112. {
  113. m_applesGathered++;
  114. m_applesInStorage++;
  115. for (const auto& tag : tags)
  116. {
  117. m_tagsStored.contains(tag) ? m_tagsStored[tag]++ : m_tagsStored[tag] = 1;
  118. }
  119. if (m_applesInStorage >= m_crateCapacity)
  120. {
  121. SpawnCrate();
  122. m_applesInStorage = 0;
  123. }
  124. }
  125. } // namespace AppleKraken