Advertisement
Guest User

CProp

a guest
Mar 29th, 2017
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.93 KB | None | 0 0
  1. #include    <tgSystem.h>
  2. #include "CPhysicsManager.h"
  3. #include "CPropManager.h"
  4. #include "CTileManager.h"
  5.  
  6. #include    "CProp.h"
  7. #include    "CCamera.h"
  8. #include    "CClock.h"
  9. #include    "CSettings.h"
  10. #include    "AppRoot.h"
  11. #include    <tgCCore.h>
  12. #include    "GameStateMachine/CGameStates.h"
  13. #include    "CCharacter.h"
  14. #include    "Managers/CModelManager.h"
  15. #include    "Managers/CWorldManager.h"
  16. #include    "Managers/CLightManager.h"
  17. #include    "CAudioHandler.h"
  18. #include    "CBodyManager.h"
  19. #include    "Renderer/CRenderCallBacks.h"
  20. #include    "Managers/CAnimationManager.h"
  21. #include    "Managers/CConfigManager.h"
  22. #include <tgMemoryDisable.h>
  23.  
  24. // Math and base includes
  25. #include <Common/Base/hkBase.h>
  26.  
  27. #include    <Physics/Physics/Dynamics/World/hknpWorld.h>
  28. #include    <Physics/Physics/hknpTypes.h>
  29. #include    <Physics/Physics/Collide/Shape/Convex/hknpConvexShape.h>
  30.  
  31. #include    <Physics/Physics/Dynamics/Material/hknpMaterial.h>
  32. #include    <Physics/Physics/Dynamics/Material/hknpMaterialLibrary.h>
  33. #include    <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsToSceneDataBridge.h>
  34. #include    <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsSceneData.h>
  35. #include    <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsSystemDataUtil.h>
  36. #include    <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsSystem.h>
  37. #include    <Common/Serialize/Util/hkSerializeUtil.h>
  38. #include    <Physics/Physics/Dynamics/World/Events/hknpEventDispatcher.h>
  39. #include    <Physics/Physics/Dynamics/World/Events/hknpEvents.h>
  40. #include <tgMemoryEnable.h>
  41.  
  42. CProp::CProp( tgCV3D Pos, tgCV3D Rot, tgCModel* Model )
  43. {
  44.     m_SkullRandX = 20.0f;
  45.     m_SkullRandY = 4.0f;
  46.     m_SkullShootForce = 15.0f;
  47.     m_BouncyPadJumpHeight = 175.0f;
  48.     m_RespawnTime = 20.0f;
  49.     m_RespawnTimer = 0.0f;
  50.     m_BounceSoundTimer = 0.0f;
  51.  
  52.     if( CConfigManager::OpenFile( "settings/Settings_Props.ini" ) )
  53.     {
  54.         CConfigManager::ReadSetting( "Skullcanon", "shootingforce",NULL,    NULL,    &m_SkullShootForce );
  55.         CConfigManager::ReadSetting( "Skullcanon", "randrangex",NULL,   NULL,    &m_SkullRandX );
  56.         CConfigManager::ReadSetting( "Skullcanon", "randrangey",NULL,   NULL,    &m_SkullRandY );
  57.         CConfigManager::ReadSetting( "Bouncypad", "jumpheight",NULL,    NULL,    &m_BouncyPadJumpHeight );
  58.         CConfigManager::ReadSetting( "General", "respawntime",NULL, NULL,    &m_RespawnTime );
  59.         CConfigManager::CloseFile();
  60.     }
  61.  
  62.     m_PlayerActivated = 10;
  63.  
  64.  
  65.     m_pWorld = CPhysicsManager::GetInstance().GetWorld();
  66.  
  67.     m_pModel = Model;
  68.  
  69.     m_IsATrap = false;
  70.     m_IsAPowerup = false;
  71.     m_IsATrigger = false;
  72.     m_ShouldAnimate = false;
  73.     m_ShouldAnimateOnCollision = false;
  74.     m_IsAnimating = false;
  75.     m_HasShot = false;
  76.     m_PlayedBounceSound = false;
  77.     m_HasAlpha = false;
  78.     m_pModelMatrix = new tgCMatrix(tgCMatrix::Identity);
  79.  
  80.     m_Position = Pos;
  81.     m_Rotation = Rot;
  82.  
  83.     // Initialize the hkt model and shape
  84. #if defined(TG_WINDOWS)
  85.     m_pResource = hkSerializeUtil::load(tgCString("%s.hkt", Model->GetAssetName().String()).String());
  86. #endif
  87. #if defined(TG_PS4)
  88.     m_pResource = hkSerializeUtil::load(tgCString("/app0/%s.hkt", Model->GetAssetName().String()).String());
  89. #endif
  90.  
  91.     hkRootLevelContainer *pRootContainer = m_pResource->getContents<hkRootLevelContainer>();
  92.     hknpPhysicsSceneData *pSceneData = pRootContainer->findObject<hknpPhysicsSceneData>();
  93.     hknpPhysicsSystemData *pSystemData = reinterpret_cast<hknpPhysicsSystemData*>(pSceneData->m_systemDatas[0].val());
  94.     m_pWorld = CPhysicsManager::GetInstance().GetWorld();
  95.  
  96.    
  97.     hknpBodyCinfo Info;
  98.     Info.m_orientation.setAxisAngle(hkVector4(0.0f, 1.0f, 0.0f), Rot.y * HK_FLOAT_DEG_TO_RAD);
  99.  
  100.     if(Rot.x > 0.0f)
  101.         Info.m_orientation.setAxisAngle(hkVector4(1.0f, 0.0f, 0.0f), Rot.x * HK_FLOAT_DEG_TO_RAD);
  102.  
  103.     // Apply correct prop type by filtering by Collision filter info
  104.     tgUInt32 ColFilterInfo = pSystemData->m_bodyCinfos[0].m_collisionFilterInfo;
  105.    
  106.     // If this is a trap
  107.     if(ColFilterInfo == 1){
  108.         // ITS A TRAP!
  109.         m_IsATrap = true;
  110.     }
  111.  
  112.     if(ColFilterInfo == 2){
  113.         // ITS A Trigger!
  114.         m_IsATrigger = true;
  115.     }
  116.  
  117.     // Additive shader
  118.     if(ColFilterInfo == 3){
  119.         for (tgUInt32 i = 0; i < m_pModel->GetNumMeshes(); i++)
  120.         {
  121.             m_pModel->GetMesh(i)->SetRenderCallBack(CRenderCallBacks::MeshAdditiveCB);
  122.         }
  123.     }
  124.  
  125.     m_HasLocalTransform = false;
  126.  
  127.     CheckForSpecialProp();
  128.  
  129.     if(m_IsATrap || m_IsAPowerup || m_IsATrigger){
  130.         pSystemData->m_bodyCinfos[0].m_flags = pSystemData->m_bodyCinfos[0].m_flags | hknpCollisionFlags::RAISE_TRIGGER_EVENTS;
  131.     }
  132.  
  133.     Info.m_orientation.normalize();
  134.     Info.m_motionType =  hknpMotionType::STATIC;
  135.     m_Transform.setIdentity();
  136.     m_Transform.setTranslation(hkVector4(Pos.x, Pos.y  , Pos.z));
  137.     m_Transform.setRotation(Info.m_orientation);
  138.  
  139.     m_pPhysicsSystem = new hknpPhysicsSystem(pSystemData, m_pWorld, m_Transform);
  140.  
  141.     m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10007, 1);
  142.     m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10008, 0);
  143.  
  144.     if(m_IsATrap || m_IsAPowerup || m_IsATrigger){
  145.         m_pWorld->getEventSignal(hknpEventType::TRIGGER, m_pPhysicsSystem->getBodyIds()[0]).subscribe(this, &CProp::onCollidingWithObject, "TrapCollision");
  146.         m_pWorld->setBodyMotionType(m_pPhysicsSystem->getBodyIds()[0], hknpMotionType::STATIC);
  147.     }
  148.     else
  149.     {
  150.         // Should be affected by explosions
  151.         if(pSystemData->m_bodyCinfos[0].m_motionType == hknpMotionType::STATIC)
  152.             m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10008, 0);
  153.         else
  154.             m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10008, 1);
  155.  
  156.     }
  157.  
  158.     if(pSystemData->m_bodyCinfos[0].m_motionType == hknpMotionType::STATIC)
  159.         m_pWorld->setBodyQuality(m_pPhysicsSystem->getBodyIds()[0], hknpBodyQualityId::PRESET_STATIC);
  160.  
  161.     pSystemData->removeReference();
  162.     pSceneData->removeReference();
  163.  
  164. }
  165.  
  166. CProp::~CProp( void )
  167. {
  168.     m_pWorld->destroyBodies(&m_pPhysicsSystem->getBodyIds()[0], 1);
  169.  
  170.     if(m_pPhysicsSystem)
  171.         m_pPhysicsSystem->removeReference();
  172.  
  173.    
  174.     if(m_ShouldAnimateOnCollision || m_ShouldAnimate)
  175.         delete m_pInterpolator;
  176.  
  177.     delete m_pModelMatrix;
  178.     if(m_HasLocalTransform)
  179.         delete m_LocalTransform;
  180.    
  181.     m_pResource->callDestructors();
  182.     m_pResource->callDestructors();
  183.     m_pResource->removeReference();
  184. }
  185.  
  186. tgCInterpolator::ECallBackResult CProp::AnimateOnceCB( tgCInterpolator& /*rInterpolator*/, void* pUserData )
  187. {
  188.     // Grab pointer to CPlayer item
  189.     CProp*  pProp   = ( CProp* )pUserData;
  190.  
  191.     pProp->SetIsAnimating( false );
  192.  
  193.     // Make the animation repeat itself
  194.     return tgCInterpolator::CALLBACK_RESULT_WRAP;
  195. }   // */ // AttackOnceAnimationCB
  196.  
  197. void CProp::onCollidingWithObject( const hknpEventHandlerInput &Input, const hknpEvent &hkEvent )
  198. {
  199.     hknpTriggerEvent rTriggerEvent = hkEvent.asTriggerEvent();
  200.     hknpBodyId OtherID = Input.getOtherBody(rTriggerEvent);
  201.     std::vector<CCharacter*>* Chars = CGameStates::GetInstance().GetStateGame()->GetCharacters();
  202.  
  203.     // Trigger collision
  204.     if(m_IsATrigger){
  205.         // If the colliding body is a player
  206.         int *PlayerBodyProperty = m_pWorld->getBodyProperty<int>(OtherID, 10000);
  207.  
  208.         // If the colliding body is a grenade
  209.         int *GrenadeBodyProperty = m_pWorld->getBodyProperty<int>(OtherID, 10010);
  210.  
  211.         if(PlayerBodyProperty){
  212.             for (tgUInt32 i = 0; i < Chars->size(); i++)
  213.             {
  214.                 CCharacter* OtherPlayer = Chars->at(i);
  215.  
  216.                 if(OtherPlayer->GetBodyID() == OtherID){
  217.                     // Check special props
  218.                     if(m_pModel->GetAssetName() == "data/models/havokshapes/env_trigger_skullcannon_v1"){
  219.                         for (tgUInt32 i = 0; i < CPropManager::GetInstance().GetAllProps().size(); i++)
  220.                         {
  221.                             CProp* Prop = CPropManager::GetInstance().GetAllProps().at(i);
  222.  
  223.                             if(Prop->GetModel().GetAssetName() == "data/models/havokshapes/env_hazobject_skullcannon_v01"){
  224.                                 Prop->SetPlayerActivated(OtherPlayer->GetPlayerTag());
  225.  
  226.                                 Prop->SetIsAnimating(true);
  227.                                 Prop->SetHasShot(false);
  228.  
  229.                             }
  230.                         }
  231.                     }
  232.                 }
  233.  
  234.                 if(m_ShouldAnimateOnCollision){
  235.                     m_IsAnimating = true;
  236.                 }
  237.             }
  238.         }
  239.  
  240.         if(GrenadeBodyProperty){
  241.             if(m_ShouldAnimateOnCollision){
  242.                 m_IsAnimating = true;
  243.             }
  244.         }
  245.  
  246.         if(m_pModel->GetAssetName() == "data/models/havokshapes/env_object_bouncypad_v1"){
  247.             if(PlayerBodyProperty)
  248.                 m_pWorld->applyBodyLinearImpulse(OtherID, hkVector4(0.0f, m_BouncyPadJumpHeight, 0.0f));
  249.             else
  250.                 m_pWorld->applyBodyLinearImpulse(OtherID, hkVector4(0.0f, m_BouncyPadJumpHeight/4, 0.0f));
  251.  
  252.             if(!m_PlayedBounceSound && CGameStates::GetInstance().GetStateGame()->CountDownFinished()){
  253.                 CAudioHandler::GetInstance().StartSound(CAudioHandler::BOUNCE);
  254.                 m_PlayedBounceSound = true;
  255.             }
  256.         }
  257.  
  258.  
  259.     }
  260.  
  261. }
  262.  
  263. void CProp::Update( tgFloat DeltaTime )
  264. {
  265.  
  266.    
  267.     m_pWorld->getBodyTransform(m_pPhysicsSystem->getBodyIds()[0]).get4x4ColumnMajor(reinterpret_cast<tgFloat*>(m_pModelMatrix));
  268.  
  269.     m_Position = m_pModelMatrix->Pos;
  270.  
  271.     // If we have fallen down
  272.     if(m_pModelMatrix->Pos.y < -8.0f){
  273.         m_RespawnTimer += DeltaTime;
  274.  
  275.         // If we should respawn
  276.         if(m_RespawnTimer >= m_RespawnTime){
  277.             tgCV3D Pos = tgCV3D(tgMathRandom(0.0f, -(tgFloat)CTileManager::GetInstance().GetGridSizeX() * 2.0f), 20.0f, tgMathRandom(0.0f, (tgFloat)CTileManager::GetInstance().GetGridSizeY() * 2.0f));
  278.             m_pWorld->setBodyPosition(m_pPhysicsSystem->getBodyIds()[0], hkVector4(Pos.x, Pos.y, Pos.z));
  279.             m_RespawnTimer = 0.0f;
  280.         }
  281.     }
  282.  
  283.     // Animate
  284.     if(m_IsAnimating || m_ShouldAnimate){
  285.  
  286.         m_pInterpolator->AddTime( DeltaTime );
  287.         m_pModel->SetAnimationMatrices(*m_pInterpolator);
  288.  
  289.     }
  290.  
  291. }
  292.  
  293. void CProp::Render( void )
  294. {
  295.     if(!m_HasAlpha){
  296.         m_pModel->GetTransform().GetMatrixLocal() = *m_pModelMatrix;
  297.         m_pModel->Update();
  298.  
  299.         if(m_HasLocalTransform){
  300.             m_LocalTransform->GetMatrixLocal() = *m_pModelMatrix;
  301.             m_LocalTransform->GetMatrixLocal().Pos = m_pModelMatrix->Pos - m_LocalTransform->GetMatrixLocal().Up;
  302.             m_LocalTransform->Update();
  303.         }
  304.  
  305.         if(!m_IsPickedUp){
  306.             if(m_pModelMatrix->Pos.y > -8.0f){
  307.            
  308.                 m_pModel->Render();
  309.             }
  310.         }
  311.     }
  312. }  
  313.  
  314. void CProp::RenderAlpha( void )
  315. {
  316.     if(m_HasAlpha){
  317.         m_pModel->GetTransform().GetMatrixLocal() = *m_pModelMatrix;
  318.         m_pModel->Update();
  319.  
  320.         if(m_pModelMatrix->Pos.y > -8.0f){
  321.             m_pModel->Render();
  322.         }
  323.     }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement