00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "gExplosion.h"
00029 #include "rModel.h"
00030 #include "rRender.h"
00031 #include "tInitExit.h"
00032 #include "gWall.h"
00033 #include "gCycle.h"
00034 #include "eGrid.h"
00035 #include "tRandom.h"
00036 #include "tMath.h"
00037
00038 #include "eSoundMixer.h"
00039 #ifdef USEPARTICLES
00040 #include "papi.h"
00041 #endif
00042
00043 static tList< gExplosion > sg_Explosions;
00044
00045 static void clamp01(REAL &c)
00046 {
00047 if (!finite(c))
00048 c = 0.5;
00049
00050 if (c<0)
00051 c = 0;
00052
00053 if (c>1)
00054 c = 1;
00055 }
00056
00057 int gExplosion::expansionSteps=5;
00058 REAL gExplosion::expansionTime = 0.2f;
00059
00060 static eCoord s_explosionCoord;
00061 static REAL s_explosionRadius;
00062 static REAL s_explosionTime;
00063 static gExplosion * s_holer = 0;
00064
00065
00066 static void S_BlowHoles( eWall * w )
00067 {
00068
00069 eCoord normal = w->Vec().Conj();
00070 normal.Normalize();
00071
00072 eCoord Pos1 = normal.Turn( w->EndPoint(0) - s_explosionCoord );
00073 eCoord Pos2 = normal.Turn( w->EndPoint(1) - s_explosionCoord );
00074
00075 tASSERT( fabs( Pos1.y - Pos2.y ) <= fabs( Pos1.y + Pos2.y + .1f ) * .1f );
00076
00077 REAL alpha = .5f;
00078 if ( Pos1.x != Pos2.x)
00079 alpha = Pos1.x / ( Pos1.x - Pos2.x );
00080
00081 REAL radius = s_explosionRadius * s_explosionRadius - Pos1.y * Pos2.y;
00082
00083
00084 if ( radius < 0 )
00085 return;
00086
00087 radius = sqrt( radius );
00088
00089
00090 gPlayerWall* wall = dynamic_cast<gPlayerWall*>( w );
00091 if ( ! wall )
00092 return;
00093
00094 REAL closestPos = wall->Pos( alpha );
00095
00096 REAL start = closestPos - radius;
00097 REAL end = closestPos + radius;
00098
00099
00100 REAL wallEnd = wall->EndPos();
00101 REAL wallBeg = wall->BegPos();
00102 if ( wallEnd <= wallBeg )
00103 {
00104 return;
00105 }
00106 REAL endAlpha = ( end - wallBeg ) / ( wallEnd - wallBeg );
00107
00108 clamp01( endAlpha );
00109 REAL endHoleTime = wall->Time( endAlpha );
00110 if ( endHoleTime > s_explosionTime )
00111 {
00112 REAL begTime = wall->BegTime();
00113 REAL endTime = wall->EndTime();
00114 REAL timeNorm = endTime - begTime;
00115 if ( timeNorm != 0.0f )
00116 {
00117 endAlpha = ( s_explosionTime - begTime ) / timeNorm;
00118 end = wall->Pos( endAlpha );
00119 }
00120 }
00121
00122 if ( end > start )
00123 {
00124 wall->BlowHole ( start, end, s_holer );
00125 }
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 gExplosion::gExplosion(eGrid *grid, const eCoord &pos,REAL time, gRealColor& color, gCycle * owner )
00142 :eReferencableGameObject(grid, pos, eCoord(0,0), NULL, true),
00143 createTime(time),
00144 expansion(0),
00145 listID(-1),
00146 owner_(owner)
00147 {
00148 eSoundMixer* mixer = eSoundMixer::GetMixer();
00149 mixer->PushButton(CYCLE_EXPLOSION, pos);
00150
00151 holeAccountedFor_ = false;
00152
00153 lastTime = time;
00154 explosion_r = color.r;
00155 explosion_g = color.g;
00156 explosion_b = color.b;
00157
00158 sg_Explosions.Add( this, listID );
00159 z=0;
00160
00161 #ifdef USEPARTICLES
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 #endif
00173
00174
00175 AddToList();
00176
00177 }
00178
00179 gExplosion::~gExplosion(){
00180 sg_Explosions.Remove( this, listID );
00181
00182 if ( s_explosionRadius > 0 )
00183 {
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 }
00194 }
00195
00196
00197
00198
00199 bool gExplosion::Timestep(REAL currentTime){
00200 lastTime=currentTime;
00202
00203 int currentExpansion = int(REAL( expansionSteps )*((currentTime - createTime)/expansionTime)) + 1;
00204 if ( currentExpansion > expansionSteps )
00205 currentExpansion = expansionSteps;
00206
00207 if ( currentExpansion > expansion )
00208 {
00209 expansion = currentExpansion;
00210
00211 s_explosionCoord = pos;
00212 REAL factor = expansion / REAL( expansionSteps );
00213 s_explosionRadius = gCycle::ExplosionRadius() * sqrt(factor);
00214 s_explosionTime = currentTime;
00215 s_holer = this;
00216
00217 if ( s_explosionRadius > 0 && (currentTime < createTime+4) )
00218 {
00219 grid->ProcessWallsInRange( &S_BlowHoles,
00220 s_explosionCoord,
00221 s_explosionRadius,
00222 this->CurrentFace() );
00223 }
00224
00225 s_holer = 0;
00226 }
00227 #ifndef USEPARTICLES
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 if (currentTime>createTime+4)
00260 return true;
00261 else
00262 return false;
00263 #else
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 if (currentTime>createTime+4)
00290 return true;
00291 else
00292 return false;
00293 #endif
00294 }
00295
00296 void gExplosion::InteractWith(eGameObject *,REAL ,int){}
00297
00298 void gExplosion::PassEdge(const eWall *,REAL ,REAL ,int){}
00299
00300 void gExplosion::Kill(){
00301 createTime=lastTime-100;
00302 }
00303
00304 bool gExplosion::AccountForHole()
00305 {
00306 bool ret = !holeAccountedFor_;
00307 holeAccountedFor_ = true;
00308 return ret;
00309 }
00310
00311 void gExplosion::OnNewWall( eWall* w )
00312 {
00313 for ( int i = sg_Explosions.Len() - 1; i>=0; --i )
00314 {
00315 const gExplosion* e = sg_Explosions(i);
00316 if ( e->expansion >= expansionSteps )
00317 {
00318
00319 s_explosionCoord = e->pos;
00320 s_explosionRadius = gCycle::ExplosionRadius();
00321 s_explosionTime = e->createTime;
00322
00323 S_BlowHoles( w );
00324 }
00325 }
00326 }
00327
00328 static tArray<Vec3> expvec;
00329
00330 static void init_exp(){
00331 int i=0;
00332 expvec[i++]=Vec3(0,0,1);
00333 expvec[i++]=Vec3(0,1,1);
00334 expvec[i++]=Vec3(0,-1,1);
00335 expvec[i++]=Vec3(1,0,1);
00336 expvec[i++]=Vec3(-1,0,1);
00337 expvec[i++]=Vec3(1,1,1);
00338 expvec[i++]=Vec3(-1,1,1);
00339 expvec[i++]=Vec3(1,-1,1);
00340 expvec[i++]=Vec3(-1,-1,1);
00341
00342 const REAL fak=7;
00343
00344 tRandomizer & randomizer = tRandomizer::GetInstance();
00345
00346 for (int j=i;j<40;j++){
00347 expvec[i++]=Vec3(fak*( randomizer.Get() -.5f ),
00348 fak*( randomizer.Get() -.5f ),
00349 1);
00350
00351
00352
00353 }
00354
00355 for (int k=expvec.Len()-1;k>=0;k--)
00356 expvec[k]=expvec[k]*(1/expvec[k].Norm());
00357 }
00358
00359 static tInitExit ie_exp(&init_exp);
00360
00361 bool sg_crashExplosion = true;
00362 bool sg_crashExplosionHud = true;
00363
00364 #ifndef DEDICATED
00365 void gExplosion::Render(const eCamera *cam){
00366
00367 #ifdef USEPARTICLES
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 #else
00394 if (sg_crashExplosion){
00395 REAL a1=(lastTime-createTime)+.01f;
00396 REAL e=a1-1;
00397
00398 if (e<0) e=0;
00399
00400 REAL fade=(2-a1);
00401 if (fade<0) fade=0;
00402 if (fade>1) fade=1;
00403
00404 a1*=100;
00405 e*=100;
00406
00407 ModelMatrix();
00408 glPushMatrix();
00409 glTranslatef(pos.x,pos.y,0);
00410
00411
00412 glDisable(GL_TEXTURE_2D);
00413
00414 glColor4f(explosion_r,explosion_g,explosion_b,fade);
00415 BeginLines();
00416 for (int i=expvec.Len()-1;i>=0;i--){
00417 glVertex3f(a1*expvec[i].x[0],a1*expvec[i].x[1],a1*expvec[i].x[2]);
00418 glVertex3f( e*expvec[i].x[0], e*expvec[i].x[1], e*expvec[i].x[2]);
00419 }
00420 RenderEnd();
00421 glPopMatrix();
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 #endif
00448
00449 }
00450
00451 void gExplosion::Render2D(tCoord scale) const {
00452 #ifndef DEDICATED
00453 if(sg_crashExplosionHud){
00454 REAL a1=(lastTime-createTime)+.01f;
00455 REAL e=a1-1;
00456
00457 if (e<0) e=0;
00458
00459 REAL fade=(2-a1);
00460 if (fade<0) fade=0;
00461 if (fade>1) fade=1;
00462
00463 a1*=100;
00464 e*=100;
00465
00466 ModelMatrix();
00467 glPushMatrix();
00468 glTranslatef(pos.x,pos.y,0);
00469
00470 glDisable(GL_TEXTURE_2D);
00471
00472 glColor4f(explosion_r,explosion_g,explosion_b,fade);
00473 BeginLines();
00474 for(int i=expvec.Len()-1;i>=0;i--){
00475 glVertex2f(a1*expvec[i].x[0],a1*expvec[i].x[1]);
00476 glVertex2f( e*expvec[i].x[0], e*expvec[i].x[1]);
00477 }
00478 RenderEnd();
00479 glPopMatrix();
00480 }
00481 #endif
00482
00483 }
00484
00485 #if 0
00486 void gExplosion::SoundMix(Uint8 *dest,unsigned int len,
00487 int viewer,REAL rvol,REAL lvol){
00488 #ifndef HAVE_LaIBSDL_MIXER
00489 sound.Mix(dest,len,viewer,rvol*4,lvol*4);
00490 #endif
00491 }
00492 #endif
00493
00494 #endif
00495
00496 void gExplosion::OnRemoveFromGame()
00497 {
00498
00499 sg_Explosions.Remove( this, listID );
00500
00501
00502 eReferencableGameObject::OnRemoveFromGame();
00503 }