Advertisement
Guest User

Untitled

a guest
Dec 26th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.56 KB | None | 0 0
  1. #include "GameProject_precompiled.h"
  2. #include <AzCore/Serialization/SerializeContext.h>
  3. #include <AzCore/Serialization/EditContext.h>
  4. #include <AzCore/Serialization/EditContextConstants.inl>
  5.  
  6.  
  7. #include <AzCore/Component/Entity.h>
  8. #include <AzCore/Slice/SliceComponent.h>
  9.  
  10. #include <AzCore/Component/EntityBus.h>
  11. #include <AzCore/Component/TransformBus.h>
  12. #include <AzCore/Component/ComponentApplicationBus.h>
  13. #include <AzCore/Asset/AssetManagerBus.h>
  14.  
  15. #include <AzFramework/Entity/GameEntityContextBus.h>
  16. #include <AzFramework/Components/ConsoleBus.h>
  17.  
  18. #include <LmbrCentral/Scripting/SpawnerComponentBus.h>
  19.  
  20. #include "..\..\Code\Source\Ai\NavigationComponent.h"
  21.  
  22. #include <GameProjectSystemComponent.h>
  23.  
  24. #include <ScriptCanvas/MyNodeLibrary.h>
  25. #include "Utility\TimeOfDayUtility.h"
  26.  
  27. #include "..\..\..\Gems\EMotionFX\Code\Include\Integration\EMotionFXBus.h"
  28.  
  29. #include <EMotionFX/Source/AnimGraph.h>
  30. #include "Source\EmotionFXNodes\MyEFXNode.h"
  31.  
  32.  
  33. namespace GameProject
  34. {
  35.     void GameProjectSystemComponent::Reflect(AZ::ReflectContext* context)
  36.     {
  37.         if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  38.         {
  39.             MyNodeLibrary::Reflect(context);
  40.             TimeOfDayUtility::Reflect(context);
  41.  
  42.             serialize->Class<GameProjectSystemComponent, AZ::Component>()
  43.                 ->Version(0)
  44.                 ;
  45.  
  46.             if (AZ::EditContext* ec = serialize->GetEditContext())
  47.             {
  48.                 ec->Class<GameProjectSystemComponent>("GameProject", "[Description of functionality provided by this System Component]")
  49.                     ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  50.                         ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
  51.                         ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  52.                     ;
  53.             }
  54.         }
  55.        
  56.         EMotionFX::MyEFXNode::Reflect(context);
  57.     }
  58.  
  59.     void GameProjectSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  60.     {
  61.         provided.push_back(AZ_CRC("GameProjectService"));
  62.     }
  63.  
  64.     void GameProjectSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  65.     {
  66.         incompatible.push_back(AZ_CRC("GameProjectService"));
  67.     }
  68.  
  69.     void GameProjectSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  70.     {
  71.         AZ_UNUSED(required);
  72.     }
  73.  
  74.     void GameProjectSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  75.     {
  76.         dependent.push_back(AZ_CRC("EmotionFXAnimationService", 0x3f8a6369));
  77.     }
  78.  
  79.     void GameProjectSystemComponent::SpawnSlice(const AZStd::string & sliceName, const AZ::Vector3 & pos)
  80.     {
  81.         AZ::Data::AssetId sliceAssetId;
  82.         EBUS_EVENT_RESULT(sliceAssetId, AZ::Data::AssetCatalogRequestBus, GetAssetIdByPath, sliceName.c_str(), AZ::AzTypeInfo<AZ::DynamicPrefabAsset>::Uuid(), true);
  83.  
  84.         if (sliceAssetId.IsValid())
  85.         {
  86.             // Using the valid ID, find the actual slice asset
  87.             AZ::Data::Asset<AZ::DynamicSliceAsset> sliceAsset;
  88.             sliceAsset.Create(sliceAssetId, true);
  89.             if (sliceAsset.GetId().IsValid())
  90.             {
  91.                 AZ::Transform transform(AZ::Transform::CreateTranslation(pos));
  92.  
  93.                 using namespace AzFramework;
  94.  
  95.                 static EntityContextId gameEntityContextId = EntityContextId::CreateNull();
  96.                 if (gameEntityContextId.IsNull())
  97.                 {
  98.                     GameEntityContextRequestBus::BroadcastResult(gameEntityContextId, &GameEntityContextRequestBus::Events::GetGameEntityContextId);
  99.                 }
  100.  
  101.                 SliceInstantiationTicket ticket;
  102.                 ticket.m_contextId = gameEntityContextId;
  103.  
  104.                 GameEntityContextRequestBus::BroadcastResult(ticket, &GameEntityContextRequestBus::Events::InstantiateDynamicSlice, sliceAsset, transform, nullptr);
  105.                 //LmbrCentral::SpawnerComponentRequestBus::EventResult(ticket, m_childEntity->GetId(), &LmbrCentral::SpawnerComponentRequestBus::Events::SpawnSliceAbsolute, sliceAsset, transform);
  106.                 SliceInstantiationResultBus::MultiHandler::BusConnect(ticket);
  107.             }
  108.         }
  109.     }
  110.  
  111.     void GameProjectSystemComponent::SpawnSlice(AZ::Data::Asset<AZ::DynamicSliceAsset>& dynSliceAsset, const AZ::Vector3 & pos)
  112.     {
  113.         if (dynSliceAsset.GetId().IsValid())
  114.         {
  115.             // Using the valid ID, find the actual slice asset
  116.             AZ::Data::Asset<AZ::DynamicSliceAsset> sliceAsset;
  117.             sliceAsset.Create(dynSliceAsset.GetId(), true);
  118.            
  119.             if (sliceAsset.GetId().IsValid())
  120.             {
  121.                 AZ::Transform transform(AZ::Transform::CreateTranslation(pos));
  122.  
  123.                 using namespace AzFramework;
  124.  
  125.                 static EntityContextId gameEntityContextId = EntityContextId::CreateNull();
  126.                 if (gameEntityContextId.IsNull())
  127.                 {
  128.                     GameEntityContextRequestBus::BroadcastResult(gameEntityContextId, &GameEntityContextRequestBus::Events::GetGameEntityContextId);
  129.                 }
  130.  
  131.                 SliceInstantiationTicket ticket;
  132.                 ticket.m_contextId = gameEntityContextId;
  133.  
  134.                 GameEntityContextRequestBus::BroadcastResult(ticket, &GameEntityContextRequestBus::Events::InstantiateDynamicSlice, sliceAsset, transform, nullptr);
  135.                 //LmbrCentral::SpawnerComponentRequestBus::EventResult(ticket, m_childEntity->GetId(), &LmbrCentral::SpawnerComponentRequestBus::Events::SpawnSliceAbsolute, sliceAsset, transform);
  136.                 SliceInstantiationResultBus::MultiHandler::BusConnect(ticket);
  137.             }
  138.         }
  139.     }
  140.  
  141.     AZ::Entity * GameProjectSystemComponent::FindEntityByName(AZStd::string entityName)
  142.     {
  143.         /// https://gamedev.amazon.com/forums/articles/8439/howto-find-component-entities-by-name-id-favorite.html
  144.  
  145.         AZ::Entity* retEntity = nullptr;
  146.  
  147.         EBUS_EVENT(AZ::ComponentApplicationBus, EnumerateEntities,
  148.             [this, &entityName, &retEntity](AZ::Entity* entity)
  149.         {
  150.             // perform the condition here (check the name, find a component etc.)
  151.             // you can use the AZ::Entity::FindComponent methods to look for a particular component type – AzCore\Component\Entity.h
  152.             if (strcmp(entity->GetName().c_str(), entityName.c_str()) == 0)
  153.             {
  154.                 // do something with this entity
  155.                 retEntity = entity;
  156.             }
  157.         }
  158.         );
  159.  
  160.         return retEntity;
  161.     }
  162.  
  163.     float GameProjectSystemComponent::GetMapSize()
  164.     {
  165.         return gEnv->p3DEngine->GetTerrainSize() * 1.0f;
  166.     }
  167.  
  168.     float GameProjectSystemComponent::GetMapHeight(float x, float y)
  169.     {
  170.         return gEnv->p3DEngine->GetTerrainZ(static_cast<int>(x), static_cast<int>(x)); //32 default height
  171.     }
  172.  
  173.     AZ::Vector3 GameProjectSystemComponent::GetMapCenter()
  174.     {
  175.         float s = GetMapSize() * 0.5f;
  176.         float h = GetMapHeight(s, s);
  177.         return AZ::Vector3(s, s, h);
  178.     }
  179.  
  180.     bool GameProjectSystemComponent::IsOnNavMesh(const AZ::Vector3 & pos, AZStd::string agentNameType)
  181.     {
  182.         if (!gEnv->pAISystem)
  183.         {
  184.             return false;
  185.         }
  186.  
  187.         INavigationSystem* navSystem = gEnv->pAISystem->GetNavigationSystem();
  188.         NavigationAgentTypeID agentType = navSystem->GetAgentTypeID(agentNameType.c_str());
  189.         bool isValid = false;
  190.         if (agentType)
  191.             isValid = navSystem->IsLocationValidInNavigationMesh(agentType, AZVec3ToLYVec3(pos));
  192.         else
  193.             AZ_Warning("StarterGame", false, "%s: Invalid agent type.", __FUNCTION__);
  194.  
  195.         return isValid;
  196.     }
  197.  
  198.     bool GameProjectSystemComponent::GetClosestPointInNavMesh(const AZ::Vector3 & pos, AZ::Vector3 & found, AZStd::string agentNameType)
  199.     {
  200.         if (IsOnNavMesh(pos, agentNameType))
  201.         {
  202.             found = pos;
  203.             return true;
  204.         }
  205.  
  206.         if (!gEnv->pAISystem)
  207.         {
  208.             found = AZ::Vector3::CreateZero();
  209.             return false;
  210.         }
  211.  
  212.         INavigationSystem* navSystem = gEnv->pAISystem->GetNavigationSystem();
  213.         NavigationAgentTypeID agentType = navSystem->GetAgentTypeID(agentNameType.c_str());
  214.         AZ_Warning("StarterGame", agentType, "%s: Invalid agent type.", __FUNCTION__);
  215.  
  216.         Vec3 closestPoint(0.0f, 0.0f, 0.0f);
  217.         if (!navSystem->GetClosestPointInNavigationMesh(agentType, AZVec3ToLYVec3(pos), 3.0f, 3.0f, &closestPoint))
  218.         {
  219.             //AZ_Warning("StarterGame", false, "%s: Failed to find the closest point.", __FUNCTION__);
  220.             found = AZ::Vector3::CreateZero();
  221.             return false;
  222.         }
  223.  
  224.         found = LYVec3ToAZVec3(closestPoint);
  225.         return true;
  226.     }
  227.  
  228.     float GameProjectSystemComponent::GetArrivalDistanceThreshold(const AZ::EntityId & entityId)
  229.     {
  230.         float threshold = 0.0f;
  231.         AZ::Entity* entity = nullptr;
  232.         AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
  233.         const AZ::Entity::ComponentArrayType& components = entity->GetComponents();
  234.  
  235.         for (size_t i = 0; i < components.size(); ++i)
  236.         {
  237.             LmbrCentral::NavigationComponent* comp = azdynamic_cast<LmbrCentral::NavigationComponent*>(components[i]);
  238.             if (comp != nullptr)
  239.             {
  240.                 threshold = comp->GetArrivalDistance();
  241.             }
  242.         }
  243.         return threshold;
  244.     }
  245.  
  246.     void GameProjectSystemComponent::OnSlicePreInstantiate(const AZ::Data::AssetId & sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress & sliceAddress)
  247.     {
  248.         //for (AZ::Entity* entity : sliceAddress.second->GetInstantiated()->m_metadataEntities)
  249.         //{
  250.         //  AZ_Printf("OnSliceInstantiated", "entityId=%d name=%s", entity->GetId().ToString().c_str(), entity->GetName().c_str());
  251.         //  //m_spawnedList.push_back(entity);
  252.         //}
  253.     }
  254.  
  255.     void GameProjectSystemComponent::OnSliceInstantiated(const AZ::Data::AssetId & sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress & sliceAddress)
  256.     {
  257.         for (AZ::Entity* entity : sliceAddress.second->GetInstantiated()->m_metadataEntities)
  258.         {
  259.             if (entity)
  260.             {
  261.                 GameProjectNotificationBus::Broadcast(&GameProjectNotificationBus::Events::OnSliceInstantiated, entity->GetId());
  262.                 //AZ_Printf("OnSliceInstantiated", "entityId=%d name=%s", entity->GetId().ToString().c_str(), entity->GetName().c_str());
  263.                 //m_spawnedList.push_back(entity);
  264.             }
  265.         }
  266.     }
  267.  
  268.     void GameProjectSystemComponent::Init()
  269.     {
  270.         using namespace ScriptCanvas;
  271.         //auto nodeRegistryVariable = AZ::Environment::FindVariable<NodeRegistry>(s_nodeRegistryName);
  272.         //if (nodeRegistryVariable)
  273.         //{
  274.         //  NodeRegistry& nodeRegistry = nodeRegistryVariable.Get();
  275.         //  MyNodeLibrary::InitNodeRegistry(nodeRegistry);
  276.         //}
  277.  
  278.         if (auto nodeRegistryVariable = GetNodeRegistry())
  279.         {
  280.             NodeRegistry& nodeRegistry = nodeRegistryVariable.Get();
  281.             MyNodeLibrary::InitNodeRegistry(nodeRegistry);
  282.         }
  283.     }
  284.  
  285.     // System component
  286.     void GameProjectSystemComponent::Activate()
  287.     {
  288.         GameProjectRequestBus::Handler::BusConnect();
  289.         EMotionFX::MyEFXNode* node = aznew EMotionFX::MyEFXNode();
  290.         EMotionFX::Integration::EMotionFXRequestBus::Broadcast(&EMotionFX::Integration::EMotionFXRequests::RegisterAnimGraphObjectType, node);
  291.     }
  292.  
  293.     void GameProjectSystemComponent::Deactivate()
  294.     {
  295.         GameProjectRequestBus::Handler::BusDisconnect();
  296.     }
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement