//Update public void update() { if (isThrown & falling) { if (burnTime > 0) { foreach (Platform p in world.getPlatforms()) { if (bounds.Intersects(p.Bounds)) { if (position.Y + 20 < p.Bounds.Top + 18) { position.Y = p.Bounds.Top - 19; velocity.Y = 0; falling = false; } } } velocity.X = xSpeed; velocity.Y += gravity; position.X += velocity.X; position.Y += velocity.Y; bounds.X = (int)position.X; bounds.Y = (int)position.Y; burnTime--; } else { //isThrown = false; falling = false; } } if (!isThrown) { this.position = player.Location; } }
private void DrawScene(GraphicsDevice device) { device.SetRenderTarget(scene); device.Clear(Color.White); spriteBatch.Begin(); player.display(spriteBatch); foreach (Platform p in world.getPlatforms()) { spriteBatch.Draw(p.Texture, p.Bounds, Color.Black); if (p.HasTorch == true) { int pX = p.Bounds.X; int pY = p.Bounds.Y; int pW = p.Bounds.Width; torchPowerup.relocate(pX + pW / 2, pY - 10); torchPowerup.display(spriteBatch); } } spriteBatch.End(); }
/// <summary> /// This method updates the state of the player based on position and keys pressed. /// </summary> /// <param name="kbState">This is the current KeyBoardState</param> /// <param name="prevState">This is the previouse KeyBoardState</param> public void move(KeyboardState kbState, KeyboardState prevState) { //Print out debugging info //Console.WriteLine("VSTATE: " + vState); //Console.WriteLine("HSTATE: " + hState); //Console.WriteLine("POSITION: " + " ( " + position.X + ", " + position.Y + " ) "); //Console.WriteLine("VELOCITY: " + " ( " + velocity.X + ", " + velocity.Y + " ) "); //Set the x velocity to 0 automatically and set the horizontal state to standing by default velocity.X = 0; hState = HorizontalState.standing; //Check key presses to move player if (kbState.IsKeyDown(Keys.Left) && kbState.IsKeyUp(Keys.Right)) { //Move left as long as there is room if (position.X > 0 + xSpeed) { hState = HorizontalState.walkingLeft; //Move the player to the left velocity.X = -1 * xSpeed; } } if (kbState.IsKeyDown(Keys.Right) && kbState.IsKeyUp(Keys.Left)) { //Move right as long as there is room if (position.X < ((float)screenWidth - xSpeed) / 2 - texture.Width / 2) { hState = HorizontalState.walkingRight; //Move the player to the right velocity.X = xSpeed; } else { world.movePlatforms((int)xSpeed); for (int i = 0; i < torches.Length; i++) { if (torches[i].Falling == false) { torches[i].Location = new Vector2(torches[i].Location.X - xSpeed, torches[i].Location.Y); } } hState = HorizontalState.walkingRight; } } // Jumping/Falling if (kbState.IsKeyDown(Keys.Up)) // If the player is currently pressing up. { if (vState == VerticalState.none || vState == VerticalState.fallingPlatform) // If the player's veritcal state is none (not falling or jumping) or the player is on a Falling Platform. { currentPlatform = null; vState = VerticalState.jumping; velocity.Y = -2 * ySpeed; } } else if (vState == VerticalState.jumping) // If the player is currently not pressing up and is jumping. { // Stop the jump. vState = VerticalState.falling; velocity.Y = -0.50f * velocity.Y; } //Check for collision detection if (vState == VerticalState.falling) { foreach (Platform p in world.getPlatforms()) { if (body.Intersects(p.Bounds)) { // && (position.X > p.Bounds.Left - 50 *.75) && (position.X < p.Bounds.Right - 50 * .75) if (position.Y + 50 < p.Bounds.Top + 18) { position.Y = p.Bounds.Top - 49; vState = VerticalState.none; velocity.Y = 0; currentPlatform = p; } } } } if (vState == VerticalState.none) { bool isColliding = false; foreach (Platform p in world.getPlatforms()) { if (body.Intersects(p.Bounds)) { isColliding = true; } } if (!isColliding) { vState = VerticalState.falling; currentPlatform = null; } } if (vState == VerticalState.fallingPlatform) // Check to see if the player is still on the platform horizontally. { if (position.X + 50 < currentPlatform.Bounds.Left || position.X > currentPlatform.Bounds.Right) { vState = VerticalState.falling; currentPlatform = null; } } if (position.Y >= screenHeight - 50) // If the player is at the bottom of the screen. { if (vState == VerticalState.falling || vState == VerticalState.fallingPlatform) // If the player's vertical state is falling or fallingPlatform. { position.Y = screenHeight - 50; vState = VerticalState.none; velocity.Y = 0; } } //Add our velocity to our position vector position.X += velocity.X; position.Y += velocity.Y; body.X = (int)position.X; body.Y = (int)position.Y; //Check to see if player is throwing torches throwTorches(kbState, prevState); //Update all torches, if any for (int i = 0; i < torches.Length; i++) { torches[i].update(); } }