public void SetNewValueForBubble(Bubble bubble, int sortedNeighborsCount) { var bubbleValueIndex = _bubbleValues.FindIndex(a => a.Equals(bubble.BubbleValue)); if (_bubbleValues[bubbleValueIndex] == GetMaxBubbleValue()) { bubble.TriggerMaxValueEffect(); if (MaxBubblePopped != null) { MaxBubblePopped.Invoke(bubble.BubbleValue); } return; } try { bubble.SetValue(_bubbleValues[bubbleValueIndex + sortedNeighborsCount - 1]); bubble.SetColor(_bubbleColors[bubbleValueIndex + sortedNeighborsCount - 1]); } catch (Exception e) { if (MaxBubblePopped != null) { MaxBubblePopped.Invoke(bubble.BubbleValue); } Debug.LogFormat("{0} for bubble {1}", e, bubble.BubbleValue); throw; } if (bubble.BubbleValue == GetMaxBubbleValue()) { bubble.TriggerMaxValueEffect(); if (MaxBubblePopped != null) { MaxBubblePopped.Invoke(bubble.BubbleValue); } return; } CreatePointsPopup(bubble.transform.position, bubble.BubbleValue, bubble.BubbleColor); bubble.TriggerParticleEffect(bubble.BubbleColor); if (OnBubblePopped != null) { OnBubblePopped.Invoke(bubble.BubbleValue); } }
public bool ThisBubbleIsAPowerup(Bubble bubble) { // This loops through all the powerup types in the game defined in the Constants class and compares it to the class name // of the bubble. If the class name of the bubble matches the name of one of the powerups, we return true for (int i = 0; i < Constants.POWERUP_TYPES.Length; i++) { if (bubble.GetType().Name == Constants.POWERUP_TYPES[i]) { return(true); } } // If this bubble didn't match with any of the names of powerups, then we return false return(false); }
private void StartNeighborChain(Bubble b) { _currentNeighborChain = new List <Bubble>(); GetNeighborChain(b, b.BubbleValue); if (_currentNeighborChain.Count == 0) { return; } var sortedNeighbors = _currentNeighborChain.OrderBy(n => n.transform.position.y).ToList(); var highestNeighbor = sortedNeighbors.Last(); MergeBubbles(highestNeighbor, sortedNeighbors); }
public bool BubbleHasSameColorNeighbor(Bubble bubble) { int index = bubbles.IndexOf(bubble); int leftBubbleIndex = GetBubbleIndexFromPosition(bubbles[index].Position.X - Constants.WORLD_UNIT, bubbles[index].Position.Y); int rightBubbleIndex = GetBubbleIndexFromPosition(bubbles[index].Position.X + Constants.WORLD_UNIT, bubbles[index].Position.Y); int upBubbleIndex = GetBubbleIndexFromPosition(bubbles[index].Position.X, bubbles[index].Position.Y - Constants.WORLD_UNIT); int downBubbleIndex = GetBubbleIndexFromPosition(bubbles[index].Position.X, bubbles[index].Position.Y + Constants.WORLD_UNIT); // For all of these, we make sure that the index corresponds to a bubble that exists, because when we get the bubble index from //the position using the corresponding method, we designed it so that it returned Constants.NO_BUBBLE if that bubble didn't exist. if (leftBubbleIndex != Constants.NO_BUBBLE && OnSameRow(index, leftBubbleIndex)) //AKA, if a bubble at this index exists { if (bubbles[index].BubbleColor == bubbles[leftBubbleIndex].BubbleColor) { return(true); } } if (rightBubbleIndex != Constants.NO_BUBBLE && rightBubbleIndex < bubbles.Count && OnSameRow(index, rightBubbleIndex)) { if (bubbles[index].BubbleColor == bubbles[rightBubbleIndex].BubbleColor) { return(true); } } if (upBubbleIndex != Constants.NO_BUBBLE) { if (bubbles[index].BubbleColor == bubbles[upBubbleIndex].BubbleColor) { return(true); } } if (downBubbleIndex != Constants.NO_BUBBLE && downBubbleIndex < bubbles.Count) { if (bubbles[index].BubbleColor == bubbles[downBubbleIndex].BubbleColor) { return(true); } } return(false); }
private void GetNeighborChain(Bubble bubble, int valueToChain) { var adjacentBubbles = GetAdjacentSpots(bubble); var neighbors = adjacentBubbles.neighbors; foreach (var n in neighbors) { if (n.BubbleValue != bubble.BubbleValue || _currentNeighborChain.Contains(n)) { continue; } _currentNeighborChain.Add(n); GetNeighborChain(n, valueToChain); } }
// This method compares the position of one bubble to that of all the other bubbles in the grid. If this bubble tries falling through any // other bubbles or through the bottom of the grid, we stop it from doing so. private void HandleCollision(Bubble bubbleToHandle) { foreach (Bubble bubble in bubbleGrid.Bubbles) { if (bubbleToHandle.Position.Y >= Constants.BOTTOM_OF_GRID) { bubbleToHandle.MoveTo(bubbleToHandle.Position.X, Constants.BOTTOM_OF_GRID); return; } Vector2 topOfBubble = new Vector2(bubble.Position.X + Constants.BUBBLE_RADIUS, bubble.Position.Y); if (bubbleToHandle.Intersects(topOfBubble)) { bubbleToHandle.MoveTo(bubbleToHandle.Position.X, bubble.Position.Y - Constants.WORLD_UNIT); return; } } }
private void MergeBubbles(Bubble highestNeighbor, List <Bubble> sortedNeighbors) { foreach (var neighbor in sortedNeighbors) { if (neighbor == highestNeighbor) { continue; } neighbor.gameObject.SetActive(false); neighbor.connected = false; } _bubbleHandler.SetNewValueForBubble(highestNeighbor, sortedNeighbors.Count); StartNeighborChain(highestNeighbor); RemoveDisconnectedBubbles(); CheckIfGridNeedsToMove(); }
public void SetValuesFrom(Bubble secondNextBullet) { SetValue(secondNextBullet.BubbleValue); SetColor(secondNextBullet.BubbleColor); }
// Long and clunky, consider rewriting public void Initialize(int difficulty) { bubbles.Clear(); /* Set up initial grid of bubbles. Poweups will be places in random spots within the grid, though not * in place of any of the outermost bubbles */ int randomCoordinateX1 = random.Next(Constants.GRID_WIDTH_IN_UNITS - 1); if (randomCoordinateX1 == 0) { randomCoordinateX1++; } int randomCoordinateY1 = random.Next(Constants.GRID_HEIGHT_IN_UNITS - 1); if (randomCoordinateY1 == 0) { randomCoordinateY1++; } int randomCoordinateX2 = random.Next(Constants.GRID_WIDTH_IN_UNITS - 1); if (randomCoordinateX2 == 0) { randomCoordinateX2++; } int randomCoordinateY2 = random.Next(Constants.GRID_HEIGHT_IN_UNITS - 1); if (randomCoordinateY1 == 0) { randomCoordinateY1++; } for (int i = 0; i < Constants.GRID_HEIGHT_IN_UNITS; i++) { for (int j = 0; j < Constants.GRID_WIDTH_IN_UNITS; j++) { Vector2 thisBubblesPosition = Vector2.Add(Constants.BUBBLE_GRID_ORIGIN, new Vector2(j * Constants.WORLD_UNIT, i * Constants.WORLD_UNIT)); // We're going to start randomly adding powerups for testing. For now, we add one at the top left. Bubble bubble; if (difficulty > 3 && difficulty < 5) { if (i == randomCoordinateX1 && j == randomCoordinateY1) { bubble = new ClearRowAndColumnPowerup(thisBubblesPosition, GenerateRandomColor(difficulty), clearRowAndColumnPowerupSprite, content); } else { bubble = new Bubble(thisBubblesPosition, GenerateRandomColor(difficulty), bubbleSprite); } } else if (difficulty > 4) { if (i == randomCoordinateX1 && j == randomCoordinateY1) { bubble = new ClearRowAndColumnPowerup(thisBubblesPosition, GenerateRandomColor(difficulty), clearRowAndColumnPowerupSprite, content); } else if (i == randomCoordinateX2 && j == randomCoordinateY2) { bubble = new ClearColorPowerup(thisBubblesPosition, GenerateRandomColor(difficulty), clearColorPowerupSprite, content); } else { bubble = new Bubble(thisBubblesPosition, GenerateRandomColor(difficulty), bubbleSprite); } } else { bubble = new Bubble(thisBubblesPosition, GenerateRandomColor(difficulty), bubbleSprite); } bubbles.Add(bubble); } } }
// private Bubble GetClosestMatchingNeighbor(Bubble collisionBubble) // { // var adjacentBubbles = GetAdjacentSpots(collisionBubble); // var neighbors = adjacentBubbles.neighbors; // // var neighborDis = 1000f; // Bubble closestNeighbor = null; // // if (neighbors.Any(n => n.BubbleValue == collisionBubble.BubbleValue) == false) return null; // // foreach (var neighbor in neighbors.Where(n => n.BubbleValue == collisionBubble.BubbleValue)) // { // var d = Vector2.Distance(neighbor.transform.position, collisionBubble.transform.position); // // if (!(d < neighborDis)) continue; // neighborDis = d; // closestNeighbor = neighbor; // } // // return closestNeighbor; // } private (List <Bubble> emptySpots, List <Bubble> neighbors) GetAdjacentSpots(Bubble bubble) { var emptySpots = new List <Bubble>(); var neighbors = new List <Bubble>(); // Debug.LogFormat("checking bubble at row: {0} and col {1} : {2}...", bubble.row, bubble.col, // bubble.BubbleValue); if (bubble.col + 1 < _colums) { var spotToCheck = _bubbleGrid[bubble.row][bubble.col + 1]; // Debug.Log("checking right..."); if (!spotToCheck.gameObject.activeSelf) { emptySpots.Add(spotToCheck); // Debug.Log("...empty spot."); } else { // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); neighbors.Add(spotToCheck); } } //left if (bubble.col - 1 >= 0) { var spotToCheck = _bubbleGrid[bubble.row][bubble.col - 1]; // Debug.Log("checking left..."); if (!spotToCheck.gameObject.activeSelf) { emptySpots.Add(spotToCheck); // Debug.Log("...empty spot."); } else { // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); neighbors.Add(spotToCheck); } } //top if (bubble.row - 1 >= 0) { var spotToCheck = _bubbleGrid[bubble.row - 1][bubble.col]; // Debug.Log("checking top..."); if (!spotToCheck.gameObject.activeSelf) { emptySpots.Add(spotToCheck); // Debug.Log("...empty spot."); } else { neighbors.Add(spotToCheck); // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); } } //bottom if (bubble.row + 1 < _bubbleGrid.Count) { var spotToCheck = _bubbleGrid[bubble.row + 1][bubble.col]; // Debug.Log("checking bottom..."); if (!spotToCheck.gameObject.activeSelf) { emptySpots.Add(spotToCheck); // Debug.Log("...empty spot."); } else { neighbors.Add(spotToCheck); // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); } } //top-left if (bubble.row - 1 >= 0 && bubble.col - 1 >= 0 && bubble.col % 2 != 0) { // Debug.Log("checking top left..."); var spotToCheck = _bubbleGrid[bubble.row - 1][bubble.col - 1]; if (!spotToCheck.gameObject.activeSelf) { emptySpots.Add(spotToCheck); // Debug.Log("...empty spot."); } else { neighbors.Add(spotToCheck); // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); } } //top-right if (bubble.row - 1 >= 0 && bubble.col + 1 < _colums && bubble.col % 2 != 0) { var spotToCheck = _bubbleGrid[bubble.row - 1][bubble.col + 1]; // Debug.Log("checking top right..."); if (!spotToCheck.gameObject.activeSelf) { emptySpots.Add(spotToCheck); // Debug.Log("...empty spot."); } else { neighbors.Add(spotToCheck); // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); } } //bottom-left if (bubble.row + 1 < _bubbleGrid.Count && bubble.col - 1 >= 0 && bubble.col % 2 == 0) { var spotToCheck = _bubbleGrid[bubble.row + 1][bubble.col - 1]; // Debug.Log("checking bottom left..."); if (!spotToCheck.gameObject.activeSelf) { emptySpots.Add(spotToCheck); // Debug.Log("...empty spot."); } else { neighbors.Add(spotToCheck); // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); } } //bottom-right if (bubble.row + 1 < _bubbleGrid.Count && bubble.col + 1 < _colums && bubble.col % 2 == 0) { var spotToCheck = _bubbleGrid[bubble.row + 1][bubble.col + 1]; // Debug.Log("checking bottom right..."); if (!spotToCheck.gameObject.activeSelf) { // Debug.Log("...empty spot."); emptySpots.Add(spotToCheck); } else { neighbors.Add(spotToCheck); // Debug.Log("...found neighbor: " + spotToCheck.BubbleValue); } } return(emptySpots, neighbors); }