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.

253 lines
5.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements a moving target that moves along a path of path_tracks
  4. // and can be shot and killed. When the target it killed it fires an
  5. // OnDeath output.
  6. //
  7. // m_flSpeed is the travel speed
  8. // m_iHealth is current health
  9. // m_iMaxHealth is the amount to reset to each time it starts
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #include "cbase.h"
  14. #include "entityoutput.h"
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. #define FGUNTARGET_START_ON 0x0001
  18. class CGunTarget : public CBaseToggle
  19. {
  20. DECLARE_CLASS( CGunTarget, CBaseToggle );
  21. public:
  22. virtual void Spawn( void );
  23. virtual void Activate( void );
  24. bool CreateVPhysics( void );
  25. virtual int BloodColor( void ) { return DONT_BLEED; }
  26. #if defined( HL2_DLL )
  27. virtual Class_T Classify( void ) { return CLASS_MILITARY; }
  28. #else
  29. virtual Class_T Classify( void ) { return CLASS_NONE; }
  30. #endif
  31. virtual int OnTakeDamage( const CTakeDamageInfo &info );
  32. virtual Vector BodyTarget( const Vector &posSrc, bool bNoisy = true ) { return GetAbsOrigin(); }
  33. // Input handlers
  34. void InputStart( inputdata_t &inputdata );
  35. void InputStop( inputdata_t &inputdata );
  36. void InputToggle( inputdata_t &inputdata );
  37. DECLARE_DATADESC();
  38. protected:
  39. void Next( void );
  40. void Start( void );
  41. void Wait( void );
  42. void Stop( void );
  43. private:
  44. bool m_on;
  45. EHANDLE m_hTargetEnt;
  46. // Outputs
  47. COutputEvent m_OnDeath;
  48. };
  49. LINK_ENTITY_TO_CLASS( func_guntarget, CGunTarget );
  50. BEGIN_DATADESC( CGunTarget )
  51. DEFINE_FIELD( m_on, FIELD_BOOLEAN ),
  52. DEFINE_FIELD( m_hTargetEnt, FIELD_EHANDLE ),
  53. // Function Pointers
  54. DEFINE_FUNCTION( Next ),
  55. DEFINE_FUNCTION( Start ),
  56. DEFINE_FUNCTION( Wait ),
  57. // Inputs
  58. DEFINE_INPUTFUNC( FIELD_VOID, "Start", InputStart ),
  59. DEFINE_INPUTFUNC( FIELD_VOID, "Stop", InputStop ),
  60. DEFINE_INPUTFUNC( FIELD_VOID, "Toggle", InputToggle ),
  61. // Outputs
  62. DEFINE_OUTPUT(m_OnDeath, "OnDeath"),
  63. END_DATADESC()
  64. void CGunTarget::Spawn( void )
  65. {
  66. SetSolid( SOLID_BSP );
  67. SetMoveType( MOVETYPE_PUSH );
  68. SetModel( STRING( GetModelName() ) );
  69. if ( m_flSpeed == 0 )
  70. m_flSpeed = 100;
  71. // Don't take damage until "on"
  72. m_takedamage = DAMAGE_NO;
  73. AddFlag( FL_NPC );
  74. m_on = false;
  75. m_iMaxHealth = m_iHealth;
  76. if ( HasSpawnFlags(FGUNTARGET_START_ON) )
  77. {
  78. SetMoveDone( &CGunTarget::Start );
  79. SetMoveDoneTime( 0.3 );
  80. }
  81. CreateVPhysics();
  82. }
  83. bool CGunTarget::CreateVPhysics( void )
  84. {
  85. VPhysicsInitShadow( false, false );
  86. return true;
  87. }
  88. void CGunTarget::Activate( void )
  89. {
  90. BaseClass::Activate();
  91. CBaseEntity *pTarg;
  92. // now find our next target
  93. pTarg = GetNextTarget();
  94. if ( pTarg )
  95. {
  96. m_hTargetEnt = pTarg;
  97. Vector nextPos = pTarg->GetAbsOrigin();
  98. Teleport( &nextPos, NULL, NULL );
  99. }
  100. }
  101. void CGunTarget::Start( void )
  102. {
  103. m_takedamage = DAMAGE_YES;
  104. AddFlag( FL_AIMTARGET );
  105. m_hTargetEnt = GetNextTarget();
  106. if ( m_hTargetEnt == NULL )
  107. return;
  108. m_iHealth = m_iMaxHealth;
  109. Next();
  110. }
  111. void CGunTarget::Next( void )
  112. {
  113. SetThink( NULL );
  114. m_hTargetEnt = GetNextTarget();
  115. CBaseEntity *pTarget = m_hTargetEnt;
  116. if ( !pTarget )
  117. {
  118. Stop();
  119. return;
  120. }
  121. SetMoveDone( &CGunTarget::Wait );
  122. LinearMove( pTarget->GetLocalOrigin(), m_flSpeed );
  123. }
  124. void CGunTarget::Wait( void )
  125. {
  126. CBaseEntity *pTarget = m_hTargetEnt;
  127. if ( !pTarget )
  128. {
  129. Stop();
  130. return;
  131. }
  132. variant_t emptyVariant;
  133. pTarget->AcceptInput( "InPass", this, this, emptyVariant, 0 );
  134. m_flWait = pTarget->GetDelay();
  135. m_target = pTarget->m_target;
  136. SetMoveDone( &CGunTarget::Next );
  137. if (m_flWait != 0)
  138. {// -1 wait will wait forever!
  139. SetMoveDoneTime( m_flWait );
  140. }
  141. else
  142. {
  143. Next();// do it RIGHT now!
  144. }
  145. }
  146. void CGunTarget::Stop( void )
  147. {
  148. SetAbsVelocity( vec3_origin );
  149. SetMoveDoneTime( -1 );
  150. m_takedamage = DAMAGE_NO;
  151. }
  152. int CGunTarget::OnTakeDamage( const CTakeDamageInfo &info )
  153. {
  154. if ( m_iHealth > 0 )
  155. {
  156. m_iHealth -= info.GetDamage();
  157. if ( m_iHealth <= 0 )
  158. {
  159. m_iHealth = 0;
  160. Stop();
  161. m_OnDeath.FireOutput( info.GetInflictor(), this );
  162. }
  163. }
  164. return 0;
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose: Input handler that starts the target moving.
  168. //-----------------------------------------------------------------------------
  169. void CGunTarget::InputStart( inputdata_t &inputdata )
  170. {
  171. Start();
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Purpose: Input handler that stops the target from moving.
  175. //-----------------------------------------------------------------------------
  176. void CGunTarget::InputStop( inputdata_t &inputdata )
  177. {
  178. Stop();
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose: Input handler that toggles the start/stop state of the target.
  182. //-----------------------------------------------------------------------------
  183. void CGunTarget::InputToggle( inputdata_t &inputdata )
  184. {
  185. if ( m_on )
  186. {
  187. Stop();
  188. }
  189. else
  190. {
  191. Start();
  192. }
  193. }