private void checkCollisions() { //------------------------------------------------------------------------------------------------------------------ // Purpose: Collision checking ... // // 1. If invader is diving: check for a collision between the player's ship and the invader ... // 2. If the invader is shooting: check for a collision between their bullets and the player's ship // 3. If the player is shooting: check for collisions between the player's bullets and the invader // 4. If the player has a sheild then they are protected against invader fire and collisions // 5. Tall up the total num of invaders still remaining on the screen ... // 6. Award points for shooting invaders //------------------------------------------------------------------------------------------------------------------ // General working vars int i = 0; int j = 0; int c = 0; // Specific vars ... int totalShot = 0; bool collision = false; bool invaderDead = false; if (!levelCompleted) { // Loop through all invader instances and check for collisions for (j = 0; j <= 59; j++) { // Void checking if player is dead ... if (!player.isDead()) { // :: Check if invader bullets have intersected with the player's ship :: if (invaders[j].isShooting()) { // Enemy's bullets rect1.X = invaders[j].getBulletRectX(); rect1.Y = invaders[j].getBulletRectY(); rect1.Width = invaders[j].getBulletRectW(); rect1.Height = invaders[j].getBulletRectH(); // Player's ship rect2.X = player.getRectX(); rect2.Y = player.getRectY(); rect2.Width = player.getRectW(); rect2.Height = player.getRectH(); // Collision? set flag and cease shooting if so if (rect1.IntersectsWith(rect2)) { collision = true; invaders[j].setDoShooting(false); } // Player have a sheild? if (player.hasSheild()) { // Player's sheild rect2.X = player.getSheildRectX(); rect2.Y = player.getSheildRectY(); rect2.Width = player.getSheildRectW(); rect2.Height = player.getSheildRectH(); if (rect1.IntersectsWith(rect2)) { particle[j] = new clsParticle(invaders[j].getBulletRectX(), invaders[j].getBulletRectY(), invaders[j].getBulletRectW(), invaders[j].getBulletRectH()); invaders[j].setDoShooting(false); } } } // :: Check for collisions between the invaders ship and the player's ship :: if (invaders[j].isActive()) { if (invaders[j].isDiving()) { // Enemy ship rect1.X = invaders[j].getRectX(); rect1.Y = invaders[j].getRectY(); rect1.Width = invaders[j].getRectW(); rect1.Height = invaders[j].getRectH(); // Player's ship rect2.X = player.getRectX(); rect2.Y = player.getRectY(); rect2.Width = player.getRectW(); rect2.Height = player.getRectH(); // Collision? set flag if so ... // invader dies along with the player, both player and invader are now inactive if (rect1.IntersectsWith(rect2)) { collision = true; dockActive[invaders[j].getDockingCell()] = false; invaders[j].setActive(false); } // Player have a sheild? if (player.hasSheild()) { // Player's sheild rect2.X = player.getSheildRectX(); rect2.Y = player.getSheildRectY(); rect2.Width = player.getSheildRectW(); rect2.Height = player.getSheildRectH(); if (rect1.IntersectsWith(rect2)) { particle[j] = new clsParticle(invaders[j].getX(), invaders[j].getY(), invaders[j].getW(), invaders[j].getH()); dockActive[invaders[j].getDockingCell()] = false; invaders[j].setActive(false); } } } } // Collision flag set? play sound effect and set player dead if (collision && clsShared.AbductionPhase == 0) { if (!player.DoubleWideShip) { player.setPlayerDead(true); //Abduction complete clsShared.global_gravity_warp_on = false; clsShared._clsPlayer.setWarpY(0); clsShared.Global_Abducted = false; clsShared.AbductionPhase = 0; } if (player.DoubleWideShip) { //Player reverts back to single size ship (and does not die) player.DoubleWideShip = false; } // Play snd effect if (true) { _clsCommon.sndEffects[6].playSND(false); _clsCommon.sndEffects[3].playSND(false); } } } //:: Check for collisions between the player's bullets and the invaders :: for (i = 0; i <= 5; i++) { for (c = 0; c <= 6; c++) { // Bullet instance must exist ... if (playerbullet[i, c] != null) { // Invader must be still active if (invaders[j].isActive()) { // 1. Player's bullet(s) rect1.X = playerbullet[i, c].getRectX(); rect1.Y = playerbullet[i, c].getRectY(); rect1.Width = playerbullet[i, c].getRectW(); rect1.Height = playerbullet[i, c].getRectH(); // 2. Enemy rect ... rect2.X = invaders[j].getRectX(); rect2.Y = invaders[j].getRectY(); rect2.Width = invaders[j].getRectW(); rect2.Height = invaders[j].getRectH(); // Collision? if (rect1.IntersectsWith(rect2)) { // Create new particle instance particle[j] = new clsParticle(invaders[j].getX(), invaders[j].getY(), invaders[j].getW(), invaders[j].getH()); // Different invaders have different degrees of points awarded ... // and some invaders require 2 or 3 shots before they are killed switch (invaders[j].getInvaderType()) { case 0: //:: Blue invader 1 shot kills :: // Inc score and set flag score += 50; invaderDead = true; if (invaders[j].ship_abducted) { //Return Ship to player clsShared.lives += 1; player.DoubleWideShip = true; clsShared.Global_Abducted = false; invaders[j].ship_abducted = false; //Play sound since Player regained ship _clsCommon.sndEffects[6].playSND(false); //WARP SOUND } break; case 1: //:: Red invader requires 2 shots to kill :: // Change colour to yellow invaders[j].setInvaderType(2); break; case 2: //:: Yellow invader, was prev red 2nd shot kills :: // Inc score and set flag invaderDead = true; score += 100; break; case 3: //:: Large green invader takes 3 shots to kill :: // Swap colour invaders[j].setInvaderType(4); break; case 4: //:: Large invader 2nd shot :: // Swap colour again invaders[j].setInvaderType(5); break; case 5: //:: Large invader final shot :: // Inc score and set flag invaderDead = true; score += 250; if (invaders[j].ship_abducted) { //Return ship to player clsShared.Global_Abducted = false; clsShared.lives += 1; player.DoubleWideShip = true; invaders[j].ship_abducted = false; _clsCommon.sndEffects[6].playSND(false); //WARP SOUND } // Drop pickup for killing this invader if (pickup == null) { // Fetch random pickup to drop bool voidPickup = false; c = getRandomNumber(0, 3); switch (c) { case 0: //:: x3 firepower :: // Void if player alrewady has it if (player.getFirePowerLevel() == 3) { voidPickup = true; } break; case 1: //:: Sheild :: // Void if player already has it if (player.hasSheild()) { voidPickup = true; } break; case 2: //:: x5 firepower :: // Void if player already has it if (player.getFirePowerLevel() == 5) { voidPickup = true; } break; } // Create new instance of pickup to scroll if (!voidPickup) { pickup = new clsPickups(invaders[j].getX(), invaders[j].getY(), c); } } break; } // Enemy flag set? invader is now inactive if (invaderDead) { dockActive[invaders[j].getDockingCell()] = false; invaders[j].setActive(false); invaderDead = false; // Play snd effect if (!sndEngineError) { _clsCommon.sndEffects[2].playSND(false); } } // Dispose of player bullet instance after collision playerbullet[i, c] = null; } } } } } // Tally up how many invaders are still active on the screen if (!invaders[j].isActive()) { totalShot += 1; } // When the entire 60 are shot the level has been completed ... if (totalShot == 60) { levelCompleted = true; // Sheild off player.setSheild(false); //bail return; } } } }
private void frmGame_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e) { //------------------------------------------------------------------------------------------------------------------ // Purpose: Terminate application -- clean up: dispose of resources //------------------------------------------------------------------------------------------------------------------ // Clean up kill instances playerbullet = null; vectorScroll = null; invaders = null; particle = null; stars = null; player = null; paths = null; _clsCommon.sndEffects = null; bkMusic = null; pickup = null; // Dump screen buffer ... scrnBufferBmp.Dispose(); graphicsBuffer.Dispose(); // Save high scores ... SaveHighScores(); // free FMOD sound engine from its duty if (!sndEngineError) { // FSOUND_Close(); } // Dump this instance and terminate this.Dispose(); System.Environment.Exit(0); }
private void doPickups() { //------------------------------------------------------------------------------------------------------------------ // Purpose: Deal with pickup weapons, scroll them down the screen and detect when the player has got it and apply it // // 1. Sheild which last for 10 secs ... // 2. X3 fire which gives the player 20 shots // 3. X5 fire which gives the player 15 shots //------------------------------------------------------------------------------------------------------------------ // The player can hold a sheild for 10 secs before they lose it if (player.hasSheild()) { player.setSheildTime(player.getSheildTime() + 1); if (player.getSheildTime() > 500) { player.setSheild(false); } } // x3 fire (20 shots) if (player.getFirePowerLevel() == 3) { if (player.getX3FireAmmo() <= 0) { player.setFirePower(1); } } // x5 fire (15 shots) if (player.getFirePowerLevel() == 5) { if (player.getX5FireAmmo() <= 0) { player.setFirePower(1); } } if (!levelCompleted) { if (pickup != null) { // Pickup ... rect1.X = pickup.getRectX(); rect1.Y = pickup.getRectY(); rect1.Width = pickup.getRectW(); rect1.Height = pickup.getRectH(); // Player's ship rect2.X = player.getRectX(); rect2.Y = player.getRectY(); rect2.Width = player.getRectW(); rect2.Height = player.getRectH(); if (pickup.getY() > ScreenHeight) { pickup = null; } else { pickup.movePickups(graphicsBuffer); } // Collision with pickup? assign the player with it and kill off instance if (rect1.IntersectsWith(rect2)) { switch (pickup.getPickupType()) { case 0: // x3 power player.setFirePower(3); player.setX3FireAmmo(200); break; case 1: // Sheild player.setSheild(true); player.setSheildTime(0); break; case 2: // x5 power player.setFirePower(5); player.setX5FireAmmo(50); break; } // Dispose pickup = null; // Play snd effect if (!sndEngineError) { _clsCommon.sndEffects[4].playSND(false); } } } } else // Kill instance { pickup = null; } }