/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { font = Content.Load<SpriteFont>("Fonts\\Font"); player = new Player(); crosshair = new Crosshair(); sniperRifle = new SniperRifle(); machineGun = new MachineGun(); weapon = sniperRifle; sniperRifle.isSelected = true; machineGun.isSelected = false; waveManager = new WaveManager(); scavengerManager = new ScavengerManager(); currentScavengeCommand = 0; victoryTexture = new AnimatedSprite(Content.Load<Texture2D>("Graphics\\Victory"), 3, 2, animationSpeed); base.Initialize(); }
/// <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) { previousKeyboardState = currentKeyboardState; previousMouseState = currentMouseState; currentKeyboardState = Keyboard.GetState(); currentMouseState = Mouse.GetState(); previousGamepadState = currentGamepadState; currentGamepadState = GamePad.GetState(PlayerIndex.One); if (currentGamepadState.Buttons.Back == ButtonState.Pressed || currentKeyboardState.IsKeyDown(Keys.Escape)) Exit(); if (isInMenu) { menu.Update(gameTime, currentGamepadState); } else if (victory) { victoryTexture.Update(); } else { if (skipTutorial) { waveManager.skipTutorialWaves(); skipTutorial = false; } if (waveManager.State != 0 && (Keyboard.GetState().IsKeyDown(Keys.R) || currentGamepadState.Buttons.X == ButtonState.Pressed)) { weapon.reload(gameTime); } if (waveManager.State != 0 && (Keyboard.GetState().IsKeyDown(Keys.Q) && !previousKeyboardState.IsKeyDown(Keys.Q) || (currentGamepadState.Buttons.Y == ButtonState.Pressed && !(previousGamepadState.Buttons.Y == ButtonState.Pressed)))) { if (sniperRifle.isSelected) { sniperRifle.isSelected = false; machineGun.isSelected = true; weapon = machineGun; } else { sniperRifle.isSelected = true; machineGun.isSelected = false; weapon = sniperRifle; } crosshair.interruptAiming(); } int scavengeCommand = -1; if (waveManager.State != 0 && (Keyboard.GetState().IsKeyDown(Keys.W) && !previousKeyboardState.IsKeyDown(Keys.W) || (currentGamepadState.Buttons.A == ButtonState.Pressed && !(previousGamepadState.Buttons.A == ButtonState.Pressed)))) { if (currentScavengeCommand == 0) { currentScavengeCommand = 1; scavengeCommand = 1; } else { currentScavengeCommand = 0; scavengeCommand = 0; } } crosshair.Update(currentMouseState, weapon, gameTime, waveManager.getWave(), scavengerManager.getActiveScavenger(), GraphicsDevice); waveManager.Update(gameTime); scavengerManager.Update(scavengeCommand, gameTime, waveManager.getWave()); } base.Update(gameTime); }
public void Update(MouseState currentMouseState, Weapon weapon, GameTime gameTime, Wave wave, Scavenger scavenger, GraphicsDevice graphicsDevice) { if (waveManager.State != 0) { if (State == 0) //Moving this check after this block creates cool redirect mechanic { aimingAngle = -100; float aimX = weapon.waveManager.getWave().layout.weaponGunpoint.X + weapon.waveManager.getWave().layout.crosshairAdjustX; float aimY = weapon.waveManager.getWave().layout.weaponGunpoint.Y + weapon.waveManager.getWave().layout.crosshairAdjustY; if (MainGame.currentGamepadState.IsConnected) { aimingVector.X = MainGame.currentGamepadState.ThumbSticks.Left.X; aimingVector.Y = -1 * MainGame.currentGamepadState.ThumbSticks.Left.Y; aimingVector.Normalize(); aimingAngle = (float)Math.Atan2(aimingVector.Y, aimingVector.X); if (!waveManager.getWave().layout.checkAimingVector(aimingVector)) { aimingAngle = -100; } } else { if (aimingAngle == -100) { aimingVector.X = currentMouseState.X - aimX; aimingVector.Y = currentMouseState.Y - aimY; aimingVector.Normalize(); aimingAngle = (float)Math.Atan2(aimingVector.Y, aimingVector.X); if (!waveManager.getWave().layout.checkAimingVector(aimingVector)) { aimingAngle = -100; } } } //If not firing, listen for aiming click //If clicked, place crosshair in front of gun //and switch to aiming state, //recording aiming vector //Also confirm that vector is going in logical direction if (currentMouseState.RightButton == ButtonState.Pressed || (MainGame.currentGamepadState.Buttons.LeftShoulder == ButtonState.Pressed)) // || (MainGame.currentGamepadState.Triggers.Left >= MainGame.triggerThreshold)) //is what we want { if (waveManager.getWave().layout.checkAimingVector(aimingVector) && weapon.reloadOver(gameTime) && !(weapon.clipSupply == 0 && weapon.ammoSupply == 0)) { aimingTimestamp = gameTime.TotalGameTime; Position.X = aimX; Position.Y = aimY; State = 1; } } } else { //If aiming, listen for firing click //If the aiming button is released, place it back offscreen //switch to not firing state //Otherwise, move crosshair along aiming vector //and if there is a firing clip, determine hit //and ammo changes or reload need if (currentMouseState.RightButton == ButtonState.Released && !(MainGame.currentGamepadState.Buttons.LeftShoulder == ButtonState.Pressed)) //&& !(MainGame.currentGamepadState.Triggers.Left >= MainGame.triggerThreshold)) //is what we want { resetPosition(); State = 0; } else { //Need to take firing cooldown/reload into consideration if (currentMouseState.LeftButton == ButtonState.Pressed || MainGame.currentGamepadState.Triggers.Right >= MainGame.triggerThreshold) //implied state==1 { weapon.playShot(gameTime); if (weapon.isFireable(gameTime)) { //Update game world here and inform weapon to draw //shot, but can't draw yet //Make scavengers in trench safe, but not others if (wave.isHit(Position) || (scavenger.action != 0 && scavenger.isHit(Position))) { weapon.ShotPoint.X = Position.X + Width / 2; weapon.ShotPoint.Y = Position.Y + Height / 2; } else { weapon.ShotPoint.X = weapon.waveManager.getWave().layout.weaponGunpoint.X + aimingVector.X * graphicsDevice.Viewport.Width; weapon.ShotPoint.Y = weapon.waveManager.getWave().layout.weaponGunpoint.Y + aimingVector.Y * graphicsDevice.Viewport.Width; } weapon.startShotCooldown(gameTime); } } float velocity = weapon.GetCrosshairVelocity((gameTime.TotalGameTime.Subtract(aimingTimestamp)).TotalMilliseconds); Position.X = aimingVector.X * velocity + Position.X; Position.Y = aimingVector.Y * velocity + Position.Y; } } } }