Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tgSystem.h>
- #include "CPhysicsManager.h"
- #include "CPropManager.h"
- #include "CTileManager.h"
- #include "CProp.h"
- #include "CCamera.h"
- #include "CClock.h"
- #include "CSettings.h"
- #include "AppRoot.h"
- #include <tgCCore.h>
- #include "GameStateMachine/CGameStates.h"
- #include "CCharacter.h"
- #include "Managers/CModelManager.h"
- #include "Managers/CWorldManager.h"
- #include "Managers/CLightManager.h"
- #include "CAudioHandler.h"
- #include "CBodyManager.h"
- #include "Renderer/CRenderCallBacks.h"
- #include "Managers/CAnimationManager.h"
- #include "Managers/CConfigManager.h"
- #include <tgMemoryDisable.h>
- // Math and base includes
- #include <Common/Base/hkBase.h>
- #include <Physics/Physics/Dynamics/World/hknpWorld.h>
- #include <Physics/Physics/hknpTypes.h>
- #include <Physics/Physics/Collide/Shape/Convex/hknpConvexShape.h>
- #include <Physics/Physics/Dynamics/Material/hknpMaterial.h>
- #include <Physics/Physics/Dynamics/Material/hknpMaterialLibrary.h>
- #include <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsToSceneDataBridge.h>
- #include <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsSceneData.h>
- #include <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsSystemDataUtil.h>
- #include <Physics/Physics/Extensions/PhysicsSystem/hknpPhysicsSystem.h>
- #include <Common/Serialize/Util/hkSerializeUtil.h>
- #include <Physics/Physics/Dynamics/World/Events/hknpEventDispatcher.h>
- #include <Physics/Physics/Dynamics/World/Events/hknpEvents.h>
- #include <tgMemoryEnable.h>
- CProp::CProp( tgCV3D Pos, tgCV3D Rot, tgCModel* Model )
- {
- m_SkullRandX = 20.0f;
- m_SkullRandY = 4.0f;
- m_SkullShootForce = 15.0f;
- m_BouncyPadJumpHeight = 175.0f;
- m_RespawnTime = 20.0f;
- m_RespawnTimer = 0.0f;
- m_BounceSoundTimer = 0.0f;
- if( CConfigManager::OpenFile( "settings/Settings_Props.ini" ) )
- {
- CConfigManager::ReadSetting( "Skullcanon", "shootingforce",NULL, NULL, &m_SkullShootForce );
- CConfigManager::ReadSetting( "Skullcanon", "randrangex",NULL, NULL, &m_SkullRandX );
- CConfigManager::ReadSetting( "Skullcanon", "randrangey",NULL, NULL, &m_SkullRandY );
- CConfigManager::ReadSetting( "Bouncypad", "jumpheight",NULL, NULL, &m_BouncyPadJumpHeight );
- CConfigManager::ReadSetting( "General", "respawntime",NULL, NULL, &m_RespawnTime );
- CConfigManager::CloseFile();
- }
- m_PlayerActivated = 10;
- m_pWorld = CPhysicsManager::GetInstance().GetWorld();
- m_pModel = Model;
- m_IsATrap = false;
- m_IsAPowerup = false;
- m_IsATrigger = false;
- m_ShouldAnimate = false;
- m_ShouldAnimateOnCollision = false;
- m_IsAnimating = false;
- m_HasShot = false;
- m_PlayedBounceSound = false;
- m_HasAlpha = false;
- m_pModelMatrix = new tgCMatrix(tgCMatrix::Identity);
- m_Position = Pos;
- m_Rotation = Rot;
- // Initialize the hkt model and shape
- #if defined(TG_WINDOWS)
- m_pResource = hkSerializeUtil::load(tgCString("%s.hkt", Model->GetAssetName().String()).String());
- #endif
- #if defined(TG_PS4)
- m_pResource = hkSerializeUtil::load(tgCString("/app0/%s.hkt", Model->GetAssetName().String()).String());
- #endif
- hkRootLevelContainer *pRootContainer = m_pResource->getContents<hkRootLevelContainer>();
- hknpPhysicsSceneData *pSceneData = pRootContainer->findObject<hknpPhysicsSceneData>();
- hknpPhysicsSystemData *pSystemData = reinterpret_cast<hknpPhysicsSystemData*>(pSceneData->m_systemDatas[0].val());
- m_pWorld = CPhysicsManager::GetInstance().GetWorld();
- hknpBodyCinfo Info;
- Info.m_orientation.setAxisAngle(hkVector4(0.0f, 1.0f, 0.0f), Rot.y * HK_FLOAT_DEG_TO_RAD);
- if(Rot.x > 0.0f)
- Info.m_orientation.setAxisAngle(hkVector4(1.0f, 0.0f, 0.0f), Rot.x * HK_FLOAT_DEG_TO_RAD);
- // Apply correct prop type by filtering by Collision filter info
- tgUInt32 ColFilterInfo = pSystemData->m_bodyCinfos[0].m_collisionFilterInfo;
- // If this is a trap
- if(ColFilterInfo == 1){
- // ITS A TRAP!
- m_IsATrap = true;
- }
- if(ColFilterInfo == 2){
- // ITS A Trigger!
- m_IsATrigger = true;
- }
- // Additive shader
- if(ColFilterInfo == 3){
- for (tgUInt32 i = 0; i < m_pModel->GetNumMeshes(); i++)
- {
- m_pModel->GetMesh(i)->SetRenderCallBack(CRenderCallBacks::MeshAdditiveCB);
- }
- }
- m_HasLocalTransform = false;
- CheckForSpecialProp();
- if(m_IsATrap || m_IsAPowerup || m_IsATrigger){
- pSystemData->m_bodyCinfos[0].m_flags = pSystemData->m_bodyCinfos[0].m_flags | hknpCollisionFlags::RAISE_TRIGGER_EVENTS;
- }
- Info.m_orientation.normalize();
- Info.m_motionType = hknpMotionType::STATIC;
- m_Transform.setIdentity();
- m_Transform.setTranslation(hkVector4(Pos.x, Pos.y , Pos.z));
- m_Transform.setRotation(Info.m_orientation);
- m_pPhysicsSystem = new hknpPhysicsSystem(pSystemData, m_pWorld, m_Transform);
- m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10007, 1);
- m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10008, 0);
- if(m_IsATrap || m_IsAPowerup || m_IsATrigger){
- m_pWorld->getEventSignal(hknpEventType::TRIGGER, m_pPhysicsSystem->getBodyIds()[0]).subscribe(this, &CProp::onCollidingWithObject, "TrapCollision");
- m_pWorld->setBodyMotionType(m_pPhysicsSystem->getBodyIds()[0], hknpMotionType::STATIC);
- }
- else
- {
- // Should be affected by explosions
- if(pSystemData->m_bodyCinfos[0].m_motionType == hknpMotionType::STATIC)
- m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10008, 0);
- else
- m_pWorld->setBodyProperty<int>(m_pPhysicsSystem->getBodyIds()[0], 10008, 1);
- }
- if(pSystemData->m_bodyCinfos[0].m_motionType == hknpMotionType::STATIC)
- m_pWorld->setBodyQuality(m_pPhysicsSystem->getBodyIds()[0], hknpBodyQualityId::PRESET_STATIC);
- pSystemData->removeReference();
- pSceneData->removeReference();
- }
- CProp::~CProp( void )
- {
- m_pWorld->destroyBodies(&m_pPhysicsSystem->getBodyIds()[0], 1);
- if(m_pPhysicsSystem)
- m_pPhysicsSystem->removeReference();
- if(m_ShouldAnimateOnCollision || m_ShouldAnimate)
- delete m_pInterpolator;
- delete m_pModelMatrix;
- if(m_HasLocalTransform)
- delete m_LocalTransform;
- m_pResource->callDestructors();
- m_pResource->callDestructors();
- m_pResource->removeReference();
- }
- tgCInterpolator::ECallBackResult CProp::AnimateOnceCB( tgCInterpolator& /*rInterpolator*/, void* pUserData )
- {
- // Grab pointer to CPlayer item
- CProp* pProp = ( CProp* )pUserData;
- pProp->SetIsAnimating( false );
- // Make the animation repeat itself
- return tgCInterpolator::CALLBACK_RESULT_WRAP;
- } // */ // AttackOnceAnimationCB
- void CProp::onCollidingWithObject( const hknpEventHandlerInput &Input, const hknpEvent &hkEvent )
- {
- hknpTriggerEvent rTriggerEvent = hkEvent.asTriggerEvent();
- hknpBodyId OtherID = Input.getOtherBody(rTriggerEvent);
- std::vector<CCharacter*>* Chars = CGameStates::GetInstance().GetStateGame()->GetCharacters();
- // Trigger collision
- if(m_IsATrigger){
- // If the colliding body is a player
- int *PlayerBodyProperty = m_pWorld->getBodyProperty<int>(OtherID, 10000);
- // If the colliding body is a grenade
- int *GrenadeBodyProperty = m_pWorld->getBodyProperty<int>(OtherID, 10010);
- if(PlayerBodyProperty){
- for (tgUInt32 i = 0; i < Chars->size(); i++)
- {
- CCharacter* OtherPlayer = Chars->at(i);
- if(OtherPlayer->GetBodyID() == OtherID){
- // Check special props
- if(m_pModel->GetAssetName() == "data/models/havokshapes/env_trigger_skullcannon_v1"){
- for (tgUInt32 i = 0; i < CPropManager::GetInstance().GetAllProps().size(); i++)
- {
- CProp* Prop = CPropManager::GetInstance().GetAllProps().at(i);
- if(Prop->GetModel().GetAssetName() == "data/models/havokshapes/env_hazobject_skullcannon_v01"){
- Prop->SetPlayerActivated(OtherPlayer->GetPlayerTag());
- Prop->SetIsAnimating(true);
- Prop->SetHasShot(false);
- }
- }
- }
- }
- if(m_ShouldAnimateOnCollision){
- m_IsAnimating = true;
- }
- }
- }
- if(GrenadeBodyProperty){
- if(m_ShouldAnimateOnCollision){
- m_IsAnimating = true;
- }
- }
- if(m_pModel->GetAssetName() == "data/models/havokshapes/env_object_bouncypad_v1"){
- if(PlayerBodyProperty)
- m_pWorld->applyBodyLinearImpulse(OtherID, hkVector4(0.0f, m_BouncyPadJumpHeight, 0.0f));
- else
- m_pWorld->applyBodyLinearImpulse(OtherID, hkVector4(0.0f, m_BouncyPadJumpHeight/4, 0.0f));
- if(!m_PlayedBounceSound && CGameStates::GetInstance().GetStateGame()->CountDownFinished()){
- CAudioHandler::GetInstance().StartSound(CAudioHandler::BOUNCE);
- m_PlayedBounceSound = true;
- }
- }
- }
- }
- void CProp::Update( tgFloat DeltaTime )
- {
- m_pWorld->getBodyTransform(m_pPhysicsSystem->getBodyIds()[0]).get4x4ColumnMajor(reinterpret_cast<tgFloat*>(m_pModelMatrix));
- m_Position = m_pModelMatrix->Pos;
- // If we have fallen down
- if(m_pModelMatrix->Pos.y < -8.0f){
- m_RespawnTimer += DeltaTime;
- // If we should respawn
- if(m_RespawnTimer >= m_RespawnTime){
- tgCV3D Pos = tgCV3D(tgMathRandom(0.0f, -(tgFloat)CTileManager::GetInstance().GetGridSizeX() * 2.0f), 20.0f, tgMathRandom(0.0f, (tgFloat)CTileManager::GetInstance().GetGridSizeY() * 2.0f));
- m_pWorld->setBodyPosition(m_pPhysicsSystem->getBodyIds()[0], hkVector4(Pos.x, Pos.y, Pos.z));
- m_RespawnTimer = 0.0f;
- }
- }
- // Animate
- if(m_IsAnimating || m_ShouldAnimate){
- m_pInterpolator->AddTime( DeltaTime );
- m_pModel->SetAnimationMatrices(*m_pInterpolator);
- }
- }
- void CProp::Render( void )
- {
- if(!m_HasAlpha){
- m_pModel->GetTransform().GetMatrixLocal() = *m_pModelMatrix;
- m_pModel->Update();
- if(m_HasLocalTransform){
- m_LocalTransform->GetMatrixLocal() = *m_pModelMatrix;
- m_LocalTransform->GetMatrixLocal().Pos = m_pModelMatrix->Pos - m_LocalTransform->GetMatrixLocal().Up;
- m_LocalTransform->Update();
- }
- if(!m_IsPickedUp){
- if(m_pModelMatrix->Pos.y > -8.0f){
- m_pModel->Render();
- }
- }
- }
- }
- void CProp::RenderAlpha( void )
- {
- if(m_HasAlpha){
- m_pModel->GetTransform().GetMatrixLocal() = *m_pModelMatrix;
- m_pModel->Update();
- if(m_pModelMatrix->Pos.y > -8.0f){
- m_pModel->Render();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement