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.

282 lines
6.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "c_te_particlesystem.h"
  10. #include "movevars_shared.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. int ramp1[][3] =
  14. {
  15. { 255, 243, 227 },
  16. { 223, 171, 39 },
  17. { 191, 119, 47 },
  18. { 127, 59, 43 },
  19. { 99, 47, 31 },
  20. { 75, 35, 19 },
  21. { 47, 23, 11 },
  22. { 175, 103, 35 },
  23. };
  24. int ramp2[][3] =
  25. {
  26. { 255, 243, 227 },
  27. { 239, 203, 31 },
  28. { 223, 171, 39 },
  29. { 207, 143, 43 },
  30. { 191, 119, 47 },
  31. { 175, 99, 47 },
  32. { 143, 67, 51 },
  33. { 115, 55, 35 },
  34. };
  35. int ramp3[][3] =
  36. {
  37. { 223, 171, 39 },
  38. { 191, 119, 47 },
  39. { 91, 91, 91 },
  40. { 75, 75, 75 },
  41. { 63, 63, 63 },
  42. { 47, 47, 47 },
  43. };
  44. #define SPARK_COLORCOUNT 9
  45. int gSparkRamp[ SPARK_COLORCOUNT ][3] =
  46. {
  47. { 255, 255, 255 },
  48. { 255, 247, 199 },
  49. { 255, 243, 147 },
  50. { 255, 243, 27 },
  51. { 239, 203, 31 },
  52. { 223, 171, 39 },
  53. { 207, 143, 43 },
  54. { 127, 59, 43 },
  55. { 35, 19, 7 }
  56. };
  57. // ------------------------------------------------------------------------ //
  58. // C_TEParticleSystem.
  59. // ------------------------------------------------------------------------ //
  60. IMPLEMENT_CLIENTCLASS_DT(C_TEParticleSystem, DT_TEParticleSystem, CTEParticleSystem)
  61. RecvPropFloat( RECVINFO(m_vecOrigin[0]) ),
  62. RecvPropFloat( RECVINFO(m_vecOrigin[1]) ),
  63. RecvPropFloat( RECVINFO(m_vecOrigin[2]) ),
  64. END_RECV_TABLE()
  65. C_TEParticleSystem::C_TEParticleSystem()
  66. {
  67. m_vecOrigin.Init();
  68. }
  69. // ------------------------------------------------------------------------ //
  70. // CTEParticleRenderer implementation.
  71. // ------------------------------------------------------------------------ //
  72. CTEParticleRenderer::CTEParticleRenderer( const char *pDebugName ) :
  73. CParticleEffect( pDebugName )
  74. {
  75. m_ParticleSize = 1.5f;
  76. m_MaterialHandle = INVALID_MATERIAL_HANDLE;
  77. }
  78. CTEParticleRenderer::~CTEParticleRenderer()
  79. {
  80. }
  81. CSmartPtr<CTEParticleRenderer> CTEParticleRenderer::Create( const char *pDebugName, const Vector &vOrigin )
  82. {
  83. CTEParticleRenderer *pRet = new CTEParticleRenderer( pDebugName );
  84. if( pRet )
  85. {
  86. pRet->SetDynamicallyAllocated( true );
  87. pRet->SetSortOrigin( vOrigin );
  88. }
  89. return pRet;
  90. }
  91. StandardParticle_t* CTEParticleRenderer::AddParticle()
  92. {
  93. if(m_MaterialHandle == INVALID_MATERIAL_HANDLE)
  94. {
  95. m_MaterialHandle = m_ParticleEffect.FindOrAddMaterial("particle/particledefault");
  96. }
  97. StandardParticle_t *pParticle =
  98. (StandardParticle_t*)BaseClass::AddParticle( sizeof(StandardParticle_t), m_MaterialHandle, m_vSortOrigin );
  99. if(pParticle)
  100. pParticle->m_EffectDataWord = 0; // (ramp)
  101. return pParticle;
  102. }
  103. void CTEParticleRenderer::RenderParticles( CParticleRenderIterator *pIterator )
  104. {
  105. const StandardParticle_t *pParticle = (const StandardParticle_t*)pIterator->GetFirst();
  106. while ( pParticle )
  107. {
  108. // Render.
  109. Vector tPos;
  110. TransformParticle(ParticleMgr()->GetModelView(), pParticle->m_Pos, tPos);
  111. float sortKey = tPos.z;
  112. Vector vColor(pParticle->m_Color[0]/255.9f, pParticle->m_Color[1]/255.9f, pParticle->m_Color[2]/255.9f);
  113. RenderParticle_ColorSize(
  114. pIterator->GetParticleDraw(),
  115. tPos,
  116. vColor,
  117. pParticle->m_Color[3]/255.9f,
  118. m_ParticleSize);
  119. pParticle = (const StandardParticle_t*)pIterator->GetNext( sortKey );
  120. }
  121. }
  122. void CTEParticleRenderer::SimulateParticles( CParticleSimulateIterator *pIterator )
  123. {
  124. StandardParticle_t *pParticle = (StandardParticle_t*)pIterator->GetFirst();
  125. while ( pParticle )
  126. {
  127. // Remove the particle?
  128. SetParticleLifetime(pParticle, GetParticleLifetime(pParticle) - pIterator->GetTimeDelta());
  129. if(GetParticleLifetime(pParticle) < 0)
  130. {
  131. pIterator->RemoveParticle( pParticle );
  132. }
  133. else
  134. {
  135. float ft = pIterator->GetTimeDelta();
  136. float time3 = 15.0 * ft;
  137. float time2 = 10.0 * ft;
  138. float time1 = 5.0 * ft;
  139. float dvel = 4* ft ;
  140. float grav = ft * sv_gravity.GetFloat() * 0.05f;
  141. int (*colorIndex)[3];
  142. int iRamp;
  143. switch(GetParticleType(pParticle))
  144. {
  145. case pt_static:
  146. break;
  147. case pt_fire:
  148. pParticle->m_EffectDataWord += (unsigned short)(time1 * (1 << SIMSHIFT));
  149. iRamp = pParticle->m_EffectDataWord >> SIMSHIFT;
  150. if(iRamp >= 6)
  151. {
  152. pParticle->m_Lifetime = -1;
  153. }
  154. else
  155. {
  156. colorIndex = &ramp3[ iRamp ];
  157. pParticle->SetColor((float)(*colorIndex)[0] / 255.0f, (float)(*colorIndex)[1] / 255.0f, (float)(*colorIndex)[2] / 255.0f);
  158. }
  159. pParticle->m_Velocity[2] += grav;
  160. break;
  161. case pt_explode:
  162. pParticle->m_EffectDataWord += (unsigned short)(time2 * (1 << SIMSHIFT));
  163. iRamp = pParticle->m_EffectDataWord >> SIMSHIFT;
  164. if(iRamp >= 8)
  165. {
  166. pParticle->m_Lifetime = -1;
  167. }
  168. else
  169. {
  170. colorIndex = &ramp1[ iRamp ];
  171. pParticle->SetColor((float)(*colorIndex)[0] / 255.0f, (float)(*colorIndex)[1] / 255.0f, (float)(*colorIndex)[2] / 255.0f);
  172. }
  173. pParticle->m_Velocity = pParticle->m_Velocity + pParticle->m_Velocity * dvel;
  174. pParticle->m_Velocity[2] -= grav;
  175. break;
  176. case pt_explode2:
  177. pParticle->m_EffectDataWord += (unsigned short)(time3 * (1 << SIMSHIFT));
  178. iRamp = pParticle->m_EffectDataWord >> SIMSHIFT;
  179. if(iRamp >= 8)
  180. {
  181. pParticle->m_Lifetime = -1;
  182. }
  183. else
  184. {
  185. colorIndex = &ramp2[ iRamp ];
  186. pParticle->SetColor((float)(*colorIndex)[0] / 255.0f, (float)(*colorIndex)[1] / 255.0f, (float)(*colorIndex)[2] / 255.0f);
  187. }
  188. pParticle->m_Velocity = pParticle->m_Velocity - pParticle->m_Velocity * ft;
  189. pParticle->m_Velocity[2] -= grav;
  190. break;
  191. case pt_grav:
  192. pParticle->m_Velocity[2] -= grav * 20;
  193. break;
  194. case pt_slowgrav:
  195. pParticle->m_Velocity[2] = grav;
  196. break;
  197. case pt_vox_grav:
  198. pParticle->m_Velocity[2] -= grav * 8;
  199. break;
  200. case pt_vox_slowgrav:
  201. pParticle->m_Velocity[2] -= grav * 4;
  202. break;
  203. case pt_blob:
  204. case pt_blob2:
  205. pParticle->m_EffectDataWord += (unsigned short)(time2 * (1 << SIMSHIFT));
  206. iRamp = pParticle->m_EffectDataWord >> SIMSHIFT;
  207. if(iRamp >= SPARK_COLORCOUNT)
  208. {
  209. pParticle->m_EffectDataWord = 0;
  210. iRamp = 0;
  211. }
  212. colorIndex = &gSparkRamp[ iRamp ];
  213. pParticle->SetColor((float)(*colorIndex)[0] / 255.0f, (float)(*colorIndex)[1] / 255.0f, (float)(*colorIndex)[2] / 255.0f);
  214. pParticle->m_Velocity[0] -= pParticle->m_Velocity[0]*0.5*ft;
  215. pParticle->m_Velocity[1] -= pParticle->m_Velocity[1]*0.5*ft;
  216. pParticle->m_Velocity[2] -= grav * 5;
  217. if ( random->RandomInt(0,3) )
  218. {
  219. SetParticleType(pParticle, pt_blob);
  220. pParticle->SetAlpha(0);
  221. }
  222. else
  223. {
  224. SetParticleType(pParticle, pt_blob2);
  225. pParticle->SetAlpha(255.9f);
  226. }
  227. break;
  228. }
  229. // Update position.
  230. pParticle->m_Pos = pParticle->m_Pos + pParticle->m_Velocity * ft;
  231. }
  232. pParticle = (StandardParticle_t*)pIterator->GetNext();
  233. }
  234. }