public void Update(Player player, KeyboardState keyboard, MouseState mouse, Engine engine) { // Rortational Origin Rectangle originRect = new Rectangle((int)position.X, (int)position.Y, (int)texture.Width, (int)texture.Height); origin = new Vector2(originRect.Width / 2, originRect.Height / 2); int viewDistance = 200; double distance = Math.Sqrt(Math.Pow(player.position.X - position.X, 2) + Math.Pow(player.position.Y - position.Y, 2)); if (distance < viewDistance) { float deltaX = player.position.X - position.X; float deltaY = player.position.Y - position.Y; float radians = (float)Math.Atan2(deltaY, deltaX); setRotation(radians); float zombieSpeed = 1.5f; float dx = (float) Math.Cos(rotation) * zombieSpeed; float dy = (float) Math.Sin(rotation) * zombieSpeed; position.X += dx; position.Y += dy; } }
/// <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 (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); // TODO: Add your update logic here MouseState mouse = Mouse.GetState(); // Game State Managing switch (currentGameState) { case GameState.MainMenu: if (buttonPlay.isClicked == true) { currentGameState = GameState.Playing; // Player Initialization player = new Player(Content.Load<Texture2D>("res/player/playerRifle")); player.setPosition(50, 50); } buttonPlay.Update(mouse); break; case GameState.Playing: player.Update(Keyboard.GetState(), Mouse.GetState(), this); foreach (Zombie z in zombies) { z.Update(player, Keyboard.GetState(), mouse, this); } // Shooting The Weapon if (mouse.LeftButton == ButtonState.Pressed) { if (!leftClicked) { bullets.Add(new Bullet(Content.Load<Texture2D>("res/bullets/bullet2"), mouse, player.position, 20)); leftClicked = true; } } // Clearing The Mouse Button else { leftClicked = false; } // Making Zombies if (mouse.RightButton == ButtonState.Pressed) { if (!rightClicked) { zombies.Add(new Zombie(Content.Load<Texture2D>("res/ZomTest/zTest"),Mouse .GetState().X, Mouse.GetState().Y)); rightClicked = true; } } // Clearing The Mouse Button else { rightClicked = false; } // Bullet Updating foreach (Bullet b in bullets) { b.Update(); if (Vector2.Distance(b.position, player.position) > 750) b.isVisible = false; } // Clearing Lost Bullets for (int x = 0; x < bullets.Count; x++) { if (!bullets[x].isVisible) bullets.RemoveAt(x); } break; case GameState.Options: break; } base.Update(gameTime); }