Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include "NpcSpawnPoint.h"
- #include "Game/GameFactory.h"
- #include "Npc/Npc.h"
- class CNpcSpawnPointRegistrator
- : public IEntityRegistrator
- {
- virtual void Register() override
- {
- CGameFactory::RegisterNativeEntity<CNpcSpawnPoint>("NpcSpawnPoint", "Gameplay");
- }
- };
- CNpcSpawnPointRegistrator g_npcSpawnerRegistrator;
- CNpcSpawnPoint::CNpcSpawnPoint()
- : m_Geometry(-1)
- , m_pActionController (nullptr)
- , m_pAnimationContext (nullptr)
- {
- }
- CNpcSpawnPoint::~CNpcSpawnPoint()
- {
- SAFE_RELEASE(m_pActionController);
- SAFE_DELETE(m_pAnimationContext);
- }
- void CNpcSpawnPoint::SpawnEntity(IEntity &otherEntity)
- {
- otherEntity.SetWorldTM(GetEntity()->GetWorldTM());
- }
- void CNpcSpawnPoint::PostInit(IGameObject * pGameObject)
- {
- // Make sure that this extension is updated regularly via the Update function below
- pGameObject->EnableUpdateSlot(this, 0);
- }
- void CNpcSpawnPoint::ProcessEvent(SEntityEvent & event)
- {
- if (gEnv->IsDedicated())
- return;
- switch (event.event)
- {
- // Physicalize on level start for Launcher
- case ENTITY_EVENT_START_LEVEL:
- // Editor specific, physicalize on reset, property change or transform change
- case ENTITY_EVENT_RESET:
- case ENTITY_EVENT_EDITOR_PROPERTY_CHANGED:
- case ENTITY_EVENT_XFORM_FINISHED_EDITOR:
- // Make sure to revive player when respawning in Editor
- Reset();
- break;
- case ENTITY_EVENT_ANIM_EVENT:
- {
- const AnimEventInstance *pAnimEvent = reinterpret_cast<const AnimEventInstance *>(event.nParam[0]);
- ICharacterInstance *pCharacter = reinterpret_cast<ICharacterInstance *>(event.nParam[1]);
- if (m_pActionController)
- m_pActionController->OnAnimationEvent(pCharacter, *pAnimEvent);
- }
- break;
- }
- }
- void CNpcSpawnPoint::Update(SEntityUpdateContext & ctx, int updateSlot)
- {
- auto *pCharacter = GetEntity()->GetCharacter(0);
- if (pCharacter)
- {
- pCharacter->GetISkeletonAnim()->SetDesiredMotionParam(eMotionParamID_BlendWeight, 1.0f, 0.0f);
- //m_pAnimationContext->state.Set(m_walkTagId, true);
- }
- if (m_pActionController != NULL)
- {
- m_pActionController->Update(ctx.fFrameTime);
- }
- }
- void CNpcSpawnPoint::Reset()
- {
- SAFE_RELEASE(m_pActionController);
- SAFE_DELETE(m_pAnimationContext);
- IEntity &entity = *GetEntity();
- // Check if we have to unload first
- if (m_Geometry != -1)
- {
- entity.FreeSlot(m_Geometry);
- m_Geometry = -1;
- }
- m_Geometry = entity.LoadCharacter(0, "Assets\\Objects\\Characters\\man\\man.cdf");
- //Matrix34 m = Matrix34::CreateScale(Vec3(1.0f, 1.0f, 1.0f)) * Matrix34::CreateIdentity();
- entity.SetScale(Vec3(0.4f, 0.4f, 0.4f));
- //entity.SetLocalTM(m);
- // Mannequin Initialization
- IMannequin& mannequinInterface = gEnv->pGame->GetIGameFramework()->GetMannequinInterface();
- IAnimationDatabaseManager& animationDatabaseManager = mannequinInterface.GetAnimationDatabaseManager();
- // Loading the controller definition that we previously created.
- // This is owned by the animation database manager
- const SControllerDef* const pControllerDef = animationDatabaseManager.LoadControllerDef("Assets\\Animations\\Mannequin\\ADB\\manControllerDefinition.xml");
- if (pControllerDef == NULL)
- {
- CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "Failed to load controller definition for Man.");
- return;
- }
- // Creation of the animation context.
- CRY_ASSERT(m_pAnimationContext == NULL);
- m_pAnimationContext = new SAnimationContext(*pControllerDef);
- // Creation of the action controller
- CRY_ASSERT(m_pActionController == NULL);
- m_pActionController = mannequinInterface.CreateActionController(&entity, *m_pAnimationContext);
- // Scope Context Setup.
- // In our controller definition we have a scope context that we called MainCharacter
- // The Scope Context Setup will associate this entity, the character instance we loaded at the beginning,
- // and the animation database where we saved our fragments to this scope context.
- const TagID mainCharacterScopeContextId = m_pAnimationContext->controllerDef.m_scopeContexts.Find("ManCharacter");
- if (mainCharacterScopeContextId == TAG_ID_INVALID)
- {
- CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "Failed to find MainCharacter scope context id for Man in controller definition.");
- return;
- }
- ICharacterInstance* const pCharacterInstance = entity.GetCharacter(0);
- CRY_ASSERT(pCharacterInstance != NULL);
- // Loading a database
- const IAnimationDatabase* const pAnimationDatabase = animationDatabaseManager.Load("Animations/Mannequin/ADB/man.adb");
- if (pAnimationDatabase == NULL)
- {
- CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "Failed to load animation database for Man.");
- return;
- }
- // Setting Scope contexts can happen at any time, and what entity or character instance we have bound to a particular scope context
- // can change during the lifetime of an action controller.
- m_pActionController->SetScopeContext(mainCharacterScopeContextId, entity, pCharacterInstance, pAnimationDatabase);
- const FragmentID idleFragmentId = m_pAnimationContext->controllerDef.m_fragmentIDs.Find("Idle");
- const int actionPriority = 10;
- m_pIdleFragment = new TAction< SAnimationContext >(actionPriority, idleFragmentId, TAG_STATE_EMPTY, 0);
- //pAction->SetSpeedBias(1.0f);
- //pAction->SetAnimWeight(1.0f);
- m_pActionController->Queue(*m_pIdleFragment.get());
- m_walkTagId = m_pAnimationContext->state.GetDef().Find("Walk");
- m_pAnimationContext->state.Set(m_walkTagId, true);
- // Disable movement coming from the animation (root joint offset), we control this entirely via physics
- //pCharacterInstance->GetISkeletonAnim()->SetAnimationDrivenMotion(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement