BVB Source Codes

CRYENGINE Show VehicleDamageBehaviorSink.cpp Source code

Return Download CRYENGINE: download VehicleDamageBehaviorSink.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "VehicleDamageBehaviorSink.h"
  5.  
  6. #include "IVehicleSystem.h"
  7. #include "Vehicle.h"
  8.  
  9. CVehicleDamageBehaviorSink::CVehicleDamageBehaviorSink()
  10.         : m_pVehicle(nullptr)
  11.         , m_formerWaterDensity(0.0f)
  12.         , m_sinkingTimer(-1)
  13.         , m_isSinking(false)
  14. {}
  15.  
  16. //------------------------------------------------------------------------
  17. bool CVehicleDamageBehaviorSink::Init(IVehicle* pVehicle, const CVehicleParams& table)
  18. {
  19.         m_pVehicle = pVehicle;
  20.  
  21.         // save the initial buoyancy here (prevents problems with multiple versions of this damage behaviour acting on the same boat)
  22.         if (IPhysicalEntity* pPhysEntity = m_pVehicle->GetEntity()->GetPhysics())
  23.         {
  24.                 pe_params_buoyancy buoyancyParams;
  25.  
  26.                 if (pPhysEntity->GetParams(&buoyancyParams))
  27.                 {
  28.                         m_formerWaterDensity = buoyancyParams.kwaterDensity;
  29.                 }
  30.         }
  31.  
  32.         return true;
  33. }
  34.  
  35. //------------------------------------------------------------------------
  36. void CVehicleDamageBehaviorSink::Reset()
  37. {
  38.         if (m_isSinking)
  39.                 ChangeSinkingBehavior(false);
  40. }
  41.  
  42. //------------------------------------------------------------------------
  43. void CVehicleDamageBehaviorSink::OnDamageEvent(EVehicleDamageBehaviorEvent event, const SVehicleDamageBehaviorEventParams& behaviorParams)
  44. {
  45.         if (event == eVDBE_Hit || event == eVDBE_VehicleDestroyed || event == eVDBE_ComponentDestroyed)
  46.         {
  47.                 if (behaviorParams.componentDamageRatio >= 1.0f)
  48.                         ChangeSinkingBehavior(true);
  49.         }
  50.         else if (event == eVDBE_Repair && behaviorParams.componentDamageRatio < 1.f)
  51.         {
  52.                 ChangeSinkingBehavior(false);
  53.         }
  54. }
  55.  
  56. //------------------------------------------------------------------------
  57. void CVehicleDamageBehaviorSink::ChangeSinkingBehavior(bool isSinking)
  58. {
  59.         IEntity* pEntity = m_pVehicle->GetEntity();
  60.         CRY_ASSERT(pEntity);
  61.  
  62.         if (isSinking && !m_isSinking)
  63.         {
  64.                 // sink vehicle
  65.                 if (IPhysicalEntity* pPhysEntity = pEntity->GetPhysics())
  66.                 {
  67.                         pe_params_buoyancy buoyancyParams;
  68.                         if (pPhysEntity->GetParams(&buoyancyParams))
  69.                         {
  70.                                 buoyancyParams.kwaterDensity *= 0.5f;
  71.                                 pPhysEntity->SetParams(&buoyancyParams);
  72.                         }
  73.                 }
  74.  
  75.                 m_sinkingTimer = m_pVehicle->SetTimer(-1, 10000, this);
  76.                 m_isSinking = true;
  77.         }
  78.         else if (!isSinking && m_isSinking)
  79.         {
  80.                 // revert vehicle sinking
  81.                 if (IPhysicalEntity* pPhysEntity = pEntity->GetPhysics())
  82.                 {
  83.                         pe_params_buoyancy buoyancyParams;
  84.                         if (pPhysEntity->GetParams(&buoyancyParams))
  85.                         {
  86.                                 buoyancyParams.kwaterDensity = m_formerWaterDensity;
  87.                                 pPhysEntity->SetParams(&buoyancyParams);
  88.                         }
  89.                 }
  90.  
  91.                 m_sinkingTimer = m_pVehicle->KillTimer(m_sinkingTimer);
  92.                 m_isSinking = false;
  93.         }
  94. }
  95.  
  96. //------------------------------------------------------------------------
  97. void CVehicleDamageBehaviorSink::OnVehicleEvent(EVehicleEvent event, const SVehicleEventParams& params)
  98. {
  99.         switch (event)
  100.         {
  101.         case eVE_Timer:
  102.                 if (IPhysicalEntity* pPhysEntity = m_pVehicle->GetEntity()->GetPhysics())
  103.                 {
  104.                         // decrease water density further
  105.                         pe_params_buoyancy buoyancyParams;
  106.  
  107.                         if (pPhysEntity->GetParams(&buoyancyParams))
  108.                         {
  109.                                 buoyancyParams.kwaterDensity -= 0.05f * m_formerWaterDensity;
  110.                                 pPhysEntity->SetParams(&buoyancyParams);
  111.  
  112.                                 if (buoyancyParams.kwaterDensity > 0.05f * m_formerWaterDensity)
  113.                                 {
  114.                                         m_sinkingTimer = m_pVehicle->SetTimer(-1, 1000, this);
  115.                                 }
  116.                         }
  117.                 }
  118.                 break;
  119.         }
  120. }
  121.  
  122. //------------------------------------------------------------------------
  123. void CVehicleDamageBehaviorSink::Serialize(TSerialize serialize, EEntityAspects)
  124. {
  125.         if (serialize.GetSerializationTarget() != eST_Network)
  126.         {
  127.                 serialize.Value("sinkTimer", m_sinkingTimer);
  128.  
  129.                 bool isNowSinking = m_isSinking;
  130.                 serialize.Value("isSinking", isNowSinking);
  131.  
  132.                 if (serialize.IsReading())
  133.                 {
  134.                         ChangeSinkingBehavior(isNowSinking);
  135.                 }
  136.         }
  137. }
  138.  
  139. DEFINE_VEHICLEOBJECT(CVehicleDamageBehaviorSink);
  140.  
downloadVehicleDamageBehaviorSink.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top