Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "c_ai_basenpc.h"
  9. #include "soundenvelope.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. class C_NPC_Manhack : public C_AI_BaseNPC
  13. {
  14. public:
  15. C_NPC_Manhack() {}
  16. DECLARE_CLASS( C_NPC_Manhack, C_AI_BaseNPC );
  17. DECLARE_CLIENTCLASS();
  18. DECLARE_DATADESC();
  19. // Purpose: Start the manhack's engine sound.
  20. virtual void OnDataChanged( DataUpdateType_t type );
  21. virtual void UpdateOnRemove( void );
  22. virtual void OnRestore();
  23. private:
  24. C_NPC_Manhack( const C_NPC_Manhack & );
  25. // Purpose: Start + stop the manhack's engine sound.
  26. void SoundInit( void );
  27. void SoundShutdown( void );
  28. CSoundPatch *m_pEngineSound1;
  29. CSoundPatch *m_pEngineSound2;
  30. CSoundPatch *m_pBladeSound;
  31. int m_nEnginePitch1;
  32. int m_nEnginePitch2;
  33. float m_flEnginePitch1Time;
  34. float m_flEnginePitch2Time;
  35. };
  36. //-----------------------------------------------------------------------------
  37. // Save/restore
  38. //-----------------------------------------------------------------------------
  39. BEGIN_DATADESC( C_NPC_Manhack )
  40. // DEFINE_SOUNDPATCH( m_pEngineSound1 ),
  41. // DEFINE_SOUNDPATCH( m_pEngineSound2 ),
  42. // DEFINE_SOUNDPATCH( m_pBladeSound ),
  43. // DEFINE_FIELD( m_nEnginePitch1, FIELD_INTEGER ),
  44. // DEFINE_FIELD( m_nEnginePitch2, FIELD_INTEGER ),
  45. // DEFINE_FIELD( m_flEnginePitch1Time, FIELD_FLOAT ),
  46. // DEFINE_FIELD( m_flEnginePitch2Time, FIELD_FLOAT ),
  47. END_DATADESC()
  48. //-----------------------------------------------------------------------------
  49. // Networking
  50. //-----------------------------------------------------------------------------
  51. IMPLEMENT_CLIENTCLASS_DT(C_NPC_Manhack, DT_NPC_Manhack, CNPC_Manhack)
  52. RecvPropIntWithMinusOneFlag(RECVINFO(m_nEnginePitch1)),
  53. RecvPropFloat(RECVINFO(m_flEnginePitch1Time)),
  54. RecvPropIntWithMinusOneFlag(RECVINFO(m_nEnginePitch2)),
  55. RecvPropFloat(RECVINFO(m_flEnginePitch2Time)),
  56. END_RECV_TABLE()
  57. //-----------------------------------------------------------------------------
  58. // Purpose: Start the manhack's engine sound.
  59. //-----------------------------------------------------------------------------
  60. void C_NPC_Manhack::OnDataChanged( DataUpdateType_t type )
  61. {
  62. BaseClass::OnDataChanged( type );
  63. if (( m_nEnginePitch1 < 0 ) || ( m_nEnginePitch2 < 0 ) )
  64. {
  65. SoundShutdown();
  66. }
  67. else
  68. {
  69. SoundInit();
  70. if ( m_pEngineSound1 && m_pEngineSound2 )
  71. {
  72. float dt = ( m_flEnginePitch1Time >= gpGlobals->curtime ) ? m_flEnginePitch1Time - gpGlobals->curtime : 0.0f;
  73. CSoundEnvelopeController::GetController().SoundChangePitch( m_pEngineSound1, m_nEnginePitch1, dt );
  74. dt = ( m_flEnginePitch2Time >= gpGlobals->curtime ) ? m_flEnginePitch2Time - gpGlobals->curtime : 0.0f;
  75. CSoundEnvelopeController::GetController().SoundChangePitch( m_pEngineSound2, m_nEnginePitch2, dt );
  76. }
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Restore
  81. //-----------------------------------------------------------------------------
  82. void C_NPC_Manhack::OnRestore()
  83. {
  84. BaseClass::OnRestore();
  85. SoundInit();
  86. }
  87. //-----------------------------------------------------------------------------
  88. // Purpose: Start the manhack's engine sound.
  89. //-----------------------------------------------------------------------------
  90. void C_NPC_Manhack::UpdateOnRemove( void )
  91. {
  92. BaseClass::UpdateOnRemove();
  93. SoundShutdown();
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose: Start the manhack's engine sound.
  97. //-----------------------------------------------------------------------------
  98. void C_NPC_Manhack::SoundInit( void )
  99. {
  100. if (( m_nEnginePitch1 < 0 ) || ( m_nEnginePitch2 < 0 ) )
  101. return;
  102. // play an engine start sound!!
  103. CPASAttenuationFilter filter( this );
  104. // Bring up the engine looping sound.
  105. if( !m_pEngineSound1 )
  106. {
  107. m_pEngineSound1 = CSoundEnvelopeController::GetController().SoundCreate( filter, entindex(), "NPC_Manhack.EngineSound1" );
  108. CSoundEnvelopeController::GetController().Play( m_pEngineSound1, 0.0, m_nEnginePitch1 );
  109. CSoundEnvelopeController::GetController().SoundChangeVolume( m_pEngineSound1, 0.7, 2.0 );
  110. }
  111. if( !m_pEngineSound2 )
  112. {
  113. m_pEngineSound2 = CSoundEnvelopeController::GetController().SoundCreate( filter, entindex(), "NPC_Manhack.EngineSound2" );
  114. CSoundEnvelopeController::GetController().Play( m_pEngineSound2, 0.0, m_nEnginePitch2 );
  115. CSoundEnvelopeController::GetController().SoundChangeVolume( m_pEngineSound2, 0.7, 2.0 );
  116. }
  117. if( !m_pBladeSound )
  118. {
  119. m_pBladeSound = CSoundEnvelopeController::GetController().SoundCreate( filter, entindex(), "NPC_Manhack.BladeSound" );
  120. CSoundEnvelopeController::GetController().Play( m_pBladeSound, 0.0, m_nEnginePitch1 );
  121. CSoundEnvelopeController::GetController().SoundChangeVolume( m_pBladeSound, 0.7, 2.0 );
  122. }
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose:
  126. //-----------------------------------------------------------------------------
  127. void C_NPC_Manhack::SoundShutdown(void)
  128. {
  129. // Kill the engine!
  130. if ( m_pEngineSound1 )
  131. {
  132. CSoundEnvelopeController::GetController().SoundDestroy( m_pEngineSound1 );
  133. m_pEngineSound1 = NULL;
  134. }
  135. // Kill the engine!
  136. if ( m_pEngineSound2 )
  137. {
  138. CSoundEnvelopeController::GetController().SoundDestroy( m_pEngineSound2 );
  139. m_pEngineSound2 = NULL;
  140. }
  141. // Kill the blade!
  142. if ( m_pBladeSound )
  143. {
  144. CSoundEnvelopeController::GetController().SoundDestroy( m_pBladeSound );
  145. m_pBladeSound = NULL;
  146. }
  147. }