Advertisement
Guest User

CTile

a guest
Mar 29th, 2017
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include    <tgSystem.h>
  2. #include "CPhysicsManager.h"
  3. #include "CTileManager.h"
  4.  
  5. #include    "CTile.h"
  6. #include    "CCamera.h"
  7. #include    "CClock.h"
  8. #include    "CSettings.h"
  9. #include    "AppRoot.h"
  10. #include    <tgCCore.h>
  11. #include    "Managers/CModelManager.h"
  12. #include    "Managers/CWorldManager.h"
  13. #include    "Managers/CLightManager.h"
  14. #include    "GameStateMachine/CGameStates.h"
  15. #include    "CAudioHandler.h"
  16. #include <tgMemoryDisable.h>
  17.  
  18. // Math and base includes
  19. #include <Common/Base/hkBase.h>
  20.  
  21. #include    <Physics/Physics/Dynamics/World/hknpWorld.h>
  22. #include    <Physics/Physics/hknpTypes.h>
  23. #include    <Physics/Physics/Collide/Shape/Convex/hknpConvexShape.h>
  24.  
  25. #include    <Physics/Physics/Dynamics/Material/hknpMaterial.h>
  26. #include    <Physics/Physics/Dynamics/Material/hknpMaterialLibrary.h>
  27.  
  28. #include <tgMemoryEnable.h>
  29.  
  30. CTile::CTile( tgCV3D Pos, tgFloat SizeXZ, tgFloat SizeY, tgUInt32 Type, tgUInt32 Theme, tgUInt32 TextureIndex)
  31. {
  32.     m_pWorld = CPhysicsManager::GetInstance().GetWorld();
  33.  
  34.     m_Type = Type;
  35.     m_Position = Pos;
  36.     m_OriginalPosition = Pos;
  37.     m_Rotation = 0;
  38.     m_SizeXZ = SizeXZ;
  39.     m_SizeY  = SizeY;
  40.    
  41.     if(Type != CTile::TILETYPE_EMPTY){
  42.         if(CGameStates::GetInstance().GetStateMenu()->GetIsEditingLevel()){
  43.             tgCString AssetName = CTileManager::GetInstance().GetTileModel(Type, Theme)->GetAssetName();
  44.             m_pModel =  CModelManager::GetInstance().LoadModel(AssetName, tgCString("tile_model%d", CTileManager::GetInstance().GetAllTiles().size()), true);
  45.  
  46.             tgCTexture* Texture;
  47.             Texture = CTileManager::GetInstance().GetTileTexture(Type, Theme);
  48.  
  49.             m_pModel->GetMesh(0)->GetSubMesh(0)->Material.SetColormap(*Texture);
  50.         }
  51.         else{
  52.             // Get correct model from tile manager
  53.             if(Type >= CTile::TILETYPE_NORESPAWNING)
  54.                 m_pModel = CTileManager::GetInstance().GetTileModelWithTextureIndex(Type, Type);
  55.             else
  56.                 m_pModel = CTileManager::GetInstance().GetTileModel(Type, Theme);
  57.         }
  58.  
  59.         m_pModelMatrix = new tgCMatrix(tgCMatrix::Identity);
  60.  
  61.  
  62.         m_RespawnTime = 10.0f;
  63.         hknpMaterial Material;
  64.         Material.m_restitution = 0.0f;
  65.         Material.m_restitutionCombinePolicy = hknpMaterial::CombinePolicy::COMBINE_GEOMETRIC_MEAN;
  66.  
  67.         hknpMaterialId MaterialID = m_pWorld->accessMaterialLibrary()->addEntry(Material);
  68.         hknpConvexShape *pShape = hknpConvexShape::createFromHalfExtents(hkVector4(SizeXZ + 0.01f, 0.2f, SizeXZ + 0.01f));
  69.         hknpBodyCinfo Info;
  70.  
  71.         Info.m_shape = pShape;
  72.         Info.m_materialId = MaterialID;
  73.         Info.m_position.set(Pos.x, Pos.y, Pos.z);
  74.         Info.m_motionType =  hknpMotionType::STATIC;
  75.         Info.m_mass = 7.5f;
  76.  
  77.         m_pModel->GetTransform().GetMatrixLocal().Translate(Pos, tgCMatrix::COMBINE_REPLACE);
  78.  
  79.         m_BodyID = m_pWorld->createBody(Info);
  80.         pShape->removeReference();
  81.  
  82.         if(Type == TILETYPE_DESTRUCTIBLE || Type == TILETYPE_DESTRUCTIBLE2 || Type == TILETYPE_DESTRUCTIBLE3 || Type >= TILETYPE_NORESPAWNING)
  83.             m_pWorld->setBodyProperty<int>(m_BodyID, 10008, 1);
  84.         else
  85.             m_pWorld->setBodyProperty<int>(m_BodyID, 10008, 0);
  86.  
  87.         m_HasPlayedBreakSound = false;
  88.         m_TileBreakSoundDelay = 0.0f;
  89.  
  90.         m_pWorld->setBodyQuality(m_BodyID, hknpBodyQualityId::PRESET_DEBRIS);
  91.     }
  92. }
  93.  
  94. CTile::~CTile( void )
  95. {
  96.     if(m_Type != CTile::TILETYPE_EMPTY){
  97.  
  98.         delete m_pModelMatrix;
  99.  
  100.         m_pWorld->destroyBodies(&m_BodyID, 1);
  101.     }
  102.  
  103. }
  104.  
  105.  
  106. void CTile::Update( tgFloat DeltaTime )
  107. {
  108.     if(m_Type != CTile::TILETYPE_EMPTY){
  109.  
  110.         m_pWorld->getBodyTransform(m_BodyID).get4x4ColumnMajor(reinterpret_cast<tgFloat*>(m_pModelMatrix));
  111.  
  112.         if((m_Type == TILETYPE_INDESTRUCTIBLE || m_Type == TILETYPE_RESPAWNPOINT) && m_pWorld->getBody(m_BodyID).getMotionType() != hknpMotionType::STATIC)
  113.             m_pWorld->setBodyMotionType(m_BodyID, hknpMotionType::STATIC);
  114.  
  115.         if(m_Type < TILETYPE_NORESPAWNING){
  116.             if(m_pWorld->getBodyTransform(m_BodyID).getTranslation()(1) <= -5.0f){
  117.                 m_RespawnTimer += DeltaTime;
  118.                 m_RespawnTimer = tgMathClamp(0.0f, m_RespawnTimer, m_RespawnTime);
  119.                 if(m_RespawnTimer >= m_RespawnTime){
  120.                     hknpBodyCinfo Info;
  121.                     m_pWorld->getBodyCinfo(m_BodyID, Info);
  122.  
  123.                     Info.m_position.set(m_OriginalPosition.x, m_OriginalPosition.y, m_OriginalPosition.z);
  124.                     Info.m_orientation.setAxisAngle(hkVector4(0.0f, 1.0f, 0.0f), 0.0f);
  125.                     Info.m_orientation.normalize();
  126.  
  127.                     hkTransform Transform = m_pWorld->getBody(m_BodyID).getTransform();
  128.  
  129.                     Transform.setRotation(Info.m_orientation);
  130.                     Transform.setTranslation(Info.m_position);
  131.  
  132.                     m_pWorld->setBodyTransform(m_BodyID, Transform);
  133.                     m_pWorld->setBodyMotionType(m_BodyID, hknpMotionType::STATIC);
  134.  
  135.                     m_RespawnTimer = 0.0f;
  136.                     m_TileBreakSoundDelay = 0.0f;
  137.                 }
  138.             }
  139.         }
  140.     }
  141. }
  142.  
  143. void CTile::Render( void )
  144. {
  145.     if(m_Type != CTile::TILETYPE_EMPTY){
  146.         m_pModel->GetTransform().GetMatrixLocal() = *m_pModelMatrix;
  147.         m_pModel->Update();
  148.  
  149.         if(m_pModel->GetTransform().GetMatrixLocal().Pos.y > -10.0f){
  150.             if(!CGameStates::GetInstance().GetStateEditor()->GetIfInEditor() || CGameStates::GetInstance().GetStateMenu()->GetIsEditingLevel()){
  151.                 m_pModel->Render();
  152.             }
  153.  
  154.         }
  155.         else{
  156.             if(m_pWorld->getBody(m_BodyID).getMotionType() == hknpMotionType::DYNAMIC)
  157.                 m_pWorld->setBodyMotionType(m_BodyID, hknpMotionType::STATIC);
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement