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.

178 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Spawn and use functions for editor-placed triggers.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "triggers.h"
  8. #include "soundenvelope.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. ConVar sk_max_super_armor( "sk_max_super_armor","500");
  12. #define MAX_SUPER_ARMOR sk_max_super_armor.GetInt()
  13. //-----------------------------------------------------------------------------
  14. // Trigger that bestows super armor
  15. //-----------------------------------------------------------------------------
  16. class CTriggerSuperArmor : public CTriggerMultiple
  17. {
  18. DECLARE_CLASS( CTriggerSuperArmor, CTriggerMultiple );
  19. DECLARE_DATADESC();
  20. public:
  21. virtual void StartTouch( CBaseEntity *pOther );
  22. virtual void EndTouch( CBaseEntity *pOther );
  23. private:
  24. virtual void Precache();
  25. virtual void Spawn( void );
  26. void StartLoopingSounds( CBaseEntity *pEntity );
  27. void StopLoopingSounds();
  28. void RechargeThink();
  29. CSoundPatch *m_pChargingSound;
  30. float m_flLoopingSoundTime;
  31. };
  32. LINK_ENTITY_TO_CLASS( trigger_super_armor, CTriggerSuperArmor );
  33. BEGIN_DATADESC( CTriggerSuperArmor )
  34. DEFINE_SOUNDPATCH( m_pChargingSound ),
  35. DEFINE_FIELD( m_flLoopingSoundTime, FIELD_TIME ),
  36. DEFINE_THINKFUNC( RechargeThink ),
  37. END_DATADESC()
  38. static const char *s_pRechargeThinkContext = "RechargeThink";
  39. void CTriggerSuperAmmor::Precache()
  40. {
  41. BaseClass::Precache();
  42. PrecacheScriptSound( "TriggerSuperArmor.StartCharging" );
  43. PrecacheScriptSound( "TriggerSuperArmor.DoneCharging" );
  44. PrecacheScriptSound( "TriggerSuperArmor.Charging" );
  45. }
  46. //-----------------------------------------------------------------------------
  47. // No retrigger
  48. //-----------------------------------------------------------------------------
  49. void CTriggerSuperArmor::Spawn( void )
  50. {
  51. Precache();
  52. BaseClass::Spawn();
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Starts looping sounds
  56. //-----------------------------------------------------------------------------
  57. void CTriggerSuperArmor::StartLoopingSounds( CBaseEntity *pEntity )
  58. {
  59. if ( m_pChargingSound )
  60. return;
  61. CReliableBroadcastRecipientFilter filter;
  62. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  63. m_pChargingSound = controller.SoundCreate( filter, pEntity->entindex(), "TriggerSuperArmor.Charging" );
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Stops looping sounds
  67. //-----------------------------------------------------------------------------
  68. void CTriggerSuperArmor::StopLoopingSounds()
  69. {
  70. if ( m_pChargingSound )
  71. {
  72. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  73. controller.SoundDestroy( m_pChargingSound );
  74. m_pChargingSound = NULL;
  75. }
  76. BaseClass::StopLoopingSounds();
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Begins super-powering the entities
  80. //-----------------------------------------------------------------------------
  81. void CTriggerSuperArmor::StartTouch( CBaseEntity *pOther )
  82. {
  83. BaseClass::StartTouch( pOther );
  84. if ( !pOther->IsPlayer() )
  85. return;
  86. if ( m_hTouchingEntities.Count() == 1 )
  87. {
  88. SetContextThink( RechargeThink, gpGlobals->curtime + 0.01f, s_pRechargeThinkContext );
  89. pOther->EmitSound( "TriggerSuperArmor.StartCharging" );
  90. m_flLoopingSoundTime = 0.56f + gpGlobals->curtime;
  91. }
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Ends super-powerings the entities
  95. //-----------------------------------------------------------------------------
  96. void CTriggerSuperArmor::EndTouch( CBaseEntity *pOther )
  97. {
  98. BaseClass::EndTouch( pOther );
  99. if ( !pOther->IsPlayer() )
  100. return;
  101. if ( m_hTouchingEntities.Count() == 0 )
  102. {
  103. StopLoopingSounds();
  104. SetContextThink( NULL, gpGlobals->curtime + 0.01f, s_pRechargeThinkContext );
  105. }
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Super-powers the entities
  109. //-----------------------------------------------------------------------------
  110. void CTriggerSuperArmor::RechargeThink()
  111. {
  112. Assert( m_hTouchingEntities.Count() == 1 );
  113. for ( int i = m_hTouchingEntities.Count(); --i >= 0; )
  114. {
  115. CBasePlayer *pPlayer = assert_cast<CBasePlayer*>( m_hTouchingEntities[i].Get() );
  116. if (( pPlayer->ArmorValue() < MAX_SUPER_ARMOR ) || ( pPlayer->GetHealth() < 100 ))
  117. {
  118. pPlayer->TakeHealth( 5, DMG_GENERIC );
  119. pPlayer->IncrementArmorValue( 15, MAX_SUPER_ARMOR );
  120. if ( pPlayer->ArmorValue() >= MAX_SUPER_ARMOR )
  121. {
  122. pPlayer->EmitSound( "TriggerSuperArmor.DoneCharging" );
  123. StopLoopingSounds();
  124. }
  125. else
  126. {
  127. if ( m_flLoopingSoundTime < gpGlobals->curtime )
  128. {
  129. StartLoopingSounds( m_hTouchingEntities[i] );
  130. }
  131. }
  132. }
  133. }
  134. SetContextThink( RechargeThink, gpGlobals->curtime + 0.1f, s_pRechargeThinkContext );
  135. }