/// <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) { //set our keyboardstate tracker update can change the gamestate on every cycle controls.Update(); if (controls.onPress(Keys.Escape, Buttons.Back)) { Exit(); } // TODO: Add your update logic here player1.Update(controls, physics, this.Content); physics.Update(this.Content); foreach (Enemy e in physics.enemies) { e.Update(controls, physics); } base.Update(gameTime); }
private void Shoot(Controls controls, Level level, GameTime gameTime) { index = rand.Next(4); // New shots if (controls.onPress(Keys.D, Buttons.RightShoulder) && !powerShotCharging) { shooting = true; shotPoint = (int)(gameTime.TotalGameTime.TotalMilliseconds); } else if (controls.onRelease(Keys.D, Buttons.RightShoulder)) { shooting = false; } if (powerup["charged"]) { if (controls.onPress(Keys.X, Buttons.RightTrigger)) { if (hydration >= powerShotCost) powerShotCharging = true; powerShotPoint = (int)(gameTime.TotalGameTime.TotalMilliseconds); powerShotRelease = (int)(gameTime.TotalGameTime.TotalMilliseconds); tryShotHydration = 0; } else if (controls.isPressed(Keys.X, Buttons.RightTrigger) && powerShotCharging) { if (tryShotHydration < powerShotCost) { tryShotHydration += 1; hydration -= 1; } } else if (controls.onRelease(Keys.X, Buttons.RightTrigger) && powerShotCharging) { powerShotRelease = (int)(gameTime.TotalGameTime.TotalMilliseconds); if (((powerShotRelease - powerShotPoint) >= powerShotDelay) && tryShotHydration == powerShotCost) { powerShotCharging = false; tryShotHydration = 0; string dir = controls.isPressed(Keys.Up, Buttons.DPadUp) ? "up" : "none"; PowerShot s = new PowerShot(this, dir); if (index == 0) { soundList["Sounds/Shot1.wav"].Play(); } else if (index == 1) { soundList["Sounds/Shot2.wav"].Play(); } else if (index == 2) { soundList["Sounds/Shot3.wav"].Play(); } else { soundList["Sounds/Shot4.wav"].Play(); } level.projectiles.Add(s); } else { powerShotCharging = false; hydration += tryShotHydration; tryShotHydration = 0; } } } // Deal with shot creation and delay if (!frozen) { // Generate regular shots int currentTime1 = (int)(gameTime.TotalGameTime.TotalMilliseconds); if (((currentTime1 - shotPoint) >= shotDelay || (currentTime1 - shotPoint) == 0) && shooting && hydration >= shotCost) { shotPoint = currentTime1; string dir = controls.isPressed(Keys.Up, Buttons.DPadUp) ? "up" : "none"; Shot s = new Shot(this, dir); if (index == 0) { soundList["Sounds/Shot1.wav"].Play(); } else if (index == 1) { soundList["Sounds/Shot2.wav"].Play(); } else if (index == 2) { soundList["Sounds/Shot3.wav"].Play(); } else { soundList["Sounds/Shot4.wav"].Play(); } level.projectiles.Add(s); hydration -= shotCost; } // Jetpack (Midair jump and downward shots) int currentTime2 = (int)(gameTime.TotalGameTime.TotalMilliseconds); if ((currentTime2 - jumpPoint) >= jumpDelay && yVel > 3 && powerup["jetpack"] && hydration >= jetpackCost && !grounded && controls.isPressed(Keys.S, Buttons.A)) { // New shot jumpPoint = currentTime2; Shot s = new Shot(this, "down"); if (index == 0) { soundList["Sounds/Shot1.wav"].Play(); } else if (index == 1) { soundList["Sounds/Shot2.wav"].Play(); } else if (index == 2) { soundList["Sounds/Shot3.wav"].Play(); } else { soundList["Sounds/Shot4.wav"].Play(); } level.projectiles.Add(s); hydration -= jetpackCost; // Slight upward boost spriteY--; movedY--; yVel = -4.5; } } }
private void Move(Controls controls, Level level) { // Sideways Acceleration if (controls.onPress(Keys.Right, Buttons.DPadRight)) xAccel += speed; else if (controls.onRelease(Keys.Right, Buttons.DPadRight)) xAccel -= speed; if (controls.onPress(Keys.Left, Buttons.DPadLeft)) xAccel -= speed; else if (controls.onRelease(Keys.Left, Buttons.DPadLeft)) xAccel += speed; // Sideways Movement double playerFriction = pushing ? (friction * 3) : friction; xVel = xVel * (1 - playerFriction) + (frozen ? 0 : xAccel * .10); movedX = Convert.ToInt32(xVel + rollerVel); spriteX += movedX; pushing = false; rollerVel = 0; // Check left/right collisions checkXCollisions(level); // Gravity yVel += level.gravity; if (yVel > level.maxFallSpeed) yVel = level.maxFallSpeed; // Round up to force movement every step movedY = Convert.ToInt32(Math.Ceiling(yVel)); spriteY += movedY; grounded = false; // Check up/down collisions checkYCollisions(level); // Determine direction if (xVel > 0.1) faceLeft = false; else if (xVel < -0.1) faceLeft = true; }
private void Jump(Controls controls, Level level, GameTime gameTime) { // Jump on button press if (controls.onPress(Keys.S, Buttons.A) && !frozen && grounded) { instance = soundList["Sounds/Jump.wav"].CreateInstance(); instance.Volume = 0.4f; instance.Play(); yVel = -jumpHeight; jumpPoint = (int)(gameTime.TotalGameTime.TotalMilliseconds); } // Cut jump short on button release else if (controls.onRelease(Keys.S, Buttons.A) && yVel < 0) { yVel /= 2; } }
private void Move(Controls controls, Level level) { // Sideways Acceleration if (controls.onPress(Keys.Right, Buttons.DPadRight)) x_accel += speed; else if (controls.onRelease(Keys.Right, Buttons.DPadRight)) x_accel -= speed; if (controls.onPress(Keys.Left, Buttons.DPadLeft)) x_accel -= speed; else if (controls.onRelease(Keys.Left, Buttons.DPadLeft)) x_accel += speed; // Sideways Movement double playerFriction = pushing ? (friction * 3) : friction; x_vel = x_vel * (1 - playerFriction) + (frozen ? 0 : x_accel * .10); spriteX += Convert.ToInt32(x_vel); pushing = false; // Check left/right collisions foreach (Sprite s in level.items) { if (s.isSolid && Intersects(s)) { // Collision with right block if (bottomWall > s.topWall && rightWall - Convert.ToInt32(x_vel) < s.leftWall && x_vel > 0) { // Push if (s is Block && ((Block)s).rightPushable && grounded) { ((Block)s).x_vel = x_vel; pushing = true; } // Hit the wall else { while (rightWall >= s.leftWall) spriteX--; } } // Push to the left else if (bottomWall > s.topWall && leftWall - Convert.ToInt32(x_vel) > s.rightWall && x_vel < 0) { // Push if (s is Block && ((Block)s).leftPushable && grounded) { ((Block)s).x_vel = x_vel; pushing = true; } // Hit the wall else { while (leftWall <= s.rightWall) spriteX++; } } } } // Gravity if (!grounded) { y_vel += level.gravity; if (y_vel > level.maxFallSpeed) y_vel = level.maxFallSpeed; spriteY += Convert.ToInt32(y_vel); } else { y_vel = 1; } grounded = false; // Check up/down collisions foreach (Sprite s in level.items) { if (s.isSolid && Intersects(s)) { // Up collision if (topWall - Convert.ToInt32(y_vel) > s.bottomWall) { y_vel = 0; while (topWall < s.bottomWall) spriteY++; } // Down collision else if (!grounded && (bottomWall - Convert.ToInt32(y_vel)) < s.topWall) { grounded = true; while (bottomWall > s.topWall) spriteY--; } } } // Determine direction if (x_vel > 0.1) faceLeft = false; else if (x_vel < -0.1) faceLeft = true; }
public void Update(Controls controls, Physics physics, ContentManager content) { // Acceleration if (controls.onPress(Keys.Right, Buttons.DPadRight)) x_accel += speed; else if (controls.onRelease(Keys.Right, Buttons.DPadRight)) x_accel -= speed; if (controls.onPress(Keys.Left, Buttons.DPadLeft)) x_accel -= speed; else if (controls.onRelease(Keys.Left, Buttons.DPadLeft)) x_accel += speed; // Movement x_vel = x_vel * (1 - friction) + (frozen() ? 0 : x_accel * .10); spriteX += Convert.ToInt32(x_vel); // Direction if (x_vel > 0.1) left = false; else if (x_vel < -0.1) left = true; // Puddle if (!frozen() && grounded && powerup["puddle"] && controls.isPressed(Keys.Down, Buttons.DPadDown)) { puddled = true; imageFile = "PC/collapse.png"; LoadContent(content); if (controls.onPress(Keys.Down, Buttons.DPadDown)) puddle_point = physics.count; } // New shots if (controls.onPress(Keys.D, Buttons.RightShoulder)) { shooting = true; shot_point = physics.count; } else if (controls.onRelease(Keys.D, Buttons.RightShoulder)) { shooting = false; } if (controls.onPress(Keys.S, Buttons.A) && grounded) { jump_point = physics.count; } else if (controls.onRelease(Keys.S, Buttons.A)) { // Cut jump short if (y_vel < 0) y_vel /= 2; } // Fall (with grace!) if (spriteY < physics.ground) { spriteY += y_vel; y_vel += physics.gravity; } else { spriteY = physics.ground; y_vel = 0; grounded = true; } // Deal with shot creation and delay if (!frozen()) { // Generate regular shots if ((physics.count - shot_point) % shot_delay == 0 && shooting) { string dir = controls.isPressed(Keys.Up, Buttons.DPadUp) ? "up" : "none"; Shot s = new Shot(this, dir); s.LoadContent(content); physics.shots.Add(s); } // Generate downward shots if ((physics.count - jump_point) % jump_delay == 0 && powerup["jetpack"] && !grounded && controls.isPressed(Keys.S, Buttons.A)) { // New shot Shot s = new Shot(this, "down"); s.LoadContent(content); physics.shots.Add(s); // Upwards spriteY -= 1; y_vel = -8; } } // Jump logic if (controls.isPressed(Keys.S, Buttons.A) && !frozen() && grounded) { spriteY -= 1; y_vel = -15; grounded = false; } Animate(controls, physics, content); CheckCollisions(physics); }
public void Update(Controls controls, Physics physics, ContentManager content) { // Acceleration if (controls.onPress(Keys.Right, Buttons.DPadRight)) { x_accel += speed; } else if (controls.onRelease(Keys.Right, Buttons.DPadRight)) { x_accel -= speed; } if (controls.onPress(Keys.Left, Buttons.DPadLeft)) { x_accel -= speed; } else if (controls.onRelease(Keys.Left, Buttons.DPadLeft)) { x_accel += speed; } // Movement x_vel = x_vel * (1 - friction) + (frozen() ? 0 : x_accel * .10); spriteX += Convert.ToInt32(x_vel); // Direction if (x_vel > 0.1) { left = false; } else if (x_vel < -0.1) { left = true; } // Puddle if (!frozen() && grounded && powerup["puddle"] && controls.isPressed(Keys.Down, Buttons.DPadDown)) { puddled = true; imageFile = "PC/collapse.png"; LoadContent(content); if (controls.onPress(Keys.Down, Buttons.DPadDown)) { puddle_point = physics.count; } } // New shots if (controls.onPress(Keys.D, Buttons.RightShoulder)) { shooting = true; shot_point = physics.count; } else if (controls.onRelease(Keys.D, Buttons.RightShoulder)) { shooting = false; } if (controls.onPress(Keys.S, Buttons.A) && grounded) { jump_point = physics.count; } else if (controls.onRelease(Keys.S, Buttons.A)) { // Cut jump short if (y_vel < 0) { y_vel /= 2; } } // Fall (with grace!) if (spriteY < physics.ground) { spriteY += y_vel; y_vel += physics.gravity; } else { spriteY = physics.ground; y_vel = 0; grounded = true; } // Deal with shot creation and delay if (!frozen()) { // Generate regular shots if ((physics.count - shot_point) % shot_delay == 0 && shooting) { string dir = controls.isPressed(Keys.Up, Buttons.DPadUp) ? "up" : "none"; Shot s = new Shot(this, dir); s.LoadContent(content); physics.shots.Add(s); } // Generate downward shots if ((physics.count - jump_point) % jump_delay == 0 && powerup["jetpack"] && !grounded && controls.isPressed(Keys.S, Buttons.A)) { // New shot Shot s = new Shot(this, "down"); s.LoadContent(content); physics.shots.Add(s); // Upwards spriteY -= 1; y_vel = -8; } } // Jump logic if (controls.isPressed(Keys.S, Buttons.A) && !frozen() && grounded) { spriteY -= 1; y_vel = -15; grounded = false; } Animate(controls, physics, content); CheckCollisions(physics); }