Version of Robotron arcade game using LPC1768, a Gameduino shield, a serial EEPROM (for high scores), two microswitch joysticks and two buttons plus a box to put it in. 20 levels of mayhem.

Dependencies:   25LCxxx_SPI CommonTypes Gameduino mbed

Committer:
RichardE
Date:
Sun Jun 09 14:28:53 2013 +0000
Revision:
9:fa7e7b37b632
Parent:
8:82d88f9381f3
Child:
10:bfa1c307c99d
Sound is now working. Now a complete working game, albeit with only 2 levels.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RichardE 0:5fa232ee5fdf 1 /*
RichardE 0:5fa232ee5fdf 2 * SOURCE FILE : LevelNormal.cpp
RichardE 0:5fa232ee5fdf 3 *
RichardE 0:5fa232ee5fdf 4 * Definition of class LevelNormal.
RichardE 0:5fa232ee5fdf 5 * Base class for all "normal" levels.
RichardE 0:5fa232ee5fdf 6 * i.e. Levels that are not special attract modes
RichardE 0:5fa232ee5fdf 7 * but have enemies who are trying to kill you
RichardE 0:5fa232ee5fdf 8 * and so on.
RichardE 0:5fa232ee5fdf 9 *
RichardE 0:5fa232ee5fdf 10 */
RichardE 0:5fa232ee5fdf 11
RichardE 5:0b0651ac7832 12 // Define this for debugging messages.
RichardE 5:0b0651ac7832 13 #define CHATTY
RichardE 5:0b0651ac7832 14
RichardE 5:0b0651ac7832 15 #ifdef CHATTY
RichardE 5:0b0651ac7832 16 #include "mbed.h"
RichardE 5:0b0651ac7832 17 extern Serial pc;
RichardE 5:0b0651ac7832 18 #endif
RichardE 5:0b0651ac7832 19
RichardE 0:5fa232ee5fdf 20 #include "LevelNormal.h"
RichardE 5:0b0651ac7832 21 #include "GameObjectLocator.h"
RichardE 5:0b0651ac7832 22 #include "FrameCounter.h"
RichardE 5:0b0651ac7832 23 #include "SpriteNumber.h"
RichardE 5:0b0651ac7832 24 #include "ArenaConst.h"
RichardE 0:5fa232ee5fdf 25
RichardE 0:5fa232ee5fdf 26 // Current instance being processed.
RichardE 0:5fa232ee5fdf 27 LevelNormal *LevelNormal::currentInstance;
RichardE 0:5fa232ee5fdf 28
RichardE 0:5fa232ee5fdf 29 /***************/
RichardE 0:5fa232ee5fdf 30 /* CONSTRUCTOR */
RichardE 0:5fa232ee5fdf 31 /***************/
RichardE 5:0b0651ac7832 32 LevelNormal::LevelNormal()
RichardE 5:0b0651ac7832 33 {
RichardE 0:5fa232ee5fdf 34 }
RichardE 0:5fa232ee5fdf 35
RichardE 0:5fa232ee5fdf 36 /**************/
RichardE 0:5fa232ee5fdf 37 /* DESTRUCTOR */
RichardE 0:5fa232ee5fdf 38 /**************/
RichardE 5:0b0651ac7832 39 LevelNormal::~LevelNormal()
RichardE 5:0b0651ac7832 40 {
RichardE 0:5fa232ee5fdf 41 }
RichardE 0:5fa232ee5fdf 42
RichardE 0:5fa232ee5fdf 43 /********************/
RichardE 0:5fa232ee5fdf 44 /* INITIALISE LEVEL */
RichardE 0:5fa232ee5fdf 45 /********************/
RichardE 5:0b0651ac7832 46 // Pass pointer to Gameduino to draw on in gd.
RichardE 5:0b0651ac7832 47 void LevelNormal::InitialiseLevel( Gameduino *gd )
RichardE 5:0b0651ac7832 48 {
RichardE 0:5fa232ee5fdf 49 // Note that if you re-arrange the following code you may need to adjust the
RichardE 0:5fa232ee5fdf 50 // SpriteNumber enumeration in SpriteNumber.h.
RichardE 5:0b0651ac7832 51 UInt8 spriteNumber = FirstEnemySprite;
RichardE 5:0b0651ac7832 52 // Initialise enemies.
RichardE 5:0b0651ac7832 53 GameObject::InitialiseAll( DataForLevel->Enemies, LevelData::MaxEnemies, &spriteNumber );
RichardE 5:0b0651ac7832 54 // Initialise humans.
RichardE 0:5fa232ee5fdf 55 spriteNumber = FirstHumanSprite;
RichardE 5:0b0651ac7832 56 GameObject::InitialiseAll( DataForLevel->Humans, LevelData::MaxHumans, &spriteNumber );
RichardE 5:0b0651ac7832 57 // Use next free sprite number for player.
RichardE 5:0b0651ac7832 58 player->SpriteNumber = PlayerSprite;
RichardE 5:0b0651ac7832 59 // Do futher initialisation for all enemies.
RichardE 5:0b0651ac7832 60 EnemyObject *object;
RichardE 5:0b0651ac7832 61 for( UInt8 e = 0; e < LevelData::MaxEnemies; ++e ) {
RichardE 5:0b0651ac7832 62 object = (EnemyObject*)DataForLevel->Enemies[ e ];
RichardE 5:0b0651ac7832 63 if( object != (EnemyObject*)NULL ) {
RichardE 0:5fa232ee5fdf 64 // Get enemy to chase the player.
RichardE 5:0b0651ac7832 65 object->SetChaseObject( player );
RichardE 0:5fa232ee5fdf 66 // Pass array of all enemies to this enemy.
RichardE 0:5fa232ee5fdf 67 object->Enemies = DataForLevel->Enemies;
RichardE 0:5fa232ee5fdf 68 // If enemy is a brain then tell it about the humans to chase.
RichardE 0:5fa232ee5fdf 69 if( object->GetEnemyType() == Brain ) {
RichardE 0:5fa232ee5fdf 70 ((BrainObject*)object)->HumansToChase = DataForLevel->Humans;
RichardE 0:5fa232ee5fdf 71 }
RichardE 5:0b0651ac7832 72 }
RichardE 0:5fa232ee5fdf 73 }
RichardE 5:0b0651ac7832 74 // Put player in the centre of the arena.
RichardE 5:0b0651ac7832 75 player->Xco = PLAYER_START_X;
RichardE 5:0b0651ac7832 76 player->Yco = PLAYER_START_Y;
RichardE 5:0b0651ac7832 77 // Kill off all player's bullets.
RichardE 5:0b0651ac7832 78 player->KillAllBullets( gd );
RichardE 5:0b0651ac7832 79 // Kill off all explosions.
RichardE 8:82d88f9381f3 80 ExplosionManager::Instance.KillAllExplosions();
RichardE 0:5fa232ee5fdf 81 }
RichardE 0:5fa232ee5fdf 82
RichardE 0:5fa232ee5fdf 83 /**********************************/
RichardE 0:5fa232ee5fdf 84 /* DRAW SCORE AND NUMBER OF LIVES */
RichardE 0:5fa232ee5fdf 85 /**********************************/
RichardE 5:0b0651ac7832 86 void LevelNormal::DrawScoreAndLives( void )
RichardE 5:0b0651ac7832 87 {
RichardE 5:0b0651ac7832 88 GDExtra::WriteBCDNumber( gd, 16, 0, player->Score, 8 );
RichardE 5:0b0651ac7832 89 // Display number of lives but limit this to 20 lives displayed.
RichardE 5:0b0651ac7832 90 UInt8 lives = ( player->Lives > 20 ) ? 20 : player->Lives;
RichardE 5:0b0651ac7832 91 gd->fill( Gameduino::RAM_PIC + VISIBLE_CHAR_WIDTH - lives, MiniPlayer, lives );
RichardE 0:5fa232ee5fdf 92 }
RichardE 0:5fa232ee5fdf 93
RichardE 0:5fa232ee5fdf 94 /******************/
RichardE 0:5fa232ee5fdf 95 /* DRAW THE LEVEL */
RichardE 0:5fa232ee5fdf 96 /******************/
RichardE 5:0b0651ac7832 97 void LevelNormal::DrawLevel( void )
RichardE 5:0b0651ac7832 98 {
RichardE 5:0b0651ac7832 99 // Set screen background to black.
RichardE 5:0b0651ac7832 100 gd->wr( Gameduino::BG_COLOR, Gameduino::RGB( 0, 0, 0 ) );
RichardE 5:0b0651ac7832 101 // Clear the screen to zero characters.
RichardE 5:0b0651ac7832 102 GDExtra::ClearScreen( gd, TransparentChar );
RichardE 5:0b0651ac7832 103 // Hide all sprties.
RichardE 5:0b0651ac7832 104 GDExtra::HideAllSprites( gd );
RichardE 5:0b0651ac7832 105 // Display level number.
RichardE 5:0b0651ac7832 106 GDExtra::WriteProgString( gd, 0, 0, StringData::LevelString );
RichardE 5:0b0651ac7832 107 GDExtra::WriteUInt16( gd, 6, 0, LevelNumber, 10, 2 );
RichardE 5:0b0651ac7832 108 // Display score.
RichardE 5:0b0651ac7832 109 GDExtra::WriteProgString( gd, 10, 0, StringData::ScoreString );
RichardE 5:0b0651ac7832 110 // Update score and lives.
RichardE 5:0b0651ac7832 111 DrawScoreAndLives();
RichardE 5:0b0651ac7832 112 // Draw border around screen.
RichardE 5:0b0651ac7832 113 CharFrame::Draw(
RichardE 5:0b0651ac7832 114 gd,
RichardE 5:0b0651ac7832 115 ARENA_BORDER_X,
RichardE 5:0b0651ac7832 116 ARENA_BORDER_Y,
RichardE 5:0b0651ac7832 117 ARENA_BORDER_WIDTH,
RichardE 5:0b0651ac7832 118 ARENA_BORDER_HEIGHT
RichardE 5:0b0651ac7832 119 );
RichardE 0:5fa232ee5fdf 120 }
RichardE 0:5fa232ee5fdf 121
RichardE 0:5fa232ee5fdf 122 /************************************************/
RichardE 0:5fa232ee5fdf 123 /* HANDLE COLLISIONS BETWEEN HUMANS AND ENEMIES */
RichardE 0:5fa232ee5fdf 124 /************************************************/
RichardE 0:5fa232ee5fdf 125 // Pass index of human in level's humans array in humanIndex.
RichardE 0:5fa232ee5fdf 126 // Pass sprite number of sprite that it hit in spriteNumber.
RichardE 5:0b0651ac7832 127 void LevelNormal::HandleHumanCollision( UInt8 humanIndex, UInt8 spriteNumber )
RichardE 5:0b0651ac7832 128 {
RichardE 5:0b0651ac7832 129 // Point to array of enemy object pointers.
RichardE 5:0b0651ac7832 130 GameObject **enemies = currentInstance->DataForLevel->Enemies;
RichardE 5:0b0651ac7832 131 EnemyObject *enemy;
RichardE 5:0b0651ac7832 132 UInt8 enemyIndex, mutantIndex;
RichardE 5:0b0651ac7832 133 // Find an enemy with given sprite number.
RichardE 5:0b0651ac7832 134 if( GameObject::FindSpriteNumber( enemies, LevelData::MaxEnemies, spriteNumber, &enemyIndex ) ) {
RichardE 5:0b0651ac7832 135 // Found enemy. Check if it squashes humans.
RichardE 5:0b0651ac7832 136 enemy = (EnemyObject*)enemies[ enemyIndex ];
RichardE 0:5fa232ee5fdf 137 // Get hold of the human that is doomed.
RichardE 0:5fa232ee5fdf 138 GameObject **humans = currentInstance->DataForLevel->Humans;
RichardE 0:5fa232ee5fdf 139 HumanObject *human = (HumanObject*)humans[ humanIndex ];
RichardE 0:5fa232ee5fdf 140 // Human must be walking around. Not rescued or already dead.
RichardE 0:5fa232ee5fdf 141 if( human->CurrentState == HumanObject::WalkingAbout ) {
RichardE 0:5fa232ee5fdf 142 if( enemy->SquashesHumans ) {
RichardE 0:5fa232ee5fdf 143 // Change human to dead state.
RichardE 0:5fa232ee5fdf 144 human->CurrentState = HumanObject::Dead;
RichardE 0:5fa232ee5fdf 145 // Make a noise.
RichardE 0:5fa232ee5fdf 146 SoundManager::Instance.PlaySound( Sounds::HumanDies, 0, 0 );
RichardE 5:0b0651ac7832 147 } else if( enemy->GetEnemyType() == Brain ) {
RichardE 0:5fa232ee5fdf 148 // Kill human by inserting a null into humans array.
RichardE 0:5fa232ee5fdf 149 humans[ humanIndex ] = (GameObject*)NULL;
RichardE 0:5fa232ee5fdf 150 // Find a free slot for a new enemy.
RichardE 0:5fa232ee5fdf 151 if( GameObject::FindUnusedObject( enemies, LevelData::MaxEnemies, &mutantIndex ) ) {
RichardE 0:5fa232ee5fdf 152 // Write a pointer to a mutant with the same index as the human that just died
RichardE 0:5fa232ee5fdf 153 // into the enemy array.
RichardE 0:5fa232ee5fdf 154 MutantObject *mutant = currentInstance->DataForLevel->Mutants + humanIndex;
RichardE 0:5fa232ee5fdf 155 enemies[ mutantIndex ] = mutant;
RichardE 0:5fa232ee5fdf 156 // Initialise mutant at coordinates of human and chasing the player.
RichardE 0:5fa232ee5fdf 157 mutant->Start( human, currentInstance->player );
RichardE 0:5fa232ee5fdf 158 // Make a noise.
RichardE 0:5fa232ee5fdf 159 // TODO : SoundManager::Instance.PlaySound( Sounds::HumanMutates, 0, 0 );
RichardE 5:0b0651ac7832 160 } else {
RichardE 0:5fa232ee5fdf 161 // Could not find a free slot for a new enemy so just erase the human sprite.
RichardE 8:82d88f9381f3 162 GDExtra::HideSprite( currentInstance->gd, human->SpriteNumber );
RichardE 0:5fa232ee5fdf 163 }
RichardE 0:5fa232ee5fdf 164 }
RichardE 0:5fa232ee5fdf 165 }
RichardE 0:5fa232ee5fdf 166 }
RichardE 0:5fa232ee5fdf 167 }
RichardE 0:5fa232ee5fdf 168
RichardE 0:5fa232ee5fdf 169 /********************************************************/
RichardE 0:5fa232ee5fdf 170 /* HANDLE COLLISIONS BETWEEN PLAYER BULLETS AND ENEMIES */
RichardE 0:5fa232ee5fdf 171 /********************************************************/
RichardE 0:5fa232ee5fdf 172 // Pass index of bullet in player's bullet array in bulletIndex.
RichardE 0:5fa232ee5fdf 173 // Pass sprite number of sprite that it hit in spriteNumber.
RichardE 5:0b0651ac7832 174 void LevelNormal::HandleBulletCollision( UInt8 bulletIndex, UInt8 spriteNumber )
RichardE 5:0b0651ac7832 175 {
RichardE 5:0b0651ac7832 176 // Point to array of enemy object pointers.
RichardE 5:0b0651ac7832 177 GameObject **enemies = currentInstance->DataForLevel->Enemies;
RichardE 5:0b0651ac7832 178 EnemyObject *enemy;
RichardE 5:0b0651ac7832 179 UInt8 enemyIndex;
RichardE 5:0b0651ac7832 180 // Find an enemy with given sprite number.
RichardE 5:0b0651ac7832 181 if( GameObject::FindSpriteNumber( enemies, LevelData::MaxEnemies, spriteNumber, &enemyIndex ) ) {
RichardE 5:0b0651ac7832 182 // Found enemy. Check if it is indestructable.
RichardE 5:0b0651ac7832 183 enemy = (EnemyObject*)enemies[ enemyIndex ];
RichardE 0:5fa232ee5fdf 184 if( enemy->HitPoints != EnemyObject::Indestructable ) {
RichardE 0:5fa232ee5fdf 185 // Enemy is not indestructable. Decrement hit points and die when it reaches zero.
RichardE 0:5fa232ee5fdf 186 enemy->HitPoints--;
RichardE 0:5fa232ee5fdf 187 if( enemy->HitPoints == 0 ) {
RichardE 0:5fa232ee5fdf 188 // Kill enemy by inserting a NULL into enemies array.
RichardE 0:5fa232ee5fdf 189 enemies[ enemyIndex ] = (GameObject*)NULL;
RichardE 0:5fa232ee5fdf 190 // Hide the enemy sprite.
RichardE 8:82d88f9381f3 191 GDExtra::HideSprite( currentInstance->gd, enemy->SpriteNumber );
RichardE 0:5fa232ee5fdf 192 // Add points to player's score.
RichardE 0:5fa232ee5fdf 193 currentInstance->player->AddToScore( enemy->GetPoints() );
RichardE 0:5fa232ee5fdf 194 }
RichardE 0:5fa232ee5fdf 195 }
RichardE 0:5fa232ee5fdf 196 // Tell enemy it has been hit by a bullet.
RichardE 0:5fa232ee5fdf 197 enemy->RegisterHitByBullet();
RichardE 0:5fa232ee5fdf 198 // Kill off the bullet.
RichardE 8:82d88f9381f3 199 currentInstance->player->KillBullet( currentInstance->gd, bulletIndex );
RichardE 0:5fa232ee5fdf 200 // Make a noise.
RichardE 0:5fa232ee5fdf 201 SoundManager::Instance.PlaySound( Sounds::Explosion, 0, 0 );
RichardE 0:5fa232ee5fdf 202 // Start explosion animation using coordinates of enemy.
RichardE 0:5fa232ee5fdf 203 ExplosionManager::Instance.StartExplosion( enemy->Xco, enemy->Yco );
RichardE 5:0b0651ac7832 204 }
RichardE 0:5fa232ee5fdf 205 }
RichardE 0:5fa232ee5fdf 206
RichardE 0:5fa232ee5fdf 207 /*********************************************************/
RichardE 0:5fa232ee5fdf 208 /* CHECK FOR COLLISIONS BETWEEN PLAYER AND OTHER OBJECTS */
RichardE 0:5fa232ee5fdf 209 /*********************************************************/
RichardE 0:5fa232ee5fdf 210 // Pass pointer to a flag that will be set true if player is dead in isDead parameter.
RichardE 5:0b0651ac7832 211 void LevelNormal::CheckPlayerCollisions( bool *isDead )
RichardE 5:0b0651ac7832 212 {
RichardE 5:0b0651ac7832 213 UInt8 enemyIndex, humanIndex;
RichardE 5:0b0651ac7832 214 // Check if player sprite has hit another sprite.
RichardE 8:82d88f9381f3 215 UInt8 hitSpriteNumber = gd->rd( Gameduino::COLLISION + player->SpriteNumber );
RichardE 5:0b0651ac7832 216 // If you get 0xFF then no collision found.
RichardE 5:0b0651ac7832 217 if( hitSpriteNumber != 0xFF ) {
RichardE 5:0b0651ac7832 218 // Check for collision with an enemy.
RichardE 5:0b0651ac7832 219 if(
RichardE 5:0b0651ac7832 220 GameObject::FindSpriteNumber(
RichardE 5:0b0651ac7832 221 DataForLevel->Enemies, LevelData::MaxEnemies, hitSpriteNumber, &enemyIndex
RichardE 5:0b0651ac7832 222 )
RichardE 5:0b0651ac7832 223 ) {
RichardE 5:0b0651ac7832 224 // Hit an enemy. Player is dead.
RichardE 5:0b0651ac7832 225 *isDead = true;
RichardE 5:0b0651ac7832 226 }
RichardE 5:0b0651ac7832 227 // Check for collision with a human that has not already been rescued or killed.
RichardE 5:0b0651ac7832 228 else if(
RichardE 5:0b0651ac7832 229 GameObject::FindSpriteNumber(
RichardE 5:0b0651ac7832 230 DataForLevel->Humans, LevelData::MaxHumans, hitSpriteNumber, &humanIndex
RichardE 5:0b0651ac7832 231 )
RichardE 5:0b0651ac7832 232 ) {
RichardE 5:0b0651ac7832 233 HumanObject *human = (HumanObject*)DataForLevel->Humans[ humanIndex ];
RichardE 0:5fa232ee5fdf 234 if( human->CurrentState == HumanObject::WalkingAbout ) {
RichardE 0:5fa232ee5fdf 235 // Change human state to rescued.
RichardE 0:5fa232ee5fdf 236 human->CurrentState = HumanObject::Rescued;
RichardE 0:5fa232ee5fdf 237 // Give player 50 points (in BCD!).
RichardE 0:5fa232ee5fdf 238 player->AddToScore( 0x50 );
RichardE 0:5fa232ee5fdf 239 // Make a noise.
RichardE 0:5fa232ee5fdf 240 SoundManager::Instance.PlaySound( Sounds::RescueHuman, 0, 0 );
RichardE 0:5fa232ee5fdf 241 }
RichardE 5:0b0651ac7832 242 }
RichardE 5:0b0651ac7832 243 }
RichardE 0:5fa232ee5fdf 244 }
RichardE 0:5fa232ee5fdf 245
RichardE 0:5fa232ee5fdf 246 /***********************************************************************************/
RichardE 0:5fa232ee5fdf 247 /* WAIT UNTIL SLOT FREE FOR A NEW SOUND, PLAY IT AND WAIT FOR ALL SOUNDS TO FINISH */
RichardE 0:5fa232ee5fdf 248 /***********************************************************************************/
RichardE 0:5fa232ee5fdf 249 // Pass sound to play in soundToPlay parameter.
RichardE 5:0b0651ac7832 250 void LevelNormal::PlaySoundAndWait( const UInt8 *soundToPlay )
RichardE 5:0b0651ac7832 251 {
RichardE 5:0b0651ac7832 252 // Keep trying to play sound until it works and meanwhile
RichardE 5:0b0651ac7832 253 // keep currently playing sounds going.
RichardE 5:0b0651ac7832 254 while( ! SoundManager::Instance.PlaySound( soundToPlay, 0, 0 ) ) {
RichardE 5:0b0651ac7832 255 // Update sound manager.
RichardE 5:0b0651ac7832 256 SoundManager::Instance.Update();
RichardE 5:0b0651ac7832 257 // Wait for frame flyback.
RichardE 9:fa7e7b37b632 258 gd->waitvblank();
RichardE 5:0b0651ac7832 259 }
RichardE 5:0b0651ac7832 260 // Now wait until all sounds have finished.
RichardE 5:0b0651ac7832 261 while( SoundManager::Instance.CountSoundsPlaying() > 0 ) {
RichardE 5:0b0651ac7832 262 // Update sound manager.
RichardE 5:0b0651ac7832 263 SoundManager::Instance.Update();
RichardE 5:0b0651ac7832 264 // Wait for frame flyback.
RichardE 9:fa7e7b37b632 265 gd->waitvblank();
RichardE 5:0b0651ac7832 266 }
RichardE 0:5fa232ee5fdf 267 }
RichardE 0:5fa232ee5fdf 268
RichardE 0:5fa232ee5fdf 269 /*************/
RichardE 0:5fa232ee5fdf 270 /* PLAY LOOP */
RichardE 0:5fa232ee5fdf 271 /*************/
RichardE 0:5fa232ee5fdf 272 // Returns code indicating how level ended.
RichardE 0:5fa232ee5fdf 273 // This method should be called from the Play method after the
RichardE 0:5fa232ee5fdf 274 // level data has been initialised and the return value returned
RichardE 0:5fa232ee5fdf 275 // by the Play method.
RichardE 5:0b0651ac7832 276 Level::LevelExitCode LevelNormal::PlayLoop( void )
RichardE 5:0b0651ac7832 277 {
RichardE 5:0b0651ac7832 278 // Do nothing if Gameduino has not been specified, level data is NULL or player has not been specified.
RichardE 5:0b0651ac7832 279 if( ( gd != (Gameduino*)NULL ) || ( DataForLevel != (LevelData*)NULL ) || ( player == (PlayerObject*)NULL ) ) {
RichardE 5:0b0651ac7832 280 // Point static pointer to current instance.
RichardE 5:0b0651ac7832 281 currentInstance = this;
RichardE 5:0b0651ac7832 282 // Do some initialisation first.
RichardE 5:0b0651ac7832 283 InitialiseLevel( gd );
RichardE 5:0b0651ac7832 284 // Redraw the screen.
RichardE 5:0b0651ac7832 285 DrawLevel();
RichardE 5:0b0651ac7832 286 // Wait for frame flyback once before entering loop so collision data is recalculated.
RichardE 5:0b0651ac7832 287 // At this point there should not be any sprites on the screen so no collisions
RichardE 5:0b0651ac7832 288 // should be found.
RichardE 5:0b0651ac7832 289 gd->waitvblank();
RichardE 5:0b0651ac7832 290 // Repeat until all enemies are dead or player is dead.
RichardE 5:0b0651ac7832 291 bool allEnemiesAreDead = false;
RichardE 5:0b0651ac7832 292 bool playerIsDead = false;
RichardE 5:0b0651ac7832 293 bool gameIsOver = false;
RichardE 5:0b0651ac7832 294 bool firstDraw = true;
RichardE 5:0b0651ac7832 295 while( ! allEnemiesAreDead && ! gameIsOver ) {
RichardE 5:0b0651ac7832 296 // Update sound manager.
RichardE 9:fa7e7b37b632 297 SoundManager::Instance.Update();
RichardE 5:0b0651ac7832 298 // Wait for frame flyback.
RichardE 5:0b0651ac7832 299 gd->waitvblank();
RichardE 5:0b0651ac7832 300 // Check for collisions between player and other objects.
RichardE 5:0b0651ac7832 301 CheckPlayerCollisions( &playerIsDead );
RichardE 5:0b0651ac7832 302 // Check for collisions between humans and enemies that squash.
RichardE 8:82d88f9381f3 303 GameObject::FindCollisions( gd, DataForLevel->Humans, LevelData::MaxHumans, &LevelNormal::HandleHumanCollision );
RichardE 5:0b0651ac7832 304 // Check for collisions between player bullets and enemies.
RichardE 8:82d88f9381f3 305 GameObject::FindCollisions( gd, player->GetBullets(), BulletManager::MaxBullets, &LevelNormal::HandleBulletCollision );
RichardE 5:0b0651ac7832 306 // Redraw the player's score and number of lives.
RichardE 5:0b0651ac7832 307 DrawScoreAndLives();
RichardE 7:e72691603fd3 308 // Draw all the enemies.
RichardE 7:e72691603fd3 309 GameObject::DrawAll( gd, DataForLevel->Enemies, LevelData::MaxEnemies );
RichardE 5:0b0651ac7832 310 // Draw all the humans.
RichardE 8:82d88f9381f3 311 GameObject::DrawAll( gd, DataForLevel->Humans, LevelData::MaxHumans );
RichardE 5:0b0651ac7832 312 // Draw all the explosions.
RichardE 8:82d88f9381f3 313 GameObject::DrawAll( gd, ExplosionManager::Instance.GetExplosions(), ExplosionManager::MaxExplosions );
RichardE 5:0b0651ac7832 314 // Draw the player.
RichardE 5:0b0651ac7832 315 player->Draw( gd );
RichardE 5:0b0651ac7832 316 // Draw the player's bullets.
RichardE 6:8bbdb70bc11c 317 GameObject::DrawAll( gd, player->GetBullets(), BulletManager::MaxBullets );
RichardE 5:0b0651ac7832 318 // Increment the frame counter.
RichardE 5:0b0651ac7832 319 FrameCounter++;
RichardE 5:0b0651ac7832 320 // After first redraw play level start sound and wait for it to end.
RichardE 5:0b0651ac7832 321 if( firstDraw ) {
RichardE 9:fa7e7b37b632 322 PlaySoundAndWait( Sounds::StartLevel );
RichardE 5:0b0651ac7832 323 firstDraw = false;
RichardE 1:dfd5eaaf96a3 324 }
RichardE 5:0b0651ac7832 325 // If player was killed then play death march and wait for it to finish.
RichardE 5:0b0651ac7832 326 if( playerIsDead ) {
RichardE 5:0b0651ac7832 327 #ifdef CHATTY
RichardE 5:0b0651ac7832 328 pc.puts( "Player got killed.\r\n" );
RichardE 5:0b0651ac7832 329 #endif
RichardE 5:0b0651ac7832 330 // Player got killed.
RichardE 9:fa7e7b37b632 331 PlaySoundAndWait( Sounds::PlayerDead );
RichardE 5:0b0651ac7832 332 // One less life for player.
RichardE 5:0b0651ac7832 333 if( player->Lives > 0 ) {
RichardE 5:0b0651ac7832 334 player->Lives--;
RichardE 5:0b0651ac7832 335 }
RichardE 5:0b0651ac7832 336 // Game is over when player has no more lives.
RichardE 5:0b0651ac7832 337 gameIsOver = ( player->Lives == 0 );
RichardE 5:0b0651ac7832 338 // If game is not over then re-initialise level using any remaining enemies.
RichardE 5:0b0651ac7832 339 if( ! gameIsOver ) {
RichardE 5:0b0651ac7832 340 #ifdef CHATTY
RichardE 5:0b0651ac7832 341 pc.puts( "Game is over.\r\n" );
RichardE 5:0b0651ac7832 342 #endif
RichardE 5:0b0651ac7832 343 // Remove all objects that do not survive a level restart (like enemy bullets).
RichardE 5:0b0651ac7832 344 GameObject::RemoveUnretainedObjects( DataForLevel->Enemies, LevelData::MaxEnemies );
RichardE 5:0b0651ac7832 345 InitialiseLevel( gd );
RichardE 5:0b0651ac7832 346 DrawLevel();
RichardE 5:0b0651ac7832 347 gd->waitvblank();
RichardE 5:0b0651ac7832 348 playerIsDead = false;
RichardE 5:0b0651ac7832 349 firstDraw = true;
RichardE 5:0b0651ac7832 350 }
RichardE 5:0b0651ac7832 351 }
RichardE 5:0b0651ac7832 352 else {
RichardE 5:0b0651ac7832 353 // Move all the enemies and check if all dead.
RichardE 5:0b0651ac7832 354 allEnemiesAreDead = ! GameObject::MoveAll( DataForLevel->Enemies, LevelData::MaxEnemies );
RichardE 5:0b0651ac7832 355 // If there are still some enemies alive then check if those that remain are indestructable.
RichardE 5:0b0651ac7832 356 // If only indestructable enemies survive then act as if all enemies are dead.
RichardE 5:0b0651ac7832 357 // You need to do this or you would never be able to complete a level that had indestructable
RichardE 5:0b0651ac7832 358 // enemies on it.
RichardE 5:0b0651ac7832 359 if( ! allEnemiesAreDead ) {
RichardE 5:0b0651ac7832 360 allEnemiesAreDead = EnemyObject::AreAllIndestructable(
RichardE 6:8bbdb70bc11c 361 (const EnemyObject**)DataForLevel->Enemies,
RichardE 6:8bbdb70bc11c 362 LevelData::MaxEnemies
RichardE 6:8bbdb70bc11c 363 );
RichardE 5:0b0651ac7832 364 }
RichardE 5:0b0651ac7832 365 // Move all the humans.
RichardE 5:0b0651ac7832 366 GameObject::MoveAll( DataForLevel->Humans, LevelData::MaxHumans );
RichardE 5:0b0651ac7832 367 // Move (update) all the explosions.
RichardE 5:0b0651ac7832 368 GameObject::MoveAll( ExplosionManager::Instance.GetExplosions(), ExplosionManager::MaxExplosions );
RichardE 5:0b0651ac7832 369 // Read the player's controls.
RichardE 5:0b0651ac7832 370 player->ReadControls();
RichardE 5:0b0651ac7832 371 // Move the player.
RichardE 5:0b0651ac7832 372 player->Move();
RichardE 5:0b0651ac7832 373 // Move the player's bullets.
RichardE 5:0b0651ac7832 374 GameObject::MoveAll( player->GetBullets(), BulletManager::MaxBullets );
RichardE 0:5fa232ee5fdf 375 }
RichardE 1:dfd5eaaf96a3 376 }
RichardE 5:0b0651ac7832 377 // Player completed level or game is over.
RichardE 5:0b0651ac7832 378 return gameIsOver ? GameOver : Completed;
RichardE 0:5fa232ee5fdf 379 }
RichardE 5:0b0651ac7832 380 else {
RichardE 5:0b0651ac7832 381 // Level data or player were not specified.
RichardE 5:0b0651ac7832 382 return Completed;
RichardE 5:0b0651ac7832 383 }
RichardE 0:5fa232ee5fdf 384 }