BVB Source Codes

CRYENGINE Show VehicleSeatActionSound.cpp Source code

Return Download CRYENGINE: download VehicleSeatActionSound.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.    Description: Implements a seat action for sounds (ie: honk on trucks)
  8.  
  9.    -------------------------------------------------------------------------
  10.    History:
  11.    - 16:11:2005: Created by Mathieu Pinard
  12.  
  13. *************************************************************************/
  14. #include "StdAfx.h"
  15. #include "IVehicleSystem.h"
  16. #include "Vehicle.h"
  17. #include "VehicleSeat.h"
  18. #include "VehicleSeatActionSound.h"
  19.  
  20. CVehicleSeatActionSound::CVehicleSeatActionSound()
  21.         : m_pVehicle(nullptr)
  22.         , m_pHelper(nullptr)
  23.         , m_pSeat(nullptr)
  24.         , m_enabled(false)
  25.         , m_audioTriggerStartId(INVALID_AUDIO_CONTROL_ID)
  26.         , m_audioTriggerStopId(INVALID_AUDIO_CONTROL_ID)
  27. {
  28. }
  29.  
  30. //------------------------------------------------------------------------
  31. bool CVehicleSeatActionSound::Init(IVehicle* pVehicle, IVehicleSeat* pSeat, const CVehicleParams& table)
  32. {
  33.         m_pVehicle = pVehicle;
  34.         m_pSeat = static_cast<CVehicleSeat*>(pSeat);
  35.  
  36.         CVehicleParams soundTable = table.findChild("Audio");
  37.         if (!soundTable)
  38.                 return false;
  39.  
  40.         gEnv->pAudioSystem->GetAudioTriggerId(soundTable.getAttr("startTrigger"), m_audioTriggerStartId);
  41.         gEnv->pAudioSystem->GetAudioTriggerId(soundTable.getAttr("stopTrigger"), m_audioTriggerStopId);
  42.  
  43.         if (soundTable.haveAttr("helper"))
  44.                 m_pHelper = m_pVehicle->GetHelper(soundTable.getAttr("helper"));
  45.  
  46.         if (!m_pHelper)
  47.                 return false;
  48.  
  49.         m_enabled = false;
  50.         return true;
  51. }
  52.  
  53. //------------------------------------------------------------------------
  54. void CVehicleSeatActionSound::Serialize(TSerialize ser, EEntityAspects aspects)
  55. {
  56.         if (aspects & CVehicle::ASPECT_SEAT_ACTION)
  57.         {
  58.                 NET_PROFILE_SCOPE("SeatAction_Sound", ser.IsReading());
  59.  
  60.                 bool enabled = m_enabled;
  61.  
  62.                 ser.Value("enabled", enabled, 'bool');
  63.  
  64.                 if (ser.IsReading())
  65.                 {
  66.                         if (m_enabled != enabled)
  67.                         {
  68.                                 if (enabled)
  69.                                         ExecuteTrigger(m_audioTriggerStartId);
  70.                                 else
  71.                                         StopTrigger();
  72.  
  73.                                 m_enabled = enabled;
  74.                         }
  75.                 }
  76.         }
  77. }
  78.  
  79. //------------------------------------------------------------------------
  80. void CVehicleSeatActionSound::StopUsing()
  81. {
  82.         if (m_enabled)
  83.                 StopTrigger();
  84. }
  85.  
  86. //------------------------------------------------------------------------
  87. void CVehicleSeatActionSound::OnAction(const TVehicleActionId actionId, int activationMode, float value)
  88. {
  89.         if (actionId == eVAI_Horn && activationMode == eAAM_OnPress)
  90.         {
  91.                 ExecuteTrigger(m_audioTriggerStartId);
  92.         }
  93.         else if (actionId == eVAI_Horn && activationMode == eAAM_OnRelease)
  94.         {
  95.                 StopTrigger();
  96.         }
  97. }
  98.  
  99. void CVehicleSeatActionSound::GetMemoryUsage(ICrySizer* s) const
  100. {
  101.         s->AddObject(this, sizeof(*this));
  102. }
  103.  
  104. void CVehicleSeatActionSound::ExecuteTrigger(const AudioControlId& controlID)
  105. {
  106.         if (controlID == INVALID_AUDIO_CONTROL_ID)
  107.                 return;
  108.  
  109.         if (m_pSeat)
  110.                 m_pSeat->ChangedNetworkState(CVehicle::ASPECT_SEAT_ACTION);
  111.  
  112.         IEntityAudioComponent* pIEntityAudioComponent = m_pVehicle->GetEntity()->GetOrCreateComponent<IEntityAudioComponent>();
  113.         assert(pIEntityAudioComponent);
  114.  
  115.         pIEntityAudioComponent->ExecuteTrigger(controlID);
  116.  
  117.         // Report the AI system about the vehicle movement sound.
  118.         if (!gEnv->bMultiplayer && gEnv->pAISystem)
  119.         {
  120.                 Vec3 pos = m_pHelper->GetVehicleSpaceTranslation();
  121.                 SAIStimulus stim(AISTIM_SOUND, AISOUND_MOVEMENT_LOUD, m_pVehicle->GetEntityId(), 0, pos, ZERO, 200.0f);
  122.                 gEnv->pAISystem->RegisterStimulus(stim);
  123.         }
  124.  
  125.         m_enabled = true;
  126. }
  127.  
  128. void CVehicleSeatActionSound::StopTrigger()
  129. {
  130.         if (m_pSeat)
  131.                 m_pSeat->ChangedNetworkState(CVehicle::ASPECT_SEAT_ACTION);
  132.  
  133.         if (m_audioTriggerStopId != INVALID_AUDIO_CONTROL_ID)
  134.         {
  135.                 ExecuteTrigger(m_audioTriggerStopId);
  136.         }
  137.         else if (m_audioTriggerStartId != INVALID_AUDIO_CONTROL_ID)
  138.         {
  139.                 IEntityAudioComponent* pIEntityAudioComponent = m_pVehicle->GetEntity()->GetOrCreateComponent<IEntityAudioComponent>();
  140.                 assert(pIEntityAudioComponent);
  141.                 pIEntityAudioComponent->StopTrigger(m_audioTriggerStartId);
  142.         }
  143.  
  144.         m_enabled = false;
  145. }
  146.  
  147. DEFINE_VEHICLEOBJECT(CVehicleSeatActionSound);
  148.  
downloadVehicleSeatActionSound.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