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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/*
* Modification History
*
* 2004-June-29 Jason Rohrer
* Created.
*
* 2004-July-3 Jason Rohrer
* Improved boss ship-in-range detection.
*
* 2004-July-5 Jason Rohrer
* Fixed memory leaks.
*
* 2004-August-19 Jason Rohrer
* Added explosion sounds.
*
* 2004-August-20 Jason Rohrer
* Added sound priorities.
*
* 2004-August-25 Jason Rohrer
* Added function to check for boss death.
*
* 2004-August-29 Jason Rohrer
* Improved distance-from-target compression formula.
*
* 2004-October-21 Jason Rohrer
* Fixed bug that caused explosion progress to get larger than 1.
*
* 2005-August-22 Jason Rohrer
* Started work on boss damage graphics.
*
* 2005-August-23 Jason Rohrer
* Finished boss damage graphics.
*
* 2005-August-26 Jason Rohrer
* Added limit to number of damage graphics on screen at once.
*
* 2005-August-29 Jason Rohrer
* Removed print message.
*/
#include "BossManager.h"
#include <math.h>
BossManager::BossManager( Enemy *inBossTemplate,
double inBossScale,
double inExplosionScale,
double inExplosionTimeInSeconds,
double inExplosionShapeParameter,
double inBossMinVelocity,
double inBossMaxVelocity,
ShipBulletManager *inShipBulletManager,
ShipBulletManager *inBossBulletManager,
ShipBulletManager *inBossDamageManager,
double inBossBulletRange,
double inBossBulletBaseVelocity,
double inMinBossBulletsPerSecond,
double inMaxBossBulletsPerSecond,
double inBossDamageTime,
double inTimeUntilFullShootingPower,
double inMaxBossHealth,
double inBossHealthRecoveryRate,
Vector3D *inBossPosition,
SoundPlayer *inSoundPlayer,
BulletSound *inBossExplosionSoundTemplate )
: mBossTemplate( inBossTemplate ),
mShipBulletManager( inShipBulletManager ),
mBossBulletManager( inBossBulletManager ),
mBossDamageManager( inBossDamageManager ),
mBossScale( inBossScale ),
mExplosionScale( inExplosionScale ),
mExplosionTimeInSeconds( inExplosionTimeInSeconds ),
mExplosionShapeParameter( inExplosionShapeParameter ),
mBossMinVelocity( inBossMinVelocity ),
mBossMaxVelocity( inBossMaxVelocity ),
mBossBulletRange( inBossBulletRange ),
mBossBulletBaseVelocity( inBossBulletBaseVelocity ),
mMinBossTimeBetweenBullets( 1 / inMinBossBulletsPerSecond ),
mMaxBossTimeBetweenBullets( 1 / inMaxBossBulletsPerSecond ),
mBossDamageTime( inBossDamageTime ),
mTimeUntilFullShootingPower( inTimeUntilFullShootingPower ),
mMaxBossHealth( inMaxBossHealth ),
mBossHealth( inMaxBossHealth ),
mHealthRecoveryRate( inBossHealthRecoveryRate ),
mBossPosition( inBossPosition ),
mSoundPlayer( inSoundPlayer ),
mBossExplosionSoundTemplate( inBossExplosionSoundTemplate ),
mCurrentlyExploding( false ),
mExplosionProgress( 0 ),
mExplosionFadeProgress( 0 ),
mTimeSinceLastBullet( 0 ),
mTimeSinceShipEnteredRange( 0 ),
mAngerLevel( 0 ),
mCurrentRadius( 0 ),
mCurrentRotationRate( 0 ),
mCurrentRotation( new Angle3D( 0, 0, 0 ) ),
mShipDistanceParameter( 0 ) {
mBossDistanceFromCenter = mBossPosition->getLength();
}
BossManager::~BossManager() {
delete mBossTemplate;
delete mBossPosition;
delete mCurrentRotation;
delete mBossExplosionSoundTemplate;
}
void BossManager::passTime( double inTimeDeltaInSeconds,
Vector3D *inShipPosition,
Vector3D *inShipVelocity ) {
mCurrentRotation->mZ += mCurrentRotationRate * inTimeDeltaInSeconds;
// check if we are being hit by ship bullets
double bulletPowerNearBoss =
mShipBulletManager->getBulletPowerInCircle(
mBossPosition, mCurrentRadius );
if( bulletPowerNearBoss > 0 ) {
// boss hit by bullet
// scale power by time delta (power is power per second)
mBossHealth -= bulletPowerNearBoss * inTimeDeltaInSeconds;
if( mBossHealth <= 0 ) {
if( !mCurrentlyExploding ) {
// start explosion
mCurrentlyExploding = true;
// play the sound
PlayableSound *sound =
mBossExplosionSoundTemplate->getPlayableSound(
mAngerLevel,
mExplosionShapeParameter,
mSoundPlayer->getSampleRate() );
// boss explosion is high priority
mSoundPlayer->playSoundNow( sound, true, 1 );
delete sound;
}
// don't allow it to go below 0
mBossHealth = 0;
}
}
else {
// not hit, recover health
mBossHealth += mHealthRecoveryRate * inTimeDeltaInSeconds;
if( mBossHealth > mMaxBossHealth ) {
mBossHealth = mMaxBossHealth;
}
}
if( mCurrentlyExploding ) {
// boss is exploding
// compute new explosion progress
// convert time delta to a progress increment for the progress
// range [0,1]
double progressFractionDelta =
inTimeDeltaInSeconds / mExplosionTimeInSeconds;
mExplosionProgress += progressFractionDelta;
if( mExplosionProgress > 1 ) {
mExplosionProgress = 1;
}
if( mExplosionProgress == 1 ) {
// end of explosion
// fade out last frame
mExplosionFadeProgress += progressFractionDelta;
if( mExplosionFadeProgress >= 1 ) {
// explosion done fading
mExplosionFadeProgress = 1;
}
}
}
if( !mCurrentlyExploding ) {
double distanceToShip = inShipPosition->getDistance( mBossPosition );
// compute anger level based on how long ship has been in range
if( distanceToShip <= mBossBulletRange ) {
// ship in range, get angry
mTimeSinceShipEnteredRange += inTimeDeltaInSeconds;
}
else {
// ship out of range, calm down
mTimeSinceShipEnteredRange -= inTimeDeltaInSeconds;
if( mTimeSinceShipEnteredRange < 0 ) {
mTimeSinceShipEnteredRange = 0;
}
}
mAngerLevel = mTimeSinceShipEnteredRange / mTimeUntilFullShootingPower;
if( mAngerLevel > 1 ) {
mAngerLevel = 1;
}
// boss moves in a cirular path
double currentVelocity =
mAngerLevel * ( mBossMaxVelocity - mBossMinVelocity ) +
mBossMinVelocity;
// move perpendicular to center radius vector
// center radius vector is our position vector
Vector3D *bossVelocityVector = new Vector3D( mBossPosition );
Angle3D *perpendicularAngle = new Angle3D( 0, 0, M_PI / 2 );
bossVelocityVector->rotate( perpendicularAngle );
delete perpendicularAngle;
bossVelocityVector->normalize();
bossVelocityVector->scale( currentVelocity );
Vector3D *bossMoveVector = new Vector3D( bossVelocityVector );
bossMoveVector->scale( inTimeDeltaInSeconds );
mBossPosition->add( bossMoveVector );
delete bossMoveVector;
// lock down distance from center to avoid cumulative errors
double newDistanceFromCenter = mBossPosition->getLength();
double adjustment = mBossDistanceFromCenter / newDistanceFromCenter;
mBossPosition->scale( adjustment );
double currentTimeBetweenBullets =
mAngerLevel *
( mMaxBossTimeBetweenBullets - mMinBossTimeBetweenBullets ) +
mMinBossTimeBetweenBullets;
mTimeSinceLastBullet += inTimeDeltaInSeconds;
if( mTimeSinceLastBullet >= currentTimeBetweenBullets ) {
if( distanceToShip <= mBossBulletRange ) {
// close enough to hit target
// fire
double bulletMoveRate = mBossBulletBaseVelocity;
// compute a vector for bullet and the angle of that vector
Vector3D *vectorToShip = new Vector3D( inShipPosition );
vectorToShip->subtract( mBossPosition );
// compensate for our velocity when we aim at ship
vectorToShip->normalize();
vectorToShip->scale( bulletMoveRate );
vectorToShip->subtract( bossVelocityVector );
// compensate for the velocity of the ship
vectorToShip->add( inShipVelocity );
// normalize to turn compensated vector into a length
// 1 direction
vectorToShip->normalize();
Vector3D *yVector = new Vector3D( 0, -1, 0 );
Angle3D *angleToPointAt =
yVector->getZAngleTo( vectorToShip );
delete yVector;
vectorToShip->scale( bulletMoveRate );
// adjust by boss motion
vectorToShip->add( bossVelocityVector );
// close range based on health
// long range based on anger level
double healthFraction = mBossHealth / mMaxBossHealth;
// get the final move rate of the bullet
bulletMoveRate = vectorToShip->getLength();
mBossBulletManager->addBullet(
healthFraction,
mAngerLevel,
1,
bulletMoveRate, // range proportional to move rate
new Vector3D( mBossPosition ),
angleToPointAt,
vectorToShip );
mTimeSinceLastBullet = 0;
}
}
if( bulletPowerNearBoss > 0 ) {
// emit damage wherever we are hit by ship bullets
// do this down here since we have computed the boss
// velocity by now
// limit to at most 10 damage graphics in progress at once
int limit = 10;
double damageParameter = mBossHealth / mMaxBossHealth;
int numBullets;
Vector3D **bulletPositions =
mShipBulletManager->getBulletPositionsInCircle(
mBossPosition,
// smaller radius to ensure damage
// appears only on boss
mCurrentRadius / 2,
&numBullets );
double bossVelocity = currentVelocity;
// how far the damage graphics will travel before they die
double damageTravelDistance = bossVelocity * mBossDamageTime;
for( int i=0; i<numBullets; i++ ) {
if( mBossDamageManager->getBulletCount() < limit ) {
mBossDamageManager->addBullet(
damageParameter,
damageParameter,
1.0, // full power
damageTravelDistance,
new Vector3D( bulletPositions[i] ),
new Angle3D( mCurrentRotation ),
// damage graphics have same velocity as boss
new Vector3D( bossVelocityVector ) );
}
delete bulletPositions[i];
}
delete [] bulletPositions;
}
delete bossVelocityVector;
// convert the distance to ship to the range [0,1] and save it
double distanceToTarget = distanceToShip;
// starting at 40 units, and up to 20 units from target, is the range
double compressedDistance =
( distanceToTarget - 20 ) /
( 40 );
// limit the range
if( compressedDistance > 1 ) {
compressedDistance = 1;
}
else if( compressedDistance < 0 ) {
compressedDistance = 0;
}
mShipDistanceParameter = compressedDistance;
}
}
SimpleVector<DrawableObject *> *BossManager::getDrawableObjects() {
double healthFraction = mBossHealth / mMaxBossHealth;
SimpleVector<DrawableObject *> *bossObjects =
mBossTemplate->getDrawableObjects(
healthFraction,
mShipDistanceParameter,
mExplosionShapeParameter,
mExplosionProgress,
&mCurrentRotationRate );
// fade out at end of explosion
double alphaMultiplier = 1 - mExplosionFadeProgress;
// compute the scale by weighting the enemy and explosion scales
double scale =
mExplosionProgress * mExplosionScale +
( 1 - mExplosionProgress ) * mBossScale;
int numObjects = bossObjects->size();
// compute the maximum radius of this boss
double maxRadius = 0;
for( int j=0; j<numObjects; j++ ) {
DrawableObject *currentObject =
*( bossObjects->getElement( j ) );
currentObject->scale( scale );
currentObject->rotate( mCurrentRotation );
currentObject->move( mBossPosition );
currentObject->fade( alphaMultiplier );
double radius = currentObject->getBorderMaxDistance( mBossPosition );
if( radius > maxRadius ) {
maxRadius = radius;
}
}
mCurrentRadius = maxRadius;
return bossObjects;
}
Vector3D *BossManager::getBossPosition() {
return new Vector3D( mBossPosition );
}
char BossManager::isBossDead() {
if( mExplosionFadeProgress >= 1 ) {
return true;
}
else {
return false;
}
}
|