private bool Validate_Movement(Vector3 future_position, Tree[] trees, Zombie[] zombies) { //Don't allow player to go off map if ((Math.Abs(future_position.X) > max_range) || (Math.Abs(future_position.Z) > max_range)) return false; //Don't allow player to go through trees if (Check_For_Tree_Collision(future_position, trees)) return false; if (Check_For_Zombie_Collision(future_position, zombies)) { if (health_timer > 15) { health -= 10; health_timer = 0; } return true; } return true; }
private bool Check_For_Zombie_Collision(Vector3 future_position, Zombie[] zombies) { for (int cur_zomb = 0; cur_zomb < zombies.Length; cur_zomb++) { if (zombies[cur_zomb].Killed) continue; if (Calc_Distance (zombies[cur_zomb].position, future_position) < GlobalState.COLLISION_DISTANCE +1) return true; } return false; }
/// <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 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); //UpdateAvatarPosition(); //UpdateCamera(); // TODO: Add your update logic here //NEW CODE float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio; lastKeyboardState = currentKeyboardState; currentKeyboardState = Keyboard.GetState(); MouseState currentMouseState = Mouse.GetState(); lastGamePadState = currentGamePadState; currentGamePadState = GamePad.GetState(PlayerIndex.One); double currentTime = gameTime.TotalGameTime.TotalSeconds; double elapsedTime = gameTime.ElapsedGameTime.TotalSeconds; explosionParticles.SetCamera(gameCamera.view_matrix, gameCamera.projection_matrix); // Allows the game to exit if ((currentKeyboardState.IsKeyDown(Keys.Escape)) || (currentGamePadState.Buttons.Back == ButtonState.Pressed)) this.Exit(); //game is currently loading if (currentGameState == GameState.Loading) { if ((lastKeyboardState.IsKeyDown(Keys.Enter) && (currentKeyboardState.IsKeyUp(Keys.Enter))) || currentGamePadState.Buttons.Start == ButtonState.Pressed) { roundTimer = roundTime; currentGameState = GameState.Running; } } if (currentGameState == GameState.Start) { if ((lastKeyboardState.IsKeyDown(Keys.Enter) && (currentKeyboardState.IsKeyUp(Keys.Enter))) || currentGamePadState.Buttons.Start == ButtonState.Pressed) { background_music.Play(); currentGameState = GameState.Running; } } if (currentGameState == GameState.Pause) { if ((lastKeyboardState.IsKeyDown(Keys.Enter) && currentKeyboardState.IsKeyUp(Keys.Enter))) currentGameState = GameState.Running; } //game is currently running if ((currentGameState == GameState.Running)) { player.Update(currentGamePadState, currentKeyboardState, trees, zombies, elapsedTime); gameCamera.Update(player.forward_direction, player.position, aspectRatio); background_music.Play(); if (currentMouseState.LeftButton == ButtonState.Pressed && (currentTime > lastShotTime + 0.5)) { gun_sound.Play(); guns.shoot(); checkBulletHit(); lastShotTime = currentTime; } zombiesKilled = 0; foreach (Zombie zombie in zombies) { if (zombie.Killed) zombiesKilled++; else zombie.Update(player.position); //check if player has reached a new level if (zombiesKilled == killTotal) { currentGameState = GameState.newLevel; background_music.Stop(); } } //check if game is won if (killTotal == GlobalState.WIN_NUM_ZOMBIES) { currentGameState = GameState.Won; background_music.Stop(); } roundTimer -= gameTime.ElapsedGameTime; //check if game is lost if (player.health == 0) { currentGameState = GameState.Lost; background_music.Stop(); } else if (currentKeyboardState.IsKeyDown(Keys.Space)) currentGameState = GameState.Pause; } if ((currentGameState == GameState.Won) || (currentGameState == GameState.Lost)) { // Reset the world for a new game if ((lastKeyboardState.IsKeyDown(Keys.Enter) && (currentKeyboardState.IsKeyUp(Keys.Enter))) || currentGamePadState.Buttons.Start == ButtonState.Pressed) { killTotal = 10; GAME_SCORE = 0; zombies = new Zombie[killTotal]; for (int index = 0; index < zombies.Length; index++) { zombies[index] = new Zombie(); zombies[index].LoadContent(Content, "Models/Zombie"); } level = 2; ResetGame(gameTime, aspectRatio); } } if (currentGameState == GameState.newLevel) { if ((lastKeyboardState.IsKeyDown(Keys.Enter) && (currentKeyboardState.IsKeyUp(Keys.Enter))) || currentGamePadState.Buttons.Start == ButtonState.Pressed) { killTotal += 10; zombies = new Zombie[killTotal]; for (int index = 0; index < zombies.Length; index++) { zombies[index] = new Zombie(); zombies[index].LoadContent(Content, "Models/Zombie"); } ResetGame(gameTime, aspectRatio); level = 3; } } base.Update(gameTime); }
public void Update(GamePadState gamepad_state, KeyboardState keyboard_state, Tree[] trees, Zombie[] zombies, double elapsedTime) { Vector3 future_position = position; health_timer++; float turn_amount = 0; if (keyboard_state.IsKeyDown(Keys.A)) turn_amount = 1; else if (keyboard_state.IsKeyDown(Keys.D)) turn_amount = -1; else if (gamepad_state.ThumbSticks.Left.X != 0) turn_amount = gamepad_state.ThumbSticks.Left.X; //calculate turn amount and direction forward_direction += turn_amount * GlobalState.PLAYER_TURN_SPEED * (float) elapsedTime; Matrix orientation_matrix = Matrix.CreateRotationY(forward_direction); Vector3 movement = Vector3.Zero; if (keyboard_state.IsKeyDown(Keys.W)) movement.Z = 1; else if (keyboard_state.IsKeyDown(Keys.S)) movement.Z = -1; else if (gamepad_state.ThumbSticks.Left.Y != 0) movement.Z = gamepad_state.ThumbSticks.Left.Y; //if (movement.Z > 0) //{ Vector3 speed = Vector3.Transform(movement, orientation_matrix); speed *= GlobalState.PLAYER_VELOCITY; future_position = position + speed; if (Validate_Movement(future_position, trees, zombies)) { position = future_position; } else position -= (3*speed); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. device = graphics.GraphicsDevice; zombie = Content.Load<Model>("Models/Zombie"); shotgun.model = Content.Load<Model>("Models/Shotgun"); handgun.model = Content.Load<Model>("Models/box"); //Guns guns.LoadContent(Content, "Models/Rifle2", "Models/box", "Textures/muzzleflash"); //guns.LoadContent(Content, "Textures/muzzleflash"); //tree = Content.Load<Model>("Models/firtree1"); grassTexture = Content.Load<Texture2D>("Textures/grass"); blackTexture = Content.Load<Texture2D>("Textures/grass"); brickTexture = Content.Load<Texture2D>("Textures/brick_wall_tex"); health_bar = Content.Load<Texture2D>("Textures/health_bar"); //Game Screens startGame = Content.Load<Texture2D>("Models/StartGame"); gameOver = Content.Load<Texture2D>("Models/GameOver"); winState = Content.Load<Texture2D>("Models/winState"); NEWLEVEL = Content.Load<Texture2D>("Models/NEWLEVEL"); pause_screen = Content.Load<Texture2D>("Models/Pause_Screen"); //Skybox skyboxModel = Content.Load<Model>("Textures/skybox2"); skyboxTransforms = new Matrix[skyboxModel.Bones.Count]; //new code ground.model = Content.Load<Model>("Models/box"); brick_wall.model = Content.Load<Model>("Models/box"); statsFont = Content.Load<SpriteFont>("Fonts/StatsFont"); spriteBatch = new SpriteBatch(GraphicsDevice); //Initialize zombies zombies = new Zombie[killTotal]; for (int index = 0; index < zombies.Length; index++) { zombies[index] = new Zombie(); zombies[index].LoadContent(Content, "Models/Zombie"); } //initialize trees trees = new Tree[GlobalState.NUM_TREES]; for (int index = 0; index < trees.Length; index++) { trees[index] = new Tree(); trees[index].LoadContent(Content, "Models/firtree1"); } //initialize player player = new Player(); player.LoadContent(Content, "Models/box"); //Function for placing trees and zombies PlaceTreesAndZombies(); // TODO: use this.Content to load your game content here SoundEffect soundEffect; soundEffect = Content.Load<SoundEffect>("audio/backgroundmusic"); background_music = soundEffect.CreateInstance(); soundEffect = Content.Load<SoundEffect>("audio/gunsound"); gun_sound = soundEffect.CreateInstance(); gun_sound.Volume = GlobalState.GUN_SOUND_VOLUME; background_music.IsLooped = true; background_music.Volume = GlobalState.BACKGROUND_MUSIC_VOLUME; }