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.

483 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "particles_simple.h"
  8. #include "citadel_effects_shared.h"
  9. #include "particles_attractor.h"
  10. #include "fx_quad.h"
  11. class C_CitadelEnergyCore : public C_BaseEntity
  12. {
  13. DECLARE_CLASS( C_CitadelEnergyCore, C_BaseEntity );
  14. DECLARE_CLIENTCLASS();
  15. public:
  16. void OnDataChanged( DataUpdateType_t updateType );
  17. RenderGroup_t GetRenderGroup( void );
  18. void ClientThink( void );
  19. void NotifyShouldTransmit( ShouldTransmitState_t state );
  20. void UpdateIdle( float percentage );
  21. void UpdateCharging( float percentage );
  22. void UpdateDischarging( void );
  23. private:
  24. bool SetupEmitters( void );
  25. inline float GetStateDurationPercentage( void );
  26. float m_flScale;
  27. int m_nState;
  28. float m_flDuration;
  29. float m_flStartTime;
  30. int m_spawnflags;
  31. CSmartPtr<CSimpleEmitter> m_pSimpleEmitter;
  32. CSmartPtr<CParticleAttractor> m_pAttractorEmitter;
  33. };
  34. IMPLEMENT_CLIENTCLASS_DT( C_CitadelEnergyCore, DT_CitadelEnergyCore, CCitadelEnergyCore )
  35. RecvPropFloat( RECVINFO(m_flScale) ),
  36. RecvPropInt( RECVINFO(m_nState) ),
  37. RecvPropFloat( RECVINFO(m_flDuration) ),
  38. RecvPropFloat( RECVINFO(m_flStartTime) ),
  39. RecvPropInt( RECVINFO(m_spawnflags) ),
  40. END_RECV_TABLE()
  41. //-----------------------------------------------------------------------------
  42. // Purpose:
  43. // Output : RenderGroup_t
  44. //-----------------------------------------------------------------------------
  45. RenderGroup_t C_CitadelEnergyCore::GetRenderGroup( void )
  46. {
  47. return RENDER_GROUP_TRANSLUCENT_ENTITY;
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Purpose:
  51. // Input : updateType -
  52. //-----------------------------------------------------------------------------
  53. void C_CitadelEnergyCore::OnDataChanged( DataUpdateType_t updateType )
  54. {
  55. BaseClass::OnDataChanged( updateType );
  56. if ( updateType == DATA_UPDATE_CREATED )
  57. {
  58. SetNextClientThink( CLIENT_THINK_ALWAYS );
  59. SetupEmitters();
  60. }
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose:
  64. //-----------------------------------------------------------------------------
  65. bool C_CitadelEnergyCore::SetupEmitters( void )
  66. {
  67. // Setup the basic core emitter
  68. if ( m_pSimpleEmitter.IsValid() == false )
  69. {
  70. m_pSimpleEmitter = CSimpleEmitter::Create( "energycore" );
  71. if ( m_pSimpleEmitter.IsValid() == false )
  72. return false;
  73. }
  74. // Setup the attractor emitter
  75. if ( m_pAttractorEmitter.IsValid() == false )
  76. {
  77. m_pAttractorEmitter = CParticleAttractor::Create( GetAbsOrigin(), "energyattractor" );
  78. if ( m_pAttractorEmitter.IsValid() == false )
  79. return false;
  80. }
  81. return true;
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose:
  85. // Input : percentage -
  86. //-----------------------------------------------------------------------------
  87. void C_CitadelEnergyCore::UpdateIdle( float percentage )
  88. {
  89. // Only do these particles if required
  90. if ( m_spawnflags & SF_ENERGYCORE_NO_PARTICLES )
  91. return;
  92. // Must be active
  93. if ( percentage >= 1.0f )
  94. return;
  95. // Emitters must be valid
  96. if ( SetupEmitters() == false )
  97. return;
  98. // Reset our sort origin
  99. m_pSimpleEmitter->SetSortOrigin( GetAbsOrigin() );
  100. SimpleParticle *sParticle;
  101. // Do the charging particles
  102. m_pAttractorEmitter->SetAttractorOrigin( GetAbsOrigin() );
  103. Vector forward, right, up;
  104. AngleVectors( GetAbsAngles(), &forward, &right, &up );
  105. Vector offset;
  106. float dist;
  107. int numParticles = floor( 4.0f * percentage );
  108. for ( int i = 0; i < numParticles; i++ )
  109. {
  110. dist = random->RandomFloat( 4.0f * percentage, 64.0f * percentage );
  111. offset = forward * dist;
  112. dist = RemapValClamped( dist, 4.0f * percentage, 64.0f * percentage, 6.0f, 1.0f );
  113. offset += right * random->RandomFloat( -4.0f * dist, 4.0f * dist );
  114. offset += up * random->RandomFloat( -4.0f * dist, 4.0f * dist );
  115. offset += GetAbsOrigin();
  116. sParticle = (SimpleParticle *) m_pAttractorEmitter->AddParticle( sizeof(SimpleParticle), m_pAttractorEmitter->GetPMaterial( "effects/strider_muzzle" ), offset );
  117. if ( sParticle == NULL )
  118. return;
  119. sParticle->m_vecVelocity = Vector(0,0,8);
  120. sParticle->m_flDieTime = 0.5f;
  121. sParticle->m_flLifetime = 0.0f;
  122. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  123. sParticle->m_flRollDelta = 0.0f;
  124. float alpha = 255 * percentage;
  125. sParticle->m_uchColor[0] = alpha;
  126. sParticle->m_uchColor[1] = alpha;
  127. sParticle->m_uchColor[2] = alpha;
  128. sParticle->m_uchStartAlpha = alpha;
  129. sParticle->m_uchEndAlpha = 0;
  130. sParticle->m_uchStartSize = random->RandomFloat( 1, 2 );
  131. sParticle->m_uchEndSize = 0;
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose:
  136. // Input : percentage -
  137. //-----------------------------------------------------------------------------
  138. void C_CitadelEnergyCore::UpdateCharging( float percentage )
  139. {
  140. // Emitters must be valid
  141. if ( SetupEmitters() == false )
  142. return;
  143. if ( percentage <= 0.0f )
  144. return;
  145. // Reset our sort origin
  146. m_pSimpleEmitter->SetSortOrigin( GetAbsOrigin() );
  147. float flScale = 4.0f * m_flScale * percentage;
  148. SimpleParticle *sParticle;
  149. // Do the core effects
  150. for ( int i = 0; i < 2; i++ )
  151. {
  152. sParticle = (SimpleParticle *) m_pSimpleEmitter->AddParticle( sizeof(SimpleParticle), m_pSimpleEmitter->GetPMaterial( "effects/strider_muzzle" ), GetAbsOrigin() );
  153. if ( sParticle == NULL )
  154. return;
  155. sParticle->m_vecVelocity = vec3_origin;
  156. sParticle->m_flDieTime = 0.1f;
  157. sParticle->m_flLifetime = 0.0f;
  158. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  159. sParticle->m_flRollDelta = 0.0f;
  160. float alpha = 255;
  161. sParticle->m_uchColor[0] = alpha;
  162. sParticle->m_uchColor[1] = alpha;
  163. sParticle->m_uchColor[2] = alpha;
  164. sParticle->m_uchStartAlpha = alpha;
  165. sParticle->m_uchEndAlpha = 0;
  166. if ( i < 2 )
  167. {
  168. sParticle->m_uchStartSize = flScale * (i+1);
  169. sParticle->m_uchEndSize = sParticle->m_uchStartSize * 2.0f;
  170. }
  171. else
  172. {
  173. if ( random->RandomInt( 0, 20 ) == 0 )
  174. {
  175. sParticle->m_uchStartSize = flScale * (i+1);
  176. sParticle->m_uchEndSize = sParticle->m_uchStartSize * 4.0f;
  177. sParticle->m_flDieTime = 0.25f;
  178. }
  179. else
  180. {
  181. sParticle->m_uchStartSize = flScale * (i+1);
  182. sParticle->m_uchEndSize = sParticle->m_uchStartSize * 2.0f;
  183. }
  184. }
  185. }
  186. // Only do these particles if required
  187. if ( m_spawnflags & SF_ENERGYCORE_NO_PARTICLES )
  188. return;
  189. // Do the charging particles
  190. m_pAttractorEmitter->SetAttractorOrigin( GetAbsOrigin() );
  191. Vector forward, right, up;
  192. AngleVectors( GetAbsAngles(), &forward, &right, &up );
  193. Vector offset;
  194. float dist;
  195. int numParticles = floor( 4.0f * percentage );
  196. for ( int i = 0; i < numParticles; i++ )
  197. {
  198. dist = random->RandomFloat( 4.0f * percentage, 64.0f * percentage );
  199. offset = forward * dist;
  200. dist = RemapValClamped( dist, 4.0f * percentage, 64.0f * percentage, 6.0f, 1.0f );
  201. offset += right * random->RandomFloat( -4.0f * dist, 4.0f * dist );
  202. offset += up * random->RandomFloat( -4.0f * dist, 4.0f * dist );
  203. offset += GetAbsOrigin();
  204. sParticle = (SimpleParticle *) m_pAttractorEmitter->AddParticle( sizeof(SimpleParticle), m_pAttractorEmitter->GetPMaterial( "effects/strider_muzzle" ), offset );
  205. if ( sParticle == NULL )
  206. return;
  207. sParticle->m_vecVelocity = Vector(0,0,8);
  208. sParticle->m_flDieTime = 0.5f;
  209. sParticle->m_flLifetime = 0.0f;
  210. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  211. sParticle->m_flRollDelta = 0.0f;
  212. float alpha = 255 * percentage;
  213. sParticle->m_uchColor[0] = alpha;
  214. sParticle->m_uchColor[1] = alpha;
  215. sParticle->m_uchColor[2] = alpha;
  216. sParticle->m_uchStartAlpha = alpha;
  217. sParticle->m_uchEndAlpha = 0;
  218. sParticle->m_uchStartSize = random->RandomFloat( 1, 2 );
  219. sParticle->m_uchEndSize = 0;
  220. }
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Purpose:
  224. // Input : percentage -
  225. //-----------------------------------------------------------------------------
  226. void C_CitadelEnergyCore::UpdateDischarging( void )
  227. {
  228. // Emitters must be valid
  229. if ( SetupEmitters() == false )
  230. return;
  231. // Reset our sort origin
  232. m_pSimpleEmitter->SetSortOrigin( GetAbsOrigin() );
  233. float flScale = 8.0f * m_flScale;
  234. Vector forward, right, up;
  235. AngleVectors( GetAbsAngles(), &forward, &right, &up );
  236. SimpleParticle *sParticle;
  237. // Base of the core effect
  238. sParticle = (SimpleParticle *) m_pSimpleEmitter->AddParticle( sizeof(SimpleParticle), m_pSimpleEmitter->GetPMaterial( "effects/strider_muzzle" ), GetAbsOrigin() );
  239. if ( sParticle == NULL )
  240. return;
  241. sParticle->m_vecVelocity = forward * 32.0f;
  242. sParticle->m_flDieTime = 0.2f;
  243. sParticle->m_flLifetime = 0.0f;
  244. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  245. sParticle->m_flRollDelta = 0.0f;
  246. float alpha = 128;
  247. sParticle->m_uchColor[0] = alpha;
  248. sParticle->m_uchColor[1] = alpha;
  249. sParticle->m_uchColor[2] = alpha;
  250. sParticle->m_uchStartAlpha = alpha;
  251. sParticle->m_uchEndAlpha = 0;
  252. sParticle->m_uchStartSize = flScale * 2.0f;
  253. sParticle->m_uchEndSize = sParticle->m_uchStartSize * 2.0f;
  254. // Make sure we encompass the complete particle here!
  255. m_pSimpleEmitter->SetParticleCullRadius( sParticle->m_uchEndSize );
  256. // Do the core effects
  257. for ( int i = 0; i < 2; i++ )
  258. {
  259. sParticle = (SimpleParticle *) m_pSimpleEmitter->AddParticle( sizeof(SimpleParticle), m_pSimpleEmitter->GetPMaterial( "effects/combinemuzzle2" ), GetAbsOrigin() );
  260. if ( sParticle == NULL )
  261. return;
  262. sParticle->m_vecVelocity = forward * ( 32.0f * (i+1) );
  263. sParticle->m_flDieTime = 0.2f;
  264. sParticle->m_flLifetime = 0.0f;
  265. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  266. sParticle->m_flRollDelta = 0.0f;
  267. float alpha = 100;
  268. sParticle->m_uchColor[0] = alpha;
  269. sParticle->m_uchColor[1] = alpha;
  270. sParticle->m_uchColor[2] = alpha;
  271. sParticle->m_uchStartAlpha = alpha;
  272. sParticle->m_uchEndAlpha = 0;
  273. if ( i < 1 )
  274. {
  275. sParticle->m_uchStartSize = flScale * (i+1);
  276. sParticle->m_uchEndSize = sParticle->m_uchStartSize * 2.0f;
  277. }
  278. else
  279. {
  280. if ( random->RandomInt( 0, 20 ) == 0 )
  281. {
  282. sParticle->m_uchStartSize = flScale * (i+1);
  283. sParticle->m_uchEndSize = 0.0f;
  284. sParticle->m_flDieTime = 0.25f;
  285. }
  286. else
  287. {
  288. sParticle->m_uchStartSize = flScale * (i+1);
  289. sParticle->m_uchEndSize = 0.0f;
  290. }
  291. }
  292. }
  293. // Only do these particles if required
  294. if ( m_spawnflags & SF_ENERGYCORE_NO_PARTICLES )
  295. return;
  296. // Do the charging particles
  297. m_pAttractorEmitter->SetAttractorOrigin( GetAbsOrigin() );
  298. Vector offset;
  299. float dist;
  300. for ( int i = 0; i < 4; i++ )
  301. {
  302. dist = random->RandomFloat( 4.0f * m_flScale, 64.0f * m_flScale );
  303. offset = forward * dist;
  304. dist = RemapValClamped( dist, 4.0f * m_flScale, 64.0f * m_flScale, 6.0f, 1.0f );
  305. offset += right * random->RandomFloat( -2.0f * dist * m_flScale, 2.0f * dist * m_flScale );
  306. offset += up * random->RandomFloat( -2.0f * dist * m_flScale, 2.0f * dist * m_flScale );
  307. offset += GetAbsOrigin();
  308. sParticle = (SimpleParticle *) m_pAttractorEmitter->AddParticle( sizeof(SimpleParticle), m_pAttractorEmitter->GetPMaterial( "effects/combinemuzzle2_dark" ), offset );
  309. if ( sParticle == NULL )
  310. return;
  311. sParticle->m_vecVelocity = Vector(0,0,2);
  312. sParticle->m_flDieTime = 0.5f;
  313. sParticle->m_flLifetime = 0.0f;
  314. sParticle->m_flRoll = Helper_RandomInt( 0, 360 );
  315. sParticle->m_flRollDelta = 0.0f;
  316. float alpha = 255;
  317. sParticle->m_uchColor[0] = alpha;
  318. sParticle->m_uchColor[1] = alpha;
  319. sParticle->m_uchColor[2] = alpha;
  320. sParticle->m_uchStartAlpha = alpha;
  321. sParticle->m_uchEndAlpha = 0;
  322. sParticle->m_uchStartSize = 1;
  323. sParticle->m_uchEndSize = 0;
  324. }
  325. }
  326. //-----------------------------------------------------------------------------
  327. // Purpose:
  328. // Output : inline float
  329. //-----------------------------------------------------------------------------
  330. inline float C_CitadelEnergyCore::GetStateDurationPercentage( void )
  331. {
  332. if ( m_flDuration == 0 )
  333. return 0.0f;
  334. return RemapValClamped( ( gpGlobals->curtime - m_flStartTime ), 0, m_flDuration, 0, 1.0f );;
  335. }
  336. //-----------------------------------------------------------------------------
  337. // Purpose:
  338. //-----------------------------------------------------------------------------
  339. void C_CitadelEnergyCore::NotifyShouldTransmit( ShouldTransmitState_t state )
  340. {
  341. BaseClass::NotifyShouldTransmit( state );
  342. // Turn off
  343. if ( state == SHOULDTRANSMIT_END )
  344. {
  345. SetNextClientThink( CLIENT_THINK_NEVER );
  346. }
  347. // Turn on
  348. if ( state == SHOULDTRANSMIT_START )
  349. {
  350. SetNextClientThink( CLIENT_THINK_ALWAYS );
  351. }
  352. }
  353. //-----------------------------------------------------------------------------
  354. // Purpose:
  355. //-----------------------------------------------------------------------------
  356. void C_CitadelEnergyCore::ClientThink( void )
  357. {
  358. if ( gpGlobals->frametime <= 0.0f )
  359. return;
  360. float flDuration = GetStateDurationPercentage();
  361. switch( m_nState )
  362. {
  363. case ENERGYCORE_STATE_OFF:
  364. UpdateIdle( 1.0f - flDuration );
  365. break;
  366. case ENERGYCORE_STATE_CHARGING:
  367. UpdateCharging( flDuration );
  368. break;
  369. case ENERGYCORE_STATE_DISCHARGING:
  370. UpdateDischarging( );
  371. break;
  372. }
  373. }