explosion.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // ExplosionGeo
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class ExplosionGeoImpl :
  8. public ExplosionGeo
  9. {
  10. private:
  11. //////////////////////////////////////////////////////////////////////////////
  12. //
  13. // Types
  14. //
  15. //////////////////////////////////////////////////////////////////////////////
  16. class ExplosionData : IObject {
  17. public:
  18. TRef<AnimatedImage> m_pimage;
  19. Vector m_position;
  20. Vector m_dposition;
  21. float m_angle;
  22. float m_timeStart;
  23. float m_scale;
  24. };
  25. class ShockwaveData : IObject {
  26. public:
  27. TRef<Image> m_pimageShockwave;
  28. Vector m_position;
  29. Vector m_dposition;
  30. Vector m_forward;
  31. Vector m_right;
  32. Color m_color;
  33. float m_timeStart;
  34. float m_scale;
  35. };
  36. typedef TList<ExplosionData> ExplosionDataList;
  37. //////////////////////////////////////////////////////////////////////////////
  38. //
  39. // Members
  40. //
  41. //////////////////////////////////////////////////////////////////////////////
  42. TList<ShockwaveData> m_listShockwave;
  43. TVector<ExplosionDataList> m_vlistExplosion;
  44. //////////////////////////////////////////////////////////////////////////////
  45. //
  46. // Value Members
  47. //
  48. //////////////////////////////////////////////////////////////////////////////
  49. Number* GetTime() { return Number::Cast(GetChild(0)); }
  50. public:
  51. ExplosionGeoImpl(Number* ptime) :
  52. ExplosionGeo(ptime),
  53. m_vlistExplosion(24)
  54. {
  55. }
  56. //////////////////////////////////////////////////////////////////////////////
  57. //
  58. // Methods
  59. //
  60. //////////////////////////////////////////////////////////////////////////////
  61. void AddExplosion(
  62. const Vector& position,
  63. const Vector& forward,
  64. const Vector& right,
  65. const Vector& dposition,
  66. float radiusExplosion,
  67. float radiusShockWave,
  68. const Color& color,
  69. int countDecals,
  70. TVector<TRef<AnimatedImage> > vpimage,
  71. Image* pimageShockwave
  72. ) {
  73. //
  74. // Add the shockwave
  75. //
  76. if (pimageShockwave != NULL) {
  77. m_listShockwave.PushFront();
  78. ShockwaveData& sdata = m_listShockwave.GetFront();
  79. sdata.m_timeStart = GetTime()->GetValue();
  80. sdata.m_pimageShockwave = pimageShockwave;
  81. sdata.m_color = color;
  82. sdata.m_position = position;
  83. sdata.m_dposition = dposition;
  84. sdata.m_scale = radiusShockWave;
  85. sdata.m_forward = forward;
  86. sdata.m_right = right;
  87. }
  88. //
  89. // Add the little explosions
  90. //
  91. int countImage = vpimage.GetCount();
  92. int indexImage = 0;
  93. for (int index = 0; index < countDecals; index++) {
  94. ExplosionDataList& list = m_vlistExplosion.Get(index);
  95. list.PushFront();
  96. ExplosionData& edata = list.GetFront();
  97. edata.m_timeStart = GetTime()->GetValue() + index * 0.25f;
  98. edata.m_pimage = vpimage[indexImage];
  99. edata.m_position = position + Vector::RandomPosition(radiusExplosion * 0.5f);
  100. edata.m_dposition = dposition;
  101. edata.m_angle = random(0, 2 * pi);
  102. edata.m_scale = radiusExplosion;
  103. indexImage++;
  104. if (indexImage >= countImage) {
  105. indexImage = 0;
  106. }
  107. }
  108. }
  109. //////////////////////////////////////////////////////////////////////////////
  110. //
  111. // Value Methods
  112. //
  113. //////////////////////////////////////////////////////////////////////////////
  114. ZString GetFunctionName() { return "ExplosionGeo"; }
  115. //////////////////////////////////////////////////////////////////////////////
  116. //
  117. // Geometry Methods
  118. //
  119. //////////////////////////////////////////////////////////////////////////////
  120. float RenderShockwaves(Context* pcontext)
  121. {
  122. float fill = 0;
  123. TList<ShockwaveData>::Iterator iter(m_listShockwave);
  124. while (!iter.End()) {
  125. ShockwaveData& sdata = iter.Value();
  126. float time = GetTime()->GetValue() - sdata.m_timeStart;
  127. float bright = 1.0f - time * time;
  128. if (bright <= 0) {
  129. iter.Remove();
  130. } else {
  131. float scale = time * sdata.m_scale;
  132. fill +=
  133. pcontext->DrawDecal(
  134. sdata.m_pimageShockwave->GetSurface(),
  135. bright * sdata.m_color,
  136. sdata.m_position + time * sdata.m_dposition,
  137. scale * sdata.m_forward,
  138. scale * sdata.m_right,
  139. 0,
  140. 0
  141. );
  142. iter.Next();
  143. }
  144. };
  145. return fill;
  146. }
  147. float RenderExplosions(
  148. Context* pcontext,
  149. ExplosionDataList& list
  150. ) {
  151. float fill = 0;
  152. ExplosionDataList::Iterator iter(list);
  153. while (!iter.End()) {
  154. ExplosionData& edata = iter.Value();
  155. float time = GetTime()->GetValue() - edata.m_timeStart;
  156. if (time >= 0) {
  157. int frame = (int)(time * 20.0f);
  158. if (frame >= edata.m_pimage->GetCount()) {
  159. iter.Remove();
  160. continue;
  161. } else {
  162. fill +=
  163. pcontext->DrawDecal(
  164. edata.m_pimage->GetSurface(frame),
  165. Color::White(),
  166. edata.m_position + time * edata.m_dposition,
  167. Vector::GetZero(),
  168. Vector::GetZero(),
  169. edata.m_scale,
  170. edata.m_angle
  171. );
  172. }
  173. }
  174. iter.Next();
  175. }
  176. return fill;
  177. }
  178. void Render(Context* pcontext)
  179. {
  180. float fill = 0;
  181. fill += RenderShockwaves(pcontext);
  182. int count = m_vlistExplosion.GetCount();
  183. for (int index = 0; index < count; index++) {
  184. fill += RenderExplosions(pcontext, m_vlistExplosion.Get(index));
  185. //
  186. // If we have passed the fill limit throw away the rest of the explosions
  187. //
  188. if (fill > 2.0f) {
  189. for (int index2 = index + 1; index2 < count; index2++) {
  190. m_vlistExplosion.Get(index2).SetEmpty();
  191. }
  192. return;
  193. }
  194. }
  195. }
  196. };
  197. //////////////////////////////////////////////////////////////////////////////
  198. //
  199. // ExplosionGeo Factory
  200. //
  201. //////////////////////////////////////////////////////////////////////////////
  202. TRef<ExplosionGeo> CreateExplosionGeo(Number* ptime)
  203. {
  204. return new ExplosionGeoImpl(ptime);
  205. }