public List <Vector3> AddNewColumn(int x, int y = 0) { GameObject newcolumn = new GameObject("C" + x); newcolumn.transform.parent = this.transform; TileColumn tc = newcolumn.AddComponent <TileColumn>(); tc.Init(new Vector2(x, y)); if (TileColumns.Count == 0 || x < TileColumns.First().ColumnPosition) { TileColumns.Insert(0, tc); MostNegativeColumn = x; } else { TileColumns.Add(tc); } var r = new List <Vector3>(); for (int i = -10; i < 10; i++) { r.Add(new Vector2(x, y + i)); } return(r); }
private void Start() { //int gridSize = Constants.gridSizeHorizontal * Constants.gridSizeVertical; columns.Clear(); for (int x = 0; x < Constants.gridSizeHorizontal; x++) { GameObject newColumn = Instantiate(Resources.Load("Column")) as GameObject; TileColumn column = newColumn.GetComponent <TileColumn>(); column.Init(x); newColumn.transform.SetParent(this.transform, false); newColumn.name = "Column " + x; columns.Add(newColumn.GetComponent <TileColumn>()); for (int y = 0; y < Constants.gridSizeVertical; y++) { GameObject newTile = Instantiate(Resources.Load("Tile")) as GameObject; newTile.transform.SetParent(newColumn.transform, false); BaseTile tile = newTile.GetComponent <BaseTile>(); //int hori = i % Constants.gridSizeHorizontal; //Row //int vert = Mathf.FloorToInt(i / Constants.gridSizeHorizontal); tile.xy = new Vector2(x, y); newTile.name = "Tile (" + x + "," + y + ")"; //F.e. Tile (0,7) tile.InitRandom(); column.tiles.Add(tile); } } }
private void Refill(TileColumn column, string direction) { int tilesNeeded = Constants.gridSizeVertical - column.tiles.Count; for (int i = 0; i < tilesNeeded; i++) { GameObject newTile = Instantiate(Resources.Load("Tile")) as GameObject; newTile.transform.SetParent(column.transform, false); if (direction == "top") { newTile.transform.SetAsFirstSibling(); } BaseTile tile = newTile.GetComponent <BaseTile>(); tile.InitRandom(); if (direction == "top") { column.tiles.Insert(0, tile); } else { column.tiles.Add(tile); } } foreach (BaseTile tile in column.tiles) { tile.transform.name = "Tile (" + column.number + ", " + column.tiles.IndexOf(tile) + ")"; //F.e. Tile (0,7) tile.xy = new Vector2(column.number, column.tiles.IndexOf(tile)); } }
void ResizeGrid() { size = new Vector2(Mathf.Floor(size.x), Mathf.Floor(size.y)); // Add or remove columns. for (int row = 0; row < grid.Count; row++) { if (grid[row].Count > size.y) { for (int col = grid[row].Count - 1; col >= size.y; col--) { grid[row][col].Destroy(); grid[row].RemoveAt(col); } } else if (grid[row].Count < size.y) { for (int col = grid[row].Count; col < size.y; col++) { grid[row].Add(GenerateTile(row, col)); } } } // Add or remove rows. if (grid.Count > size.x) { for (int row = grid.Count - 1; row >= size.x; row--) { for (int col = 0; col < grid[row].Count; col++) { grid[row][col].Destroy(); } grid.RemoveAt(row); } } else if (grid.Count < size.x) { for (int row = grid.Count; row < size.x; row++) { TileColumn newColumn = new TileColumn(); for (int col = 0; col < size.y; col++) { newColumn.Add(GenerateTile(row, col)); } grid.Add(newColumn); } } for (int row = 0; row < grid.Count; row++) { for (int col = 0; col < grid[row].Count; col++) { grid[row][col].transform.SetSiblingIndex(row * (int)size.y + col); } } }
private void AddRow() { for (int i = 0; i < m_Target.Tiles.Length; i++) { TileColumn temp = new TileColumn(m_Target.Tiles[i].Length + 1); for (int j = 0; j < m_Target.Tiles[i].Length; j++) { temp[j] = m_Target.Tiles[i][j]; } m_Target.Tiles[i] = temp; } m_Height++; EditorUtility.SetDirty(target); }
private void AddColumn() { TileColumn[] temp = new TileColumn[m_Width + 1]; for (int i = 0; i < m_Width; i++) { temp[i] = m_Target.Tiles[i]; } temp[m_Width] = new TileColumn(m_Height); m_Target.Tiles = temp; m_Width++; EditorUtility.SetDirty(target); }
private void RemoveColumn() { if (m_Width <= 1) { return; } TileColumn[] temp = new TileColumn[m_Target.GetWidth() - 1]; for (int i = 0; i < m_Target.GetWidth() - 1; i++) { temp[i] = m_Target.Tiles[i]; } m_Target.Tiles = temp; m_Width--; EditorUtility.SetDirty(target); }
private void ConvertTileToBooster(Vector2 position, TileTypes.ESubState requestedType, string path) { TileColumn column = columns[(int)position.x]; BaseTile targetTile = column.tiles.Find(item => item.x == position.x && item.y == position.y); if (targetTile) { int index = column.tiles.IndexOf(targetTile); GameObject newTile = Instantiate(Resources.Load(path)) as GameObject; newTile.transform.SetParent(column.transform, false); column.tiles.RemoveAt(index); Destroy(targetTile.gameObject); column.tiles.Insert(index, newTile.GetComponent <BaseTile>()); newTile.GetComponent <BaseTile>().type.Type = requestedType; newTile.transform.SetSiblingIndex(index); newTile.GetComponent <BaseTile>().xy = new Vector2(column.number, index); newTile.transform.name = "Tile (" + column.number + ", " + index + ")"; //F.e. Tile (0,7) GameObject boosterIcon = Instantiate(Resources.Load(path + "Icon")) as GameObject; boosterIcon.transform.SetParent(newTile.transform, false); } }
private void RemoveRow() { if (m_Height <= 1) { return; } for (int i = 0; i < m_Width; i++) { TileColumn temp = new TileColumn(m_Height - 1); for (int j = 0; j < m_Height - 1; j++) { temp[j] = m_Target.Tiles[i][j]; } m_Target.Tiles[i] = temp; } m_Height--; EditorUtility.SetDirty(target); }
// Update is called once per frame void FixedUpdate() { int rightMostColumn = (int)(player.transform.position.x + 10); int leftMostColumn = (int)(player.transform.position.x - 10); List <Vector3> NewTiles = new List <Vector3>(); //columns to the right if (rightMostColumn - MostNegativeColumn > TileColumns.Count - 1) { //column has never been spawned int currentMax = TileColumns.Last().ColumnPosition; for (int i = currentMax + 1; i <= rightMostColumn; i++) { NewTiles.AddRange(AddNewColumn(i, (int)player.transform.position.y)); if (i - MostNegativeColumn - 21 >= 0) { TileColumn removed = TileColumns [i - MostNegativeColumn - 21]; if (removed.Spawned) { removed.DespawnColumn(); } } } } else if (!TileColumns [rightMostColumn - MostNegativeColumn].Spawned) { //need to respawn the column TileColumns[rightMostColumn - MostNegativeColumn].SpawnColumn((int)player.transform.position.y); TileColumn removed = TileColumns[rightMostColumn - MostNegativeColumn - 21]; if (removed.Spawned) { removed.DespawnColumn(); } } //columns to the left if (leftMostColumn - MostNegativeColumn < 0) { //column has never been spawned int currentMin = TileColumns.First().ColumnPosition; for (int i = currentMin - 1; i >= leftMostColumn; i--) { NewTiles.AddRange(AddNewColumn(i, (int)player.transform.position.y)); TileColumn removed = TileColumns[i - MostNegativeColumn + 21]; if (removed.Spawned) { removed.DespawnColumn(); } } } else if (!TileColumns[leftMostColumn - MostNegativeColumn].Spawned) { //need to respawn the column TileColumns[leftMostColumn - MostNegativeColumn].SpawnColumn((int)player.transform.position.y); TileColumn removed = TileColumns[leftMostColumn - MostNegativeColumn + 21]; if (removed.Spawned) { removed.DespawnColumn(); } } foreach (TileColumn c in TileColumns.Where(tc => tc.Spawned)) { NewTiles.AddRange( c.ChangeSpawnedTiles((int)player.transform.position.y) ); } if (NewTiles.Count > 0) { EventHandler <TileSpawnedEventArgs> handler = NewTilesSpawned; if (handler != null) { handler(this, new TileSpawnedEventArgs(NewTiles)); } } }