private void SpawnNewItems() { //int -> number of position to move down, List<GridItem> -> list of items to move down about this(key) positions. Dictionary <int, List <GridItem> > itemsToMove = new Dictionary <int, List <GridItem> >(); int lenghtY = GridItems.GetLength(1); foreach (int x in positionsForNewItems.Keys) { int numberOfDestroyedItems = 0; for (int y = 0; y < lenghtY; y++) { if (GridItems[x, y] == null) { numberOfDestroyedItems++; } else if (numberOfDestroyedItems > 0) { if (!itemsToMove.ContainsKey(numberOfDestroyedItems)) { itemsToMove.Add(numberOfDestroyedItems, new List <GridItem>()); } int newY = y - numberOfDestroyedItems; GridItems[x, newY] = GridItems[x, y]; GridItems[x, newY].Position = new IntVector2(x, newY); itemsToMove[numberOfDestroyedItems].Add(GridItems[x, newY]); #if UNITY_EDITOR GridItems[x, newY].UpdateGameObjectName(); #endif } } if (!itemsToMove.ContainsKey(numberOfDestroyedItems)) { itemsToMove.Add(numberOfDestroyedItems, new List <GridItem>()); } //Spawn new items for (int i = lenghtY - numberOfDestroyedItems; i < lenghtY; i++) { ItemsGenerator.GenerateNewItem(x, i, numberOfDestroyedItems, true); itemsToMove[numberOfDestroyedItems].Add(GridItems[x, i]); } } MoveItems(itemsToMove, false, AnimManager.FallDownItems); }
/// <summary>Verify that the coordinates are within the boundary of the board</summary> private bool InsideBorders(params IntVector2[] coordinates) { int gridItemsXLenght = GridItems.GetLength(0); int gridItemsYLenght = GridItems.GetLength(1); //Use "for" loop instead of foreach for GB oprimization :) int lenght = coordinates.Length; for (int i = 0; i < lenght; i++) { if (coordinates[i].x >= gridItemsXLenght || coordinates[i].y >= gridItemsYLenght || coordinates[i].x < 0 || coordinates[i].y < 0) { return(false); } } return(true); }
public void CheckMatches(IntVector2 field, bool skipReshuffle = false) { matchesItems.Clear(); positionsForNewItems.Clear(); if (field == IntVector2.Zero) { field = new IntVector2(GridItems.GetLength(0), GridItems.GetLength(1)); } CheckMatchesOnField(field, skipReshuffle); if (matchesItems.Count >= 3 && MovementAnimationSystem.animatingObjects.Count == 0) { int points = (int)(Mathf.Pow(matchesItems.Count, 2) * 10); AddPionts(points); DestroyMatchesAndSpawnNewItems(); } else if (matchesItems.Count < 3 && switchReshuffle == 0) { InputBlocked = false; } }
private void Reshuffle() { //int -> number of position to move (x or y), List<GridItem> -> list of items to move about key(int) positions. Dictionary <int, List <GridItem> > itemsToMove = new Dictionary <int, List <GridItem> >(); itemsToMove.Add(1, new List <GridItem>()); itemsToMove.Add(-1, new List <GridItem>()); //if reshuffle has finished and possible match still doesn't exist, reshuffle again in another way. The value means kind of the way. switchReshuffle = switchReshuffle > 5 ? 0 : switchReshuffle + 1; int LenghtY = GridItems.GetLength(1); int LenghtX = GridItems.GetLength(0); //Vertical if (switchReshuffle % 2 == 0) { for (int y = 0; y < LenghtY - 1; y += 2) { for (int x = 0; x < LenghtX; x++) { if (switchReshuffle == 2 && x % 2 != 0) { continue; } else if (switchReshuffle == 4 && x % 2 == 0) { continue; } else if (switchReshuffle == 6) { y++; x = 0; continue; } //Changing items position. GridItem item = GridItems[x, y]; GridItems[x, y] = GridItems[x, y + 1]; GridItems[x, y].Position = new IntVector2(x, y); item.Position = new IntVector2(x, y + 1); GridItems[x, y + 1] = item; itemsToMove[-1].Add(GridItems[x, y + 1]); itemsToMove[1].Add(GridItems[x, y]); #if UNITY_EDITOR || BUILD_DEBUG GridItems[x, y].UpdateGameObjectName(); GridItems[x, y + 1].UpdateGameObjectName(); #endif } } MoveItems(itemsToMove, false, AnimManager.ReshuffleItems); } //Horizontal else { for (int x = 0; x < LenghtX - 1; x += 2) { for (int y = 0; y < LenghtY; y++) { if (switchReshuffle == 1 && y % 2 != 0) { continue; } else if (switchReshuffle == 3 && y % 2 == 0) { continue; } else if (switchReshuffle == 5) { x++; y = 0; switchReshuffle = 0; continue; } //Changing items position. GridItem item = GridItems[x, y]; GridItems[x, y] = GridItems[x + 1, y]; GridItems[x, y].Position = new IntVector2(x, y); item.Position = new IntVector2(x + 1, y); GridItems[x + 1, y] = item; itemsToMove[-1].Add(GridItems[x + 1, y]); itemsToMove[1].Add(GridItems[x, y]); #if UNITY_EDITOR || BUILD_DEBUG GridItems[x, y].UpdateGameObjectName(); GridItems[x + 1, y].UpdateGameObjectName(); #endif } } MoveItems(itemsToMove, true, AnimManager.ReshuffleItems); } }