protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load <SpriteFont>("Font"); //Camera cameraTransform = new Transform(); cameraTransform.LocalPosition = new Vector3(1200, 1150, 1550); cameraTransform.Rotation = new Quaternion(-0.7f, 0, 0, 0.13f); camera = new Camera(); camera.Transform = cameraTransform; cameraTransform.Rotate(Vector3.Right, Time.ElapsedGameTime * 180); camera.FarPlane = 10000; //Light light = new Light(); //Sound effect gunSound = Content.Load <SoundEffect>("Gun"); hitSound = Content.Load <SoundEffect>("Hit"); missSound = Content.Load <SoundEffect>("Explosion"); failSound = Content.Load <SoundEffect>("Fail"); BackgroundMusic = Content.Load <SoundEffect>("Music"); levelComplete = Content.Load <SoundEffect>("LevelComplete"); soundInstance = BackgroundMusic.CreateInstance(); soundInstance.Volume = 0.1f; soundInstance.Play(); targetList = new Target[numAsteroids]; stars = Content.Load <Texture2D>("Background"); random = new Random(); //Main Ship ship = new Ship(Content, camera, GraphicsDevice, light, "Spaceship_V3"); ship.Transform.LocalPosition = new Vector3(1250, 0, 2200); ship.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); //Ship 2 ship2 = new OtherShip(Content, camera, GraphicsDevice, light, "OtherSpaceship_V3"); ship2.Transform.LocalPosition = new Vector3(2000, 0, 2000); ship2.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); //Ship 3 ship3 = new OtherShip(Content, camera, GraphicsDevice, light, "OtherSpaceship_V3"); ship3.Transform.LocalPosition = new Vector3(10, 0, 1500); ship3.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); //Ship 4 ship4 = new OtherShip(Content, camera, GraphicsDevice, light, "OtherSpaceship_V3"); ship4.Transform.LocalPosition = new Vector3(500, 0, 1500); ship4.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); for (int i = 0; i < GameConstants.NumBullets; i++) { bulletList[i] = new Bullet(Content, camera, GraphicsDevice, light); bulletList[i].Transform.Scale = new Vector3(0.3f, 0.3f, 0.3f); bulletMov[i] = Vector3.Zero; } ResetAsteroids(); // look at the below private method targetList[0].Transform.LocalPosition = new Vector3(1250, 0, 500); targetList[1].Transform.LocalPosition = new Vector3(250, 0, 500); // *** Particle particleManager = new ParticleManager(GraphicsDevice, 100); particleEffect = Content.Load <Effect>("ParticleShader-complete"); particleTex = Content.Load <Texture2D>("fire"); IsMouseVisible = true; }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } InputManager.Update(); Time.Update(gameTime); numAsteroids = currentLevel + 1; //Debug Console.WriteLine(currentLevel + "\n"); if (proceed == true && currentLevel < 4 && gameOver == false) { if (currentLevel == 1) { ship.Update(); } if (currentLevel == 2) { ship.Update(); } if (currentLevel == 3) { ship.Update(); } for (int i = 0; i < GameConstants.NumBullets; i++) { bulletList[i].Update(); } for (int i = 0; i < numAsteroids; i++) { targetList[i].Update(); } //Level 1 if (currentLevel == 1 && contentLoaded == false && restart == true) { highScore = false; ship.Transform.LocalPosition = new Vector3(1250, 0, 2200); ship.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship2.Transform.LocalPosition = new Vector3(2000, 0, 2000); ship2.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship2.Transform.Rotation = new Quaternion(0, 0, 0, 0.6614378f); for (int i = 0; i < GameConstants.NumBullets; i++) { bulletList[i] = new Bullet(Content, camera, GraphicsDevice, light); bulletList[i].Transform.Scale = new Vector3(0.3f, 0.3f, 0.3f); bulletMov[i] = Vector3.Zero; } ResetAsteroids(); // look at the below private method targetList[0].Transform.LocalPosition = new Vector3(1250, 0, 500); targetList[1].Transform.LocalPosition = new Vector3(250, 0, 500); contentLoaded = true; restart = false; } //Level 2 if (currentLevel == 2 && contentLoaded == false) { ship.Transform.LocalPosition = new Vector3(2000, 0, 2200); ship.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship2.Transform.LocalPosition = new Vector3(1500, 0, 2000); ship2.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship2.Transform.Rotation = new Quaternion(0, 0, 0, 0.6614378f); ship3.Transform.LocalPosition = new Vector3(10, 0, 1500); ship3.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship3.Transform.Rotation = new Quaternion(0, 0, 0, 0.6614378f); for (int i = 0; i < GameConstants.NumBullets; i++) { bulletList[i] = new Bullet(Content, camera, GraphicsDevice, light); bulletList[i].Transform.Scale = new Vector3(0.3f, 0.3f, 0.3f); bulletMov[i] = Vector3.Zero; } ResetAsteroids(); // look at the below private method targetList[0].Transform.LocalPosition = new Vector3(2000, 0, 500); targetList[1].Transform.LocalPosition = new Vector3(1500, 0, 500); targetList[2].Transform.LocalPosition = new Vector3(1000, 0, 500); contentLoaded = true; } //Level 3 if (currentLevel == 3 && contentLoaded == false) { ship.Transform.LocalPosition = new Vector3(2000, 0, 2200); ship.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship2.Transform.LocalPosition = new Vector3(1500, 0, 2000); ship2.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship2.Transform.Rotation = new Quaternion(0, 0, 0, 0.6614378f); ship3 = new OtherShip(Content, camera, GraphicsDevice, light, "OtherSpaceship_V3"); ship3.Transform.LocalPosition = new Vector3(10, 0, 1500); ship3.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); ship4 = new OtherShip(Content, camera, GraphicsDevice, light, "OtherSpaceship_V3"); ship4.Transform.LocalPosition = new Vector3(500, 0, 1500); ship4.Transform.Scale = new Vector3(0.25f, 0.25f, 0.25f); for (int i = 0; i < GameConstants.NumBullets; i++) { bulletList[i] = new Bullet(Content, camera, GraphicsDevice, light); bulletList[i].Transform.Scale = new Vector3(0.3f, 0.3f, 0.3f); bulletMov[i] = Vector3.Zero; } ResetAsteroids(); // look at the below private method targetList[0].Transform.LocalPosition = new Vector3(1250, 0, 500); targetList[1].Transform.LocalPosition = new Vector3(2250, 0, 500); targetList[2].Transform.LocalPosition = new Vector3(100, 0, 500); targetList[3].Transform.LocalPosition = new Vector3(2000, 0, 500); contentLoaded = true; } //Shoot if (InputManager.IsMousePressed(0) && lose == false && ship1shot == false) { ship1shot = true; //Bullet from main ship for (int i = 0; i < GameConstants.NumBullets; i++) { bulletMov[i] = ship.Transform.Forward; firstShotFired = true; if (!bulletList[i].isActive) { bulletList[i].Rigidbody.Velocity = (ship.Transform.Forward) * GameConstants.BulletSpeedAdjustment; bulletList[i].Transform.LocalPosition = ship.Transform.Position + (200 * bulletList[i].Transform.Forward); bulletList[i].isActive = true; score -= GameConstants.ShotPenalty; // sound soundInstance = gunSound.CreateInstance(); soundInstance.Play(); break; //exit the loop } } } //Ship 2 Shoot if (bulletList[0].isActive && bulletList[0].Transform.LocalPosition.Z <= ship2.Transform.LocalPosition.Z && lose == false && ship2shot == false) { ship2shot = true; for (int i = 0; i < GameConstants.NumBullets; i++) { bulletMov[i] = ship.Transform.Forward; if (!bulletList[i].isActive) { bulletList[i].Rigidbody.Velocity = (ship.Transform.Forward) * GameConstants.BulletSpeedAdjustment; bulletList[i].Transform.LocalPosition = ship2.Transform.Position + (200 * bulletList[i].Transform.Forward); bulletList[i].isActive = true; score -= GameConstants.ShotPenalty; // sound soundInstance = gunSound.CreateInstance(); soundInstance.Play(); break; //exit the loop } } } //Ship 3 Shoot if (bulletList[0].isActive && bulletList[0].Transform.LocalPosition.Z <= ship3.Transform.LocalPosition.Z && lose == false && ship3shot == false && currentLevel > 1) { ship3shot = true; for (int i = 0; i < GameConstants.NumBullets; i++) { bulletMov[i] = ship.Transform.Forward; if (!bulletList[i].isActive) { bulletList[i].Rigidbody.Velocity = (ship.Transform.Forward) * GameConstants.BulletSpeedAdjustment; bulletList[i].Transform.LocalPosition = ship3.Transform.Position + (200 * bulletList[i].Transform.Forward); bulletList[i].isActive = true; score -= GameConstants.ShotPenalty; // sound soundInstance = gunSound.CreateInstance(); soundInstance.Play(); break; //exit the loop } } } //Ship 4 Shoot if (bulletList[0].isActive && bulletList[0].Transform.LocalPosition.Z <= ship4.Transform.LocalPosition.Z && lose == false && ship4shot == false && currentLevel > 2) { ship4shot = true; for (int i = 0; i < GameConstants.NumBullets; i++) { bulletMov[i] = ship.Transform.Forward; if (!bulletList[i].isActive) { bulletList[i].Rigidbody.Velocity = (ship.Transform.Forward) * GameConstants.BulletSpeedAdjustment; bulletList[i].Transform.LocalPosition = ship4.Transform.Position + (200 * bulletList[i].Transform.Forward); bulletList[i].isActive = true; score -= GameConstants.ShotPenalty; // sound soundInstance = gunSound.CreateInstance(); soundInstance.Play(); break; //exit the loop } } } //Toggle Instructions if (InputManager.IsKeyPressed(Keys.I)) { showInstructions = !showInstructions; } for (int i = 0; i < GameConstants.NumBullets; i++) { bulletMov[i] = ship.Transform.Forward; } if (levelOver == true) { levelOver = false; proceed = false; } //Target Stuff Vector3 normal; for (int i = 0; i < targetList.Length; i++) { if (targetList[i].isActive) { if (ship.Collider.Collides(targetList[i].Collider, out normal)) { lose = true; } for (int j = 0; j < bulletList.Length; j++) { if (bulletList[j].isActive) { if (targetList[i].Collider.Collides(bulletList[j].Collider, out normal)) { //Sound Effect soundInstance = hitSound.CreateInstance(); soundInstance.Play(); // Particles Particle particle = particleManager.getNext(); particle.Position = targetList[i].Transform.Position; particle.Velocity = new Vector3( random.Next(-5, 5), 2, random.Next(-50, 50)); particle.Acceleration = new Vector3(0, 3, 0); particle.MaxAge = random.Next(1, 6); particle.Init(); targetList[i].isActive = false; bulletList[j].isActive = false; score += GameConstants.KillBonus; break; //no need to check other bullets } } } } } // particles update particleManager.Update(); for (int i = 0; i < bulletList.Length; i++) { bulletList[i].Transform.LocalPosition += bulletMov[i] * 100; } win = true; for (int i = 0; i < targetList.Length; i++) { if (targetList[i].isActive == true) { win = false; } // targetList[i].Transform.LocalPosition += (ship.Transform.LocalPosition - targetList[i].Transform.LocalPosition) * 0.001f; } //Check for win win = true; for (int i = 0; i < targetList.Length; i++) { if (targetList[i].isActive == true) { win = false; } } //Win if (win == true && levelOver == false && currentLevel < 4) { soundInstance = levelComplete.CreateInstance(); soundInstance.Play(); score += GameConstants.KillBonus; win = false; levelOver = true; Console.WriteLine("REEEEEEEEEEEEEE"); currentLevel++; numAsteroids++; contentLoaded = false; proceed = false; if (currentLevel == 2) { targetList = new Target[numAsteroids]; ResetAsteroids(); targetList[0].Transform.LocalPosition = new Vector3(250, 0, 500); targetList[1].Transform.LocalPosition = new Vector3(1250, 0, 500); targetList[2].Transform.LocalPosition = new Vector3(2000, 0, 500); } if (currentLevel == 3) { targetList = new Target[numAsteroids]; ResetAsteroids(); targetList[0].Transform.LocalPosition = new Vector3(250, 0, 500); targetList[1].Transform.LocalPosition = new Vector3(1000, 0, 500); targetList[2].Transform.LocalPosition = new Vector3(1250, 0, 500); targetList[3].Transform.LocalPosition = new Vector3(2000, 0, 500); } // firstShotFired = false; //allGone = false; // contentLoaded = false; } //Check if bullets aren't active allGone = true; for (int i = 0; i < bulletList.Length; i++) { if (bulletList[i].isActive && firstShotFired == true) { allGone = false; } } if (firstShotFired == true) { //Console.WriteLine(bulletList[0].Transform.Position); for (int i = 0; i < bulletList.Length && bulletList[i].isActive; i++) { if (bulletList[i].Transform.Position.X < -500 || bulletList[i].Transform.Position.X > 2800 || bulletList[i].Transform.Position.Z <0 || bulletList[i].Transform.Position.Z> (GameConstants.PlayfieldSizeY + 200)) { lose = true; // Console.WriteLine(i + ": " + bulletList[i].Transform.Position); } } } if (allGone == true && firstShotFired == true && win == false && levelOver == false) { lose = true; } if (lose == true && levelOver == false) { soundInstance = failSound.CreateInstance(); soundInstance.Play(); levelOver = true; } //Clear Bullets if (proceed == false) { for (int i = 0; i < bulletList.Length; i++) { bulletList[i].isActive = false; } } /* * //Debug * if (InputManager.IsKeyDown(Keys.P)) * { * cameraTransform.Rotate(Vector3.Right, -Time.ElapsedGameTime); * } * * if (InputManager.IsKeyDown(Keys.PageUp)) * { * cameraTransform.Position += Vector3.Down * 50; * } * * if (InputManager.IsKeyDown(Keys.PageDown)) * { * cameraTransform.Position += Vector3.Up * 50; * } * * if (InputManager.IsKeyDown(Keys.Up)) * { * cameraTransform.Position += Vector3.Forward * 50; * } * * if (InputManager.IsKeyDown(Keys.Down)) * { * cameraTransform.Position += Vector3.Backward * 50; * } * * if (InputManager.IsKeyDown(Keys.Right)) * { * cameraTransform.Position += Vector3.Right * 50; * } * * if (InputManager.IsKeyDown(Keys.Left)) * { * cameraTransform.Position += Vector3.Left * 50; * } */ } //Wait on click else { if (InputManager.IsKeyPressed(Keys.Enter)) { proceed = true; win = false; lose = false; titleScreen = false; levelOver = false; firstShotFired = false; ship1shot = false; ship2shot = false; ship3shot = false; ship4shot = false; if (InputManager.IsKeyDown(Keys.I)) { showInstructions = !showInstructions; } // allGone = false; // contentLoaded = false; if (gameOver == true || gameBeaten == true) { numAsteroids = 2; win = false; lose = false; currentLevel = 0; gameOver = false; gameBeaten = false; timeTaken = 0; titleScreen = true; timeStop = false; restart = true; } } } base.Update(gameTime); }