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.

102 lines
2.8 KiB

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