-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileGenerator.cs
More file actions
46 lines (37 loc) · 1.18 KB
/
TileGenerator.cs
File metadata and controls
46 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections.Generic;
using UnityEngine;
public class TileGenerator : MonoBehaviour
{
[SerializeField] TileFactory tileFactory;
public void GenerateTile(CellController[,] grid)
{
CellController cell = GetRandomCell(grid);
TileController tile = tileFactory.GenerateDefaultTile(cell.transform.position);
int number = GenerateRandomNumber();
tile.SetTile(number);
cell.PopulateCell(tile);
}
private CellController GetRandomCell(CellController[,] grid)
{
List<CellController> emptyCells = GetEmptyCells(grid);
int randomIndex = Random.Range(0, emptyCells.Count);
return emptyCells[randomIndex];
}
private List<CellController> GetEmptyCells(CellController[,] grid)
{
List<CellController> emptyCells = new List<CellController>();
foreach (CellController cell in grid)
{
if (!cell.IsOccupied && cell.IsActive)
{
emptyCells.Add(cell);
}
}
return emptyCells;
}
private int GenerateRandomNumber()
{
int randomNumber = Random.Range(0, 10) < 9 ? 2 : 4;
return randomNumber;
}
}