private void CreateObstacleRow() { TileStates[] RowLayout = new TileStates[TilesX]; RowObstacleData Data = new RowObstacleData(); Data.HasObstacles = true; RowObstacleData PrevData = Rows[0]; int ObstacleCount = 0; int ObstacleTarget = Random.Range(MinObstacleCountH, MaxObstacleCountH); //at least one gap has to line up with the row below this one List <int> GapIndices = new List <int>(); for (int i = 0; i < TilesX; i++) { if (PrevData.HasObstacles == false || PrevData.States[i] == TileStates.PathGap) { GapIndices.Add(i); } } int StartIndex = GapIndices[Random.Range(0, GapIndices.Count)]; int EndIndex = StartIndex; RowLayout[StartIndex] = TileStates.PathGap; //the gap should be forced to the min gap width, randomly add gap tiles left and right int GapWidth = 1; while (GapWidth < MinObstacleGapH) { int Dir = Random.Range(0, 2); if (Dir == 0) { if (StartIndex == 0) { continue; } StartIndex--; RowLayout[StartIndex] = TileStates.PathGap; GapWidth++; } else { if (EndIndex == TilesX - 1) { continue; } EndIndex++; RowLayout[EndIndex] = TileStates.PathGap; GapWidth++; } } for (int i = 0; i < TilesX; i++) { if (RowLayout[i] == TileStates.Unset) { RowLayout[i] = TileStates.Gap; } } //add obstacles at random until the obstacle count is met while (ObstacleCount < ObstacleTarget) { int a = Random.Range(0, TilesX); if (RowLayout[a] == TileStates.Gap) { RowLayout[a] = TileStates.Block; ObstacleCount++; } } for (int x = 0; x < TilesX; x++) { Data.States[x] = RowLayout[x]; if (RowLayout[x] != TileStates.Block) { continue; } GameObject GridTile; SpriteRenderer Renderer; if (RemovedFGTiles.Count > 0) { GridTile = RemovedFGTiles.Dequeue(); Renderer = GridTile.GetComponent <SpriteRenderer>(); } else { GridTile = new GameObject("FG Tile" + FGTileIndex++); Renderer = GridTile.AddComponent <SpriteRenderer>(); } Renderer.sprite = Resources.Load <Sprite>("Wall"); GridTile.transform.position = new Vector3(FirstTileX + x, TopLine, -1); //if (RowLayout[x] == TileStates.PathGap) Renderer.color = new Color(0, 1, 0, 0.5f); //else Renderer.color = new Color(1, 1, 1, 1); FGTiles.Add(GridTile); } Rows.Insert(0, Data); if (Rows.Count > 5) { Rows.RemoveAt(Rows.Count - 1); } }
void AddTileRow() { if (State == GameStates.ThumbActive || State == GameStates.ThumbLifted) { Score++; } bool EvenTile = EvenRow; EvenRow = !EvenRow; //add the background tiles for (int x = 0; x < TilesX; x++) { GameObject GridTile; SpriteRenderer Renderer; if (RemovedBGTiles.Count > 0) { GridTile = RemovedBGTiles.Dequeue(); Renderer = GridTile.GetComponent <SpriteRenderer>(); } else { GridTile = new GameObject("BG Tile" + BGTileIndex++); Renderer = GridTile.AddComponent <SpriteRenderer>(); } string TileName; if (EvenTile) { TileName = "WhiteTile"; } else { TileName = "BlackTile"; } if (Random.Range(1, 100) < 10) { TileName = "Cracked" + TileName; if (Random.Range(1, 100) < 50) { TileName += "1"; } else { TileName += "2"; } } else { TileName = "Clean" + TileName; } Renderer.sprite = Resources.Load <Sprite>("Tiles/" + TileName); GridTile.transform.position = new Vector3(FirstTileX + x, TopLine, 0); EvenTile = !EvenTile; BGTiles.Add(GridTile); } if (RowCounter > 6) { if (RowCounter % 60 == 0) { IncreaseDifficulty(); } if (CreatingObstacle) { CreateObstacleRow(); LinesToGo--; if (LinesToGo == 0) { CreatingObstacle = false; LinesToGo = Random.Range(MinObstacleGapV, MaxObstacleGapV + 1); print("" + LinesToGo); } } else { RowObstacleData Data = new RowObstacleData(); Rows.Insert(0, Data); if (Rows.Count > 5) { Rows.RemoveAt(Rows.Count - 1); } LinesToGo--; if (LinesToGo == 0) { CreatingObstacle = true; LinesToGo = Random.Range(MinObstacleSizeV, MaxObstacleSizeV + 1); } } } DemoLinesCleared++; RowCounter++; TopLine++; if (State == GameStates.ThumbActive || State == GameStates.ThumbLifted) { CurrentScrollSpeed += ScrollSpeedIncrease; if (CurrentScrollSpeed > MaxScrollSpeed) { CurrentScrollSpeed = MaxScrollSpeed; } } }