Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "GameProject_precompiled.h"
- #include <AzCore/Serialization/SerializeContext.h>
- #include <AzCore/Serialization/EditContext.h>
- #include <AzCore/Serialization/EditContextConstants.inl>
- #include <AzCore/Component/Entity.h>
- #include <AzCore/Slice/SliceComponent.h>
- #include <AzCore/Component/EntityBus.h>
- #include <AzCore/Component/TransformBus.h>
- #include <AzCore/Component/ComponentApplicationBus.h>
- #include <AzCore/Asset/AssetManagerBus.h>
- #include <AzFramework/Entity/GameEntityContextBus.h>
- #include <AzFramework/Components/ConsoleBus.h>
- #include <LmbrCentral/Scripting/SpawnerComponentBus.h>
- #include "..\..\Code\Source\Ai\NavigationComponent.h"
- #include <GameProjectSystemComponent.h>
- #include <ScriptCanvas/MyNodeLibrary.h>
- #include "Utility\TimeOfDayUtility.h"
- #include "..\..\..\Gems\EMotionFX\Code\Include\Integration\EMotionFXBus.h"
- #include <EMotionFX/Source/AnimGraph.h>
- #include "Source\EmotionFXNodes\MyEFXNode.h"
- namespace GameProject
- {
- void GameProjectSystemComponent::Reflect(AZ::ReflectContext* context)
- {
- if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
- {
- MyNodeLibrary::Reflect(context);
- TimeOfDayUtility::Reflect(context);
- serialize->Class<GameProjectSystemComponent, AZ::Component>()
- ->Version(0)
- ;
- if (AZ::EditContext* ec = serialize->GetEditContext())
- {
- ec->Class<GameProjectSystemComponent>("GameProject", "[Description of functionality provided by this System Component]")
- ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
- ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("System"))
- ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
- ;
- }
- }
- EMotionFX::MyEFXNode::Reflect(context);
- }
- void GameProjectSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
- {
- provided.push_back(AZ_CRC("GameProjectService"));
- }
- void GameProjectSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
- {
- incompatible.push_back(AZ_CRC("GameProjectService"));
- }
- void GameProjectSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
- {
- AZ_UNUSED(required);
- }
- void GameProjectSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
- {
- dependent.push_back(AZ_CRC("EmotionFXAnimationService", 0x3f8a6369));
- }
- void GameProjectSystemComponent::SpawnSlice(const AZStd::string & sliceName, const AZ::Vector3 & pos)
- {
- AZ::Data::AssetId sliceAssetId;
- EBUS_EVENT_RESULT(sliceAssetId, AZ::Data::AssetCatalogRequestBus, GetAssetIdByPath, sliceName.c_str(), AZ::AzTypeInfo<AZ::DynamicPrefabAsset>::Uuid(), true);
- if (sliceAssetId.IsValid())
- {
- // Using the valid ID, find the actual slice asset
- AZ::Data::Asset<AZ::DynamicSliceAsset> sliceAsset;
- sliceAsset.Create(sliceAssetId, true);
- if (sliceAsset.GetId().IsValid())
- {
- AZ::Transform transform(AZ::Transform::CreateTranslation(pos));
- using namespace AzFramework;
- static EntityContextId gameEntityContextId = EntityContextId::CreateNull();
- if (gameEntityContextId.IsNull())
- {
- GameEntityContextRequestBus::BroadcastResult(gameEntityContextId, &GameEntityContextRequestBus::Events::GetGameEntityContextId);
- }
- SliceInstantiationTicket ticket;
- ticket.m_contextId = gameEntityContextId;
- GameEntityContextRequestBus::BroadcastResult(ticket, &GameEntityContextRequestBus::Events::InstantiateDynamicSlice, sliceAsset, transform, nullptr);
- //LmbrCentral::SpawnerComponentRequestBus::EventResult(ticket, m_childEntity->GetId(), &LmbrCentral::SpawnerComponentRequestBus::Events::SpawnSliceAbsolute, sliceAsset, transform);
- SliceInstantiationResultBus::MultiHandler::BusConnect(ticket);
- }
- }
- }
- void GameProjectSystemComponent::SpawnSlice(AZ::Data::Asset<AZ::DynamicSliceAsset>& dynSliceAsset, const AZ::Vector3 & pos)
- {
- if (dynSliceAsset.GetId().IsValid())
- {
- // Using the valid ID, find the actual slice asset
- AZ::Data::Asset<AZ::DynamicSliceAsset> sliceAsset;
- sliceAsset.Create(dynSliceAsset.GetId(), true);
- if (sliceAsset.GetId().IsValid())
- {
- AZ::Transform transform(AZ::Transform::CreateTranslation(pos));
- using namespace AzFramework;
- static EntityContextId gameEntityContextId = EntityContextId::CreateNull();
- if (gameEntityContextId.IsNull())
- {
- GameEntityContextRequestBus::BroadcastResult(gameEntityContextId, &GameEntityContextRequestBus::Events::GetGameEntityContextId);
- }
- SliceInstantiationTicket ticket;
- ticket.m_contextId = gameEntityContextId;
- GameEntityContextRequestBus::BroadcastResult(ticket, &GameEntityContextRequestBus::Events::InstantiateDynamicSlice, sliceAsset, transform, nullptr);
- //LmbrCentral::SpawnerComponentRequestBus::EventResult(ticket, m_childEntity->GetId(), &LmbrCentral::SpawnerComponentRequestBus::Events::SpawnSliceAbsolute, sliceAsset, transform);
- SliceInstantiationResultBus::MultiHandler::BusConnect(ticket);
- }
- }
- }
- AZ::Entity * GameProjectSystemComponent::FindEntityByName(AZStd::string entityName)
- {
- /// https://gamedev.amazon.com/forums/articles/8439/howto-find-component-entities-by-name-id-favorite.html
- AZ::Entity* retEntity = nullptr;
- EBUS_EVENT(AZ::ComponentApplicationBus, EnumerateEntities,
- [this, &entityName, &retEntity](AZ::Entity* entity)
- {
- // perform the condition here (check the name, find a component etc.)
- // you can use the AZ::Entity::FindComponent methods to look for a particular component type – AzCore\Component\Entity.h
- if (strcmp(entity->GetName().c_str(), entityName.c_str()) == 0)
- {
- // do something with this entity
- retEntity = entity;
- }
- }
- );
- return retEntity;
- }
- float GameProjectSystemComponent::GetMapSize()
- {
- return gEnv->p3DEngine->GetTerrainSize() * 1.0f;
- }
- float GameProjectSystemComponent::GetMapHeight(float x, float y)
- {
- return gEnv->p3DEngine->GetTerrainZ(static_cast<int>(x), static_cast<int>(x)); //32 default height
- }
- AZ::Vector3 GameProjectSystemComponent::GetMapCenter()
- {
- float s = GetMapSize() * 0.5f;
- float h = GetMapHeight(s, s);
- return AZ::Vector3(s, s, h);
- }
- bool GameProjectSystemComponent::IsOnNavMesh(const AZ::Vector3 & pos, AZStd::string agentNameType)
- {
- if (!gEnv->pAISystem)
- {
- return false;
- }
- INavigationSystem* navSystem = gEnv->pAISystem->GetNavigationSystem();
- NavigationAgentTypeID agentType = navSystem->GetAgentTypeID(agentNameType.c_str());
- bool isValid = false;
- if (agentType)
- isValid = navSystem->IsLocationValidInNavigationMesh(agentType, AZVec3ToLYVec3(pos));
- else
- AZ_Warning("StarterGame", false, "%s: Invalid agent type.", __FUNCTION__);
- return isValid;
- }
- bool GameProjectSystemComponent::GetClosestPointInNavMesh(const AZ::Vector3 & pos, AZ::Vector3 & found, AZStd::string agentNameType)
- {
- if (IsOnNavMesh(pos, agentNameType))
- {
- found = pos;
- return true;
- }
- if (!gEnv->pAISystem)
- {
- found = AZ::Vector3::CreateZero();
- return false;
- }
- INavigationSystem* navSystem = gEnv->pAISystem->GetNavigationSystem();
- NavigationAgentTypeID agentType = navSystem->GetAgentTypeID(agentNameType.c_str());
- AZ_Warning("StarterGame", agentType, "%s: Invalid agent type.", __FUNCTION__);
- Vec3 closestPoint(0.0f, 0.0f, 0.0f);
- if (!navSystem->GetClosestPointInNavigationMesh(agentType, AZVec3ToLYVec3(pos), 3.0f, 3.0f, &closestPoint))
- {
- //AZ_Warning("StarterGame", false, "%s: Failed to find the closest point.", __FUNCTION__);
- found = AZ::Vector3::CreateZero();
- return false;
- }
- found = LYVec3ToAZVec3(closestPoint);
- return true;
- }
- float GameProjectSystemComponent::GetArrivalDistanceThreshold(const AZ::EntityId & entityId)
- {
- float threshold = 0.0f;
- AZ::Entity* entity = nullptr;
- AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityId);
- const AZ::Entity::ComponentArrayType& components = entity->GetComponents();
- for (size_t i = 0; i < components.size(); ++i)
- {
- LmbrCentral::NavigationComponent* comp = azdynamic_cast<LmbrCentral::NavigationComponent*>(components[i]);
- if (comp != nullptr)
- {
- threshold = comp->GetArrivalDistance();
- }
- }
- return threshold;
- }
- void GameProjectSystemComponent::OnSlicePreInstantiate(const AZ::Data::AssetId & sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress & sliceAddress)
- {
- //for (AZ::Entity* entity : sliceAddress.second->GetInstantiated()->m_metadataEntities)
- //{
- // AZ_Printf("OnSliceInstantiated", "entityId=%d name=%s", entity->GetId().ToString().c_str(), entity->GetName().c_str());
- // //m_spawnedList.push_back(entity);
- //}
- }
- void GameProjectSystemComponent::OnSliceInstantiated(const AZ::Data::AssetId & sliceAssetId, const AZ::SliceComponent::SliceInstanceAddress & sliceAddress)
- {
- for (AZ::Entity* entity : sliceAddress.second->GetInstantiated()->m_metadataEntities)
- {
- if (entity)
- {
- GameProjectNotificationBus::Broadcast(&GameProjectNotificationBus::Events::OnSliceInstantiated, entity->GetId());
- //AZ_Printf("OnSliceInstantiated", "entityId=%d name=%s", entity->GetId().ToString().c_str(), entity->GetName().c_str());
- //m_spawnedList.push_back(entity);
- }
- }
- }
- void GameProjectSystemComponent::Init()
- {
- using namespace ScriptCanvas;
- //auto nodeRegistryVariable = AZ::Environment::FindVariable<NodeRegistry>(s_nodeRegistryName);
- //if (nodeRegistryVariable)
- //{
- // NodeRegistry& nodeRegistry = nodeRegistryVariable.Get();
- // MyNodeLibrary::InitNodeRegistry(nodeRegistry);
- //}
- if (auto nodeRegistryVariable = GetNodeRegistry())
- {
- NodeRegistry& nodeRegistry = nodeRegistryVariable.Get();
- MyNodeLibrary::InitNodeRegistry(nodeRegistry);
- }
- }
- // System component
- void GameProjectSystemComponent::Activate()
- {
- GameProjectRequestBus::Handler::BusConnect();
- EMotionFX::MyEFXNode* node = aznew EMotionFX::MyEFXNode();
- EMotionFX::Integration::EMotionFXRequestBus::Broadcast(&EMotionFX::Integration::EMotionFXRequests::RegisterAnimGraphObjectType, node);
- }
- void GameProjectSystemComponent::Deactivate()
- {
- GameProjectRequestBus::Handler::BusDisconnect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement