SHARE
TWEET

Untitled

a guest Sep 4th, 2016 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "StdAfx.h"
  2. #include "NpcSpawnPoint.h"
  3.  
  4. #include "Game/GameFactory.h"
  5.  
  6. #include "Npc/Npc.h"
  7.  
  8. class CNpcSpawnPointRegistrator
  9.     : public IEntityRegistrator
  10. {
  11.     virtual void Register() override
  12.     {
  13.         CGameFactory::RegisterNativeEntity<CNpcSpawnPoint>("NpcSpawnPoint", "Gameplay");
  14.     }
  15. };
  16.  
  17. CNpcSpawnPointRegistrator g_npcSpawnerRegistrator;
  18.  
  19. CNpcSpawnPoint::CNpcSpawnPoint()
  20.     : m_Geometry(-1)
  21.     , m_pActionController (nullptr)
  22.     , m_pAnimationContext (nullptr)
  23.  
  24. {
  25.  
  26.  
  27. }
  28.  
  29. CNpcSpawnPoint::~CNpcSpawnPoint()
  30. {
  31.     SAFE_RELEASE(m_pActionController);
  32.     SAFE_DELETE(m_pAnimationContext);
  33. }
  34.  
  35. void CNpcSpawnPoint::SpawnEntity(IEntity &otherEntity)
  36. {
  37.     otherEntity.SetWorldTM(GetEntity()->GetWorldTM());
  38. }
  39.  
  40. void CNpcSpawnPoint::PostInit(IGameObject * pGameObject)
  41. {
  42.     // Make sure that this extension is updated regularly via the Update function below
  43.     pGameObject->EnableUpdateSlot(this, 0);
  44. }
  45.  
  46. void CNpcSpawnPoint::ProcessEvent(SEntityEvent & event)
  47. {
  48.     if (gEnv->IsDedicated())
  49.         return;
  50.  
  51.     switch (event.event)
  52.     {
  53.         // Physicalize on level start for Launcher
  54.     case ENTITY_EVENT_START_LEVEL:
  55.         // Editor specific, physicalize on reset, property change or transform change
  56.     case ENTITY_EVENT_RESET:
  57.     case ENTITY_EVENT_EDITOR_PROPERTY_CHANGED:
  58.     case ENTITY_EVENT_XFORM_FINISHED_EDITOR:
  59.         // Make sure to revive player when respawning in Editor
  60.         Reset();
  61.         break;
  62.     case ENTITY_EVENT_ANIM_EVENT:
  63.     {
  64.         const AnimEventInstance *pAnimEvent = reinterpret_cast<const AnimEventInstance *>(event.nParam[0]);
  65.         ICharacterInstance *pCharacter = reinterpret_cast<ICharacterInstance *>(event.nParam[1]);
  66.        
  67.         if (m_pActionController)
  68.             m_pActionController->OnAnimationEvent(pCharacter, *pAnimEvent);
  69.     }
  70.         break;
  71.     }
  72. }
  73.  
  74. void CNpcSpawnPoint::Update(SEntityUpdateContext & ctx, int updateSlot)
  75. {
  76.  
  77.     auto *pCharacter = GetEntity()->GetCharacter(0);
  78.  
  79.     if (pCharacter)
  80.     {
  81.         pCharacter->GetISkeletonAnim()->SetDesiredMotionParam(eMotionParamID_BlendWeight, 1.0f, 0.0f);
  82.  
  83.         //m_pAnimationContext->state.Set(m_walkTagId, true);
  84.     }
  85.  
  86.     if (m_pActionController != NULL)
  87.     {
  88.         m_pActionController->Update(ctx.fFrameTime);
  89.     }
  90. }
  91.  
  92. void CNpcSpawnPoint::Reset()
  93. {
  94.     SAFE_RELEASE(m_pActionController);
  95.     SAFE_DELETE(m_pAnimationContext);
  96.    
  97.     IEntity &entity = *GetEntity();
  98.  
  99.     // Check if we have to unload first
  100.     if (m_Geometry != -1)
  101.     {
  102.         entity.FreeSlot(m_Geometry);
  103.         m_Geometry = -1;
  104.     }
  105.  
  106.     m_Geometry = entity.LoadCharacter(0, "Assets\\Objects\\Characters\\man\\man.cdf");
  107.    
  108.     //Matrix34 m = Matrix34::CreateScale(Vec3(1.0f, 1.0f, 1.0f)) * Matrix34::CreateIdentity();
  109.    
  110.     entity.SetScale(Vec3(0.4f, 0.4f, 0.4f));
  111.     //entity.SetLocalTM(m);
  112.  
  113.  
  114.     // Mannequin Initialization
  115.     IMannequin& mannequinInterface = gEnv->pGame->GetIGameFramework()->GetMannequinInterface();
  116.     IAnimationDatabaseManager& animationDatabaseManager = mannequinInterface.GetAnimationDatabaseManager();
  117.  
  118.     // Loading the controller definition that we previously created.
  119.     // This is owned by the animation database manager
  120.     const SControllerDef* const pControllerDef = animationDatabaseManager.LoadControllerDef("Assets\\Animations\\Mannequin\\ADB\\manControllerDefinition.xml");
  121.     if (pControllerDef == NULL)
  122.     {
  123.         CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "Failed to load controller definition for Man.");
  124.         return;
  125.     }
  126.  
  127.     // Creation of the animation context.
  128.     CRY_ASSERT(m_pAnimationContext == NULL);
  129.     m_pAnimationContext = new SAnimationContext(*pControllerDef);
  130.  
  131.     // Creation of the action controller
  132.     CRY_ASSERT(m_pActionController == NULL);
  133.     m_pActionController = mannequinInterface.CreateActionController(&entity, *m_pAnimationContext);
  134.  
  135.     // Scope Context Setup.
  136.     // In our controller definition we have a scope context that we called MainCharacter
  137.     // The Scope Context Setup will associate this entity, the character instance we loaded at the beginning,
  138.     // and the animation database where we saved our fragments to this scope context.
  139.     const TagID mainCharacterScopeContextId = m_pAnimationContext->controllerDef.m_scopeContexts.Find("ManCharacter");
  140.     if (mainCharacterScopeContextId == TAG_ID_INVALID)
  141.     {
  142.         CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "Failed to find MainCharacter scope context id for Man in controller definition.");
  143.         return;
  144.     }
  145.  
  146.     ICharacterInstance* const pCharacterInstance = entity.GetCharacter(0);
  147.     CRY_ASSERT(pCharacterInstance != NULL);
  148.  
  149.     // Loading a database
  150.     const IAnimationDatabase* const pAnimationDatabase = animationDatabaseManager.Load("Animations/Mannequin/ADB/man.adb");
  151.     if (pAnimationDatabase == NULL)
  152.     {
  153.         CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "Failed to load animation database for Man.");
  154.         return;
  155.     }
  156.  
  157.     // Setting Scope contexts can happen at any time, and what entity or character instance we have bound to a particular scope context
  158.     // can change during the lifetime of an action controller.
  159.     m_pActionController->SetScopeContext(mainCharacterScopeContextId, entity, pCharacterInstance, pAnimationDatabase);
  160.  
  161.     const FragmentID idleFragmentId = m_pAnimationContext->controllerDef.m_fragmentIDs.Find("Idle");
  162.     const int actionPriority = 10;
  163.  
  164.     m_pIdleFragment = new TAction< SAnimationContext >(actionPriority, idleFragmentId, TAG_STATE_EMPTY, 0);
  165.     //pAction->SetSpeedBias(1.0f);
  166.     //pAction->SetAnimWeight(1.0f);
  167.  
  168.     m_pActionController->Queue(*m_pIdleFragment.get());
  169.  
  170.     m_walkTagId = m_pAnimationContext->state.GetDef().Find("Walk");
  171.     m_pAnimationContext->state.Set(m_walkTagId, true);
  172.     // Disable movement coming from the animation (root joint offset), we control this entirely via physics
  173.     //pCharacterInstance->GetISkeletonAnim()->SetAnimationDrivenMotion(1);
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top