override public void Update(GameTime time) { oldKeyboardState = currentKeyboardState; currentKeyboardState = Keyboard.GetState(); Keys[] pressedKeys; pressedKeys = currentKeyboardState.GetPressedKeys(); foreach (Keys key in pressedKeys) { if (oldKeyboardState.IsKeyUp(key)) { if (key == Keys.Back) { // overflows if (textString.Length > 0) { textString = textString.Remove(textString.Length - 1, 1); } } else { if (key == Keys.Enter) { Sound.pauseSound("Sound\\menu", content); GamePlayScreen level = new GamePlayScreen(); level.Level = this.level; level.Terrain = "Models\\" + this.level; level.TerrainTexture = "Textures\\" + this.level; level.Background = "Sound\\" + this.level; level.Name = textString; ScreenManager.AddScreen(level); ScreenManager.RemoveScreen(this); } else if (textString.Length > 5) { return; } else if (key == Keys.Space) { textString += " "; } else if (AllowedKeys.Contains(key.ToString())) { textString += key.ToString(); } } } } }
/// <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> override public void Update(GameTime gameTime) { timeInMillis += gameTime.ElapsedGameTime.Milliseconds; LevelTime -= gameTime.ElapsedGameTime.Milliseconds; if (LevelTime < 0 || ball.position.Y <= pitHeight) { Sound.pauseSound("Sound\\" + this.level, content); GameOver startOver = new GameOver(); startOver.Level = this.level; startOver.Name = this.name; ScreenManager.AddScreen(startOver); } lastKeyboardState = currentKeyboardState; lastGamePadState = currentGamePadState; lastMousState = currentMouseState; currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); currentMouseState = Mouse.GetState(); Int32 middleX = device.Viewport.Bounds.Width / 2; Int32 middleY = device.Viewport.Bounds.Height / 2;; #if XBOX // Exit when the Escape key or Back button is pressed if (currentGamePadState.Buttons.Back == ButtonState.Pressed) { ScreenManager.RemoveScreen(this); } if (!ball.onGround && currentGamePadState.IsButtonDown(Buttons.B) && !lastGamePadState.IsButtonDown(Buttons.B)) { ball.dash(); } else if (currentGamePadState.IsButtonDown(Buttons.A) && !lastGamePadState.IsButtonDown(Buttons.A)) { if (ball.onGround && !ball.jumpDelayTimer.IsRunning) { //ball.dashDelayTimer.Start(); ball.jump(); } } if (currentGamePadState.DPad.Down == ButtonState.Pressed) { Vector3 offset = camera.DesiredPositionOffset; offset.Y = MathHelper.Clamp(offset.Y += 2, 20, 150); if (Math.Abs(offset.Y - camera.DesiredPositionOffset.Y) > 0.0001) { Sound.playSoundOnce("Sound\\rotateCam", content); } camera.DesiredPositionOffset = offset; } else if (currentGamePadState.DPad.Up == ButtonState.Pressed) { Vector3 offset = camera.DesiredPositionOffset; offset.Y = MathHelper.Clamp(offset.Y -= 2, 20, 150); if (Math.Abs(offset.Y - camera.DesiredPositionOffset.Y) > 0.0001) { Sound.playSoundOnce("Sound\\rotateCam", content); } camera.DesiredPositionOffset = offset; } if (currentGamePadState.IsButtonDown(Buttons.LeftShoulder) && !lastGamePadState.IsButtonDown(Buttons.LeftShoulder)) { if (currentBallProperties.Equals(beachBallProperties)) { currentBallProperties = airBallProperties; } else if (currentBallProperties.Equals(airBallProperties)) { currentBallProperties = metalBallProperties; } else if (currentBallProperties.Equals(metalBallProperties)) { currentBallProperties = beachBallProperties; } Sound.playSoundOnce(currentBallProperties.ChangeToSound, content); } else if (currentGamePadState.IsButtonDown(Buttons.RightShoulder) && !lastGamePadState.IsButtonDown(Buttons.RightShoulder)) { if (currentBallProperties.Equals(beachBallProperties)) { currentBallProperties = metalBallProperties; } else if (currentBallProperties.Equals(metalBallProperties)) { currentBallProperties = airBallProperties; } else if (currentBallProperties.Equals(airBallProperties)) { currentBallProperties = beachBallProperties; } Sound.playSoundOnce(currentBallProperties.ChangeToSound, content); } else if (currentGamePadState.IsButtonDown(Buttons.Back) && !lastGamePadState.IsButtonDown(Buttons.Back)) { Sound.pauseSound(background, content); ScreenManager.AddScreen(new MainMenu()); } if (currentGamePadState.IsButtonDown(Buttons.RightStick) && !lastGamePadState.IsButtonDown(Buttons.RightStick)) { ball.Reset(); camera.Reset(); } #endif #if WINDOWS // Exit when the Escape key or Back button is pressed if (currentKeyboardState.IsKeyDown(Keys.Escape) || currentGamePadState.Buttons.Back == ButtonState.Pressed) { ScreenManager.RemoveScreen(this); } if (!ball.onGround && !lastKeyboardState.IsKeyDown(Keys.Space) && currentKeyboardState.IsKeyDown(Keys.Space)) { ball.dash(); } else if (currentKeyboardState.IsKeyDown(Keys.Space)) { if (ball.onGround && !ball.jumpDelayTimer.IsRunning) { //ball.dashDelayTimer.Start(); ball.jump(); } } if (currentKeyboardState.IsKeyDown(Keys.I) || (currentMouseState.RightButton == ButtonState.Pressed) && (currentMouseState.Y > middleY)) { Vector3 offset = camera.DesiredPositionOffset; offset.Y = MathHelper.Clamp(offset.Y += 2, 20, 150); if (Math.Abs(offset.Y - camera.DesiredPositionOffset.Y) > 0.0001) { Sound.playSoundOnce("Sound\\rotateCam", content, 0.5f); } camera.DesiredPositionOffset = offset; } else if (currentKeyboardState.IsKeyDown(Keys.K) || (currentMouseState.RightButton == ButtonState.Pressed) && (currentMouseState.Y <= middleY)) { Vector3 offset = camera.DesiredPositionOffset; offset.Y = MathHelper.Clamp(offset.Y -= 2, 20, 150); if (Math.Abs(offset.Y - camera.DesiredPositionOffset.Y) > 0.0001) { Sound.playSoundOnce("Sound\\rotateCam", content, 0.5f); } camera.DesiredPositionOffset = offset; } if (currentKeyboardState.IsKeyDown(Keys.Q) && !lastKeyboardState.IsKeyDown(Keys.Q)) { if (currentBallProperties.Equals(beachBallProperties)) { currentBallProperties = airBallProperties; } else if (currentBallProperties.Equals(airBallProperties)) { currentBallProperties = metalBallProperties; } else if (currentBallProperties.Equals(metalBallProperties)) { currentBallProperties = beachBallProperties; } Sound.playSoundOnce(currentBallProperties.ChangeToSound, content); } else if (currentKeyboardState.IsKeyDown(Keys.E) && !lastKeyboardState.IsKeyDown(Keys.E)) { if (currentBallProperties.Equals(beachBallProperties)) { currentBallProperties = metalBallProperties; } else if (currentBallProperties.Equals(metalBallProperties)) { currentBallProperties = airBallProperties; } else if (currentBallProperties.Equals(airBallProperties)) { currentBallProperties = beachBallProperties; } Sound.playSoundOnce(currentBallProperties.ChangeToSound, content); } else if (currentKeyboardState.IsKeyDown(Keys.M)) { Sound.pauseSound(background, content); ScreenManager.AddScreen(new MainMenu()); } if (currentKeyboardState.IsKeyDown(Keys.Z)) { System.Diagnostics.Debug.WriteLine(ball.position); } bool touchTopLeft = currentMouseState.LeftButton == ButtonState.Pressed && lastMousState.LeftButton != ButtonState.Pressed && currentMouseState.X < device.Viewport.Width / 10 && currentMouseState.Y < device.Viewport.Height / 10; if (currentKeyboardState.IsKeyDown(Keys.R) || currentGamePadState.Buttons.RightStick == ButtonState.Pressed) { ball.Reset(); camera.Reset(); } #endif ArrowRotation = RotateToFace(ball.position, ExitPosition, Vector3.Up); ExitRotation = RotateToFace(ExitPosition, ball.position, Vector3.Up); if (ExitBox.Contains(ball.position) == ContainmentType.Contains || currentKeyboardState.IsKeyDown(Keys.P)) { long nowTicks = DateTime.Now.Ticks; #if WINDOWS sendScore(name, level, (int)(LevelTime), nowTicks); #endif VictoryScreen victoryScreen = new VictoryScreen(); victoryScreen.Level = this.level; victoryScreen.Name = this.name; victoryScreen.Ticks = nowTicks; ScreenManager.AddScreen(victoryScreen); } //Test stuff if (heightMap.IsOnHeightmap(ball.position)) { heightMap.GetHeightAndNormal(ball.position, out h, out norm); norm.Normalize(); h = Vector3.Dot(new Vector3(0, 1, 0), norm); a = Math.Acos(h); //a = getSignedAngle(new Vector3(0, 1, 0), norm); b = Math.Atan2(norm.Y, norm.X); } for (int x = 0; x < coinList.Count; x++) { coinList[x].Update(gameTime); if (coinList[x].detectIntersection(ball.position)) { LevelTime += 5000f; Sound.playSoundOnce("Sound\\ding", content); coinList.RemoveAt(x); } } for (int x = 0; x < teleporterList.Count; x++) { teleporterList[x].Update(gameTime); if (teleporterList[x].detectIntersection(ball.position)) { Sound.playSoundOnce("Sound\\pop", content); ball.position = new Vector3(RandomNumber(-2500, 3000), 0, RandomNumber(-2500, 3000)); } } ball.properties = currentBallProperties; ball.Update(gameTime); camera.ChasePosition = ball.position; camera.ChaseDirection = ball.Direction; camera.Up = ball.Up; camera.Reset(); particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); particleEngine.Update(); base.Update(gameTime); }