int SurroundingType(int type, Globals.Coord pos) { int ret = 0; for (int xx = pos.x - 1; xx < pos.x + 2; xx++) { for (int yy = pos.y - 1; yy < pos.y + 2; yy++) { if (xx == pos.x || yy == pos.y) { if (IsInGrid(xx, yy)) { if (tiles [xx, yy] != null) { if (tiles [xx, yy].type == type) { ret++; } } } } } } return(ret); }
void SpawnExtraTiles() { if (HasFreeTiles()) { List <Globals.Coord> free = GetEmptySpaces(); int spawnCount = Random.Range(minSpawn, maxSpawn); for (int xx = 0; xx < spawnCount;) { int picked = Random.Range(0, free.Count); int type = Random.Range(0, GameManager.Instance.colors.Length); int surroundCount = SurroundingType(type, free [picked]); Globals.Coord pos = free [picked]; if (surroundCount < 2) { free.RemoveAt(picked); StartCoroutine(CreateTile(pos.x, pos.y, type)); if (free.Count <= 0) { Debug.Log("No free spaces found. Game over."); break; } xx++; } } } else { Debug.Log("No free spaces found. Game over."); } ResetGrid(); }
public bool IsInGrid(Globals.Coord pos) { bool ret = false; if (pos.x >= 0 && pos.x < xSize) { if (pos.y >= 0 && pos.y < ySize) { ret = true; } } return(ret); }
public void Shuffle() { if (currState == Globals.State.Waiting) { //Get list of positions available. List <Globals.Coord> positions = new List <Globals.Coord> (); for (int xx = 0; xx < xSize; xx++) { for (int yy = 0; yy < ySize; yy++) { if (grid [xx, yy] == true) { Globals.Coord pos = new Globals.Coord(xx, yy); positions.Add(pos); } } } //Clear all tiles from the grid. tiles = new Tile[xSize, ySize]; for (int xx = 0; xx < allTiles.Count;) { int pickedPos = Random.Range(0, positions.Count); Globals.Coord pos = positions [pickedPos]; int surroundCount = SurroundingType(allTiles[xx].type, positions [pickedPos]); if (surroundCount < 2) { if (tiles [positions [pickedPos].x, positions [pickedPos].y] == null) { positions.RemoveAt(pickedPos); Vector2 targetPos = PosToVector2(pos.x, pos.y); tiles [allTiles[xx].pos.x, allTiles[xx].pos.y] = null; allTiles[xx].pos.x = pos.x; allTiles[xx].pos.y = pos.y; tiles [pos.x, pos.y] = allTiles[xx]; StartCoroutine(MoveTile(allTiles [xx], targetPos, Random.Range(0.1f, 0.5f))); xx++; } } } } ResetGrid(); }
List <Globals.Coord> GetEmptySpaces() { List <Globals.Coord> empties = new List <Globals.Coord> (); for (int xx = 0; xx < xSize; xx++) { for (int yy = 0; yy < ySize; yy++) { if (grid [xx, yy]) { if (tiles [xx, yy] == null) { Globals.Coord newCoord = new Globals.Coord(xx, yy); empties.Add(newCoord); } } } } return(empties); }
public static Globals.Coord AddDirection(Coord orig, Direction dir) { Globals.Coord ret = orig; if (dir == Direction.Left) { ret.x -= 1; } else if (dir == Direction.Right) { ret.x += 1; } else if (dir == Direction.Up) { ret.y += 1; } else if (dir == Direction.Down) { ret.y -= 1; } else if (dir == Direction.UpLeft) { ret.x -= 1; ret.y += 1; } else if (dir == Direction.UpRight) { ret.x += 1; ret.y += 1; } else if (dir == Direction.DownLeft) { ret.x -= 1; ret.y -= 1; } else if (dir == Direction.DownRight) { ret.x += 1; ret.y -= 1; } return(ret); }