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