/
GameController.cs
174 lines (144 loc) · 4.69 KB
/
GameController.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
// Game State
enum State { Playing, GameOver }
State gameState;
// Variable for shapes holder
public GameObject[] shapes;
GameObject shapeToBeSpawned;
// Player feedback UI
public Text scoreText;
int score;
int consecutiveCount;
public GameObject gameOverUI;
// Variable for mapping
bool[,] map = new bool[10, 20];
int blockCountInOneLine;
// Variables for play area and game difficulty
int playAreaWidth = 10;
int playAreaHeight = 20;
int difficulty;
public Text hardModeText;
public Text modeText;
// Variables for next shape panel in the left
public Transform nextShapeHolder;
public GameObject[] nextShapes;
GameObject nextShape;
NextShape nextShapeToBeDestroyed;
private void Start()
{
difficulty = AudioController.instance.difficulty;
switch (difficulty)
{
case 0:
modeText.text = "EASY";
break;
case 1:
modeText.text = "MEDIUM";
break;
case 2:
modeText.text = "HARD";
break;
default:
break;
}
gameState = State.Playing;
string sceneName = SceneManager.GetActiveScene().name;
AudioController.instance.PlayTheme(sceneName);
shapeToBeSpawned = shapes[Random.Range(0, shapes.Length)];
SpawnNextBlock();
blockCountInOneLine = 0;
score = 0;
}
public void SpawnNextBlock()
{
if (gameState == State.Playing)
{
Instantiate(shapeToBeSpawned, shapeToBeSpawned.transform.position, Quaternion.identity);
int randomizer = Random.Range(0, shapes.Length);
shapeToBeSpawned = shapes[randomizer];
if (difficulty < 2)
InstantiateNextShape(randomizer);
else
hardModeText.gameObject.SetActive(true);
}
else
{
GameOver();
}
}
public void MapBlock(Vector3 position)
{
if ((int)position.y + 10 >= 10)
gameState = State.GameOver;
else
map[(int)position.x + 4, (int)position.y + playAreaHeight] = true;
}
public void CheckLines()
{
for (int i = 0; i < playAreaHeight; i++)
{
for (int j = 0; j < playAreaWidth; j++)
{
if (map[j, i])
blockCountInOneLine++;
}
if (blockCountInOneLine == playAreaWidth)
{
// Make mapping back to default.
for (int j = i; j < playAreaHeight; j++)
{
for (int k = 0; k < playAreaWidth; k++)
{
map[k, j] = false;
}
}
Block[] blocks = FindObjectsOfType<Block>();
foreach (Block block in blocks)
{
// Destroy one line of blocks
if ((int)(block.transform.localPosition.y) == (i - playAreaHeight))
Destroy(block.gameObject);
// Move downward remaining blocks above
if ((int)(block.transform.localPosition.y) > (i - playAreaHeight))
{
block.MoveBlock(Vector3.down);
MapBlock(block.transform.localPosition);
}
}
AddScore();
consecutiveCount++;
i--;
}
blockCountInOneLine = 0;
}
consecutiveCount = 0;
}
private void GameOver()
{
AudioController.instance.StopTheme();
AudioController.instance.PlaySFX("Game Over");
GameOverController gameOverController = gameOverUI.GetComponent<GameOverController>();
gameOverController.Initiate();
gameOverController.SetScoreText(score);
gameOverController.CheckHighScore();
}
private void AddScore()
{
score += 100 * (int)Mathf.Pow(3, consecutiveCount);
scoreText.text = score.ToString("D6");
}
private void InstantiateNextShape(int index)
{
nextShapeToBeDestroyed = FindObjectOfType<NextShape>();
if (nextShapeToBeDestroyed != null)
Destroy(nextShapeToBeDestroyed.gameObject);
nextShape = nextShapes[index];
Instantiate(nextShape, nextShapeHolder.transform.position, Quaternion.identity);
}
}