PairedGroup GetPairedGroup(HexagonGroup group) { Hexagon a = HexagonManager.Instance.GetHexagon(group.A); Hexagon b = HexagonManager.Instance.GetHexagon(group.B); Hexagon c = HexagonManager.Instance.GetHexagon(group.C); if (a.IsSameColorWith(b)) { OffsetCoordinate[] pairs = new OffsetCoordinate[2]; pairs[0] = group.A; pairs[1] = group.B; OffsetCoordinate other = group.C; return(new PairedGroup(pairs, other, a.GetColorIndex())); } if (b.IsSameColorWith(c)) { OffsetCoordinate[] pairs = new OffsetCoordinate[2]; pairs[0] = group.B; pairs[1] = group.C; OffsetCoordinate other = group.A; return(new PairedGroup(pairs, other, b.GetColorIndex())); } if (c.IsSameColorWith(a)) { OffsetCoordinate[] pairs = new OffsetCoordinate[2]; pairs[0] = group.C; pairs[1] = group.A; OffsetCoordinate other = group.B; return(new PairedGroup(pairs, other, c.GetColorIndex())); } return(null); }
/// <summary> /// Returns true if there are no possible moves that result in a match on the grid /// </summary> /// <returns>true; no more move left, false moves possible.</returns> public bool IsDeadlocked() { matchesOnGridSet.Clear(); // Check only on even coloums; checking all coloums is redundant. for (int x = 0; x < gridInfo.x; x += 2) { for (int y = 0; y < gridInfo.y; y++) { for (int i = 0; i < 6; i++) { HexagonGroup group = GetGroupAtCorner(x, y, (HexagonPiece.Edge)i); if (!group.IsEmpty) { for (int j = 0; j < 2; j++) { group.RotateClockwise(); HexagonMatch match = TryGetMatchingPiecesAt(group); if (match != null) { PoolManager.Instance.Push(match); group.RotateClockwise(2 - j); return(false); } } group.RotateClockwise(); } } } } return(true); }
// Find the match that this hexagon piece is part of (implementation) private void TryGetMatchingPiecesAt(HexagonPiece piece, HexagonMatch match) { bool isPieceAddedToMatch = false; // Iterate over each possible group that is formed with this hexagon piece and see if that group is a match for (int i = 0; i < 6; i++) { HexagonGroup _group = GetGroupAtCorner(piece.GridPos.x, piece.GridPos.y, (HexagonPiece.Edge)i); if (_group.IsEmpty || !_group.IsMatching) { continue; } if (!isPieceAddedToMatch) { match.Add(piece); isPieceAddedToMatch = true; } if (matchesOnGridSet.Add(_group.Piece1)) { TryGetMatchingPiecesAt(_group.Piece1, match); } if (matchesOnGridSet.Add(_group.Piece2)) { TryGetMatchingPiecesAt(_group.Piece2, match); } if (matchesOnGridSet.Add(_group.Piece3)) { TryGetMatchingPiecesAt(_group.Piece3, match); } } }
/// <summary> /// Select a group. /// </summary> /// <param name="clickPos"></param> public void SelectGroup(Vector3 clickPos) { if (selectedGroup != null) // When a selection already exists, clear it. { RemoveSelection(); return; } Vector3 closestPoint = Vector3.zero; float smallestDistance = float.MaxValue; int midPointNumber = 0; for (int i = 0; i < hexagonGroups.Count; i++) { float clickDist = Vector3.Distance(clickPos, hexagonGroups[i].GetPosition()); if (smallestDistance > clickDist) { smallestDistance = clickDist; closestPoint = hexagonGroups[i].GetPosition(); midPointNumber = i; } } selectedGroup = hexagonGroups[midPointNumber]; foreach (var trans in hexagonGroups[midPointNumber].GetHexagonsTransformList()) { GameObject obj = Instantiate(selectionGO, trans); selectionObjects.Add(obj); } Debug.Log("closest groupPoint to " + clickPos + " is " + closestPoint + " with a distance of " + smallestDistance); Debug.Log("selected grouping is " + midPointNumber); }
/// <summary> /// Remove the current group selection. /// </summary> public void RemoveSelection() { Debug.Log("attempting to delete selection objects"); foreach (var obj in selectionObjects) { Debug.Log("in selectionObjects"); Destroy(obj); } selectionObjects.Clear(); selectedGroup = null; }
public HexagonGroup NearestGroupTo(Vector2 position) { HexagonGroup nearest = _groupList[0]; float minDistance = Vector2.Distance(nearest.GetCenter(), position); for (int i = 1; i < _groupList.Count; i++) { if (minDistance > Vector2.Distance(_groupList[i].GetCenter(), position)) { minDistance = Vector2.Distance(_groupList[i].GetCenter(), position); nearest = _groupList[i]; } } return(nearest); }
// Finds the match that have at least one hexagon piece from this group public HexagonMatch TryGetMatchingPiecesAt(HexagonGroup group) { matchesOnGridSet.Clear(); HexagonMatch result = TryGetMatchingPiecesAt(group.Piece1); if (result == null) { result = TryGetMatchingPiecesAt(group.Piece2); } if (result == null) { result = TryGetMatchingPiecesAt(group.Piece3); } return(result); }
/// <summary> /// If points resides inside the grid, locate the group that are closest to the point /// </summary> /// <param name="point"></param> /// <param name="group">false;null true; sets group to the found one.</param> /// <returns>false; point is outside of grid, true; inside.</returns> public bool TryGetGroup(Vector2 point, out HexagonGroup group) { if (point.x <= 0f || point.x >= gridSize.x || point.y <= 0f || point.y >= gridSize.y) { group = new HexagonGroup(); return(false); } // Calculate the row and column indices of the hexagon piece that the point resides inside GetCoordinatesFrom(point, out int x, out int y); // Find the hexagon piece's corner that is closest to the point HexagonPiece.Edge corner = grid[x][y].HandleEdge(grid[x][y].GetClosestEdge(point - (Vector2)grid[x][y].transform.localPosition)); group = GetGroupAtCorner(x, y, corner); return(true); }
private void OnTap(Vector3 pos) { if (_gameOver || _restarting || _rotating) { return; } HexagonGroup group = HexagonGroupManager.Instance.NearestGroupTo(pos + (Vector3)HexagonManager.Instance.CenterOffset()); float distance = Vector3.Distance(HexagonManager.Instance.PixelToWorld(group.GetCenter()), pos); //checking if tapping other than grid. if (distance < 1f) { _selectedGroup = HexagonGroupManager.Instance.NearestGroupTo(pos + (Vector3)HexagonManager.Instance.CenterOffset()); SetSelectedGroupToRotator(); _anyGroupSelected = true; } }
public int ColorCountOfGroup(HexagonGroup group, int colorIndex) { int count = 0; if (colorIndex == HexagonManager.Instance.GetHexagon(group.A).GetColorIndex()) { count++; } if (colorIndex == HexagonManager.Instance.GetHexagon(group.B).GetColorIndex()) { count++; } if (colorIndex == HexagonManager.Instance.GetHexagon(group.C).GetColorIndex()) { count++; } return(count); }