File: ShipBulletManager.cpp

package info (click to toggle)
transcend 0.3.dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,832 kB
  • ctags: 2,912
  • sloc: cpp: 26,890; ansic: 693; sh: 210; makefile: 131; perl: 67
file content (416 lines) | stat: -rw-r--r-- 12,645 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * Modification History
 *
 * 2004-June-16   Jason Rohrer
 * Created.
 *
 * 2004-June-22   Jason Rohrer
 * Fixed a memory leak.
 *
 * 2004-August-15   Jason Rohrer
 * Added sound support.
 *
 * 2004-August-19   Jason Rohrer
 * Fixed a memory leak.
 *
 * 2004-August-20   Jason Rohrer
 * Added sound priorities.
 *
 * 2004-August-30   Jason Rohrer
 * Added bullet scaling parameters.
 *
 * 2004-October-13   Jason Rohrer
 * Added function for getting bullet count.
 *
 * 2005-August-22   Jason Rohrer
 * Added smooth fade-out during last tenth of each bullet's life.
 * Started work on boss damage graphics.
 *
 * 2005-August-23   Jason Rohrer
 * Finished boss damage graphics.
 */



#include "ShipBulletManager.h"



ShipBulletManager::ShipBulletManager( ShipBullet *inBulletTemplate,
                                      double inBulletScale,
                                      SoundPlayer *inPlayer,
                                      BulletSound *inBulletSoundTemplate,
                                      double inWorldWidth,
                                      double inWorldHeight )
    : mBulletTemplate( inBulletTemplate ),
      mBulletScale( inBulletScale ),
      mSoundPlayer( inPlayer ),
      mBulletSoundTemplate( inBulletSoundTemplate ),
      mCloseRangeParameters( new SimpleVector<double>() ),
      mFarRangeParameters( new SimpleVector<double>() ),
      mPowerModifiers( new SimpleVector<double>() ),
      mCurrentPowers( new SimpleVector<double>() ),
      mRangesInScreenUnits( new SimpleVector<double>() ),
      mRangeFractions( new SimpleVector<double>() ),
      mTimePassed( new SimpleVector<double>() ),
      mStartingPositions( new SimpleVector<Vector3D*>() ),
      mVelocitiesInScreenUnitsPerSecond( new SimpleVector<Vector3D*>() ),
      mCurrentPositions( new SimpleVector<Vector3D*>() ),
      mCurrentRotations( new SimpleVector<Angle3D*>() ),
      mCurrentRotationRates( new SimpleVector<double>() ),
      mSholdBeDestroyedFlags( new SimpleVector<char>() ) {

    mMaxXPosition = inWorldWidth / 2;
    mMinXPosition = -mMaxXPosition;

    mMaxYPosition = inWorldHeight / 2;
    mMinYPosition = -mMaxYPosition;
    
    }


        
ShipBulletManager::~ShipBulletManager() {
    delete mBulletTemplate;
    if( mBulletTemplate != NULL ) {
        delete mBulletSoundTemplate;
        }

    delete mCloseRangeParameters;
    delete mFarRangeParameters;
    delete mPowerModifiers;
    delete mCurrentPowers;
    
    delete mRangesInScreenUnits;
    delete mRangeFractions;
    delete mTimePassed;
    
    int numBullets = mStartingPositions->size();

    for( int i=0; i<numBullets; i++ ) {

        delete *( mStartingPositions->getElement( i ) );
        delete *( mVelocitiesInScreenUnitsPerSecond->getElement( i ) );
        delete *( mCurrentPositions->getElement( i ) );
        delete *( mCurrentRotations->getElement( i ) );
        }

    
    delete mStartingPositions;
    delete mVelocitiesInScreenUnitsPerSecond;
    delete mCurrentPositions;
    delete mCurrentRotations;
    
    delete mCurrentRotationRates;
    delete mSholdBeDestroyedFlags;
    }



