private void UpdateMovement(GameTime theGameTime, KeyboardState aCurrentKeyboardState) { if (mCurrentState != State.Walking) return; mSpeed = Vector2.Zero; mDirection = Vector2.Zero; Position.Y = currentBrick.Position.Y - Source.Height; if (aCurrentKeyboardState.IsKeyDown(Keys.Left)) { timeElapsed += (float)theGameTime.ElapsedGameTime.TotalSeconds; if (timeElapsed > TimeToUpdate) { timeElapsed -= TimeToUpdate; Source = new Rectangle(currentFrame * 20, WalkingLeftFrameY, 20, FrameHeight); if (++currentFrame == NrOfFrames) currentFrame = 0; } if (Position.X > 0) { mSpeed.X = JumperSpeed; mDirection.X = MoveLeft; } } else if (aCurrentKeyboardState.IsKeyDown(Keys.Right)) { timeElapsed += (float)theGameTime.ElapsedGameTime.TotalSeconds; if (timeElapsed > TimeToUpdate) { timeElapsed -= TimeToUpdate; Source = new Rectangle(currentFrame * 20, WalkingRightFrameY, 20, FrameHeight); if (++currentFrame == NrOfFrames) currentFrame = 0; } if (Position.X + Size.Width < windowWidth) { mSpeed.X = JumperSpeed; mDirection.X = MoveRight; } } else { Source = new Rectangle(0, 80, 20, FrameHeight); } if (currentBrick.PowerUp(new Rectangle((int)Position.X, (int)Position.Y, FrameWidth, FrameHeight))) { } if(Position.X < currentBrick.Position.X - 10 || Position.X >= currentBrick.Position.X + currentBrick.Source.Width) { //System.Diagnostics.Debug.WriteLine("FALLING"); mCurrentState = State.Falling; Source = new Rectangle(0, FrameHeight * 2, FrameWidth, FrameHeight); mSpeed.Y = JumperSpeed*2; mDirection.Y = MoveDown; currentBrick = null; parachute = false; } }
private void UpdateFalling(KeyboardState aCurrentKeyboardState, List<Brick> bricks) { if (mCurrentState != State.Falling) return; mDirection.Y = MoveDown; mSpeed.Y = JumperSpeed; if (aCurrentKeyboardState.IsKeyDown(Keys.Left)) { if (Position.X > 0) { mSpeed.X = (float)JumperSpeed / 1.5f; mDirection.X = MoveLeft; } } else if (aCurrentKeyboardState.IsKeyDown(Keys.Right)) { if (Position.X + Size.Width < windowWidth) { mSpeed.X = (float)JumperSpeed / 1.5f; mDirection.X = MoveRight; } } if(aCurrentKeyboardState.IsKeyDown(Keys.F) && !parachute) { parachute = true; Source = new Rectangle(FrameWidth * 3, FrameHeight * 2, FrameWidth, FrameHeight); } if (Position.X <= 0) { mDirection.X = MoveRight; } else if (Position.X + Size.Width > windowWidth) { mDirection.X = MoveLeft; } if (Position.Y >= windowHeight) Position.Y = 0; var brick = bricks.FirstOrDefault( x => x.Position.X - 10 <= Position.X && x.Position.X + x.Source.Width - 10 >= Position.X && x.Position.Y - 2 <= (int)(Position.Y + Source.Height) && (int)(Position.Y + Source.Height) <= (x.Position.Y + x.Source.Height)); if (brick == null) { mSpeed.Y = !parachute ? JumperSpeed * 2 : JumperSpeed; mDirection.Y = MoveDown; } else { //System.Diagnostics.Debug.WriteLine("WALKING"); parachute = false; currentBrick = brick; mCurrentState = State.Walking; Source = new Rectangle(mDirection.X == 1 ? 40 : 0, WalkingRightFrameY, FrameWidth, FrameHeight); Position.Y = brick.Position.Y - Source.Height; } }