splash.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. addFieldV("delayMS", TypeRangedS32, Offset(delayMS, SplashData), &CommonValidators::PositiveInt, "Time to delay, in milliseconds, before actually starting this effect.\n");
  104. addFieldV("delayVariance", TypeRangedS32, Offset(delayVariance, SplashData), &CommonValidators::PositiveInt, "Time variance for delayMS.\n");
  105. addFieldV("lifetimeMS", TypeRangedS32, Offset(lifetimeMS, SplashData), &CommonValidators::PositiveInt, "Lifetime for this effect, in milliseconds.\n");
  106. addFieldV("lifetimeVariance", TypeRangedS32, Offset(lifetimeVariance, SplashData), &CommonValidators::PositiveInt, "Time variance for lifetimeMS.\n");
  107. addFieldV("width", TypeRangedF32, Offset(width, SplashData), &CommonValidators::PositiveFloat, "Width for the X and Y coordinates to create this effect within.");
  108. addFieldV("numSegments", TypeRangedS32, Offset(numSegments, SplashData), &CommonValidators::NaturalNumber, "Number of ejection points in the splash ring.\n");
  109. addFieldV("velocity", TypeRangedF32, Offset(velocity, SplashData), &CommonValidators::PositiveFloat, "Velocity for the splash effect to travel.\n");
  110. addFieldV("height", TypeRangedF32, Offset(height, SplashData), &CommonValidators::PositiveFloat, "Height for the splash to reach.\n");
  111. addFieldV("acceleration", TypeRangedF32, Offset(acceleration, SplashData), &CommonValidators::PositiveFloat, "Constant acceleration value to place upon the splash effect.\n");
  112. addFieldV("times", TypeRangedF32, Offset(times, SplashData), &CommonValidators::NormalizedFloat, 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. addFieldV("texWrap", TypeRangedF32, Offset(texWrap, SplashData), &CommonValidators::NormalizedFloat, "Amount to wrap the texture around the splash ring, 0.0f - 1.0f.\n");
  116. addFieldV("texFactor", TypeRangedF32, Offset(texFactor, SplashData), &CommonValidators::NormalizedFloat, "Factor in which to apply the texture to the splash ring, 0.0f - 1.0f.\n");
  117. addFieldV("ejectionFreq", TypeRangedF32, Offset(ejectionFreq, SplashData), &CommonValidators::PositiveFloat, "Frequency in which to emit splash rings.\n");
  118. addFieldV("ejectionAngle", TypeRangedF32, Offset(ejectionAngle, SplashData), &CommonValidators::DegreeRange, "Rotational angle to create a splash ring.\n");
  119. addFieldV("ringLifetime", TypeRangedF32, Offset(ringLifetime, SplashData), &CommonValidators::PositiveFloat, "Lifetime, in milliseconds, for a splash ring.\n");
  120. addFieldV("startRadius", TypeRangedF32, Offset(startRadius, SplashData), &CommonValidators::PositiveFloat, "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 (!isSoundValid())
  239. {
  240. Con::errorf(ConsoleLogEntry::General, "SplashData::preload: Invalid Sound asset.");
  241. //return false;
  242. }
  243. S32 i;
  244. for( i=0; i<NUM_EMITTERS; i++ )
  245. {
  246. if( !emitterList[i] && emitterIDList[i] != 0 )
  247. {
  248. if( Sim::findObject( emitterIDList[i], emitterList[i] ) == false)
  249. {
  250. Con::errorf( ConsoleLogEntry::General, "SplashData::onAdd: Invalid packet, bad datablockId(particle emitter): 0x%x", emitterIDList[i] );
  251. }
  252. }
  253. }
  254. for( i=0; i<NUM_TEX; i++ )
  255. {
  256. if (mTexture[i].isNull())
  257. {
  258. _setTexture(getTexture(i), i);
  259. }
  260. }
  261. }
  262. if( !explosion && explosionId != 0 )
  263. {
  264. if( !Sim::findObject(explosionId, explosion) )
  265. {
  266. Con::errorf(ConsoleLogEntry::General, "SplashData::preload: Invalid packet, bad datablockId(explosion): %d", explosionId);
  267. }
  268. }
  269. return true;
  270. }
  271. //--------------------------------------------------------------------------
  272. // Splash
  273. //--------------------------------------------------------------------------
  274. Splash::Splash()
  275. : mDataBlock( NULL )
  276. {
  277. dMemset( mEmitterList, 0, sizeof( mEmitterList ) );
  278. mDelayMS = 0;
  279. mCurrMS = 0;
  280. mRandAngle = 0;
  281. mEndingMS = 1000;
  282. mActive = false;
  283. mRadius = 0.0;
  284. mVelocity = 1.0;
  285. mHeight = 0.0;
  286. mTimeSinceLastRing = 0.0;
  287. mDead = false;
  288. mElapsedTime = 0.0;
  289. mInitialNormal.set( 0.0, 0.0, 1.0 );
  290. mFade = 0;
  291. mFog = 0;
  292. // Only allocated client side.
  293. mNetFlags.set( IsGhost );
  294. }
  295. //--------------------------------------------------------------------------
  296. // Destructor
  297. //--------------------------------------------------------------------------
  298. Splash::~Splash()
  299. {
  300. }
  301. //--------------------------------------------------------------------------
  302. // Set initial state
  303. //--------------------------------------------------------------------------
  304. void Splash::setInitialState(const Point3F& point, const Point3F& normal, const F32 fade)
  305. {
  306. mInitialPosition = point;
  307. mInitialNormal = normal;
  308. mFade = fade;
  309. mFog = 0.0f;
  310. }
  311. //--------------------------------------------------------------------------
  312. // OnAdd
  313. //--------------------------------------------------------------------------
  314. bool Splash::onAdd()
  315. {
  316. // first check if we have a server connection, if we dont then this is on the server
  317. // and we should exit, then check if the parent fails to add the object
  318. NetConnection* conn = NetConnection::getConnectionToServer();
  319. if(!conn || !Parent::onAdd())
  320. return false;
  321. if( !mDataBlock )
  322. {
  323. Con::errorf("Splash::onAdd - Fail - No datablock");
  324. return false;
  325. }
  326. mDelayMS = mDataBlock->delayMS + sgRandom.randI( -mDataBlock->delayVariance, mDataBlock->delayVariance );
  327. mEndingMS = mDataBlock->lifetimeMS + sgRandom.randI( -mDataBlock->lifetimeVariance, mDataBlock->lifetimeVariance );
  328. mVelocity = mDataBlock->velocity;
  329. mHeight = mDataBlock->height;
  330. mTimeSinceLastRing = 1.0 / mDataBlock->ejectionFreq;
  331. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  332. {
  333. if( mDataBlock->emitterList[i] != NULL )
  334. {
  335. ParticleEmitter * pEmitter = new ParticleEmitter;
  336. pEmitter->onNewDataBlock( mDataBlock->emitterList[i], false );
  337. if( !pEmitter->registerObject() )
  338. {
  339. Con::warnf( ConsoleLogEntry::General, "Could not register emitter for particle of class: %s", mDataBlock->getName() );
  340. delete pEmitter;
  341. pEmitter = NULL;
  342. }
  343. mEmitterList[i] = pEmitter;
  344. }
  345. }
  346. spawnExplosion();
  347. mObjBox.minExtents = Point3F( -1, -1, -1 );
  348. mObjBox.maxExtents = Point3F( 1, 1, 1 );
  349. resetWorldBox();
  350. gClientSceneGraph->addObjectToScene(this);
  351. removeFromProcessList();
  352. ClientProcessList::get()->addObject(this);
  353. conn->addObject(this);
  354. return true;
  355. }
  356. //--------------------------------------------------------------------------
  357. // OnRemove
  358. //--------------------------------------------------------------------------
  359. void Splash::onRemove()
  360. {
  361. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  362. {
  363. if( mEmitterList[i] )
  364. {
  365. mEmitterList[i]->deleteWhenEmpty();
  366. mEmitterList[i] = NULL;
  367. }
  368. }
  369. ringList.clear();
  370. removeFromScene();
  371. Parent::onRemove();
  372. }
  373. //--------------------------------------------------------------------------
  374. // On New Data Block
  375. //--------------------------------------------------------------------------
  376. bool Splash::onNewDataBlock( GameBaseData *dptr, bool reload )
  377. {
  378. mDataBlock = dynamic_cast<SplashData*>(dptr);
  379. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  380. return false;
  381. scriptOnNewDataBlock();
  382. return true;
  383. }
  384. //--------------------------------------------------------------------------
  385. // Process tick
  386. //--------------------------------------------------------------------------
  387. void Splash::processTick(const Move*)
  388. {
  389. mCurrMS += TickMs;
  390. if( isServerObject() )
  391. {
  392. if( mCurrMS >= mEndingMS )
  393. {
  394. mDead = true;
  395. if( mCurrMS >= (mEndingMS + mDataBlock->ringLifetime * 1000) )
  396. {
  397. deleteObject();
  398. }
  399. }
  400. }
  401. else
  402. {
  403. if( mCurrMS >= mEndingMS )
  404. {
  405. mDead = true;
  406. deleteObject();
  407. }
  408. }
  409. }
  410. //--------------------------------------------------------------------------
  411. // Advance time
  412. //--------------------------------------------------------------------------
  413. void Splash::advanceTime(F32 dt)
  414. {
  415. if (dt == 0.0)
  416. return;
  417. mElapsedTime += dt;
  418. updateColor();
  419. updateWave( dt );
  420. updateEmitters( dt );
  421. updateRings( dt );
  422. if( !mDead )
  423. {
  424. emitRings( dt );
  425. }
  426. }
  427. //----------------------------------------------------------------------------
  428. // Update emitters
  429. //----------------------------------------------------------------------------
  430. void Splash::updateEmitters( F32 dt )
  431. {
  432. Point3F pos = getPosition();
  433. for( U32 i=0; i<SplashData::NUM_EMITTERS; i++ )
  434. {
  435. if( mEmitterList[i] )
  436. {
  437. mEmitterList[i]->emitParticles( pos, pos, mInitialNormal, Point3F( 0.0, 0.0, 0.0 ), (S32) (dt * 1000) );
  438. }
  439. }
  440. }
  441. //----------------------------------------------------------------------------
  442. // Update wave
  443. //----------------------------------------------------------------------------
  444. void Splash::updateWave( F32 dt )
  445. {
  446. mVelocity += mDataBlock->acceleration * dt;
  447. mRadius += mVelocity * dt;
  448. }
  449. //----------------------------------------------------------------------------
  450. // Update color
  451. //----------------------------------------------------------------------------
  452. void Splash::updateColor()
  453. {
  454. for(SplashRingList::Iterator ring = ringList.begin(); ring != ringList.end(); ++ring)
  455. {
  456. F32 t = F32(ring->elapsedTime) / F32(ring->lifetime);
  457. for( U32 i = 1; i < SplashData::NUM_TIME_KEYS; i++ )
  458. {
  459. if( mDataBlock->times[i] >= t )
  460. {
  461. F32 firstPart = t - mDataBlock->times[i-1];
  462. F32 total = (mDataBlock->times[i] -
  463. mDataBlock->times[i-1]);
  464. firstPart /= total;
  465. ring->color.interpolate( mDataBlock->colors[i-1],
  466. mDataBlock->colors[i],
  467. firstPart);
  468. break;
  469. }
  470. }
  471. }
  472. }
  473. //----------------------------------------------------------------------------
  474. // Create ring
  475. //----------------------------------------------------------------------------
  476. SplashRing Splash::createRing()
  477. {
  478. SplashRing ring;
  479. U32 numPoints = mDataBlock->numSegments + 1;
  480. Point3F ejectionAxis( 0.0, 0.0, 1.0 );
  481. Point3F axisx;
  482. if (mFabs(ejectionAxis.z) < 0.999f)
  483. mCross(ejectionAxis, Point3F(0, 0, 1), &axisx);
  484. else
  485. mCross(ejectionAxis, Point3F(0, 1, 0), &axisx);
  486. axisx.normalize();
  487. for( U32 i=0; i<numPoints; i++ )
  488. {
  489. F32 t = F32(i) / F32(numPoints);
  490. AngAxisF thetaRot( axisx, mDataBlock->ejectionAngle * (M_PI / 180.0));
  491. AngAxisF phiRot( ejectionAxis, t * (M_PI * 2.0));
  492. Point3F pointAxis = ejectionAxis;
  493. MatrixF temp;
  494. thetaRot.setMatrix(&temp);
  495. temp.mulP(pointAxis);
  496. phiRot.setMatrix(&temp);
  497. temp.mulP(pointAxis);
  498. Point3F startOffset = axisx;
  499. temp.mulV( startOffset );
  500. startOffset *= mDataBlock->startRadius;
  501. SplashRingPoint point;
  502. point.position = getPosition() + startOffset;
  503. point.velocity = pointAxis * mDataBlock->velocity;
  504. ring.points.push_back( point );
  505. }
  506. ring.color = mDataBlock->colors[0];
  507. ring.lifetime = mDataBlock->ringLifetime;
  508. ring.elapsedTime = 0.0;
  509. ring.v = mDataBlock->texFactor * mFmod( mElapsedTime, 1.0 );
  510. return ring;
  511. }
  512. //----------------------------------------------------------------------------
  513. // Emit rings
  514. //----------------------------------------------------------------------------
  515. void Splash::emitRings( F32 dt )
  516. {
  517. mTimeSinceLastRing += dt;
  518. S32 numNewRings = (S32) (mTimeSinceLastRing * F32(mDataBlock->ejectionFreq));
  519. mTimeSinceLastRing -= numNewRings / mDataBlock->ejectionFreq;
  520. for( S32 i=numNewRings-1; i>=0; i-- )
  521. {
  522. F32 t = F32(i) / F32(numNewRings);
  523. t *= dt;
  524. t += mTimeSinceLastRing;
  525. SplashRing ring = createRing();
  526. updateRing( ring, t );
  527. ringList.pushBack( ring );
  528. }
  529. }
  530. //----------------------------------------------------------------------------
  531. // Update rings
  532. //----------------------------------------------------------------------------
  533. void Splash::updateRings( F32 dt )
  534. {
  535. SplashRingList::Iterator ring;
  536. for(SplashRingList::Iterator i = ringList.begin(); i != ringList.end(); /*Trickiness*/)
  537. {
  538. ring = i++;
  539. ring->elapsedTime += dt;
  540. if( !ring->isActive() )
  541. {
  542. ringList.erase( ring );
  543. }
  544. else
  545. {
  546. updateRing( *ring, dt );
  547. }
  548. }
  549. }
  550. //----------------------------------------------------------------------------
  551. // Update ring
  552. //----------------------------------------------------------------------------
  553. void Splash::updateRing( SplashRing& ring, F32 dt )
  554. {
  555. for( U32 i=0; i<ring.points.size(); i++ )
  556. {
  557. if( mDead )
  558. {
  559. Point3F vel = ring.points[i].velocity;
  560. vel.normalize();
  561. vel *= mDataBlock->acceleration;
  562. ring.points[i].velocity += vel * dt;
  563. }
  564. ring.points[i].velocity += Point3F( 0.0f, 0.0f, -9.8f ) * dt;
  565. ring.points[i].position += ring.points[i].velocity * dt;
  566. }
  567. }
  568. //----------------------------------------------------------------------------
  569. // Explode
  570. //----------------------------------------------------------------------------
  571. void Splash::spawnExplosion()
  572. {
  573. if( !mDataBlock->explosion ) return;
  574. /// could just play the explosion one, but explosion could be weapon specific,
  575. /// splash sound could be liquid specific. food for thought.
  576. SFXTrack* sound_prof = mDataBlock->getSoundProfile();
  577. if (sound_prof)
  578. {
  579. SFX->playOnce(sound_prof, &getTransform());
  580. }
  581. Explosion* pExplosion = new Explosion;
  582. pExplosion->onNewDataBlock(mDataBlock->explosion, false);
  583. MatrixF trans = getTransform();
  584. trans.setPosition( getPosition() );
  585. pExplosion->setTransform( trans );
  586. pExplosion->setInitialState( trans.getPosition(), VectorF(0,0,1), 1);
  587. if (!pExplosion->registerObject())
  588. delete pExplosion;
  589. }
  590. //--------------------------------------------------------------------------
  591. // packUpdate
  592. //--------------------------------------------------------------------------
  593. U32 Splash::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  594. {
  595. U32 retMask = Parent::packUpdate(con, mask, stream);
  596. if( stream->writeFlag(mask & GameBase::InitialUpdateMask) )
  597. {
  598. mathWrite(*stream, mInitialPosition);
  599. }
  600. return retMask;
  601. }
  602. //--------------------------------------------------------------------------
  603. // unpackUpdate
  604. //--------------------------------------------------------------------------
  605. void Splash::unpackUpdate(NetConnection* con, BitStream* stream)
  606. {
  607. Parent::unpackUpdate(con, stream);
  608. if( stream->readFlag() )
  609. {
  610. mathRead(*stream, &mInitialPosition);
  611. setPosition( mInitialPosition );
  612. }
  613. }