void ShipBulletManager::addBullet(
    double inCloseRangeParameter,
    double inFarRangeParameter,
    double inPowerModifier,
    double inRangeInScreenUnits,
    Vector3D *inStartingPosition,
    Angle3D *inStartingRotation,
    Vector3D *inVelocityInScreenUnitsPerSecond ) {

    mCloseRangeParameters->push_back( inCloseRangeParameter );
    mFarRangeParameters->push_back( inFarRangeParameter );
    mPowerModifiers->push_back( inPowerModifier );
    mCurrentPowers->push_back( 0 );
    
    mRangesInScreenUnits->push_back( inRangeInScreenUnits );
    mRangeFractions->push_back( 0 );
    mTimePassed->push_back( 0 );
    
    mStartingPositions->push_back( inStartingPosition );
    mCurrentRotations->push_back( inStartingRotation );
    mCurrentRotationRates->push_back( 0 );

    mVelocitiesInScreenUnitsPerSecond->push_back(
        inVelocityInScreenUnitsPerSecond );
    mCurrentPositions->push_back( new Vector3D( inStartingPosition ) );

    mSholdBeDestroyedFlags->push_back( false );

    if( mSoundPlayer != NULL ) {
        // play the sound
        PlayableSound *sound = mBulletSoundTemplate->getPlayableSound(
            inCloseRangeParameter,
            inFarRangeParameter,
            mSoundPlayer->getSampleRate() );
        
        // shot sounds are low priority
        mSoundPlayer->playSoundNow( sound, false, inPowerModifier );
        
        delete sound;
        }
    }



int ShipBulletManager::getBulletCount() {
    return mCloseRangeParameters->size();
    }



double ShipBulletManager::getBulletPowerInCircle( Vector3D *inCircleCenter,
                                                  double inCircleRadius ) {
    double powerSum = 0;
    
    int numBullets = mCloseRangeParameters->size();

    for( int i=0; i<numBullets; i++ ) {
        double currentRotationRate;
        double power;
        
        SimpleVector<DrawableObject *> *bulletObjects =
            mBulletTemplate->getDrawableObjects(
                *( mCloseRangeParameters->getElement( i ) ),
                *( mFarRangeParameters->getElement( i ) ),
                *( mRangeFractions->getElement( i ) ),
                &power,
                &currentRotationRate );


        int numObjects = bulletObjects->size();

        char alreadyHit = false;
        for( int j=0; j<numObjects && !alreadyHit; j++ ) {

            DrawableObject *currentObject =
                *( bulletObjects->getElement( j ) );

            if( !alreadyHit ) {
                // check if this part of bullet hits

                currentObject->scale( mBulletScale );
                currentObject->rotate(
                    *( mCurrentRotations->getElement( i ) ) );
                currentObject->move(
                    *( mCurrentPositions->getElement( i ) ) );

                if( currentObject->isBorderInCircle( inCircleCenter,
                                                     inCircleRadius ) ) {
                    powerSum += *( mCurrentPowers->getElement( i ) );
                    alreadyHit = true;
                    }
             
                }
            // otherwise, ignore this part of bullet
            }

        for( int j=0; j<numObjects; j++ ) {

            DrawableObject *currentObject =
                *( bulletObjects->getElement( j ) );
            
            delete currentObject;
            }

        delete bulletObjects;
        }

    return powerSum;
    }



Vector3D **ShipBulletManager::getBulletPositionsInCircle(
    Vector3D *inCircleCenter,
    double inCircleRadius,
    int *outNumBullets ) {

    SimpleVector<Vector3D*> *positionsInCircle =
        new SimpleVector<Vector3D*>();

    int numBullets = mStartingPositions->size();

    int i;
    for( i=0; i<numBullets; i++ ) {
        Vector3D *position = *( mCurrentPositions->getElement( i ) );

        if( position->getDistance( inCircleCenter ) <= inCircleRadius ) {

            positionsInCircle->push_back( new Vector3D( position ) );
            }
        }

    *outNumBullets = positionsInCircle->size();
    Vector3D **returnArray = positionsInCircle->getElementArray();

    
    delete positionsInCircle;

    
    return returnArray;
    }




