BVB Source Codes

CRYENGINE Show VehicleDamageBehaviorDestroy.cpp Source code

Return Download CRYENGINE: download VehicleDamageBehaviorDestroy.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 "VehicleDamageBehaviorDestroy.h"
  5.  
  6. #include "IVehicleSystem.h"
  7. #include "Vehicle.h"
  8. #include "VehicleComponent.h"
  9. #include "VehicleSeat.h"
  10.  
  11. CVehicleDamageBehaviorDestroy::CVehicleDamageBehaviorDestroy()
  12.         : m_pVehicle(nullptr)
  13. {}
  14.  
  15. //------------------------------------------------------------------------
  16. bool CVehicleDamageBehaviorDestroy::Init(IVehicle* pVehicle, const CVehicleParams& table)
  17. {
  18.         m_pVehicle = (CVehicle*) pVehicle;
  19.  
  20.         if (table.haveAttr("effect"))
  21.                 m_effectName = table.getAttr("effect");
  22.  
  23.         return true;
  24. }
  25.  
  26. //------------------------------------------------------------------------
  27. void CVehicleDamageBehaviorDestroy::Reset()
  28. {
  29. }
  30.  
  31. //------------------------------------------------------------------------
  32. void CVehicleDamageBehaviorDestroy::OnDamageEvent(EVehicleDamageBehaviorEvent event, const SVehicleDamageBehaviorEventParams& behaviorParams)
  33. {
  34.         if (event == eVDBE_Repair)
  35.                 return;
  36.  
  37.         if (behaviorParams.componentDamageRatio >= 1.0f)
  38.                 SetDestroyed(true, behaviorParams.shooterId);
  39. }
  40.  
  41. //------------------------------------------------------------------------
  42. void CVehicleDamageBehaviorDestroy::SetDestroyed(bool isDestroyed, EntityId shooterId)
  43. {
  44.         IEntity* pVehicleEntity = m_pVehicle->GetEntity();
  45.  
  46.         bool destroyed = m_pVehicle->IsDestroyed();
  47.         m_pVehicle->SetDestroyedStatus(isDestroyed);
  48.  
  49.         // destroy the vehicle
  50.         if (!destroyed && isDestroyed)
  51.         {
  52.                 CryLog("%s being driven by %s has been destroyed!", m_pVehicle->GetEntity()->GetEntityTextDescription().c_str(), m_pVehicle->GetDriver() ? m_pVehicle->GetDriver()->GetEntity()->GetEntityTextDescription().c_str() : "nobody");
  53.                 INDENT_LOG_DURING_SCOPE();
  54.  
  55.                 // call OnVehicleDestroyed function on lua side
  56.                 HSCRIPTFUNCTION scriptFunction(NULL);
  57.                 if (pVehicleEntity->GetScriptTable()->GetValue("OnVehicleDestroyed", scriptFunction) && scriptFunction)
  58.                 {
  59.                         IScriptSystem* pIScriptSystem = gEnv->pScriptSystem;
  60.                         Script::Call(pIScriptSystem, scriptFunction, pVehicleEntity->GetScriptTable());
  61.                         pIScriptSystem->ReleaseFunc(scriptFunction);
  62.                 }
  63.  
  64.                 // notify the other components
  65.                 TVehicleComponentVector& components = m_pVehicle->GetComponents();
  66.                 for (TVehicleComponentVector::iterator ite = components.begin(); ite != components.end(); ++ite)
  67.                 {
  68.                         CVehicleComponent* pComponent = *ite;
  69.                         pComponent->OnVehicleDestroyed();
  70.                 }
  71.  
  72.                 // notify vehicle parts
  73.  
  74.                 TVehiclePartVector parts = m_pVehicle->GetParts();
  75.                 IVehiclePart::SVehiclePartEvent partEvent;
  76.                 partEvent.type = IVehiclePart::eVPE_Damaged;
  77.                 partEvent.fparam = 1.0f;
  78.  
  79.                 for (TVehiclePartVector::iterator ite = parts.begin(); ite != parts.end(); ++ite)
  80.                 {
  81.                         IVehiclePart* pPart = ite->second;
  82.                         pPart->OnEvent(partEvent);
  83.                 }
  84.  
  85.                 // notify active movement type
  86.                 if (IVehicleMovement* pVehicleMovement = m_pVehicle->GetMovement())
  87.                 {
  88.                         SVehicleMovementEventParams movementParams;
  89.                         movementParams.fValue = 1.0f;
  90.                         movementParams.bValue = true;
  91.                         pVehicleMovement->OnEvent(IVehicleMovement::eVME_Damage, movementParams);
  92.  
  93.                         movementParams.fValue = 1.0f;
  94.                         movementParams.bValue = false;
  95.                         pVehicleMovement->OnEvent(IVehicleMovement::eVME_VehicleDestroyed, movementParams);
  96.                 }
  97.  
  98.                 // broadcast event through all vehicle notifiers (eg. seats..)
  99.                 SVehicleEventParams eventParams;
  100.                 eventParams.entityId = shooterId;
  101.                 m_pVehicle->BroadcastVehicleEvent(eVE_Destroyed, eventParams);
  102.  
  103.                 // send event to Entity and AI system
  104.  
  105.                 const char pEventName[] = "Destroy";
  106.                 const bool val = true;
  107.  
  108.                 SEntityEvent entityEvent;
  109.                 entityEvent.event = ENTITY_EVENT_SCRIPT_EVENT;
  110.                 entityEvent.nParam[0] = (INT_PTR) pEventName;
  111.                 entityEvent.nParam[1] = IEntityClass::EVT_BOOL;
  112.                 entityEvent.nParam[2] = (INT_PTR)&val;
  113.                 m_pVehicle->GetEntity()->SendEvent(entityEvent);
  114.  
  115.                 IAISystem* pAISystem = gEnv->pAISystem;
  116.                 if (pAISystem && pAISystem->IsEnabled() && pAISystem->GetSmartObjectManager())
  117.                 {
  118.                         gEnv->pAISystem->GetSmartObjectManager()->SetSmartObjectState(m_pVehicle->GetEntity(), "Dead");
  119.                 }
  120.  
  121.                 m_pVehicle->OnDestroyed();
  122.         }
  123. }
  124.  
  125. //------------------------------------------------------------------------
  126. void CVehicleDamageBehaviorDestroy::Serialize(TSerialize ser, EEntityAspects aspects)
  127. {
  128. }
  129.  
  130. DEFINE_VEHICLEOBJECT(CVehicleDamageBehaviorDestroy);
  131.  
downloadVehicleDamageBehaviorDestroy.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