splash.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/fx/splash.h"
  23. #include "console/consoleTypes.h"
  24. #include "gfx/primBuilder.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "sfx/sfxSystem.h"
  27. #include "sfx/sfxProfile.h"
  28. #include "scene/sceneManager.h"
  29. #include "scene/sceneRenderState.h"
  30. #include "core/stream/bitStream.h"
  31. #include "math/mathIO.h"
  32. #include "T3D/fx/explosion.h"
  33. #include "T3D/fx/particle.h"
  34. #include "T3D/fx/particleEmitter.h"
  35. #include "T3D/fx/particleEmitterNode.h"
  36. #include "T3D/gameBase/gameProcess.h"
  37. #include "sim/netConnection.h"
  38. #include "renderInstance/renderPassManager.h"
  39. #include "console/engineAPI.h"
  40. namespace
  41. {
  42. MRandomLCG sgRandom(0xdeadbeef);
  43. } // namespace {}
  44. //----------------------------------------------------------------------------
  45. IMPLEMENT_CO_DATABLOCK_V1(SplashData);
  46. IMPLEMENT_CO_NETOBJECT_V1(Splash);
  47. ConsoleDocClass( SplashData,
  48. "@brief Acts as the physical point in space in white a Splash is created from.\n"
  49. "@ingroup FX\n"
  50. );
  51. ConsoleDocClass( Splash,
  52. "@brief Manages the ring used for a Splash effect.\n"
  53. "@ingroup FX\n"
  54. );
  55. //--------------------------------------------------------------------------
  56. // Splash Data
  57. //--------------------------------------------------------------------------
  58. SplashData::SplashData()
  59. {
  60. soundProfile = NULL;
  61. soundProfileId = 0;
  62. scale.set(1, 1, 1);
  63. dMemset( emitterList, 0, sizeof( emitterList ) );
  64. dMemset( emitterIDList, 0, sizeof( emitterIDList ) );
  65. delayMS = 0;
  66. delayVariance = 0;
  67. lifetimeMS = 1000;
  68. lifetimeVariance = 0;
  69. width = 4.0;
  70. numSegments = 10;
  71. velocity = 5.0;
  72. height = 0.0;
  73. acceleration = 0.0;
  74. texWrap = 1.0;
  75. texFactor = 3.0;
  76. ejectionFreq = 5;
  77. ejectionAngle = 45.0;
  78. ringLifetime = 1.0;
  79. startRadius = 0.5;
  80. explosion = NULL;
  81. explosionId = 0;
  82. dMemset( textureName, 0, sizeof( textureName ) );
  83. U32 i;
  84. for( i=0; i<NUM_TIME_KEYS; i++ )
  85. times[i] = 1.0;
  86. times[0] = 0.0;
  87. for( i=0; i<NUM_TIME_KEYS; i++ )
  88. colors[i].set( 1.0, 1.0, 1.0, 1.0 );
  89. }
  90. //--------------------------------------------------------------------------
  91. // Init fields
  92. //--------------------------------------------------------------------------
  93. void SplashData::initPersistFields()
  94. {
  95. addField("soundProfile", TYPEID< SFXProfile >(), Offset(soundProfile, SplashData), "SFXProfile effect to play.\n");
  96. addField("scale", TypePoint3F, Offset(scale, SplashData), "The scale of this splashing effect, defined as the F32 points X, Y, Z.\n");
  97. addField("emitter", TYPEID< ParticleEmitterData >(), Offset(emitterList, SplashData), NUM_EMITTERS, "List of particle emitters to create at the point of this Splash effect.\n");
  98. addField("delayMS", TypeS32, Offset(delayMS, SplashData), "Time to delay, in milliseconds, before actually starting this effect.\n");
  99. addField("delayVariance", TypeS32, Offset(delayVariance, SplashData), "Time variance for delayMS.\n");
  100. addField("lifetimeMS", TypeS32, Offset(lifetimeMS, SplashData), "Lifetime for this effect, in milliseconds.\n");
  101. addField("lifetimeVariance", TypeS32, Offset(lifetimeVariance, SplashData), "Time variance for lifetimeMS.\n");
  102. addField("width", TypeF32, Offset(width, SplashData), "Width for the X and Y coordinates to create this effect within.");
  103. addField("numSegments", TypeS32, Offset(numSegments, SplashData), "Number of ejection points in the splash ring.\n");
  104. addField("velocity", TypeF32, Offset(velocity, SplashData), "Velocity for the splash effect to travel.\n");
  105. addField("height", TypeF32, Offset(height, SplashData), "Height for the splash to reach.\n");
  106. addField("acceleration", TypeF32, Offset(acceleration, SplashData), "Constant acceleration value to place upon the splash effect.\n");
  107. addField("times", TypeF32, Offset(times, SplashData), NUM_TIME_KEYS, "Times to transition through the splash effect. Up to 4 allowed. Values are 0.0 - 1.0, and corrispond to the life of the particle where 0 is first created and 1 is end of lifespace.\n" );
  108. addField("colors", TypeColorF, Offset(colors, SplashData), NUM_TIME_KEYS, "Color values to set the splash effect, rgba. Up to 4 allowed. Will transition through colors based on values set in the times value. Example: colors[0] = \"0.6 1.0 1.0 0.5\".\n" );
  109. addField("texture", TypeFilename, Offset(textureName, SplashData), NUM_TEX, "Imagemap file to use as the texture for the splash effect.\n");
  110. addField("texWrap", TypeF32, Offset(texWrap, SplashData), "Amount to wrap the texture around the splash ring, 0.0f - 1.0f.\n");
  111. addField("texFactor", TypeF32, Offset(texFactor, SplashData), "Factor in which to apply the texture to the splash ring, 0.0f - 1.0f.\n");
  112. addField("ejectionFreq", TypeF32, Offset(ejectionFreq, SplashData), "Frequency in which to emit splash rings.\n");
  113. addField("ejectionAngle", TypeF32, Offset(ejectionAngle, SplashData), "Rotational angle to create a splash ring.\n");
  114. addField("ringLifetime", TypeF32, Offset(ringLifetime, SplashData), "Lifetime, in milliseconds, for a splash ring.\n");
  115. addField("startRadius", TypeF32, Offset(startRadius, SplashData), "Starting radius size of a splash ring.\n");
  116. addField("explosion", TYPEID< ExplosionData >(), Offset(explosion, SplashData), "ExplosionData object to create at the creation position of this splash effect.\n");
  117. Parent::initPersistFields();
  118. }
  119. //--------------------------------------------------------------------------
  120. // On add - verify data settings
  121. //--------------------------------------------------------------------------
  122. bool SplashData::onAdd()
  123. {
  124. if (Parent::onAdd() == false)
  125. return false;
  126. return true;
  127. }
  128. //--------------------------------------------------------------------------
  129. // Pack data
  130. //--------------------------------------------------------------------------
  131. void SplashData::packData(BitStream* stream)
  132. {
  133. Parent::packData(stream);
  134. mathWrite(*stream, scale);
  135. stream->write(delayMS);
  136. stream->write(delayVariance);
  137. stream->write(lifetimeMS);
  138. stream->write(lifetimeVariance);
  139. stream->write(width);
  140. stream->write(numSegments);
  141. stream->write(velocity);
  142. stream->write(height);
  143. stream->write(acceleration);
  144. stream->write(texWrap);
  145. stream->write(texFactor);
  146. stream->write(ejectionFreq);
  147. stream->write(ejectionAngle);
  148. stream->write(ringLifetime);
  149. stream->write(startRadius);
  150. if( stream->writeFlag( explosion ) )
  151. {
  152. stream->writeRangedU32(explosion->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
  153. }
  154. S32 i;
  155. for( i=0; i<NUM_EMITTERS; i++ )
  156. {
  157. if( stream->writeFlag( emitterList[i] != NULL ) )
  158. {
  159. stream->writeRangedU32( emitterList[i]->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  160. }
  161. }
  162. for( i=0; i<NUM_TIME_KEYS; i++ )
  163. {
  164. stream->write( colors[i] );
  165. }
  166. for( i=0; i<NUM_TIME_KEYS; i++ )
  167. {
  168. stream->write( times[i] );
  169. }
  170. for( i=0; i<NUM_TEX; i++ )
  171. {
  172. stream->writeString(textureName[i]);
  173. }
  174. }
  175. //--------------------------------------------------------------------------
  176. // Unpack data
  177. //--------------------------------------------------------------------------
  178. void SplashData::unpackData(BitStream* stream)
  179. {
  180. Parent::unpackData(stream);
  181. mathRead(*stream, &scale);
  182. stream->read(&delayMS);
  183. stream->read(&delayVariance);
  184. stream->read(&lifetimeMS);
  185. stream->read(&lifetimeVariance);
  186. stream->read(&width);
  187. stream->read(&numSegments);
  188. stream->read(&velocity);
  189. stream->read(&height);
  190. stream->read(&acceleration);
  191. stream->read(&texWrap);
  192. stream->read(&texFactor);
  193. stream->read(&ejectionFreq);
  194. stream->read(&ejectionAngle);
  195. stream->read(&ringLifetime);
  196. stream->read(&startRadius);
  197. if( stream->readFlag() )
  198. {
  199. explosionId = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  200. }
  201. U32 i;
  202. for( i=0; i<NUM_EMITTERS; i++ )
  203. {
  204. if( stream->readFlag() )
  205. {
  206. emitterIDList[i] = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  207. }
  208. }
  209. for( i=0; i<NUM_TIME_KEYS; i++ )
  210. {
  211. stream->read( &colors[i] );
  212. }
  213. for( i=0; i<NUM_TIME_KEYS; i++ )
  214. {
  215. stream->read( &times[i] );
  216. }
  217. for( i=0; i<NUM_TEX; i++ )
  218. {
  219. textureName[i] = stream->readSTString();
  220. }
  221. }
  222. //--------------------------------------------------------------------------
  223. // Preload data - load resources
  224. //--------------------------------------------------------------------------
  225. bool SplashData::preload(bool server, String &errorStr)
  226. {
  227. if (Parent::preload(server, errorStr) == false)
  228. return false;
  229. if (!server)
  230. {
  231. S32 i;
  232. for( i=0; i<NUM_EMITTERS; i++ )
  233. {
  234. if( !emitterList[i] && emitterIDList[i] != 0 )
  235. {
  236. if( Sim::findObject( emitterIDList[i], emitterList[i] ) == false)
  237. {
  238. Con::errorf( ConsoleLogEntry::General, "SplashData::onAdd: Invalid packet, bad datablockId(particle emitter): 0x%x", emitterIDList[i] );
  239. }
  240. }
  241. }
  242. for( i=0; i<NUM_TEX; i++ )
  243. {
  244. if (textureName[i] && textureName[i][0])
  245. {
  246. textureHandle[i] = GFXTexHandle(textureName[i], &GFXDefaultStaticDiffuseProfile, avar("%s() - textureHandle[%d] (line %d)", __FUNCTION__, i, __LINE__) );
  247. }
  248. }
  249. }
  250. if( !explosion && explosionId != 0 )
  251. {
  252. if( !Sim::findObject(explosionId, explosion) )
  253. {
  254. Con::errorf(ConsoleLogEntry::General, "SplashData::preload: Invalid packet, bad datablockId(explosion): %d", explosionId);
  255. }
  256. }
  257. return true;
  258. }
  259. //--------------------------------------------------------------------------
  260. // Splash
  261. //--------------------------------------------------------------------------
  262. Splash::Splash()
  263. : mDataBlock( NULL )
  264. {
  265. dMemset( mEmitterList, 0, sizeof( mEmitterList ) );
  266. mDelayMS = 0;
  267. mCurrMS = 0;
  268. mEndingMS = 1000;
  269. mActive = false;
  270. mRadius = 0.0;
  271. mVelocity = 1.0;
  272. mHeight = 0.0;
  273. mTimeSinceLastRing = 0.0;
  274. mDead = false;
  275. mElapsedTime = 0.0;
  276. mInitialNormal.set( 0.0, 0.0, 1.0 );
  277. // Only allocated client side.
  278. mNetFlags.set( IsGhost );
  279. }
  280. //--------------------------------------------------------------------------
  281. // Destructor
  282. //--------------------------------------------------------------------------
  283. Splash::~Splash()
  284. {
  285. }
  286. //--------------------------------------------------------------------------
  287. // Set initial state
  288. //--------------------------------------------------------------------------
  289. void Splash::setInitialState(const Point3F& point, const Point3F& normal, const F32 fade)
  290. {
  291. mInitialPosition = point;
  292. mInitialNormal = normal;
  293. mFade = fade;
  294. mFog = 0.0f;
  295. }
  296. //--------------------------------------------------------------------------
  297. // OnAdd
  298. //--------------------------------------------------------------------------
  299. bool Splash::onAdd()
  300. {
  301. // first check if we have a server connection, if we dont then this is on the server
  302. // and we should exit, then check if the parent fails to add the object
  303. NetConnection* conn = NetConnection::getConnectionToServer();
  304. if(!conn || !Parent::onAdd())
  305. return false;
  306. if( !mDataBlock )
  307. {
  308. Con::errorf("Splash::onAdd - Fail - No datablock");
  309. return false;
  310. }
  311. mDelayMS = mDataBlock->delayMS + sgRandom.randI( -mDataBlock->delayVariance, mDataBlock->delayVariance );
  312. mEndingMS = mDataBlock->lifetimeMS + sgRandom.randI( -mDataBlock->lifetimeVariance, mDataBlock->lifetimeVariance );
  313. mVelocity = mDataBlock->velocity;
  314. mHeight = mDataBlock->height;
  315. mTimeSinceLastRing = 1.0 / mDataBlock->ejectionFreq;
  316. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  317. {
  318. if( mDataBlock->emitterList[i] != NULL )
  319. {
  320. ParticleEmitter * pEmitter = new ParticleEmitter;
  321. pEmitter->onNewDataBlock( mDataBlock->emitterList[i], false );
  322. if( !pEmitter->registerObject() )
  323. {
  324. Con::warnf( ConsoleLogEntry::General, "Could not register emitter for particle of class: %s", mDataBlock->getName() );
  325. delete pEmitter;
  326. pEmitter = NULL;
  327. }
  328. mEmitterList[i] = pEmitter;
  329. }
  330. }
  331. spawnExplosion();
  332. mObjBox.minExtents = Point3F( -1, -1, -1 );
  333. mObjBox.maxExtents = Point3F( 1, 1, 1 );
  334. resetWorldBox();
  335. gClientSceneGraph->addObjectToScene(this);
  336. removeFromProcessList();
  337. ClientProcessList::get()->addObject(this);
  338. conn->addObject(this);
  339. return true;
  340. }
  341. //--------------------------------------------------------------------------
  342. // OnRemove
  343. //--------------------------------------------------------------------------
  344. void Splash::onRemove()
  345. {
  346. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  347. {
  348. if( mEmitterList[i] )
  349. {
  350. mEmitterList[i]->deleteWhenEmpty();
  351. mEmitterList[i] = NULL;
  352. }
  353. }
  354. ringList.clear();
  355. if( getSceneManager() )
  356. getSceneManager()->removeObjectFromScene(this);
  357. if( getContainer() )
  358. getContainer()->removeObject(this);
  359. Parent::onRemove();
  360. }
  361. //--------------------------------------------------------------------------
  362. // On New Data Block
  363. //--------------------------------------------------------------------------
  364. bool Splash::onNewDataBlock( GameBaseData *dptr, bool reload )
  365. {
  366. mDataBlock = dynamic_cast<SplashData*>(dptr);
  367. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  368. return false;
  369. scriptOnNewDataBlock();
  370. return true;
  371. }
  372. //--------------------------------------------------------------------------
  373. // Process tick
  374. //--------------------------------------------------------------------------
  375. void Splash::processTick(const Move*)
  376. {
  377. mCurrMS += TickMs;
  378. if( isServerObject() )
  379. {
  380. if( mCurrMS >= mEndingMS )
  381. {
  382. mDead = true;
  383. if( mCurrMS >= (mEndingMS + mDataBlock->ringLifetime * 1000) )
  384. {
  385. deleteObject();
  386. }
  387. }
  388. }
  389. else
  390. {
  391. if( mCurrMS >= mEndingMS )
  392. {
  393. mDead = true;
  394. }
  395. }
  396. }
  397. //--------------------------------------------------------------------------
  398. // Advance time
  399. //--------------------------------------------------------------------------
  400. void Splash::advanceTime(F32 dt)
  401. {
  402. if (dt == 0.0)
  403. return;
  404. mElapsedTime += dt;
  405. updateColor();
  406. updateWave( dt );
  407. updateEmitters( dt );
  408. updateRings( dt );
  409. if( !mDead )
  410. {
  411. emitRings( dt );
  412. }
  413. }
  414. //----------------------------------------------------------------------------
  415. // Update emitters
  416. //----------------------------------------------------------------------------
  417. void Splash::updateEmitters( F32 dt )
  418. {
  419. Point3F pos = getPosition();
  420. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  421. {
  422. if( mEmitterList[i] )
  423. {
  424. mEmitterList[i]->emitParticles( pos, pos, mInitialNormal, Point3F( 0.0, 0.0, 0.0 ), (S32) (dt * 1000) );
  425. }
  426. }
  427. }
  428. //----------------------------------------------------------------------------
  429. // Update wave
  430. //----------------------------------------------------------------------------
  431. void Splash::updateWave( F32 dt )
  432. {
  433. mVelocity += mDataBlock->acceleration * dt;
  434. mRadius += mVelocity * dt;
  435. }
  436. //----------------------------------------------------------------------------
  437. // Update color
  438. //----------------------------------------------------------------------------
  439. void Splash::updateColor()
  440. {
  441. for(SplashRingList::Iterator ring = ringList.begin(); ring != ringList.end(); ++ring)
  442. {
  443. F32 t = F32(ring->elapsedTime) / F32(ring->lifetime);
  444. for( U32 i = 1; i < SplashData::NUM_TIME_KEYS; i++ )
  445. {
  446. if( mDataBlock->times[i] >= t )
  447. {
  448. F32 firstPart = t - mDataBlock->times[i-1];
  449. F32 total = (mDataBlock->times[i] -
  450. mDataBlock->times[i-1]);
  451. firstPart /= total;
  452. ring->color.interpolate( mDataBlock->colors[i-1],
  453. mDataBlock->colors[i],
  454. firstPart);
  455. break;
  456. }
  457. }
  458. }
  459. }
  460. //----------------------------------------------------------------------------
  461. // Create ring
  462. //----------------------------------------------------------------------------
  463. SplashRing Splash::createRing()
  464. {
  465. SplashRing ring;
  466. U32 numPoints = mDataBlock->numSegments + 1;
  467. Point3F ejectionAxis( 0.0, 0.0, 1.0 );
  468. Point3F axisx;
  469. if (mFabs(ejectionAxis.z) < 0.999f)
  470. mCross(ejectionAxis, Point3F(0, 0, 1), &axisx);
  471. else
  472. mCross(ejectionAxis, Point3F(0, 1, 0), &axisx);
  473. axisx.normalize();
  474. for( U32 i=0; i<numPoints; i++ )
  475. {
  476. F32 t = F32(i) / F32(numPoints);
  477. AngAxisF thetaRot( axisx, mDataBlock->ejectionAngle * (M_PI / 180.0));
  478. AngAxisF phiRot( ejectionAxis, t * (M_PI * 2.0));
  479. Point3F pointAxis = ejectionAxis;
  480. MatrixF temp;
  481. thetaRot.setMatrix(&temp);
  482. temp.mulP(pointAxis);
  483. phiRot.setMatrix(&temp);
  484. temp.mulP(pointAxis);
  485. Point3F startOffset = axisx;
  486. temp.mulV( startOffset );
  487. startOffset *= mDataBlock->startRadius;
  488. SplashRingPoint point;
  489. point.position = getPosition() + startOffset;
  490. point.velocity = pointAxis * mDataBlock->velocity;
  491. ring.points.push_back( point );
  492. }
  493. ring.color = mDataBlock->colors[0];
  494. ring.lifetime = mDataBlock->ringLifetime;
  495. ring.elapsedTime = 0.0;
  496. ring.v = mDataBlock->texFactor * mFmod( mElapsedTime, 1.0 );
  497. return ring;
  498. }
  499. //----------------------------------------------------------------------------
  500. // Emit rings
  501. //----------------------------------------------------------------------------
  502. void Splash::emitRings( F32 dt )
  503. {
  504. mTimeSinceLastRing += dt;
  505. S32 numNewRings = (S32) (mTimeSinceLastRing * F32(mDataBlock->ejectionFreq));
  506. mTimeSinceLastRing -= numNewRings / mDataBlock->ejectionFreq;
  507. for( S32 i=numNewRings-1; i>=0; i-- )
  508. {
  509. F32 t = F32(i) / F32(numNewRings);
  510. t *= dt;
  511. t += mTimeSinceLastRing;
  512. SplashRing ring = createRing();
  513. updateRing( ring, t );
  514. ringList.pushBack( ring );
  515. }
  516. }
  517. //----------------------------------------------------------------------------
  518. // Update rings
  519. //----------------------------------------------------------------------------
  520. void Splash::updateRings( F32 dt )
  521. {
  522. SplashRingList::Iterator ring;
  523. for(SplashRingList::Iterator i = ringList.begin(); i != ringList.end(); /*Trickiness*/)
  524. {
  525. ring = i++;
  526. ring->elapsedTime += dt;
  527. if( !ring->isActive() )
  528. {
  529. ringList.erase( ring );
  530. }
  531. else
  532. {
  533. updateRing( *ring, dt );
  534. }
  535. }
  536. }
  537. //----------------------------------------------------------------------------
  538. // Update ring
  539. //----------------------------------------------------------------------------
  540. void Splash::updateRing( SplashRing& ring, F32 dt )
  541. {
  542. for( U32 i=0; i<ring.points.size(); i++ )
  543. {
  544. if( mDead )
  545. {
  546. Point3F vel = ring.points[i].velocity;
  547. vel.normalize();
  548. vel *= mDataBlock->acceleration;
  549. ring.points[i].velocity += vel * dt;
  550. }
  551. ring.points[i].velocity += Point3F( 0.0f, 0.0f, -9.8f ) * dt;
  552. ring.points[i].position += ring.points[i].velocity * dt;
  553. }
  554. }
  555. //----------------------------------------------------------------------------
  556. // Explode
  557. //----------------------------------------------------------------------------
  558. void Splash::spawnExplosion()
  559. {
  560. if( !mDataBlock->explosion ) return;
  561. Explosion* pExplosion = new Explosion;
  562. pExplosion->onNewDataBlock(mDataBlock->explosion, false);
  563. MatrixF trans = getTransform();
  564. trans.setPosition( getPosition() );
  565. pExplosion->setTransform( trans );
  566. pExplosion->setInitialState( trans.getPosition(), VectorF(0,0,1), 1);
  567. if (!pExplosion->registerObject())
  568. delete pExplosion;
  569. }
  570. //--------------------------------------------------------------------------
  571. // packUpdate
  572. //--------------------------------------------------------------------------
  573. U32 Splash::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  574. {
  575. U32 retMask = Parent::packUpdate(con, mask, stream);
  576. if( stream->writeFlag(mask & GameBase::InitialUpdateMask) )
  577. {
  578. mathWrite(*stream, mInitialPosition);
  579. }
  580. return retMask;
  581. }
  582. //--------------------------------------------------------------------------
  583. // unpackUpdate
  584. //--------------------------------------------------------------------------
  585. void Splash::unpackUpdate(NetConnection* con, BitStream* stream)
  586. {
  587. Parent::unpackUpdate(con, stream);
  588. if( stream->readFlag() )
  589. {
  590. mathRead(*stream, &mInitialPosition);
  591. setPosition( mInitialPosition );
  592. }
  593. }