private void LoadLevel() { world = new World(new Vector2(0, 9.8f)); floor = new Block(world, blockTexture, new Vector2(1200, 100), new Vector2(GraphicsDevice.Viewport.Width, 100.0f), 0, true); floor.Position = new Vector2(GraphicsDevice.Viewport.Width / 2.0f, GraphicsDevice.Viewport.Height); floor.body.BodyType = BodyType.Static; blockList = new List <Block>(); ballList = new List <int>(); LevelData.LoadLevel(this, world, blockTexture, ref blockList, ref ballList, currentLevel); cannonPosition = new Vector2(200, LevelData.screenHeight - 70); cannonPower = 25; powerInt = LevelData.GetPowerInt(cannonPower); cannonAngle = (float)Math.PI / 4; powerUp = true; gameState = GameState.Level; }
private void UpdateLevel(GameTime gameTime) { world.Step((float)gameTime.ElapsedGameTime.TotalSeconds); //Check if level is won. bool won = true; foreach (Block b in blockList) { if (b.colorNum > 0) { won = false; } } if (won) { gameState = GameState.LevelWon; return; } if (powerUp) { cannonPower += 5; if (cannonPower == 375) { powerUp = false; } } else { cannonPower -= 5; if (cannonPower == 25) { powerUp = true; } } powerInt = LevelData.GetPowerInt(cannonPower); if (keyboardState.IsKeyDown(Keys.Space) && oldKeyboardState.IsKeyUp(Keys.Space) || keyboardState.IsKeyDown(Keys.Enter) && oldKeyboardState.IsKeyUp(Keys.Enter)) { //Check if level is lost. bool lost = false; if (ballList.Count == 0) { lost = true; } if (lost) { gameState = GameState.LevelLose; return; } soundCannon.Play(); Block ball = new Block(world, ballTexture, new Vector2(50, 0), new Vector2(cannonPosition.X, cannonPosition.Y), ballList.ElementAt(0), false); ball.body.ApplyForce(new Vector2(cannonPower * (float)Math.Cos(cannonAngle), -1 * cannonPower * (float)Math.Sin(cannonAngle))); blockList.Add(ball); ballList.RemoveAt(0); } if (keyboardState.IsKeyDown(Keys.Up) && cannonAngle < Math.PI / 2) { cannonAngle += (float)Math.PI / 300; } if (keyboardState.IsKeyDown(Keys.Down) && cannonAngle > 0) { cannonAngle -= (float)Math.PI / 300; } if (keyboardState.IsKeyDown(Keys.R) && oldKeyboardState.IsKeyUp(Keys.R)) { LoadLevel(); } if (keyboardState.IsKeyDown(Keys.Escape) && oldKeyboardState.IsKeyUp(Keys.Escape)) { gameState = GameState.LevelSelect; } }