public void DestroyMatchedElements(GridSegmentElement element) { if (element.CubeColour == CubeColours.Dead){ return; } elementsToClear.Clear(); bool[,] checkedIndices = new bool[GridController.GridWidth, GridController.GridHeight]; var frontmostElements = GridController.Instance.GetFrontmostElementsForGrid(); IntPair clickedPosition = DeterminePositionOfClickedElementInGrid(element); DetermineMatchedElements(frontmostElements, checkedIndices, element, clickedPosition); if (elementsToClear.Count >= matchThreshold){ DestroyElements(elementsToClear); GUIController.Instance.BoostEQOffsetX(); bloomController.PopBlockEffect(); WireframeController.Instance.UpdateWireframe(); } }
/* Builds a 2D array of GridSegmentElements with no other GridSegmentElements in front of them. Indices in the array are not searched for again, because they are marked found in a separate 2D array of bools. */ public GridSegmentElement[,] GetFrontmostElementsForGrid() { GridSegmentElement[,] frontmostElements = new GridSegmentElement[gridHeight, gridWidth]; bool[,] foundElements = new bool[gridHeight, gridWidth]; int found = gridHeight * gridWidth; for (int i = 0; i < gridSegments.Count && found > 0; i++){ for (int j = 0; j < gridHeight; j++){ for (int k = 0; k < gridWidth ; k++){ if (!foundElements[j,k]){ var elementToCheck = gridSegments[i].GetSegmentRowAtIndex(k).GetSegmentElementAtIndex(j); if (!elementToCheck.Destroyed){ found--; frontmostElements[j,k] = elementToCheck; foundElements[j,k] = true; } } } } } return frontmostElements; }
/* Recursively checks a 2D array of GridSegmentElements for adjacent matches to a target element. It works its way out from the coordinates of the target element, and marks checked indices to avoid checking anything more than once */ void DetermineMatchedElements(GridSegmentElement[,] frontmostElements, bool[,] checkedIndices, GridSegmentElement element, IntPair coords) { // Will happen if a player manages to make it through all grid segments if(frontmostElements[coords.x, coords.y] == null){ return; } if (frontmostElements[coords.x, coords.y].CubeColour == element.CubeColour && !checkedIndices[coords.x, coords.y]){ checkedIndices[coords.x, coords.y] = true; elementsToClear.Add(frontmostElements[coords.x, coords.y]); } else { return; } IntPair right = new IntPair(coords.x + 1, coords.y), left = new IntPair(coords.x - 1, coords.y), up = new IntPair(coords.x, coords.y + 1), down = new IntPair(coords.x, coords.y - 1); if (up.y < GridController.GridHeight){ DetermineMatchedElements(frontmostElements, checkedIndices, element, up); } if (down.y >= 0){ DetermineMatchedElements(frontmostElements, checkedIndices, element, down); } if (right.x < GridController.GridWidth){ DetermineMatchedElements(frontmostElements, checkedIndices, element, right); } if (left.x >= 0){ DetermineMatchedElements(frontmostElements, checkedIndices, element, left); } }
IntPair DeterminePositionOfClickedElementInGrid(GridSegmentElement element) { var row = element.GetComponentInParent<GridSegmentRow>(); return new IntPair(row.GetIndexOfElementInRow(element), row.GetComponentInParent<GridSegment>().GetIndexOfRowInSegment(row)); }
public GridSegmentRow GetRowContainingElement(GridSegmentElement element) { return segmentRows.FirstOrDefault(row => row.GetIndexOfElementInRow(element) >= 0); }
void FindAndKillAllSegmentsAtIndex(GridSegment segment, GridSegmentElement element) { var row = segment.GetRowContainingElement(element); int y = segment.GetIndexOfRowInSegment(row); int x = row.GetIndexOfElementInRow(element); gridSegments.ForEach(gridSegment => gridSegment.KillElementAtIndex(x, y)); }
public int GetIndexOfElementInRow(GridSegmentElement element) { return elements.IndexOf(element); }
public void AddElement(GridSegmentElement element) { elements.Add(element); }