-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrickManager.cs
159 lines (127 loc) · 4.21 KB
/
BrickManager.cs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class BrickManager : MonoBehaviour
{
#region Singleton
private static BrickManager _instance;
public static BrickManager Instance => _instance;
public static event Action OnLevelLoaded;
private void Awake()
{
if (_instance != null)
{
Destroy(gameObject);
}
else
{
_instance = this;
}
}
#endregion
private int maxRows = 8;
private int maxCols = 19;
private GameObject bricksContainer;
private float initialBricksSpawnPositionX = -8.15f;
private float initialBricksSpawnPositionY = 4.25f;
private float shiftAmountX = 0.68f;
private float shiftAmountY = 0.68f;
public Brick brickPrefab;
public Sprite[] Sprites;
public Color[] BrickColors;
public List<Brick> RemainingBricks { get; set; }
public List<int[,]> LevelsData { get; set; }
public int initialBricksCount { get; set; }
public int CurrentLevel;
private void Start()
{
this.bricksContainer = new GameObject("BricksContainer");
this.LevelsData = this.LoadLevelsData();
this.GenerateBricks();
}
private void GenerateBricks()
{
this.RemainingBricks = new List<Brick>();
int[,] currentLevelData = this.LevelsData[this.CurrentLevel];
float currentSpawnX = initialBricksSpawnPositionX;
float currentSpawnY = initialBricksSpawnPositionY;
float zShift = 0;
for (int row = 0; row < this.maxRows; row++)
{
for (int col = 0; col < this.maxCols; col++)
{
int brickType = currentLevelData[row, col];
if (brickType > 0)
{
Brick newBrick = Instantiate(brickPrefab, new Vector3(currentSpawnX, currentSpawnY, 0.0f - zShift), Quaternion.identity) as Brick;
newBrick.Init(bricksContainer.transform, this.Sprites[brickType - 1], this.BrickColors[brickType], brickType);
this.RemainingBricks.Add(newBrick);
zShift += 0.0001f;
}
currentSpawnX += shiftAmountX;
if (col + 1 == this.maxCols)
{
currentSpawnX = initialBricksSpawnPositionX;
}
}
currentSpawnY -= shiftAmountY;
}
this.initialBricksCount = this.RemainingBricks.Count;
OnLevelLoaded?.Invoke();
}
private List<int[,]> LoadLevelsData()
{
TextAsset text = Resources.Load("levels") as TextAsset;
string[] rows = text.text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
List<int[,]> levelsData = new List<int[,]>();
int[,] currentLevel = new int[maxRows, maxCols];
int currentRow = 0;
for (int row = 0; row < rows.Length; row++)
{
string line = rows[row];
if (line.IndexOf("--") == -1)
{
string[] bricks = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int col = 0; col < bricks.Length; col++)
{
currentLevel[currentRow, col] = int.Parse(bricks[col]);
}
currentRow++;
}
else
{
// end of current level
currentRow = 0;
levelsData.Add(currentLevel);
currentLevel = new int[maxRows, maxCols];
}
}
return levelsData;
}
public void LoadLevel(int level)
{
this.CurrentLevel = level;
this.ClearRemainingBricks();
this.GenerateBricks();
}
private void ClearRemainingBricks()
{
foreach (var brick in this.RemainingBricks.ToList())
{
Destroy(brick.gameObject);
}
}
public void LoadNextLevel()
{
this.CurrentLevel++;
if (this.CurrentLevel >= this.LevelsData.Count)
{
GameManager.Instance.ShowVictoryScreen();
}
else
{
this.LoadLevel(this.CurrentLevel);
}
}
}