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.

128 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Simple model entity that randomly moves and changes direction
  4. // when activated.
  5. //
  6. //=============================================================================//
  7. #include "cbase.h"
  8. class CMyModelEntity : public CBaseAnimating
  9. {
  10. public:
  11. DECLARE_CLASS( CMyModelEntity, CBaseAnimating );
  12. DECLARE_DATADESC();
  13. void Spawn( void );
  14. void Precache( void );
  15. void MoveThink( void );
  16. // Input function
  17. void InputToggle( inputdata_t &inputData );
  18. private:
  19. bool m_bActive;
  20. float m_flNextChangeTime;
  21. };
  22. LINK_ENTITY_TO_CLASS( my_model_entity, CMyModelEntity );
  23. // Start of our data description for the class
  24. BEGIN_DATADESC( CMyModelEntity )
  25. // Save/restore our active state
  26. DEFINE_FIELD( m_bActive, FIELD_BOOLEAN ),
  27. DEFINE_FIELD( m_flNextChangeTime, FIELD_TIME ),
  28. // Links our input name from Hammer to our input member function
  29. DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
  30. // Declare our think function
  31. DEFINE_THINKFUNC( MoveThink ),
  32. END_DATADESC()
  33. // Name of our entity's model
  34. #define ENTITY_MODEL "models/gibs/airboat_broken_engine.mdl"
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Precache assets used by the entity
  37. //-----------------------------------------------------------------------------
  38. void CMyModelEntity::Precache( void )
  39. {
  40. PrecacheModel( ENTITY_MODEL );
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Sets up the entity's initial state
  44. //-----------------------------------------------------------------------------
  45. void CMyModelEntity::Spawn( void )
  46. {
  47. Precache();
  48. SetModel( ENTITY_MODEL );
  49. SetSolid( SOLID_BBOX );
  50. UTIL_SetSize( this, -Vector(20,20,20), Vector(20,20,20) );
  51. m_bActive = false;
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Think function to randomly move the entity
  55. //-----------------------------------------------------------------------------
  56. void CMyModelEntity::MoveThink( void )
  57. {
  58. // See if we should change direction again
  59. if ( m_flNextChangeTime < gpGlobals->curtime )
  60. {
  61. // Randomly take a new direction and speed
  62. Vector vecNewVelocity = RandomVector( -64.0f, 64.0f );
  63. SetAbsVelocity( vecNewVelocity );
  64. // Randomly change it again within one to three seconds
  65. m_flNextChangeTime = gpGlobals->curtime + random->RandomFloat( 1.0f, 3.0f );
  66. }
  67. // Snap our facing to where we're heading
  68. Vector velFacing = GetAbsVelocity();
  69. QAngle angFacing;
  70. VectorAngles( velFacing, angFacing );
  71. SetAbsAngles( angFacing );
  72. // Think every 20Hz
  73. SetNextThink( gpGlobals->curtime + 0.05f );
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose: Toggle the movement of the entity
  77. //-----------------------------------------------------------------------------
  78. void CMyModelEntity::InputToggle( inputdata_t &inputData )
  79. {
  80. // Toggle our active state
  81. if ( !m_bActive )
  82. {
  83. // Start thinking
  84. SetThink( &CMyModelEntity::MoveThink );
  85. SetNextThink( gpGlobals->curtime + 0.05f );
  86. // Start flying
  87. SetMoveType( MOVETYPE_FLY );
  88. // Set our next time for changing our speed and direction
  89. m_flNextChangeTime = gpGlobals->curtime;
  90. m_bActive = true;
  91. }
  92. else
  93. {
  94. // Stop thinking
  95. SetThink( NULL );
  96. // Stop moving
  97. SetAbsVelocity( vec3_origin );
  98. SetMoveType( MOVETYPE_NONE );
  99. m_bActive = false;
  100. }
  101. }