private static void GenerateAdjacentTiles(GameTile tile, GameTileType type, Dictionary <Point, GameTile> tiles) { // West var point = tile.Position + new Point(-1, 0); AddTileAtPointIfItDoesntExist(type, tiles, point); // NorthWest point = tile.Position + new Point(-1, -1); AddTileAtPointIfItDoesntExist(type, tiles, point); // NorthEast point = tile.Position + new Point(0, -1); AddTileAtPointIfItDoesntExist(type, tiles, point); // East point = tile.Position + new Point(1, 0); AddTileAtPointIfItDoesntExist(type, tiles, point); // SouthEast point = tile.Position + new Point(1, 1); AddTileAtPointIfItDoesntExist(type, tiles, point); // SouthWest point = tile.Position + new Point(0, 1); AddTileAtPointIfItDoesntExist(type, tiles, point); }
/* * Match Avatar Tile * * @param tiles */ public void MatchAvatarTile(GameObject tile) { GameTileType tempTileType = tile.GetComponent <GameTileBase>().GetGameTileType(); for (int i = 0; i < gameBoard.width; i++) { for (int j = 0; j < gameBoard.height; j++) { if (gameBoard.allGameTiles[i, j]) { GameTileBase tempTile = gameBoard.allGameTiles[i, j].GetComponent <GameTileBase>(); // Match all tiles if (tile.GetComponent <GameTileBase>().GetTileType() == TileType.Avatar) { tempTile.SetHasMatched(true); } // Finds tile of same type else if (gameBoard.allGameTiles[i, j].GetComponent <GameTileBase>().GetGameTileType() == tempTileType) { ChainSpecialMatches(tempTile.gameObject); tempTile.SetHasMatched(true); } } } } }
private bool CheckGliderAlignment() { HashSet <GameTileType> gameTileTypes = new HashSet <GameTileType>(); foreach (GameObject tile in currentMatches) { GameTileType testType = tile.GetComponent <GameTileBase>().GetGameTileType(); if (testType != GameTileType.None) { gameTileTypes.Add(testType); } } foreach (GameTileType type in gameTileTypes) { int typeCounter = 0; foreach (GameObject tile in currentMatches) { if (tile.GetComponent <GameTileBase>().GetGameTileType() == type) { typeCounter++; if (typeCounter >= 5) { return(true); } } } } return(false); }
private static void AddTileAtPointIfItDoesntExist(GameTileType type, Dictionary <Point, GameTile> tiles, Point point) { if (tiles.ContainsKey(point)) { return; } tiles.Add(point, new GameTile(point, type)); }
/* * Ensures no match, when tiles are spawned * * @param col * @param row * @param tileType * */ public bool CheckSetUpMatch(int col, int row, GameTileType tileType) { // Quick method to check when the col/row after third are tested if (col > 1 && row > 1) { // Horizontal check GameTileType leftTile1Type = allGameTiles[col - 1, row].GetComponent <GameTileBase>().GetGameTileType(); GameTileType leftTile2Type = allGameTiles[col - 2, row].GetComponent <GameTileBase>().GetGameTileType(); if (tileType == leftTile1Type && tileType == leftTile2Type) { return(true); } // Vertical Check GameTileType BottomTile1Type = allGameTiles[col, row - 1].GetComponent <GameTileBase>().GetGameTileType(); GameTileType BottomTile2Type = allGameTiles[col, row - 2].GetComponent <GameTileBase>().GetGameTileType(); if (tileType == BottomTile1Type && tileType == BottomTile2Type) { return(true); } } // Checks early edge tiles else if (col <= 1 || row <= 1) { if (row > 1) { if (allGameTiles[col, row - 1].GetComponent <GameTileBase>() && allGameTiles[col, row - 2].GetComponent <GameTileBase>()) { GameTileType LeftTile1Type = allGameTiles[col, row - 1].GetComponent <GameTileBase>().GetGameTileType(); GameTileType LeftTile2Type = allGameTiles[col, row - 2].GetComponent <GameTileBase>().GetGameTileType(); if (tileType == LeftTile1Type && tileType == LeftTile2Type) { return(true); } } } if (col > 1) { if (allGameTiles[col - 1, row].GetComponent <GameTileBase>() && allGameTiles[col - 2, row].GetComponent <GameTileBase>()) { GameTileType BottomTile1Type = allGameTiles[col - 1, row].GetComponent <GameTileBase>().GetGameTileType(); GameTileType BottomTile2Type = allGameTiles[col - 2, row].GetComponent <GameTileBase>().GetGameTileType(); if (tileType == BottomTile1Type && tileType == BottomTile2Type) { return(true); } } } } return(false); }
/* * Shuffles the board */ public void ShuffleBoard() { // New collection to store tiles in List <GameObject> newTilesCollection = new List <GameObject>(); // Gets all the pieces on the board for (int i = 0; i < gameBoard.width; i++) { for (int j = 0; j < gameBoard.height; j++) { if (gameBoard.allGameTiles[i, j]) { newTilesCollection.Add(gameBoard.allGameTiles[i, j]); } } } // Shuffle the new board for (int i = 0; i < gameBoard.width; i++) { for (int j = 0; j < gameBoard.height; j++) { int newIndex = Random.Range(0, newTilesCollection.Count); // Use a similiar check to ensure that the board doesn't shuffle into an immediate match GameTileType tempGameTileType = newTilesCollection[newIndex].GetComponent <GameTileBase>().GetGameTileType(); int maxLoops = 0; while (gameBoard.CheckSetUpMatch(i, j, tempGameTileType) && maxLoops < 25) { newIndex = Random.Range(0, newTilesCollection.Count); maxLoops++; } GameTileBase tileScript = newTilesCollection[newIndex].GetComponent <GameTileBase>(); // Assigns variables to the tiles new position tileScript.currentCol = i; tileScript.currentRow = j; gameBoard.allGameTiles[i, j] = newTilesCollection[newIndex]; newTilesCollection.Remove(newTilesCollection[newIndex]); } } // Deadlock Check if (IsGameDeadlocked()) { ShuffleBoard(); return; } else { // immediately check for matches matchesManager.CheckForMatches(); } }
private static void AddLayer(GameTileType type, Dictionary <Point, GameTile> tiles) { var existingTiles = tiles.Values.ToArray(); foreach (var tile in existingTiles) { GenerateAdjacentTiles(tile, type, tiles); } }
public GameTileContent Get(GameTileType type) { switch (type) { case GameTileType.Empty: return(Get(emptyPrefab)); case GameTileType.Wall: return(Get(wallPrefab)); } Debug.Assert(false, "Unsupported non-tower type: " + type); return(null); }
/* * Setter for gameTileType */ public void SetGameTileType(GameTileType gameTileType) { this.gameTileType = gameTileType; if (tileType == TileType.Avatar) { tileImage.sprite = LoadTileSprite(Utilities.AvatarIcon); } else { tileImage.sprite = LoadTileSprite(Utilities.FindTileType(tileType, gameTileType)); } }
/* * Finds tiles to destroy by Avatar tile */ private List <GameObject> GetAvatarMatches(GameObject tile) { GameTileBase tileScript = tile.GetComponent <GameTileBase>(); List <GameObject> tiles = new List <GameObject>(); if (!tile) { return(tiles); } GameTileType gameTileType = tileScript.GetGameTileType(); print("Bending " + tile + " and " + gameTileType); for (int i = 0; i < gameBoard.width; i++) { for (int j = 0; j < gameBoard.height; j++) { // Checks for the avatar tile in list if (tileScript == gameBoard.allGameTiles[i, j].GetComponent <GameTileBase>()) { tiles.Add(gameBoard.allGameTiles[i, j]); gameBoard.allGameTiles[tileScript.currentCol, tileScript.currentRow].GetComponent <GameTileBase>().SetHasMatched(true); } // Finds tiles of same game tile type if (gameBoard.allGameTiles[i, j].GetComponent <GameTileBase>().GetGameTileType() == gameTileType) { tiles.Add(gameBoard.allGameTiles[i, j]); gameBoard.allGameTiles[i, j].GetComponent <GameTileBase>().SetHasMatched(true); } // All tiles, if both are avtar else if (tileScript.GetTileType() == TileType.Avatar && tileScript.GetOtherTile().GetComponent <GameTileBase>().GetTileType() == TileType.Avatar) { tiles.Add(gameBoard.allGameTiles[i, j]); gameBoard.allGameTiles[i, j].GetComponent <GameTileBase>().SetHasMatched(true); } } } return(tiles); }
/* * Spawns a tile * * @param tileObject - prefab to spawn * @param name - for readabilty * @param x - column * @param y - row * @param returnObject - should return GameObject? * * @return GameObject */ public GameObject SpawnTile(GameObject tileObject, string name, int x, int y, bool returnObject) { // Offest spawn location by the gameboard location Vector2 tempCoord = new Vector2(gameObject.transform.position.x + x, gameObject.transform.position.y + y); //First random type GameTileType tempTileType = (GameTileType)Random.Range(0, NumOfTileTypes); // Sets type and checks for no initial match on start if (returnObject) { int maxLoops = 0; // To stop infinite loops while (CheckSetUpMatch(x, y, tempTileType) && maxLoops < 25) { tempTileType = (GameTileType)Random.Range(0, NumOfTileTypes); maxLoops++; } } // Spawns tile in scene GameObject tempTile = Instantiate(tileObject, tempCoord, Quaternion.identity, gameGridObject.transform); if (tempTile.GetComponent <GameTileBase>()) { tempTile.GetComponent <GameTileBase>().SetGameTileType(tempTileType); tempTile.GetComponent <GameTileBase>().currentCol = x; tempTile.GetComponent <GameTileBase>().currentRow = y; tempTile.GetComponent <GameTileBase>().gameGridObject = gameGridObject; } // For organisational and debugging purposes tempTile.name = string.Format("{0}: ({1}, {2})", name, x, y); if (returnObject) { return(tempTile); } else { return(null); } }
/* * Spawns game tile, when refilling board * * @param x - column * @param y - row */ private void RefillGameTileSpawn(int x, int y) { if (allGameTiles[x, y]) { return; } // Spawn location, so tiles slide in Vector2 tempCoord = new Vector2(x, y); tempCoord.x += Utilities.ColumnOffset; tempCoord.y += Utilities.RowOffset + 5.0f; //First random type GameTileType tempTileType = (GameTileType)Random.Range(0, NumOfTileTypes); // Sets type and checks for no initial match on start int maxLoops = 0; // Stops infinite loop // Ensures no match on slide in while (CheckSetUpMatch(x, y, tempTileType) && maxLoops < 25) { tempTileType = (GameTileType)Random.Range(0, NumOfTileTypes); maxLoops++; } // Spawns tile GameObject tempTile = Instantiate(s_GameTile, tempCoord, Quaternion.identity, gameGridObject.transform); if (tempTile.GetComponent <GameTileBase>()) { tempTile.GetComponent <GameTileBase>().SetGameTileType(tempTileType); tempTile.GetComponent <GameTileBase>().currentCol = x; tempTile.GetComponent <GameTileBase>().currentRow = y; } // For organisational and debugging purposes tempTile.name = string.Format("New {0}: ({1}, {2})", name, x, y); allGameTiles[x, y] = tempTile; }
/* * Makes Avatar tile (Colour Bomb) */ public void GenerateAvatarTile() { isRowChar = false; isRowChar = false; // Fail safe Reset gameBoard.allGameTiles[currentCol, currentRow] = gameObject; // Spawn destroy particle gameBoard.DestroyParticle(currentCol, currentRow); if (arrowMask.GetComponent <SpriteRenderer>().enabled) { arrowMask.GetComponent <SpriteRenderer>().enabled = false; } gameTileType = GameTileType.None; tileType = TileType.Avatar; SetGameTileType(gameTileType); //Change score scoreManager.AddToScore(1); }
// Helps makes string path to tile image public static string FindTileType(TileType tileType, GameTileType gameTile) { return(string.Format("{0}/T_Avatar{1}Icon{0}", tileType, gameTile)); }
public GameTile(Point position, GameTileType type) { Position = position; Type = type; Occupant = null; }
/* * Checks for matches on board */ private IEnumerator CheckForMatches_Cor() { yield return(new WaitForSeconds(gameBoard.GetDestructionWaitTime() / 3)); if (!isChecking) { //print("matching"); isChecking = true; if (gameBoard) { for (int i = 0; i < gameBoard.width; i++) { for (int j = 0; j < gameBoard.height; j++) { GameObject currentTile = gameBoard.allGameTiles[i, j]; // Null pointer check if (currentTile) { GameTileType currentTileType = currentTile.GetComponent <GameTileBase>().GetGameTileType(); // Horizontal Check if (i >= 0 && i < gameBoard.width) { GameObject[] NearbyTiles = GetTestingTiles(i, j, 1, 0); GameObject rowTile1 = NearbyTiles[0]; GameObject rowTile2 = NearbyTiles[1]; if (rowTile1 && rowTile2) { GameTileType rowTile1Type = rowTile1.GetComponent <GameTileBase>().GetGameTileType(); GameTileType rowTile2Type = rowTile2.GetComponent <GameTileBase>().GetGameTileType(); // Compare types if (currentTileType == rowTile1Type && currentTileType == rowTile2Type) { GameObject[] tiles = { currentTile, rowTile1, rowTile2 }; MatchCharTile(tiles); MatchGliderTile(tiles); AddToList(tiles); } } // Vertical check if (j >= 0 && j < gameBoard.height) { NearbyTiles = GetTestingTiles(i, j, 0, 1); GameObject colTile1 = NearbyTiles[0]; GameObject colTile2 = NearbyTiles[1]; if (colTile1 && colTile2) { GameTileType colTile1Type = colTile1.GetComponent <GameTileBase>().GetGameTileType(); GameTileType colTile2Type = colTile2.GetComponent <GameTileBase>().GetGameTileType(); // Compare types if (currentTileType == colTile1Type && currentTileType == colTile2Type) { GameObject[] tiles = { currentTile, colTile1, colTile2 }; MatchCharTile(tiles); MatchGliderTile(tiles); AddToList(tiles); } } } } } } } } isChecking = false; } //yield return null; }