public void addBubble(Bubble bubble) { bubble.velocity3D = new Vector3(0,0,0); bubble.worldPosition -= new Vector3(0, 0, ceiling * 1.94f); Vector2 gridCoordinates = new Vector2(bubble.worldToGrid().X, bubble.worldToGrid().Z); if ((int)gridCoordinates.Y % 2 == 1 && (int)gridCoordinates.X == GRID_WIDTH - 1) { if (grid[(int)gridCoordinates.X - 1, (int)gridCoordinates.Y] == null) gridCoordinates.X -= 1; else if (grid[(int)gridCoordinates.X, (int)gridCoordinates.Y + 1] == null) gridCoordinates.Y += 1; } bubble.heading = 0; bubble.angularVelocity = 0; bubble.gridPosition = gridCoordinates; bubble.positionPixels = bubble.gridToPixels(ceiling) + new Vector2(0, ceiling * 30); grid[(int)gridCoordinates.X, (int)gridCoordinates.Y] = bubble; bubble.worldPosition = new Vector3(bubble.gridPosition.X * 2 - RAMP_SIZE + 5 + ( bubble.gridPosition.Y % 2), -RAMP_SIZE + 1, bubble.gridPosition.Y * 1.94f - RAMP_SIZE * 1.5f /*, bubble.gridPosition.Y - RAMP_SIZE + 1 0.0f*/); /* * TO DO: * * FIGURE OUT WHY BUBBLE FLOAT WHEN CEILING MOVES DOWN * */ Stats.recordBubble(bubble); }
public static void removeBubble(Bubble bubble) { bubbleCount--; if (bubbles[(int)bubble.color] != 0) bubbles[(int)bubble.color]--; score += 10; }
private List<Bubble> adjacentBubbles(Bubble bubble, int[,] visitedGrid) { int x = (int)bubble.gridPosition.X; int y = (int)bubble.gridPosition.Y; List<Bubble> bubbles = new List<Bubble>(); if (visitedGrid[x, y] == 1) return bubbles; visitedGrid[x, y] = 1; bubbles.Add(bubble); if (x > 0) { //check left if (grid[x - 1, y] != null) { if (grid[x - 1, y].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x - 1, y], visitedGrid)); } } if (y > 0) { //check top left if (grid[x - ((y+1) % 2), y - 1] != null) { if (grid[x - ((y+1) % 2), y - 1].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x - ((y+1) % 2), y - 1], visitedGrid)); } } } if (y < GRID_HEIGHT - 1) { if (grid[x - ((y+1) % 2), y + 1] != null) { if (grid[x - ((y+1) % 2), y + 1].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x - ((y+1) % 2), y + 1], visitedGrid)); } } } } if (x == 0 && (y % 2 == 1)) { if (y > 0) { //check top left if (grid[x, y - 1] != null) { if (grid[x, y - 1].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x, y - 1], visitedGrid)); } } } if (y < GRID_HEIGHT - 1) { if (grid[x, y + 1] != null) { if (grid[x, y + 1].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x , y + 1], visitedGrid)); } } } } if (x < GRID_WIDTH-1) { //check right if (grid[x + 1, y] != null) { if (grid[x + 1, y].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x + 1, y], visitedGrid)); } } if (bubble.gridPosition.Y > 0) { //check top right if (grid[x + y%2, y - 1] != null) { if (grid[x + y % 2, y - 1].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x + y%2, y - 1], visitedGrid)); } } } if (bubble.gridPosition.Y < GRID_HEIGHT - 1) { //bottom right if (grid[x + y%2, y + 1] != null) { if (grid[x + y%2, y + 1].color == bubble.color) { bubbles.AddRange(adjacentBubbles(grid[x + y%2, y + 1], visitedGrid)); } } } } return bubbles; }
public bool popBubbles(Bubble addedBubble) { int[,] visitedGrid = new int[GRID_WIDTH, GRID_HEIGHT]; List<Bubble> bubbles = new List<Bubble>(); bubbles = adjacentBubbles(addedBubble, visitedGrid); if (bubbles.Count > 2) { foreach (Bubble b in bubbles) { b.popping = true; grid[(int)b.gridPosition.X, (int)b.gridPosition.Y].texture = sprites; poppingBubbles.Add(grid[(int)b.gridPosition.X, (int)b.gridPosition.Y]); Stats.removeBubble(grid[(int)b.gridPosition.X, (int)b.gridPosition.Y]); grid[(int)b.gridPosition.X, (int)b.gridPosition.Y] = null; } return true; } return false; }
public void initializeLevel() { if (this.level == 1) { for (int j = 0; j < 4; j++) { for (int i = 0; i < (GRID_WIDTH - j%2); i++) { Bubble b1 = new Bubble(); if (j > 1) { if (i < 8) b1 = new Bubble(Bubble.Colors.Green, i, j, ballSprites); if (i < 6) b1 = new Bubble(Bubble.Colors.Blue, i, j, ballSprites); if (i < 4) b1 = new Bubble(Bubble.Colors.Yellow, i, j, ballSprites); if (i < 2) b1 = new Bubble(Bubble.Colors.Red, i, j, ballSprites); } else { if (i < 8) b1 = new Bubble(Bubble.Colors.Yellow, i, j, ballSprites); if (i < 6) b1 = new Bubble(Bubble.Colors.Red, i, j, ballSprites); if (i < 4) b1 = new Bubble(Bubble.Colors.Green, i, j, ballSprites); if (i < 2) b1 = new Bubble(Bubble.Colors.Blue, i, j, ballSprites); } addBubble(b1); } } } if (this.level == 2) { for (int j = 0; j < 6; j++) { for (int i = 1; i < (GRID_WIDTH -1); i++) { Bubble b1 = new Bubble(); if (j > 3) { if (i < 8) b1 = new Bubble(Bubble.Colors.Red, i, j, ballSprites); if (i < 6) b1 = new Bubble(Bubble.Colors.Grey, i, j, ballSprites); if (i < 4) b1 = new Bubble(Bubble.Colors.Green, i, j, ballSprites); if (i < 2) b1 = new Bubble(Bubble.Colors.Blue, i, j, ballSprites); }else if (j > 1) { if (i < 8) b1 = new Bubble(Bubble.Colors.Red, i, j, ballSprites); if (i < 6) b1 = new Bubble(Bubble.Colors.Orange, i, j, ballSprites); if (i < 4) b1 = new Bubble(Bubble.Colors.Yellow, i, j, ballSprites); if (i < 2) b1 = new Bubble(Bubble.Colors.Blue, i, j, ballSprites); } else { if (i < 8) b1 = new Bubble(Bubble.Colors.Blue, i, j, ballSprites); if (i < 6) b1 = new Bubble(Bubble.Colors.Purple, i, j, ballSprites); if (i < 4) b1 = new Bubble(Bubble.Colors.Green, i, j, ballSprites); if (i < 2) b1 = new Bubble(Bubble.Colors.Orange, i, j, ballSprites); } addBubble(b1); } } } }
public bool connectToTop(Bubble b, int[,] visited) { bool right = false, left = false, topR = false, topL = false; if (b != null && visited[(int)b.gridPosition.X, (int)b.gridPosition.Y] != 1) { visited[(int)b.gridPosition.X, (int)b.gridPosition.Y] = 1; if (b.gridPosition.Y == 0) { return true; } if (b.gridPosition.Y % 2 == 1) { //Check TOP LEFT recursively topL = connectToTop(grid[(int)b.gridPosition.X, (int)b.gridPosition.Y - 1], visited); if (b.gridPosition.X + 1 < GRID_WIDTH) { //Check TOP RIGHT topR = connectToTop(grid[(int)b.gridPosition.X + 1, (int)b.gridPosition.Y - 1], visited); //Check RIGHT right = connectToTop(grid[(int)b.gridPosition.X + 1, (int)b.gridPosition.Y], visited); } if (b.gridPosition.X - 1 >= 0) { //Check LEFT left = connectToTop(grid[(int)b.gridPosition.X - 1, (int)b.gridPosition.Y], visited); } } else if(b.gridPosition.Y % 2 == 0) { //Check TOP RIGHT recursively topR = connectToTop(grid[(int)b.gridPosition.X, (int)b.gridPosition.Y - 1], visited); if (b.gridPosition.X - 1 >= 0) { //Check TOP LEFT topL = connectToTop(grid[(int)b.gridPosition.X - 1, (int)b.gridPosition.Y - 1], visited); //Check LEFT left = connectToTop(grid[(int)b.gridPosition.X - 1, (int)b.gridPosition.Y], visited); } if (b.gridPosition.X + 1 < GRID_WIDTH) { //Check RIGHT right = connectToTop(grid[(int)b.gridPosition.X + 1, (int)b.gridPosition.Y], visited); } } return topL || topR || right || left; } else return false; }
public bool collides(Bubble shot) { foreach (Bubble b in grid) { if (b != null) { if (!b.popping && !b.falling) { float distance = Math.Abs(Vector3.Distance(shot.worldPosition, b.worldPosition + new Vector3(0, 0, 2 * ceiling))); if (distance <= 2) return true; } } } if(shot.worldPosition.Z <= -RAMP_SIZE*1.5 + ceiling*1.94) return true; else return false; }
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); if (currentGameState == GameState.Playing1 || currentGameState == GameState.Playing2) { //Increase the timer by the number of milliseconds since update was last called timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; interval += (float)gameTime.ElapsedGameTime.TotalMilliseconds; KeyboardState keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.Right)) { blueTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; controller.MoveArrowRight(); } if (keyboardState.IsKeyDown(Keys.Left)) { blueTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; controller.MoveArrowLeft(); } if (keyboardState.IsKeyDown(Keys.Up) || interval >= AUTO_FIRE) { movingBall = loadedBall; if (loadedBall.velocity.Y == 0) { Stats.ballsFired++; movingBall.velocity = 10 * controller.getArrowVector(); } interval = 0; } if (movingBall != null) { greenTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (greenTimer >= SPRITE_TIMER - 25) { greenCurrentFrame = (greenCurrentFrame+1); if (greenCurrentFrame >= 4) greenCurrentFrame = 3; greenTimer = 0; } movingBall.Move(); if (controller.collides(movingBall)) { greenTimer = 0; greenCurrentFrame = 0; movingBall.texture = pokeBalls; controller.addBubble(movingBall); controller.popBubbles(movingBall); controller.fallBubbles(); loadedBall = sackBall; loadedBall.positionPixels = new Vector2(306, 360); loadedBall.worldPosition = new Vector3(0, -10, 0); sackBall = new Bubble((PuzzleBobbleA2.Bubble.Colors)controller.generateColor()); movingBall = null; } } //Check to see if we shoudl drop down one level if (Stats.ballsFired >= (controller.ceiling + 1) * (Stats.bubbleCount / 2 + (DIFFICULTY_RATING - controller.level))) { controller.ceiling++; } //Check the timer is more than the chosen interval controller.Pop(gameTime); if (controller.GameOver()) { currentGameState = GameState.GameOver; //this.Exit(); } if (blueTimer >= SPRITE_TIMER) { blueTimer = 0; blueCurrentFrame = (blueCurrentFrame + 1) % 6; } } if (currentGameState == GameState.Playing2) { if (Stats.bubbleCount == 0 && controller.fallingBubbles.Count == 0 && controller.poppingBubbles.Count == 0) { currentGameState = GameState.Credits; //controller = new GameController(1); //Initialize(); } return; } if (currentGameState == GameState.Playing1) { if (Stats.bubbleCount == 0 && controller.fallingBubbles.Count == 0 && controller.poppingBubbles.Count == 0) { currentGameState = GameState.Playing2; controller = new GameController(2, graphics); int oldscore = Stats.score; Initialize(); Stats.score = oldscore; } return; } if (currentGameState == GameState.MainMenu) { KeyboardState keyboardState1 = Keyboard.GetState(); if (keyboardState1.IsKeyDown(Keys.Space)) { currentGameState = GameState.Playing1; } return; } if (currentGameState == GameState.GameOver) { KeyboardState keyboardState1 = Keyboard.GetState(); if (keyboardState1.IsKeyDown(Keys.Space)) { currentGameState = GameState.Playing1; controller = new GameController(1, graphics); Initialize(); } return; } if (currentGameState == GameState.Credits) { KeyboardState keyboardState1 = Keyboard.GetState(); if (keyboardState1.IsKeyDown(Keys.Space)) { currentGameState = GameState.MainMenu; controller = new GameController(1, graphics); Initialize(); } return; } base.Update(gameTime); }
protected override void Initialize() { base.Initialize(); Stats.reset(); controller.setLevelTile(levelTile); if (currentGameState == GameState.Playing2) controller.setLevelTile(levelTile2); controller.sprites = sprite; controller.ballSprites = pokeBalls; controller.initializeLevel(); sackBall = new Bubble((PuzzleBobbleA2.Bubble.Colors)controller.generateColor()); sackBall.texture = pokeBalls; loadedBall = new Bubble((PuzzleBobbleA2.Bubble.Colors)controller.generateColor()); loadedBall.texture = pokeBalls; loadedBall.positionPixels = new Vector2(306, 360); interval = 0f; timer = 0f; greenCurrentFrame = 0; greenTimer = 0; blueCurrentFrame = 0; blueTimer = 0; }
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit(); if (currentGameState == GameState.Playing1 || currentGameState == GameState.Playing2) { //Increase the timer by the number of milliseconds since update was last called timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; interval += (float)gameTime.ElapsedGameTime.TotalMilliseconds; KeyboardState keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.Right)) { blueTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; controller.MoveArrowRight(); } if (keyboardState.IsKeyDown(Keys.Left)) { blueTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; controller.MoveArrowLeft(); } if (keyboardState.IsKeyDown(Keys.Up) || interval >= AUTO_FIRE ) { movingBall = loadedBall; if (loadedBall.velocity3D.Length() == 0) { Stats.ballsFired++; movingBall.heading = (float)Math.Atan(controller.getArrowVector().X / controller.getArrowVector().Y); movingBall.velocity3D = new Vector3(-INITIAL_VELOCITY * (float)Math.Sin(movingBall.heading), 0, -INITIAL_VELOCITY * (float)Math.Cos(movingBall.heading)); } interval = 0; } if (keyboardState.IsKeyDown(Keys.W)) { // view = Matrix.CreateLookAt(new Vector3(0, viewDist++, 10), new Vector3(0, 0, 0), Vector3.UnitY); angle = 0.005f; view = view * Matrix.CreateRotationX(angle); } if (keyboardState.IsKeyDown(Keys.S)) { // view = Matrix.CreateLookAt(new Vector3(0, viewDist--, 10), new Vector3(0, 0, 0), Vector3.UnitY); angle = -0.005f; view = view * Matrix.CreateRotationX(angle); } if (keyboardState.IsKeyDown(Keys.A)) { view = Matrix.CreateLookAt(new Vector3(pane--, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY); } if (keyboardState.IsKeyDown(Keys.D)) { view = Matrix.CreateLookAt(new Vector3(pane++, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY); } if (keyboardState.IsKeyDown(Keys.D1)) { view = Matrix.CreateLookAt(new Vector3(0, -11, 16), new Vector3(0, -10f, 15), Vector3.UnitY); } if (keyboardState.IsKeyDown(Keys.D3)) { view = Matrix.CreateLookAt(new Vector3(0, -5, 25), new Vector3(0, 2f, 0), Vector3.UnitY); } if (movingBall != null) { greenTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (greenTimer >= SPRITE_TIMER - 25) { greenCurrentFrame = (greenCurrentFrame+1); if (greenCurrentFrame >= 4) greenCurrentFrame = 3; greenTimer = 0; } sackBall.Roll(); movingBall.Move(); if (controller.collides(movingBall)) { greenTimer = 0; greenCurrentFrame = 0; movingBall.texture = pokeBalls; controller.addBubble(movingBall); controller.popBubbles(movingBall); controller.fallBubbles(); loadedBall = sackBall; loadedBall.positionPixels = new Vector2(306, 360); loadedBall.worldPosition = new Vector3(0, 1-RAMP_SIZE, RAMP_SIZE - 2); sackBall = new Bubble((PuzzleBobbleA2.Bubble.Colors)controller.generateColor()); movingBall = null; } } //Check to see if we shoudl drop down one level if (Stats.ballsFired >= (controller.ceiling + 1) * (Stats.bubbleCount / 2 + (DIFFICULTY_RATING - controller.level))) { controller.ceiling++; } //Check the timer is more than the chosen interval controller.Pop(gameTime); if (controller.GameOver()) { currentGameState = GameState.GameOver; //this.Exit(); } if (blueTimer >= SPRITE_TIMER) { blueTimer = 0; blueCurrentFrame = (blueCurrentFrame + 1) % 6; } } int count = 0; //CHECK FOR BUBBLE COUNT INCONSISTENCY foreach (Bubble b in controller.grid) { if (b != null) count++; } if (Stats.bubbleCount != count) { Stats.bubbleCount = count; } if (currentGameState == GameState.Playing2) { if (count == 0 && controller.fallingBubbles.Count == 0 && controller.poppingBubbles.Count == 0) { currentGameState = GameState.Credits; //controller = new GameController(1); //Initialize(); } return; } if (currentGameState == GameState.Playing1) { if (count == 0 && controller.fallingBubbles.Count == 0 && controller.poppingBubbles.Count == 0) { currentGameState = GameState.Playing2; controller = new GameController(2, graphics); int oldscore = Stats.score; Initialize(); Stats.score = oldscore; } return; } if (currentGameState == GameState.MainMenu) { KeyboardState keyboardState1 = Keyboard.GetState(); if (keyboardState1.IsKeyDown(Keys.Space)) { currentGameState = GameState.Playing1; } return; } if (currentGameState == GameState.GameOver) { KeyboardState keyboardState1 = Keyboard.GetState(); if (keyboardState1.IsKeyDown(Keys.Space)) { currentGameState = GameState.Playing1; controller = new GameController(1, graphics); Initialize(); } return; } if (currentGameState == GameState.Credits) { KeyboardState keyboardState1 = Keyboard.GetState(); if (keyboardState1.IsKeyDown(Keys.Space)) { currentGameState = GameState.MainMenu; controller = new GameController(1, graphics); Initialize(); } return; } base.Update(gameTime); }
public static void recordBubble(Bubble bubble) { bubbleCount++; //ballsFired++; bubbles[(int)bubble.color]++; }
public static float getColorPercentage(Bubble.Colors color) { return bubbles[(int)color] / bubbleCount; }