void ShipBulletManager::passTime( double inTimeDeltaInSeconds ) {
    int numBullets = mStartingPositions->size();

    int i;
    for( i=0; i<numBullets; i++ ) {
        *( mTimePassed->getElement( i ) ) += inTimeDeltaInSeconds;

        Angle3D *currentRotation = *( mCurrentRotations->getElement( i ) );

        double rotationRate = *( mCurrentRotationRates->getElement( i ) );
        
        currentRotation->mZ += rotationRate * inTimeDeltaInSeconds;


        // compute new position
        Vector3D *newPosition =
            new Vector3D( *( mStartingPositions->getElement( i ) ) );

        Vector3D *travelVector = new Vector3D(
            *( mVelocitiesInScreenUnitsPerSecond->getElement( i ) ) );

        travelVector->scale( *( mTimePassed->getElement( i ) ) );

        newPosition->add( travelVector );

        delete *( mCurrentPositions->getElement( i ) );

        *( mCurrentPositions->getElement( i ) ) = newPosition;
        
        double distanceTraveled = travelVector->getLength();

        delete travelVector;


        // compute new range fraction
        double newRangeFraction;
        double rangeLength = *( mRangesInScreenUnits->getElement( i ) );
        
        Vector3D *velocity =
            *( mVelocitiesInScreenUnitsPerSecond->getElement( i ) );
        
        if( velocity->getLength() == 0 ) {
            // treat range as bullet lifetime in seconds
            double oldRangeFraction = *( mRangeFractions->getElement( i ) );
            
            newRangeFraction =
                oldRangeFraction +
                inTimeDeltaInSeconds / rangeLength;
            }
        else {
            // treat range as a distance       
            newRangeFraction = distanceTraveled /  rangeLength;
            }
        

        if( newRangeFraction >= 1 ) {
            // bullet has reached end of range
            // should be destroyed
            *( mSholdBeDestroyedFlags->getElement( i ) ) = true;

            if( newRangeFraction > 1 ) {
                newRangeFraction = 1;
                }                                        
            }
        
        *( mRangeFractions->getElement( i ) ) = newRangeFraction;
        }

    
    // walk through vectors and destroy bullets that are flagged
    // note that our loop range is adjusted as the vector length shrinks
    for( i=0; i<mCloseRangeParameters->size(); i++ ) {
        if( *( mSholdBeDestroyedFlags->getElement( i ) ) ) {

            mCloseRangeParameters->deleteElement( i );
            mFarRangeParameters->deleteElement( i );
            mPowerModifiers->deleteElement( i );

            mRangesInScreenUnits->deleteElement( i );
            mRangeFractions->deleteElement( i );

            mTimePassed->deleteElement( i );

            delete *( mStartingPositions->getElement( i ) );
            mStartingPositions->deleteElement( i );

            delete *( mCurrentRotations->getElement( i ) );
            mCurrentRotations->deleteElement( i );

            mCurrentRotationRates->deleteElement( i );

            delete *( mVelocitiesInScreenUnitsPerSecond->getElement( i ) );
            mVelocitiesInScreenUnitsPerSecond->deleteElement( i );

            delete *( mCurrentPositions->getElement( i ) );
            mCurrentPositions->deleteElement( i );

            mSholdBeDestroyedFlags->deleteElement( i );
            }
        }
    }



SimpleVector<DrawableObject*> *ShipBulletManager::getDrawableObjects() {

    SimpleVector<DrawableObject*> *returnVector =
        new SimpleVector<DrawableObject*>();
    
    int numBullets = mCloseRangeParameters->size();

    for( int i=0; i<numBullets; i++ ) {
        double power;
        double modifiedPower;
        double currentRotationRate;
        
        SimpleVector<DrawableObject *> *bulletObjects =
            mBulletTemplate->getDrawableObjects(
                *( mCloseRangeParameters->getElement( i ) ),
                *( mFarRangeParameters->getElement( i ) ),
                *( mRangeFractions->getElement( i ) ),
                &power,
                &currentRotationRate );

        double powerModifier = *( mPowerModifiers->getElement( i ) );
        
        modifiedPower = power * powerModifier;


        double endOfLifeFadeFactor = 1;
        double rangeFraction = *( mRangeFractions->getElement( i ) );
        if( rangeFraction > 0.9 ) {
            // fade out during last tenth of bullet life
            endOfLifeFadeFactor = 1.0 - (rangeFraction - 0.9) / 0.1;
            }

        double fadeFactor = powerModifier * endOfLifeFadeFactor;
        
        *( mCurrentPowers->getElement( i ) ) = modifiedPower;
        *( mCurrentRotationRates->getElement( i ) ) = currentRotationRate;

        int numObjects = bulletObjects->size();
        
        for( int j=0; j<numObjects; j++ ) {

            DrawableObject *currentObject =
                *( bulletObjects->getElement( j ) );

            currentObject->fade( fadeFactor );

            currentObject->scale( mBulletScale );
            currentObject->rotate( *( mCurrentRotations->getElement( i ) ) );
            currentObject->move( *( mCurrentPositions->getElement( i ) ) );

            returnVector->push_back( currentObject );
            }

        delete bulletObjects;
        }

    return returnVector;
    }