-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardManager.cs
More file actions
136 lines (109 loc) · 3.96 KB
/
BoardManager.cs
File metadata and controls
136 lines (109 loc) · 3.96 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using UnityEngine;
using HolagoGames;
using UnityEngine.UI;
public class BoardManager : MonoBehaviour
{
[SerializeField] private CellController cellPrefab;
[SerializeField] private InputHandler inputHandler;
[SerializeField] private GameManager gameManager;
private TileGenerator tileGenerator;
public BoardData boardData;
private float cellSpacing = 30f;
private bool tileGenerated;
public CellController[,] Grid { get; private set; }
private void OnEnable()
{
Holago.SystemContainer.EventSystem.OnMovesCompleted.Register(OnMovesCompleted);
}
private void OnDisable()
{
Holago.SystemContainer.EventSystem.OnMovesCompleted.UnRegister(OnMovesCompleted);
}
private void Awake()
{
tileGenerator = GetComponent<TileGenerator>();
}
private void Start()
{
GenerateGrid();
for (int i = 0; i < 2; i++)
{
tileGenerator.GenerateTile(Grid);
}
}
private void OnMovesCompleted()
{
gameManager.IsGameOver();
inputHandler.IsInputEnabled = true;
if (tileGenerated) return;
tileGenerator.GenerateTile(Grid);
tileGenerated = true;
}
private void GenerateGrid()
{
Vector2 cellSize = cellPrefab.GetComponent<RectTransform>().sizeDelta;
float centerX = (boardData.columns - 1) / 2f;
float centerY = (boardData.rows - 1) / 2f;
Grid = new CellController[boardData.rows, boardData.columns];
for (int i = 0; i < boardData.rows; i++)
{
for (int j = 0; j < boardData.columns; j++)
{
Vector3 cellPosition = new Vector2((j - centerX) * (cellSize.x + cellSpacing),
(centerY - i) * (cellSize.y + cellSpacing));
CellController cell = Instantiate(cellPrefab, transform);
cell.transform.localPosition = cellPosition;
cell.Initialize();
Grid[i, j] = cell;
Grid[i, j].IsActive = boardData.cellData[i * boardData.rows + j];
SetCellVisibility(cell);
}
}
}
private void SetCellVisibility(CellController cell)
{
if (cell.IsActive)
{
cell.gameObject.GetComponent<Image>().enabled = true;
}
else
{
cell.gameObject.GetComponent<Image>().enabled = false;
}
}
public void MoveTiles(bool isHorizontal)
{
tileGenerated = false;
MoveInDirection(isHorizontal);
}
private void MoveInDirection(bool isHorizontal)
{
int line = isHorizontal ? boardData.columns : boardData.rows;
int start = isHorizontal ? inputHandler.startColumn : inputHandler.startRow;
int step = isHorizontal ? inputHandler.stepX : inputHandler.stepY;
for (int i = 0; i < line; i++)
{
for (int j = start; j >= 0 && j < line; j -= step)
{
CellController currentCell = isHorizontal ? Grid[i, j] : Grid[j, i];
while (true)
{
if (!currentCell.IsOccupied) break;
int nextGrid = j + step;
if (nextGrid < 0 || nextGrid >= line) break;
CellController nextCell = isHorizontal ? Grid[i, nextGrid] : Grid[nextGrid, i];
if (nextCell.IsOccupied && currentCell.IsOccupied &&
nextCell.tileArray[0].GetTileValue() != currentCell.tileArray[0].GetTileValue())
break;
TileController movingTile = currentCell.tileArray[0];
if (nextCell.IsFull || !nextCell.IsActive) break;
nextCell.PopulateCell(movingTile);
movingTile.MoveTile(nextCell.transform.position);
currentCell.ClearCell();
j = nextGrid;
currentCell = nextCell;
}
}
}
}
}