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.

332 lines
7.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Volumetric dust motes.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "func_dust_shared.h"
  9. #include "te_particlesystem.h"
  10. #include "IEffects.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. class CFunc_Dust : public CBaseEntity
  14. {
  15. public:
  16. DECLARE_CLASS( CFunc_Dust, CBaseEntity );
  17. DECLARE_SERVERCLASS();
  18. DECLARE_DATADESC();
  19. CFunc_Dust();
  20. virtual ~CFunc_Dust();
  21. // CBaseEntity overrides.
  22. public:
  23. virtual void Spawn();
  24. virtual void Activate();
  25. virtual void Precache();
  26. virtual bool KeyValue( const char *szKeyName, const char *szValue );
  27. // Input handles.
  28. public:
  29. void InputTurnOn( inputdata_t &inputdata );
  30. void InputTurnOff( inputdata_t &inputdata );
  31. // FGD properties.
  32. public:
  33. CNetworkVar( color32, m_Color );
  34. CNetworkVar( int, m_SpawnRate );
  35. CNetworkVar( float, m_flSizeMin );
  36. CNetworkVar( float, m_flSizeMax );
  37. CNetworkVar( int, m_SpeedMax );
  38. CNetworkVar( int, m_LifetimeMin );
  39. CNetworkVar( int, m_LifetimeMax );
  40. CNetworkVar( int, m_DistMax );
  41. CNetworkVar( float, m_FallSpeed );
  42. public:
  43. CNetworkVar( int, m_DustFlags ); // Combination of DUSTFLAGS_
  44. private:
  45. int m_iAlpha;
  46. };
  47. class CFunc_DustMotes : public CFunc_Dust
  48. {
  49. DECLARE_CLASS( CFunc_DustMotes, CFunc_Dust );
  50. public:
  51. CFunc_DustMotes();
  52. };
  53. class CFunc_DustCloud : public CFunc_Dust
  54. {
  55. DECLARE_CLASS( CFunc_DustCloud, CFunc_Dust );
  56. public:
  57. };
  58. IMPLEMENT_SERVERCLASS_ST_NOBASE( CFunc_Dust, DT_Func_Dust )
  59. SendPropInt( SENDINFO(m_Color), 32, SPROP_UNSIGNED ),
  60. SendPropInt( SENDINFO(m_SpawnRate), 12, SPROP_UNSIGNED ),
  61. SendPropInt( SENDINFO(m_SpeedMax), 12, SPROP_UNSIGNED ),
  62. SendPropFloat( SENDINFO(m_flSizeMin), 0, SPROP_NOSCALE ),
  63. SendPropFloat( SENDINFO(m_flSizeMax), 0, SPROP_NOSCALE ),
  64. SendPropInt( SENDINFO(m_DistMax), 16, SPROP_UNSIGNED ),
  65. SendPropInt( SENDINFO(m_LifetimeMin), 4, SPROP_UNSIGNED ),
  66. SendPropInt( SENDINFO(m_LifetimeMax), 4, SPROP_UNSIGNED ),
  67. SendPropInt( SENDINFO(m_DustFlags), DUST_NUMFLAGS, SPROP_UNSIGNED ),
  68. SendPropModelIndex( SENDINFO(m_nModelIndex) ),
  69. SendPropFloat( SENDINFO(m_FallSpeed), 0, SPROP_NOSCALE ),
  70. SendPropDataTable( SENDINFO_DT( m_Collision ), &REFERENCE_SEND_TABLE(DT_CollisionProperty) ),
  71. END_SEND_TABLE()
  72. BEGIN_DATADESC( CFunc_Dust )
  73. DEFINE_FIELD( m_DustFlags,FIELD_INTEGER ),
  74. DEFINE_KEYFIELD( m_Color, FIELD_COLOR32, "Color" ),
  75. DEFINE_KEYFIELD( m_SpawnRate, FIELD_INTEGER, "SpawnRate" ),
  76. DEFINE_KEYFIELD( m_flSizeMin, FIELD_FLOAT, "SizeMin" ),
  77. DEFINE_KEYFIELD( m_flSizeMax, FIELD_FLOAT, "SizeMax" ),
  78. DEFINE_KEYFIELD( m_SpeedMax, FIELD_INTEGER, "SpeedMax" ),
  79. DEFINE_KEYFIELD( m_LifetimeMin, FIELD_INTEGER, "LifetimeMin" ),
  80. DEFINE_KEYFIELD( m_LifetimeMax, FIELD_INTEGER, "LifetimeMax" ),
  81. DEFINE_KEYFIELD( m_DistMax, FIELD_INTEGER, "DistMax" ),
  82. DEFINE_FIELD( m_iAlpha, FIELD_INTEGER ),
  83. DEFINE_KEYFIELD( m_FallSpeed, FIELD_FLOAT, "FallSpeed" ),
  84. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOn", InputTurnOn ),
  85. DEFINE_INPUTFUNC( FIELD_VOID, "TurnOff", InputTurnOff )
  86. END_DATADESC()
  87. LINK_ENTITY_TO_CLASS( func_dustmotes, CFunc_DustMotes );
  88. LINK_ENTITY_TO_CLASS( func_dustcloud, CFunc_DustCloud );
  89. // ------------------------------------------------------------------------------------- //
  90. // CFunc_DustMotes implementation.
  91. // ------------------------------------------------------------------------------------- //
  92. CFunc_DustMotes::CFunc_DustMotes()
  93. {
  94. m_DustFlags |= DUSTFLAGS_SCALEMOTES;
  95. }
  96. // ------------------------------------------------------------------------------------- //
  97. // CFunc_Dust implementation.
  98. // ------------------------------------------------------------------------------------- //
  99. CFunc_Dust::CFunc_Dust()
  100. {
  101. m_DustFlags = DUSTFLAGS_ON;
  102. m_FallSpeed = 0.0f;
  103. }
  104. CFunc_Dust::~CFunc_Dust()
  105. {
  106. }
  107. void CFunc_Dust::Spawn()
  108. {
  109. Precache();
  110. // Bind to our bmodel.
  111. SetModel( STRING( GetModelName() ) );
  112. //AddSolidFlags( FSOLID_NOT_SOLID );
  113. AddSolidFlags( FSOLID_VOLUME_CONTENTS );
  114. //Since keyvalues can arrive in any order, and UTIL_StringToColor32 stomps alpha,
  115. //install the alpha value here.
  116. color32 clr = { m_Color.m_Value.r, m_Color.m_Value.g, m_Color.m_Value.b, (byte)m_iAlpha };
  117. m_Color.Set( clr );
  118. BaseClass::Spawn();
  119. }
  120. void CFunc_Dust::Precache()
  121. {
  122. PrecacheMaterial( "particle/sparkles" );
  123. }
  124. void CFunc_Dust::Activate()
  125. {
  126. BaseClass::Activate();
  127. }
  128. bool CFunc_Dust::KeyValue( const char *szKeyName, const char *szValue )
  129. {
  130. if( stricmp( szKeyName, "StartDisabled" ) == 0 )
  131. {
  132. if( szValue[0] == '1' )
  133. m_DustFlags &= ~DUSTFLAGS_ON;
  134. else
  135. m_DustFlags |= DUSTFLAGS_ON;
  136. return true;
  137. }
  138. else if( stricmp( szKeyName, "Alpha" ) == 0 )
  139. {
  140. m_iAlpha = atoi( szValue );
  141. return true;
  142. }
  143. else if( stricmp( szKeyName, "Frozen" ) == 0 )
  144. {
  145. if( szValue[0] == '1' )
  146. m_DustFlags |= DUSTFLAGS_FROZEN;
  147. else
  148. m_DustFlags &= ~DUSTFLAGS_FROZEN;
  149. return true;
  150. }
  151. else
  152. {
  153. return BaseClass::KeyValue( szKeyName, szValue );
  154. }
  155. }
  156. void CFunc_Dust::InputTurnOn( inputdata_t &inputdata )
  157. {
  158. if( !(m_DustFlags & DUSTFLAGS_ON) )
  159. {
  160. m_DustFlags |= DUSTFLAGS_ON;
  161. }
  162. }
  163. void CFunc_Dust::InputTurnOff( inputdata_t &inputdata )
  164. {
  165. if( m_DustFlags & DUSTFLAGS_ON )
  166. {
  167. m_DustFlags &= ~DUSTFLAGS_ON;
  168. }
  169. }
  170. //
  171. // Dust
  172. //
  173. class CTEDust : public CTEParticleSystem
  174. {
  175. public:
  176. DECLARE_CLASS( CTEDust, CTEParticleSystem );
  177. DECLARE_SERVERCLASS();
  178. CTEDust( const char *name );
  179. virtual ~CTEDust( void );
  180. virtual void Test( const Vector& current_origin, const QAngle& current_angles ) { };
  181. CNetworkVar( float, m_flSize );
  182. CNetworkVar( float, m_flSpeed );
  183. CNetworkVector( m_vecDirection );
  184. };
  185. CTEDust::CTEDust( const char *name ) : BaseClass( name )
  186. {
  187. m_flSize = 1.0f;
  188. m_flSpeed = 1.0f;
  189. m_vecDirection.Init();
  190. }
  191. CTEDust::~CTEDust( void )
  192. {
  193. }
  194. IMPLEMENT_SERVERCLASS_ST( CTEDust, DT_TEDust )
  195. SendPropFloat( SENDINFO(m_flSize), -1, SPROP_COORD ),
  196. SendPropFloat( SENDINFO(m_flSpeed), -1, SPROP_COORD ),
  197. SendPropVector( SENDINFO(m_vecDirection), 4, 0, -1.0f, 1.0f ), // cheap normal
  198. END_SEND_TABLE()
  199. static CTEDust g_TEDust( "Dust" );
  200. //-----------------------------------------------------------------------------
  201. // Purpose:
  202. // Input : &pos -
  203. // &angles -
  204. //-----------------------------------------------------------------------------
  205. void TE_Dust( IRecipientFilter& filter, float delay,
  206. const Vector &pos, const Vector &dir, float size, float speed )
  207. {
  208. g_TEDust.m_vecOrigin = pos;
  209. g_TEDust.m_vecDirection = dir;
  210. g_TEDust.m_flSize = size;
  211. g_TEDust.m_flSpeed = speed;
  212. Assert( dir.Length() < 1.01 ); // make sure it's a normal
  213. //Send it
  214. g_TEDust.Create( filter, delay );
  215. }
  216. class CEnvDustPuff : public CPointEntity
  217. {
  218. DECLARE_CLASS( CEnvDustPuff, CPointEntity );
  219. public:
  220. DECLARE_DATADESC();
  221. protected:
  222. // Input handlers
  223. void InputSpawnDust( inputdata_t &inputdata );
  224. float m_flScale;
  225. color32 m_rgbaColor;
  226. };
  227. LINK_ENTITY_TO_CLASS( env_dustpuff, CEnvDustPuff );
  228. BEGIN_DATADESC( CEnvDustPuff )
  229. DEFINE_KEYFIELD( m_flScale, FIELD_FLOAT, "scale" ),
  230. DEFINE_KEYFIELD( m_rgbaColor, FIELD_COLOR32, "color" ),
  231. // Function Pointers
  232. DEFINE_INPUTFUNC( FIELD_VOID, "SpawnDust", InputSpawnDust ),
  233. END_DATADESC()
  234. //-----------------------------------------------------------------------------
  235. // Purpose:
  236. // Input : &inputdata -
  237. //-----------------------------------------------------------------------------
  238. void CEnvDustPuff::InputSpawnDust( inputdata_t &inputdata )
  239. {
  240. Vector dir;
  241. AngleVectors( GetAbsAngles(), &dir );
  242. VectorNormalize( dir );
  243. g_pEffects->Dust( GetAbsOrigin(), dir, m_flScale, m_flSpeed );
  244. }