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.

106 lines
2.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: 'Button' which activates after a specified amount of weight is touching it.
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. // NOTE: This has to be the last file included!
  8. #include "tier0/memdbgon.h"
  9. class CWeightButton : public CBaseEntity
  10. {
  11. public:
  12. DECLARE_DATADESC();
  13. DECLARE_CLASS( CWeightButton, CBaseEntity );
  14. void Spawn( void );
  15. bool CreateVPhysics( void );
  16. COutputEvent m_OnPressed; // After threshold weight has been reached
  17. COutputEvent m_OnReleased; // After weight has been removed to go below weight threshold
  18. float m_fStressToActivate; // Amount of weight required to activate
  19. bool m_bHasBeenPressed; // Once the button has been pressed, fire one
  20. // output until the weight is reduced below the threshold
  21. void TriggerThink ( void );
  22. };
  23. LINK_ENTITY_TO_CLASS( func_weight_button, CWeightButton );
  24. BEGIN_DATADESC( CWeightButton )
  25. DEFINE_KEYFIELD( m_fStressToActivate, FIELD_FLOAT, "WeightToActivate" ),
  26. DEFINE_FIELD( m_bHasBeenPressed, FIELD_BOOLEAN ),
  27. DEFINE_OUTPUT( m_OnPressed, "OnPressed" ),
  28. DEFINE_OUTPUT( m_OnReleased, "OnReleased" ),
  29. DEFINE_THINKFUNC( TriggerThink ),
  30. END_DATADESC()
  31. void CWeightButton::Spawn()
  32. {
  33. BaseClass::Spawn();
  34. // Convert movedir from angles to a vector
  35. SetMoveType( MOVETYPE_VPHYSICS );
  36. SetSolid( SOLID_VPHYSICS );
  37. SetModel( STRING( GetModelName() ) );
  38. CreateVPhysics();
  39. SetThink( &CWeightButton::TriggerThink );
  40. SetNextThink( gpGlobals->curtime + TICK_INTERVAL );
  41. m_bHasBeenPressed = false;
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Purpose: Create VPhysics collision for this entity
  45. //-----------------------------------------------------------------------------
  46. bool CWeightButton::CreateVPhysics()
  47. {
  48. VPhysicsInitShadow( false, false );
  49. return true;
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose: Every second, check total stress and fire an output if we have reached
  53. // our threshold. If the stress is relieved below our threshold, fire a different output.
  54. //-----------------------------------------------------------------------------
  55. void CWeightButton::TriggerThink( void )
  56. {
  57. vphysics_objectstress_t vpobj_StressOut;
  58. IPhysicsObject* pMyPhysics = VPhysicsGetObject();
  59. if ( !pMyPhysics )
  60. {
  61. SetNextThink( TICK_NEVER_THINK );
  62. return;
  63. }
  64. float fStress = CalculateObjectStress( pMyPhysics, this, &vpobj_StressOut );
  65. // fStress = vpobj_StressOut.receivedStress;
  66. if ( fStress > m_fStressToActivate && !m_bHasBeenPressed )
  67. {
  68. m_OnPressed.FireOutput( this, this );
  69. m_bHasBeenPressed = true;
  70. }
  71. else if ( fStress < m_fStressToActivate && m_bHasBeenPressed )
  72. {
  73. m_OnReleased.FireOutput( this, this );
  74. m_bHasBeenPressed = false;
  75. }
  76. // think every tick
  77. SetNextThink( gpGlobals->curtime + TICK_INTERVAL );
  78. }