public void CalculateSameColorCluster() { m_SameColorClusters.Clear(); ClearVisitedCellsForSeperatedCluster(); List <IntVector2> coordsToVisit = new List <IntVector2>(); IntVector2 startCoords = new IntVector2(0, 0); while (FindNonAddedCoordForSeperated(out startCoords)) { BubbleCluster bubbleCluster = new BubbleCluster(); m_SameColorClusters.Add(bubbleCluster); coordsToVisit.Add(startCoords); Color InitColor = m_Grid[startCoords.x, startCoords.y].m_Color; while (coordsToVisit.Count > 0) { //Remove current cell from the list int indexToRemove = coordsToVisit.Count - 1; IntVector2 currentCoords = coordsToVisit[indexToRemove]; coordsToVisit.RemoveAt(indexToRemove); //Skip iteration if current coordinate/cell is already visited if (m_AddedBBForSeperatedCluster[currentCoords.x, currentCoords.y] == true) { continue; } //Retrieve ClicableCube object based on current coordinates DestroyableBubble currentBubble = m_Grid[currentCoords.x, currentCoords.y]; //Skip iteration if current bubble is not the same color with the shooting bubble if (currentBubble.m_Color != InitColor) { continue; } //Add current ClicableCube to the cluster bubbleCluster.AddBubble(currentBubble); //Set status of the current cell to visited m_AddedBBForSeperatedCluster[currentCoords.x, currentCoords.y] = true; //Search around the current cell for enabled neighbour cells. If found add a coordinate to the need to visit container. AddCoordsIfNeeded(currentCoords, new IntVector2(1, 0), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(-1, 0), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(0, 1), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(0, -1), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(1, 1), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(-1, 1), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(-1, -1), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(1, -1), ref coordsToVisit); } } }
//collision check void OnCollisionEnter(Collision collision) { if (collision.gameObject.GetComponent <DestroyableBubble>() != null && !collision.gameObject.GetComponent <DestroyableBubble>().m_isSeperated) { DestroyableBubble bubble = collision.gameObject.GetComponent <DestroyableBubble>(); m_bubbleGrid.RecalculatedClusters(bubble.m_BubbleIntVec2, m_Color); m_bubbleGrid.MarkBubble(); RespawnShootingBubble(); } }
public void RecalculatedClusters(IntVector2 startCoords, Color ShootingBubbleColor) { ClearVisitedCells(); List <IntVector2> coordsToVisit = new List <IntVector2>(); m_idx = 0;//index in bubble cluster, used for marking bubble //Add the same cell to the container which stores a coordinates/cells that needs to be visited coordsToVisit.Add(startCoords); //The logic below must be executed until we have no more coordinates to be visited while (coordsToVisit.Count > 0) { //Remove current cell from the list int indexToRemove = coordsToVisit.Count - 1; IntVector2 currentCoords = coordsToVisit[indexToRemove]; coordsToVisit.RemoveAt(indexToRemove); //Skip iteration if current coordinate/cell is already visited if (m_VisitedCells[currentCoords.x, currentCoords.y] == true) { continue; } //Set status of the current cell to visited m_VisitedCells[currentCoords.x, currentCoords.y] = true; //Retrieve ClicableCube object based on current coordinates DestroyableBubble currentBubble = m_Grid[currentCoords.x, currentCoords.y]; //Skip iteration if current bubble is not the same color with the shooting bubble if (currentBubble.m_Color != ShootingBubbleColor) { continue; } ////Add current ClicableCube to the cluster m_DestroyedBBClusters.AddBubble(currentBubble); ////Search around the current cell for enabled neighbour cells. If found add a coordinate to the need to visit container. AddCoordsIfNeeded(currentCoords, new IntVector2(1, 0), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(-1, 0), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(0, 1), ref coordsToVisit); AddCoordsIfNeeded(currentCoords, new IntVector2(0, -1), ref coordsToVisit); } }
// Start is called before the first frame update void Start() { m_Grid = new DestroyableBubble[GridDimX, GridDimY]; m_VisitedCells = new bool[GridDimX, GridDimY]; m_AddedBBForSeperatedCluster = new bool[GridDimX, GridDimY]; GenerateBubbles(); m_DestroyedBBClusters = new BubbleCluster(); m_SameColorClusters = new List <BubbleCluster>(); m_SeperatedClusters = new List <BubbleCluster>(); m_Turret = GameObject.FindGameObjectWithTag("Turret").GetComponent <Turret>(); }
public void AddBubble(DestroyableBubble bubble) { m_Bubbles.Add(bubble); }