splash.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. INIT_ASSET(Sound);
  63. scale.set(1, 1, 1);
  64. dMemset( emitterList, 0, sizeof( emitterList ) );
  65. dMemset( emitterIDList, 0, sizeof( emitterIDList ) );
  66. delayMS = 0;
  67. delayVariance = 0;
  68. lifetimeMS = 1000;
  69. lifetimeVariance = 0;
  70. width = 4.0;
  71. numSegments = 10;
  72. velocity = 5.0;
  73. height = 0.0;
  74. acceleration = 0.0;
  75. texWrap = 1.0;
  76. texFactor = 3.0;
  77. ejectionFreq = 5;
  78. ejectionAngle = 45.0;
  79. ringLifetime = 1.0;
  80. startRadius = 0.5;
  81. explosion = NULL;
  82. explosionId = 0;
  83. U32 i;
  84. for (i = 0; i < NUM_TEX; i++)
  85. {
  86. INIT_IMAGEASSET_ARRAY(Texture, GFXStaticTextureSRGBProfile, i);
  87. }
  88. for( i=0; i<NUM_TIME_KEYS; i++ )
  89. times[i] = 1.0;
  90. times[0] = 0.0;
  91. for( i=0; i<NUM_TIME_KEYS; i++ )
  92. colors[i].set( 1.0, 1.0, 1.0, 1.0 );
  93. }
  94. //--------------------------------------------------------------------------
  95. // Init fields
  96. //--------------------------------------------------------------------------
  97. void SplashData::initPersistFields()
  98. {
  99. docsURL;
  100. INITPERSISTFIELD_SOUNDASSET(Sound, SplashData, "Sound to play when splash, splashes.");
  101. addField("scale", TypePoint3F, Offset(scale, SplashData), "The scale of this splashing effect, defined as the F32 points X, Y, Z.\n");
  102. addField("emitter", TYPEID< ParticleEmitterData >(), Offset(emitterList, SplashData), NUM_EMITTERS, "List of particle emitters to create at the point of this Splash effect.\n");
  103. addField("delayMS", TypeS32, Offset(delayMS, SplashData), "Time to delay, in milliseconds, before actually starting this effect.\n");
  104. addField("delayVariance", TypeS32, Offset(delayVariance, SplashData), "Time variance for delayMS.\n");
  105. addField("lifetimeMS", TypeS32, Offset(lifetimeMS, SplashData), "Lifetime for this effect, in milliseconds.\n");
  106. addField("lifetimeVariance", TypeS32, Offset(lifetimeVariance, SplashData), "Time variance for lifetimeMS.\n");
  107. addField("width", TypeF32, Offset(width, SplashData), "Width for the X and Y coordinates to create this effect within.");
  108. addField("numSegments", TypeS32, Offset(numSegments, SplashData), "Number of ejection points in the splash ring.\n");
  109. addField("velocity", TypeF32, Offset(velocity, SplashData), "Velocity for the splash effect to travel.\n");
  110. addField("height", TypeF32, Offset(height, SplashData), "Height for the splash to reach.\n");
  111. addField("acceleration", TypeF32, Offset(acceleration, SplashData), "Constant acceleration value to place upon the splash effect.\n");
  112. 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" );
  113. 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" );
  114. INITPERSISTFIELD_IMAGEASSET_ARRAY(Texture, NUM_TEX, SplashData, "Image to use as the texture for the splash effect.\n");
  115. addField("texWrap", TypeF32, Offset(texWrap, SplashData), "Amount to wrap the texture around the splash ring, 0.0f - 1.0f.\n");
  116. addField("texFactor", TypeF32, Offset(texFactor, SplashData), "Factor in which to apply the texture to the splash ring, 0.0f - 1.0f.\n");
  117. addField("ejectionFreq", TypeF32, Offset(ejectionFreq, SplashData), "Frequency in which to emit splash rings.\n");
  118. addField("ejectionAngle", TypeF32, Offset(ejectionAngle, SplashData), "Rotational angle to create a splash ring.\n");
  119. addField("ringLifetime", TypeF32, Offset(ringLifetime, SplashData), "Lifetime, in milliseconds, for a splash ring.\n");
  120. addField("startRadius", TypeF32, Offset(startRadius, SplashData), "Starting radius size of a splash ring.\n");
  121. addField("explosion", TYPEID< ExplosionData >(), Offset(explosion, SplashData), "ExplosionData object to create at the creation position of this splash effect.\n");
  122. Parent::initPersistFields();
  123. }
  124. //--------------------------------------------------------------------------
  125. // On add - verify data settings
  126. //--------------------------------------------------------------------------
  127. bool SplashData::onAdd()
  128. {
  129. if (Parent::onAdd() == false)
  130. return false;
  131. return true;
  132. }
  133. //--------------------------------------------------------------------------
  134. // Pack data
  135. //--------------------------------------------------------------------------
  136. void SplashData::packData(BitStream* stream)
  137. {
  138. Parent::packData(stream);
  139. PACKDATA_ASSET(Sound);
  140. mathWrite(*stream, scale);
  141. stream->write(delayMS);
  142. stream->write(delayVariance);
  143. stream->write(lifetimeMS);
  144. stream->write(lifetimeVariance);
  145. stream->write(width);
  146. stream->write(numSegments);
  147. stream->write(velocity);
  148. stream->write(height);
  149. stream->write(acceleration);
  150. stream->write(texWrap);
  151. stream->write(texFactor);
  152. stream->write(ejectionFreq);
  153. stream->write(ejectionAngle);
  154. stream->write(ringLifetime);
  155. stream->write(startRadius);
  156. if( stream->writeFlag( explosion ) )
  157. {
  158. stream->writeRangedU32(explosion->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
  159. }
  160. S32 i;
  161. for( i=0; i<NUM_EMITTERS; i++ )
  162. {
  163. if( stream->writeFlag( emitterList[i] != NULL ) )
  164. {
  165. stream->writeRangedU32( emitterList[i]->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  166. }
  167. }
  168. for( i=0; i<NUM_TIME_KEYS; i++ )
  169. {
  170. stream->write( colors[i] );
  171. }
  172. for( i=0; i<NUM_TIME_KEYS; i++ )
  173. {
  174. stream->write( times[i] );
  175. }
  176. for( i=0; i<NUM_TEX; i++ )
  177. {
  178. PACKDATA_ASSET_ARRAY(Texture, i);
  179. }
  180. }
  181. //--------------------------------------------------------------------------
  182. // Unpack data
  183. //--------------------------------------------------------------------------
  184. void SplashData::unpackData(BitStream* stream)
  185. {
  186. Parent::unpackData(stream);
  187. UNPACKDATA_ASSET(Sound);
  188. mathRead(*stream, &scale);
  189. stream->read(&delayMS);
  190. stream->read(&delayVariance);
  191. stream->read(&lifetimeMS);
  192. stream->read(&lifetimeVariance);
  193. stream->read(&width);
  194. stream->read(&numSegments);
  195. stream->read(&velocity);
  196. stream->read(&height);
  197. stream->read(&acceleration);
  198. stream->read(&texWrap);
  199. stream->read(&texFactor);
  200. stream->read(&ejectionFreq);
  201. stream->read(&ejectionAngle);
  202. stream->read(&ringLifetime);
  203. stream->read(&startRadius);
  204. if( stream->readFlag() )
  205. {
  206. explosionId = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  207. }
  208. U32 i;
  209. for( i=0; i<NUM_EMITTERS; i++ )
  210. {
  211. if( stream->readFlag() )
  212. {
  213. emitterIDList[i] = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  214. }
  215. }
  216. for( i=0; i<NUM_TIME_KEYS; i++ )
  217. {
  218. stream->read( &colors[i] );
  219. }
  220. for( i=0; i<NUM_TIME_KEYS; i++ )
  221. {
  222. stream->read( &times[i] );
  223. }
  224. for( i=0; i<NUM_TEX; i++ )
  225. {
  226. UNPACKDATA_ASSET_ARRAY(Texture, i);
  227. }
  228. }
  229. //--------------------------------------------------------------------------
  230. // Preload data - load resources
  231. //--------------------------------------------------------------------------
  232. bool SplashData::preload(bool server, String &errorStr)
  233. {
  234. if (Parent::preload(server, errorStr) == false)
  235. return false;
  236. if (!server)
  237. {
  238. if (getSound() != StringTable->EmptyString())
  239. {
  240. _setSound(getSound());
  241. if(!getSoundProfile())
  242. Con::errorf(ConsoleLogEntry::General, "SplashData::preload: Cant get an sfxProfile for splash.");
  243. }
  244. S32 i;
  245. for( i=0; i<NUM_EMITTERS; i++ )
  246. {
  247. if( !emitterList[i] && emitterIDList[i] != 0 )
  248. {
  249. if( Sim::findObject( emitterIDList[i], emitterList[i] ) == false)
  250. {
  251. Con::errorf( ConsoleLogEntry::General, "SplashData::onAdd: Invalid packet, bad datablockId(particle emitter): 0x%x", emitterIDList[i] );
  252. }
  253. }
  254. }
  255. for( i=0; i<NUM_TEX; i++ )
  256. {
  257. if (mTexture[i].isNull())
  258. {
  259. _setTexture(getTexture(i), i);
  260. }
  261. }
  262. }
  263. if( !explosion && explosionId != 0 )
  264. {
  265. if( !Sim::findObject(explosionId, explosion) )
  266. {
  267. Con::errorf(ConsoleLogEntry::General, "SplashData::preload: Invalid packet, bad datablockId(explosion): %d", explosionId);
  268. }
  269. }
  270. return true;
  271. }
  272. //--------------------------------------------------------------------------
  273. // Splash
  274. //--------------------------------------------------------------------------
  275. Splash::Splash()
  276. : mDataBlock( NULL )
  277. {
  278. dMemset( mEmitterList, 0, sizeof( mEmitterList ) );
  279. mDelayMS = 0;
  280. mCurrMS = 0;
  281. mRandAngle = 0;
  282. mEndingMS = 1000;
  283. mActive = false;
  284. mRadius = 0.0;
  285. mVelocity = 1.0;
  286. mHeight = 0.0;
  287. mTimeSinceLastRing = 0.0;
  288. mDead = false;
  289. mElapsedTime = 0.0;
  290. mInitialNormal.set( 0.0, 0.0, 1.0 );
  291. mFade = 0;
  292. mFog = 0;
  293. // Only allocated client side.
  294. mNetFlags.set( IsGhost );
  295. }
  296. //--------------------------------------------------------------------------
  297. // Destructor
  298. //--------------------------------------------------------------------------
  299. Splash::~Splash()
  300. {
  301. }
  302. //--------------------------------------------------------------------------
  303. // Set initial state
  304. //--------------------------------------------------------------------------
  305. void Splash::setInitialState(const Point3F& point, const Point3F& normal, const F32 fade)
  306. {
  307. mInitialPosition = point;
  308. mInitialNormal = normal;
  309. mFade = fade;
  310. mFog = 0.0f;
  311. }
  312. //--------------------------------------------------------------------------
  313. // OnAdd
  314. //--------------------------------------------------------------------------
  315. bool Splash::onAdd()
  316. {
  317. // first check if we have a server connection, if we dont then this is on the server
  318. // and we should exit, then check if the parent fails to add the object
  319. NetConnection* conn = NetConnection::getConnectionToServer();
  320. if(!conn || !Parent::onAdd())
  321. return false;
  322. if( !mDataBlock )
  323. {
  324. Con::errorf("Splash::onAdd - Fail - No datablock");
  325. return false;
  326. }
  327. mDelayMS = mDataBlock->delayMS + sgRandom.randI( -mDataBlock->delayVariance, mDataBlock->delayVariance );
  328. mEndingMS = mDataBlock->lifetimeMS + sgRandom.randI( -mDataBlock->lifetimeVariance, mDataBlock->lifetimeVariance );
  329. mVelocity = mDataBlock->velocity;
  330. mHeight = mDataBlock->height;
  331. mTimeSinceLastRing = 1.0 / mDataBlock->ejectionFreq;
  332. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  333. {
  334. if( mDataBlock->emitterList[i] != NULL )
  335. {
  336. ParticleEmitter * pEmitter = new ParticleEmitter;
  337. pEmitter->onNewDataBlock( mDataBlock->emitterList[i], false );
  338. if( !pEmitter->registerObject() )
  339. {
  340. Con::warnf( ConsoleLogEntry::General, "Could not register emitter for particle of class: %s", mDataBlock->getName() );
  341. delete pEmitter;
  342. pEmitter = NULL;
  343. }
  344. mEmitterList[i] = pEmitter;
  345. }
  346. }
  347. spawnExplosion();
  348. mObjBox.minExtents = Point3F( -1, -1, -1 );
  349. mObjBox.maxExtents = Point3F( 1, 1, 1 );
  350. resetWorldBox();
  351. gClientSceneGraph->addObjectToScene(this);
  352. removeFromProcessList();
  353. ClientProcessList::get()->addObject(this);
  354. conn->addObject(this);
  355. return true;
  356. }
  357. //--------------------------------------------------------------------------
  358. // OnRemove
  359. //--------------------------------------------------------------------------
  360. void Splash::onRemove()
  361. {
  362. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  363. {
  364. if( mEmitterList[i] )
  365. {
  366. mEmitterList[i]->deleteWhenEmpty();
  367. mEmitterList[i] = NULL;
  368. }
  369. }
  370. ringList.clear();
  371. removeFromScene();
  372. Parent::onRemove();
  373. }
  374. //--------------------------------------------------------------------------
  375. // On New Data Block
  376. //--------------------------------------------------------------------------
  377. bool Splash::onNewDataBlock( GameBaseData *dptr, bool reload )
  378. {
  379. mDataBlock = dynamic_cast<SplashData*>(dptr);
  380. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  381. return false;
  382. scriptOnNewDataBlock();
  383. return true;
  384. }
  385. //--------------------------------------------------------------------------
  386. // Process tick
  387. //--------------------------------------------------------------------------
  388. void Splash::processTick(const Move*)
  389. {
  390. mCurrMS += TickMs;
  391. if( isServerObject() )
  392. {
  393. if( mCurrMS >= mEndingMS )
  394. {
  395. mDead = true;
  396. if( mCurrMS >= (mEndingMS + mDataBlock->ringLifetime * 1000) )
  397. {
  398. deleteObject();
  399. }
  400. }
  401. }
  402. else
  403. {
  404. if( mCurrMS >= mEndingMS )
  405. {
  406. mDead = true;
  407. deleteObject();
  408. }
  409. }
  410. }
  411. //--------------------------------------------------------------------------
  412. // Advance time
  413. //--------------------------------------------------------------------------
  414. void Splash::advanceTime(F32 dt)
  415. {
  416. if (dt == 0.0)
  417. return;
  418. mElapsedTime += dt;
  419. updateColor();
  420. updateWave( dt );
  421. updateEmitters( dt );
  422. updateRings( dt );
  423. if( !mDead )
  424. {
  425. emitRings( dt );
  426. }
  427. }
  428. //----------------------------------------------------------------------------
  429. // Update emitters
  430. //----------------------------------------------------------------------------
  431. void Splash::updateEmitters( F32 dt )
  432. {
  433. Point3F pos = getPosition();
  434. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  435. {
  436. if( mEmitterList[i] )
  437. {
  438. mEmitterList[i]->emitParticles( pos, pos, mInitialNormal, Point3F( 0.0, 0.0, 0.0 ), (S32) (dt * 1000) );
  439. }
  440. }
  441. }
  442. //----------------------------------------------------------------------------
  443. // Update wave
  444. //----------------------------------------------------------------------------
  445. void Splash::updateWave( F32 dt )
  446. {
  447. mVelocity += mDataBlock->acceleration * dt;
  448. mRadius += mVelocity * dt;
  449. }
  450. //----------------------------------------------------------------------------
  451. // Update color
  452. //----------------------------------------------------------------------------
  453. void Splash::updateColor()
  454. {
  455. for(SplashRingList::Iterator ring = ringList.begin(); ring != ringList.end(); ++ring)
  456. {
  457. F32 t = F32(ring->elapsedTime) / F32(ring->lifetime);
  458. for( U32 i = 1; i < SplashData::NUM_TIME_KEYS; i++ )
  459. {
  460. if( mDataBlock->times[i] >= t )
  461. {
  462. F32 firstPart = t - mDataBlock->times[i-1];
  463. F32 total = (mDataBlock->times[i] -
  464. mDataBlock->times[i-1]);
  465. firstPart /= total;
  466. ring->color.interpolate( mDataBlock->colors[i-1],
  467. mDataBlock->colors[i],
  468. firstPart);
  469. break;
  470. }
  471. }
  472. }
  473. }
  474. //----------------------------------------------------------------------------
  475. // Create ring
  476. //----------------------------------------------------------------------------
  477. SplashRing Splash::createRing()
  478. {
  479. SplashRing ring;
  480. U32 numPoints = mDataBlock->numSegments + 1;
  481. Point3F ejectionAxis( 0.0, 0.0, 1.0 );
  482. Point3F axisx;
  483. if (mFabs(ejectionAxis.z) < 0.999f)
  484. mCross(ejectionAxis, Point3F(0, 0, 1), &axisx);
  485. else
  486. mCross(ejectionAxis, Point3F(0, 1, 0), &axisx);
  487. axisx.normalize();
  488. for( U32 i=0; i<numPoints; i++ )
  489. {
  490. F32 t = F32(i) / F32(numPoints);
  491. AngAxisF thetaRot( axisx, mDataBlock->ejectionAngle * (M_PI / 180.0));
  492. AngAxisF phiRot( ejectionAxis, t * (M_PI * 2.0));
  493. Point3F pointAxis = ejectionAxis;
  494. MatrixF temp;
  495. thetaRot.setMatrix(&temp);
  496. temp.mulP(pointAxis);
  497. phiRot.setMatrix(&temp);
  498. temp.mulP(pointAxis);
  499. Point3F startOffset = axisx;
  500. temp.mulV( startOffset );
  501. startOffset *= mDataBlock->startRadius;
  502. SplashRingPoint point;
  503. point.position = getPosition() + startOffset;
  504. point.velocity = pointAxis * mDataBlock->velocity;
  505. ring.points.push_back( point );
  506. }
  507. ring.color = mDataBlock->colors[0];
  508. ring.lifetime = mDataBlock->ringLifetime;
  509. ring.elapsedTime = 0.0;
  510. ring.v = mDataBlock->texFactor * mFmod( mElapsedTime, 1.0 );
  511. return ring;
  512. }
  513. //----------------------------------------------------------------------------
  514. // Emit rings
  515. //----------------------------------------------------------------------------
  516. void Splash::emitRings( F32 dt )
  517. {
  518. mTimeSinceLastRing += dt;
  519. S32 numNewRings = (S32) (mTimeSinceLastRing * F32(mDataBlock->ejectionFreq));
  520. mTimeSinceLastRing -= numNewRings / mDataBlock->ejectionFreq;
  521. for( S32 i=numNewRings-1; i>=0; i-- )
  522. {
  523. F32 t = F32(i) / F32(numNewRings);
  524. t *= dt;
  525. t += mTimeSinceLastRing;
  526. SplashRing ring = createRing();
  527. updateRing( ring, t );
  528. ringList.pushBack( ring );
  529. }
  530. }
  531. //----------------------------------------------------------------------------
  532. // Update rings
  533. //----------------------------------------------------------------------------
  534. void Splash::updateRings( F32 dt )
  535. {
  536. SplashRingList::Iterator ring;
  537. for(SplashRingList::Iterator i = ringList.begin(); i != ringList.end(); /*Trickiness*/)
  538. {
  539. ring = i++;
  540. ring->elapsedTime += dt;
  541. if( !ring->isActive() )
  542. {
  543. ringList.erase( ring );
  544. }
  545. else
  546. {
  547. updateRing( *ring, dt );
  548. }
  549. }
  550. }
  551. //----------------------------------------------------------------------------
  552. // Update ring
  553. //----------------------------------------------------------------------------
  554. void Splash::updateRing( SplashRing& ring, F32 dt )
  555. {
  556. for( U32 i=0; i<ring.points.size(); i++ )
  557. {
  558. if( mDead )
  559. {
  560. Point3F vel = ring.points[i].velocity;
  561. vel.normalize();
  562. vel *= mDataBlock->acceleration;
  563. ring.points[i].velocity += vel * dt;
  564. }
  565. ring.points[i].velocity += Point3F( 0.0f, 0.0f, -9.8f ) * dt;
  566. ring.points[i].position += ring.points[i].velocity * dt;
  567. }
  568. }
  569. //----------------------------------------------------------------------------
  570. // Explode
  571. //----------------------------------------------------------------------------
  572. void Splash::spawnExplosion()
  573. {
  574. if( !mDataBlock->explosion ) return;
  575. /// could just play the explosion one, but explosion could be weapon specific,
  576. /// splash sound could be liquid specific. food for thought.
  577. SFXProfile* sound_prof = mDataBlock->getSoundProfile();
  578. if (sound_prof)
  579. {
  580. SFX->playOnce(sound_prof, &getTransform());
  581. }
  582. Explosion* pExplosion = new Explosion;
  583. pExplosion->onNewDataBlock(mDataBlock->explosion, false);
  584. MatrixF trans = getTransform();
  585. trans.setPosition( getPosition() );
  586. pExplosion->setTransform( trans );
  587. pExplosion->setInitialState( trans.getPosition(), VectorF(0,0,1), 1);
  588. if (!pExplosion->registerObject())
  589. delete pExplosion;
  590. }
  591. //--------------------------------------------------------------------------
  592. // packUpdate
  593. //--------------------------------------------------------------------------
  594. U32 Splash::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  595. {
  596. U32 retMask = Parent::packUpdate(con, mask, stream);
  597. if( stream->writeFlag(mask & GameBase::InitialUpdateMask) )
  598. {
  599. mathWrite(*stream, mInitialPosition);
  600. }
  601. return retMask;
  602. }
  603. //--------------------------------------------------------------------------
  604. // unpackUpdate
  605. //--------------------------------------------------------------------------
  606. void Splash::unpackUpdate(NetConnection* con, BitStream* stream)
  607. {
  608. Parent::unpackUpdate(con, stream);
  609. if( stream->readFlag() )
  610. {
  611. mathRead(*stream, &mInitialPosition);
  612. setPosition( mInitialPosition );
  613. }
  614. }