Counter Strike : Global Offensive Source Code
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.

135 lines
4.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "decals.h"
  8. #include "env_player_surface_trigger.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. LINK_ENTITY_TO_CLASS( env_player_surface_trigger, CEnvPlayerSurfaceTrigger );
  12. BEGIN_DATADESC( CEnvPlayerSurfaceTrigger )
  13. DEFINE_KEYFIELD( m_iTargetGameMaterial, FIELD_INTEGER, "gamematerial" ),
  14. DEFINE_FIELD( m_iCurrentGameMaterial, FIELD_INTEGER ),
  15. DEFINE_FIELD( m_bDisabled, FIELD_BOOLEAN ),
  16. DEFINE_THINKFUNC( UpdateMaterialThink ),
  17. // Inputs
  18. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  19. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  20. // Outputs
  21. DEFINE_OUTPUT(m_OnSurfaceChangedToTarget, "OnSurfaceChangedToTarget"),
  22. DEFINE_OUTPUT(m_OnSurfaceChangedFromTarget, "OnSurfaceChangedFromTarget"),
  23. END_DATADESC()
  24. // Global list of surface triggers
  25. CUtlVector< CHandle<CEnvPlayerSurfaceTrigger> > g_PlayerSurfaceTriggers;
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. CEnvPlayerSurfaceTrigger::~CEnvPlayerSurfaceTrigger( void )
  30. {
  31. g_PlayerSurfaceTriggers.FindAndRemove( this );
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose:
  35. //-----------------------------------------------------------------------------
  36. void CEnvPlayerSurfaceTrigger::Spawn( void )
  37. {
  38. SetSolid( SOLID_NONE );
  39. SetMoveType( MOVETYPE_NONE );
  40. m_iCurrentGameMaterial = 0;
  41. m_bDisabled = false;
  42. g_PlayerSurfaceTriggers.AddToTail( this );
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Purpose:
  46. //-----------------------------------------------------------------------------
  47. void CEnvPlayerSurfaceTrigger::OnRestore( void )
  48. {
  49. BaseClass::OnRestore();
  50. g_PlayerSurfaceTriggers.AddToTail( this );
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose:
  54. //-----------------------------------------------------------------------------
  55. void CEnvPlayerSurfaceTrigger::SetPlayerSurface( CBasePlayer *pPlayer, char gameMaterial )
  56. {
  57. // Ignore players in the air (stops bunny hoppers escaping triggers)
  58. if ( gameMaterial == 0 )
  59. return;
  60. // Loop through the surface triggers and tell them all about the change
  61. int iCount = g_PlayerSurfaceTriggers.Count();
  62. for ( int i = 0; i < iCount; i++ )
  63. {
  64. g_PlayerSurfaceTriggers[i]->PlayerSurfaceChanged( pPlayer, gameMaterial );
  65. }
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose:
  69. //-----------------------------------------------------------------------------
  70. void CEnvPlayerSurfaceTrigger::PlayerSurfaceChanged( CBasePlayer *pPlayer, char gameMaterial )
  71. {
  72. if ( m_bDisabled )
  73. return;
  74. // Fire the output if we've changed, but only if it involves the target material
  75. if ( gameMaterial != (char)m_iCurrentGameMaterial &&
  76. ( gameMaterial == m_iTargetGameMaterial || m_iCurrentGameMaterial == m_iTargetGameMaterial ) )
  77. {
  78. DevMsg( 2, "Player changed material to %d (was %d)\n", gameMaterial, m_iCurrentGameMaterial );
  79. m_iCurrentGameMaterial = (int)gameMaterial;
  80. SetThink( &CEnvPlayerSurfaceTrigger::UpdateMaterialThink );
  81. SetNextThink( gpGlobals->curtime );
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Think function to fire outputs. Done this way so that sv_alternate ticks
  86. // doesn't allow multiple surface changes in the same tick to fire outputs.
  87. //-----------------------------------------------------------------------------
  88. void CEnvPlayerSurfaceTrigger::UpdateMaterialThink( void )
  89. {
  90. if ( m_iCurrentGameMaterial == m_iTargetGameMaterial )
  91. {
  92. m_OnSurfaceChangedToTarget.FireOutput( NULL, this );
  93. }
  94. else
  95. {
  96. m_OnSurfaceChangedFromTarget.FireOutput( NULL, this );
  97. }
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Purpose:
  101. //-----------------------------------------------------------------------------
  102. void CEnvPlayerSurfaceTrigger::InputDisable( inputdata_t &inputdata )
  103. {
  104. m_bDisabled = true;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose:
  108. //-----------------------------------------------------------------------------
  109. void CEnvPlayerSurfaceTrigger::InputEnable( inputdata_t &inputdata )
  110. {
  111. m_bDisabled = false;
  112. }