bool CheckParticleCollision(Collision sprite1, ParticleManager particles) { for (int particle = 0; particle < particles.particles.Count; particle++) { sprite2 = particles.GetCollider(particle); if (sprite1.visiblePixelCollision(sprite2)) { particles.Kill(particle); particles.Update(); particle--; return(true); } } return(false); }
void UpdateList(List <SpriteStripManager> list, SpriteStripManager item, GameTime gameTime) { for (int i = 0; i < list.Count; i++) { int hit = 0; if (list[i].myID == 2) { // if the item in the list is an alien it reduces the shotcooldown and allows the ai to control it. list[i].shotcooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds; if (myShip.Scale == 1) { ai.Think(list[i], myShip, controller, EnemyBulletParticles, soundlistinstance[0]); } } controller.Update(list[i]); list[i].Update(); sprite1 = list[i].GetCollider(); if (list[i].isCollectable == false) { //these checks are for when the player hits objects with it's bullets for (int particle = 0; particle < PlayerBulletParticles.particles.Count; particle++) { sprite2 = PlayerBulletParticles.GetCollider(particle); if (CheckParticleCollision(sprite1, PlayerBulletParticles)) { //create 2 small asteroids when a large one is destroyed if (list[i].Scale >= 0.5 && list[i].myID == 1) { spawner.SpawnChildren(item, list, asteroidIdle, i, (int)Difficulty.difficulty / 2, ranGen); numAsteroid += (int)Difficulty.difficulty / 2; } if (list[i].Scale > 0.5 && list[i].myID == 2) { //spawns 0 aliens at difficulties 0-2, 1 alien from 3-5 and 2 aliens on the other difficulties when an alien is destroyed spawner.SpawnChildren(item, list, alienIdle, i, (int)Difficulty.difficulty / 3, ranGen); numAliens += (int)Difficulty.difficulty / 3; } //remove asteroid when hit by player shot GenerateParticleEffects(list[i]); if (list[i].myID == 1) { if (list[i].Scale == 1) { soundlist[1].Play(); } else if (list[i].Scale == 0.5) { soundlist[2].Play(); } else { soundlist[3].Play(); } hud.score += 50; extralifecounter += 50; numAsteroid--; } else { numAliens--; hud.score += 100; extralifecounter += 100; soundlist[4].Play(); } list.RemoveAt(i); hit = 1; } } } //if the player has been hit before than the following is skipped if (hit == 0) { //these checks are for when the player collides with other objects. if (timesincelasthit > 2 || list[i].isCollectable) { sprite2 = myShip.GetCollider(); if (sprite1.visiblePixelCollision(sprite2)) { if (list[i].Scale >= 0.5 && list[i].myID == 1) { spawner.SpawnChildren(item, list, asteroidIdle, i, (int)Difficulty.difficulty / 2, ranGen); numAsteroid += (int)Difficulty.difficulty / 2; } if (list[i].Scale > 0.5 && list[i].myID == 2) { //spawns 0 aliens at difficulties 0-2, 1 alien from 3-5 and 2 aliens on the other difficulties when an alien is destroyed spawner.SpawnChildren(item, list, alienIdle, i, (int)Difficulty.difficulty / 3, ranGen); numAliens += (int)Difficulty.difficulty / 3; } //remove asteroid when hit by player shot if (list[i].myID == 1) { numAsteroid--; hud.score += 25; extralifecounter += 25; if (list[i].Scale == 1) { soundlist[1].Play(); } else if (list[i].Scale == 0.5) { soundlist[2].Play(); } else { soundlist[3].Play(); } } else if (list[i].myID == 2) { numAliens--; hud.score += 50; extralifecounter += 50; soundlist[4].Play(); } else { numStars--; hud.score += 100; extralifecounter += 50; soundlist[5].Play(); } if (!list[i].isCollectable) { //if the item collided with is not a collectable then the player is destroyed. GenerateParticleEffects(list[i]); GenerateParticleEffects(myShip); hud.lives--; myShip.Scale = 0; timesincelasthit = 0; soundlist[1].Play(); } list.RemoveAt(i); } } } } }
void UpdateGameplay(GameTime gameTime) { //if (soundlistinstance[10].State != SoundState.Playing) //{ // soundlistinstance[10].Volume = 0.1f; // soundlistinstance[10].IsLooped = true; // soundlistinstance[10].Play(); //} //main gameplay loop for each gametype elapsedtime += (float)gameTime.ElapsedGameTime.TotalSeconds; if (CheckObjectiveComplete()) { RemoveEnemies(); if (hud.objective == "") { InitializeNewGame(); } else { myPortal.Scale = 1; } } if (extralifecounter > 10000) { extralifecounter = 0; hud.lives++; soundlist[6].Play(); } if (hud.lives <= 0) { //soundlistinstance[10].Stop(); _state = GameState.HighScores; RemoveEnemies(); Update(gameTime); } Respawn(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (myShip.Scale == 1) { //if the ships scale isn't one that means that the player is currently dead so inputs aren't needed. input.Update(); if (input.UpdateThumbStickAngle() != 0) { controller.RotateTo(myShip, input.UpdateThumbStickAngle()); } if (input.IsKeyDown(Keys.Left)) { controller.Rotate(myShip, -0.05f); } if (input.IsKeyDown(Keys.Right)) { controller.Rotate(myShip, 0.05f); } if (input.IsKeyDown(Keys.Up) || input.IsButtonDown(Buttons.RightTrigger)) { soundlistinstance[6].IsLooped = true; soundlistinstance[6].Play(); myShip.setCurrentAction("Moving"); controller.Move(myShip); } else if (input.isMouseButtonDown(1)) { soundlistinstance[6].IsLooped = true; soundlistinstance[6].Play(); myShip.setCurrentAction("Moving"); Vector2 mouse = input.getMousePos(); float angle = (float)((Math.Atan2(mouse.Y - myShip.YPos, mouse.X - myShip.XPos) + MathHelper.Pi / 2)); controller.RotateTo(myShip, angle); controller.Move(myShip); } else { soundlistinstance[6].Pause(); myShip.setCurrentAction("Idle"); controller.Stop(myShip); } if (input.isMouseButtonDown(2)) { Vector2 mouse = input.getMousePos(); float angle = (float)((Math.Atan2(mouse.Y - myShip.YPos, mouse.X - myShip.XPos) + MathHelper.Pi / 2)); controller.RotateTo(myShip, angle); } if (input.WasKeyPressed(Keys.Space) || input.WasButtonPressed(Buttons.A)) { controller.Shoot(myShip, PlayerBulletParticles, soundlistinstance[0]); } else if (input.isMouseButtonClick(2)) { Vector2 mouse = input.getMousePos(); float angle = (float)((Math.Atan2(mouse.Y - myShip.YPos, mouse.X - myShip.XPos) + MathHelper.Pi / 2)); controller.RotateTo(myShip, angle); controller.Shoot(myShip, PlayerBulletParticles, soundlistinstance[0]); } } controller.Update(myShip); myShip.Update(); PlayerBulletParticles.Update(); EnemyBulletParticles.Update(); ParticleEffects.Update(); //this causes the game to pause for 2 seconds before starting each time it changes gametype if (elapsedtime > 2) { if (hud.objective != "DESTROY ALL ASTEROIDS" && hud.objective != "DESTROY ALL ALIENS") { if (elapsedtime % (10 - Difficulty.difficulty) < gameTime.ElapsedGameTime.TotalSeconds) { //spawns asteroids every 10 seconds to begin with and increases the spawning rate as time goes on spawner.NewItem(newobject, objectlist, asteroidIdle, 1, ranGen); numAsteroid++; if (hud.objective == "") { //everytime an asteroid is spawned in the tutorial the difficulty increases Difficulty.difficulty++; } } if (elapsedtime % (12 - Difficulty.difficulty) < gameTime.ElapsedGameTime.TotalSeconds) { //spawns aliens every 12 seconds to begin with and increases the spawning rate as time goes on spawner.NewItem(newobject, objectlist, alienIdle, 2, ranGen); numAliens++; if (ranGen.Next(1, 4) > 3) { //25% chance of spawning a small alien. objectlist[objectlist.Count - 1].Scale = 0.5f; } } } timesincelasthit += (float)gameTime.ElapsedGameTime.TotalSeconds; UpdateList(objectlist, newobject, gameTime); sprite1 = myShip.GetCollider(); if (timesincelasthit > 2) { //the player is invincble for 2 seconds after being hit if (CheckParticleCollision(sprite1, EnemyBulletParticles)) { GenerateParticleEffects(myShip); hud.lives--; myShip.Scale = 0; timesincelasthit = 0; soundlist[2].Play(); } } if (myPortal.Scale == 1) { //after the objective is completed a portal spawns and sucks the player into it. if (lastPlayed == 7 && soundlistinstance[7].State == SoundState.Stopped) { soundlistinstance[8].Play(); lastPlayed = 8; } else if (soundlistinstance[8].State == SoundState.Stopped) { soundlistinstance[7].Play(); lastPlayed = 7; } controller.Move(myShip, (float)(Math.Atan2(myShip.YPos - myPortal.YPos, myShip.XPos - myPortal.XPos) + MathHelper.Pi / 2), -1); myPortal.Update(); sprite2 = myPortal.GetCollider(); if (sprite1.visiblePixelCollision(sprite2)) { soundlistinstance[8].Stop(); myPortal.Scale = 0; InitializeNewGame(); } } } if (hud.objective == "DESTROY ALL ALIENS") { hud.objectivenumber = numAliens; } if (hud.objective == "DESTROY ALL ASTEROIDS") { hud.objectivenumber = numAsteroid; } if (hud.objective == "COLLECT") { hud.objectivenumber = numStars; } hud.Update(gameTime); }