// Spawn a new Superman enemy at a random location private void SpawnSuperman() { /* * Renerate random Location */ // for X location: Spawn in between 100 left to 100 right -> Domain is from 100 to 700 Horizontally int x = GetRandomLocation(GameVariables.SPAWN_LOCATION_MIN, graphics.PreferredBackBufferWidth - 2 * GameVariables.SPAWN_LOCATION_MIN); // for Y location: Spawn in between 100 top to 100 down -> Domain is from 100 to 500 Vertically int y = GetRandomLocation(GameVariables.SPAWN_LOCATION_MIN, graphics.PreferredBackBufferHeight - 2 * GameVariables.SPAWN_LOCATION_MIN); /* * Renerate random Speed for Superman */ float speed = GetRandomSpeed(GameVariables.SUPERMAN_MAX_SPEED); float angle = RandomGenerator.NextFloat((float)Math.PI * 2); Vector2 velocity = new Vector2((float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle))); /* * Make a new Alive Man of Steel */ Console.WriteLine(x + y); Superman newSuperMan = new Superman(Content, "superfat", x, y, velocity, SupermanShoot, SupermanGetHit); //make the superman does not spawn right at the collistion List <Rectangle> collideRectangles = getListCollideRectangles(); while (!CollisionUtils.IsCollisionFree(newSuperMan.sRectangle, collideRectangles)) { // for X location: Spawn in between 100 left to 100 right -> Domain is from 100 to 700 Horizontally newSuperMan.x = GetRandomLocation(GameVariables.SPAWN_LOCATION_MIN, graphics.PreferredBackBufferWidth - 2 * GameVariables.SPAWN_LOCATION_MIN); // for Y location: Spawn in between 100 top to 100 down -> Domain is from 100 to 500 Vertically newSuperMan.y = GetRandomLocation(GameVariables.SPAWN_LOCATION_MIN, graphics.PreferredBackBufferHeight - 2 * GameVariables.SPAWN_LOCATION_MIN); } /* * Add New Enemies */ supermen.Add(newSuperMan); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit // Allows user to exit the game by using Escape Button if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // TODO: Add your update logic here // Making "GOKU" character following the mouse state MouseState mouse = Mouse.GetState(); goku.Update(gameTime, mouse); // Update every objects in the game every milisecond foreach (Superman superfat in supermen) { superfat.Update(gameTime); } foreach (Weapon weapon in weapons) { weapon.Update(gameTime); } foreach (Explosion explosion in explosions) { explosion.Update(gameTime); } // Check and Update the collisions between enemies for (int i = 0; i < supermen.Count; i++) { for (int j = i + 1; j < supermen.Count; j++) { if (supermen[i].Alive && supermen[j].Alive) { EnemiesCollisionInfor collis = CollisionUtils.CheckCollision( gameTime.ElapsedGameTime.Milliseconds, GameVariables.SCREEN_WIDTH, GameVariables.SCREEN_WIDTH, supermen[i].sVelocity, supermen[i].sRectangle, supermen[j].sVelocity, supermen[j].sRectangle); if (collis != null) { if (collis.firstOutBounds) { supermen[i].Alive = false; } else { supermen[i].sVelocity = collis.getFirstVelocity; supermen[i].sRectangle = collis.getFirstRectangle; } if (collis.firstOutBounds) { supermen[j].Alive = false; } else { supermen[j].sVelocity = collis.getSecondVelocity; supermen[j].sRectangle = collis.getSecondRectangle; } } } } } // Check and Update what happen if the Goku's fire hits a superman foreach (Superman superfat in supermen) { foreach (Weapon weapon in weapons) { if (weapon.Type == WeaponType.GokuWeapon && weapon.Active && superfat.Alive && superfat.getRectangle.Intersects(weapon.wRectangle)) { weapon.Active = false; superfat.Alive = false; score += GameVariables.KILLED_SUPERMAN_AWARD; // Make a new Explosion when the superfat is not Alive Explosion newExplode = new Explosion(explosionSprite, superfat.currentLocation.X, superfat.currentLocation.Y); newExplode.Sound = explosion; // Add the Explosion to the Array List explosions.Add(newExplode); //play sound if (!Game1.IsLosing) { newExplode.Sound.Play(); } } } } // Check if Goku gets hit by touching the superman foreach (Superman superfat in supermen) { if (superfat.Alive && goku.getRectangle.Intersects(superfat.sRectangle)) { goku.health -= GameVariables.SUPERMAN_DAMAGES_HIMSELF; superfat.Alive = false; score += GameVariables.KILLED_SUPERMAN_AWARD; explosions.Add(new Explosion(explosionSprite, superfat.currentLocation.X, superfat.currentLocation.Y)); if (!Game1.IsLosing) { GokuGetHit.Play(); explosion.Play(); } } } // Check if Goku gets hit by touch the superman's fires foreach (Weapon weapon in weapons) { if (weapon.Type == WeaponType.SupermanWeapon && weapon.wRectangle.Intersects(goku.getRectangle)) { weapon.Active = false; goku.health -= GameVariables.SUPERMAN_SHOOTING_DAMAGES; if (!Game1.IsLosing) { GokuGetHit.Play(); } } } // Clean out the superman objects which has the Aclive status is false for (int i = supermen.Count - 1; i >= 0; i--) // remove from the back of the list { if (!supermen[i].Alive) { supermen.RemoveAt(i); } } while (supermen.Count < GameVariables.MAX_NUM_SUPERMEN) { SpawnSuperman(); } // Clean out the weapon objects which has the Aclive status is false for (int i = weapons.Count - 1; i >= 0; i--) // remove from the back of the list { if (!weapons[i].Active) { weapons.RemoveAt(i); } } // Clean out the explosion when it finishes for (int i = explosions.Count - 1; i >= 0; i--) // remove from the back of the list { if (explosions[i].Finish) { explosions.RemoveAt(i); } } // display current score and health healthString = GameVariables.HEALTH + goku.health; scoreString = GameVariables.SCORE + score; // Always check if we are losing or not IsLostAlready(); base.Update(gameTime); }