Bubble Shooter Game in Unity: Part 11

So what if we wanted to pick the code from the last tutorial in this series and make the grid scroll endlessly down the screen? I’d need to add new rows of balls, and in order to do that I’d have to flip the grid upside down (mainly because since the beginning of this series I’ve been using a grid that starts at the bottom of the screen.)

Here’s the new Grid class:

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class Grid : MonoBehaviour {
 
	public int ROWS = 20;
 
	public int COLUMNS = 14;
 
	public float TILE_SIZE = 0.68f;
 
	public float GRID_SPEED = 0.1f;
 
	public float changeTypeRate = 0.5f;
 
	public int emptyLines = 16;
 
	public GameObject gridBallGO;
 
	[HideInInspector]
	public float GRID_OFFSET_X = 0;
 
 
	[HideInInspector]
	public List<List<Ball>> gridBalls;
 
	private List<Ball> matchList;
 
	private List<Ball.BALL_TYPE> typePool;
 
	private Ball.BALL_TYPE lastType;
 
	private int bullets = 0;
 
	void Start () {
 
		matchList = new List<Ball> ();
		lastType = (Ball.BALL_TYPE)Random.Range (0, 5);
		typePool = new List<Ball.BALL_TYPE> ();
 
		var i = 0;
		var total = 100000;
		while (i < total) {
			typePool.Add (GetBallType ());
			i++;
		}
 
		Shuffle(typePool);
 
		BuildGrid ();
 
	}
 
 
	void BuildGrid ()
	{
		gridBalls = new List<List<Ball>> ();
 
 
		GRID_OFFSET_X = (COLUMNS * TILE_SIZE) * 0.5f;
		GRID_OFFSET_X -= TILE_SIZE * 0.5f;
 
 
		for (int row = 0; row < ROWS; row++) {
 
			var rowBalls = new List<Ball>();
 
			for (int column = 0; column < COLUMNS; column++) {
 
				var item = Instantiate (gridBallGO) as GameObject;
				var ball = item.GetComponent<Ball>();
 
				ball.SetBallPosition(this, column, row);
				ball.SetType (typePool [0]);
				typePool.RemoveAt (0);
 
				ball.transform.parent = gameObject.transform;
				rowBalls.Add (ball);
 
				if (gridBalls.Count < emptyLines ) {
					ball.gameObject.SetActive (false);
				}
			}
 
			gridBalls.Add(rowBalls);
		}
 
		var p = transform.position;
		p.y -= 4.7f;
		transform.position = p;
 
	}
 
 
	void AddLine () {
 
 
		ROWS++;
 
		var rowBalls = new List<Ball>();
 
		for (int column = 0; column < COLUMNS; column++) {
 
			var item = Instantiate (gridBallGO) as GameObject;
 
			var ball = item.GetComponent<Ball>();
			ball.transform.parent = gameObject.transform;
			ball.SetBallPosition(this, column, gridBalls.Count-1);
			ball.SetType (typePool [0]);
			ball.connected = true;
 
			typePool.RemoveAt (0);
 
			rowBalls.Add (ball);
		}
		gridBalls.Add(rowBalls);
 
	}
 
 
	public void AddBall (Ball collisionBall, Bullet bullet) {
 
		var neighbors = BallEmptyNeighbors(collisionBall);
		var minDistance = 10000.0f;
		Ball minBall = null;
		foreach (var n in neighbors) {
			var d = Vector2.Distance (n.transform.position, bullet.transform.position);
			if ( d < minDistance ) {
				minDistance = d;
				minBall = n;
			}
		}
		bullet.gameObject.SetActive (false);
		minBall.SetType (bullet.type);
		minBall.gameObject.SetActive (true);
 
		CheckMatchesForBall (minBall);
 
	}
 
 
	public void CheckMatchesForBall (Ball ball) {
 
		matchList.Clear ();
 
		foreach (var r in gridBalls) {
			foreach (var b in r) {
				b.visited = false;
			}
		}
 
 
		//search for matches around ball
		var initialResult = GetMatches( ball );
		matchList.AddRange (initialResult);
 
		while (true) {
 
			var allVisited = true;
			for (var i = matchList.Count - 1; i >= 0 ; i--) {
				var b = matchList [i];
				if (!b.visited) {
					AddMatches (GetMatches (b));
					allVisited = false;
				}
			}
 
			if (allVisited) {
 
				if (matchList.Count > 2) {
 
					foreach (var b in matchList) {
						b.gameObject.SetActive (false);
					}
 
					CheckForDisconnected ();
 
					//remove disconnected balls
					var i = gridBalls.Count - 1;
					while (i >= 0) {
						foreach (var b in gridBalls[i]) {
							if (!b.connected) {
								b.gameObject.SetActive (false);
							}
						}
						i--;
					}
				}
				return;
			}
		}
	}
 
 
	void CheckForDisconnected () {
		//set all balls as disconnected
		foreach (var r in gridBalls) {
			foreach (var b in r) {
				b.connected = false;
			}
		}
		//connect visible balls in last row 
		foreach (var b in gridBalls[ROWS-1]) {
			if (b.gameObject.activeSelf)
				b.connected = true;
		}
 
 
		//now set connect property on the rest of the balls
		var i = ROWS-1;
		while (i >= 0) {
			foreach (var b in gridBalls[i]) {
				if (b.gameObject.activeSelf) {
					var neighbors = BallActiveNeighbors (b);
					var connected = false;
 
					foreach (var n in neighbors) {
						if (n.connected) {
							connected = true;
							break;
						}
					}
 
					if (connected) {
						b.connected = true;
						foreach (var n in neighbors) {
							if (n.gameObject.activeSelf) {
								n.connected = true;
							}
						}
					} 
				}
			}
			i--;
		}
	}
 
 
	List<Ball> GetMatches (Ball ball) {
		ball.visited = true;
		var result = new List<Ball> () { ball };
		var n = BallActiveNeighbors (ball);
 
		foreach (var b in n) {
			if (b.type == ball.type) {
				result.Add (b);
			}
		}
 
		return result;
	}
 
	void AddMatches (List<Ball> matches) {
		foreach (var b in matches) {
			if (!matchList.Contains (b))
				matchList.Add (b);
		}
	}
 
 
 
	Ball.BALL_TYPE GetBallType () {
		var random = Random.Range (0.0f, 1.0f);
		if (random > changeTypeRate) {
			lastType = (Ball.BALL_TYPE)Random.Range (0, 5);
		}
		return lastType;
	}
 
 
 
	List<Ball> BallEmptyNeighbors (Ball ball) {
		var result = new List<Ball> ();
		if (ball.column + 1 < COLUMNS) {
			if (!gridBalls [ball.row] [ball.column + 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column + 1]);
		}
 
		//left
		if (ball.column - 1 >= 0) {
			if (!gridBalls [ball.row] [ball.column - 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column - 1]);
		}
		//top
		if (ball.row - 1 >= 0) {
			if (!gridBalls [ball.row - 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row - 1] [ball.column]);
		}
 
		//bottom
		if (ball.row + 1 < gridBalls.Count) {
			if (!gridBalls [ball.row + 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row + 1] [ball.column]);
		}
 
		if (ball.column % 2 == 0) {
			//bottom-left
			if (ball.row + 1 < gridBalls.Count && ball.column - 1 >= 0) {
				if (!gridBalls [ball.row + 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column - 1]);
			}
 
			//bottom-right
			if (ball.row + 1 < gridBalls.Count && ball.column + 1 < COLUMNS) {
				if (!gridBalls [ball.row + 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column + 1]);
			}
		} else {
			//top-left
			if (ball.row - 1 >= 0 && ball.column - 1 >= 0) {
				if (!gridBalls [ball.row - 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column - 1]);
			}
 
			//top-right
			if (ball.row - 1 >= 0 && ball.column + 1 < COLUMNS) {
				if (!gridBalls [ball.row - 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column + 1]);
			}
		}
 
 
		return result;
	}
 
	List<Ball> BallActiveNeighbors (Ball ball) {
 
		var result = new List<Ball> ();
		//right
		if (ball.column + 1 < COLUMNS) {
			if (gridBalls [ball.row] [ball.column + 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column + 1]);
		}
 
		//left
		if (ball.column - 1 >= 0) {
			if (gridBalls [ball.row] [ball.column - 1].gameObject.activeSelf)
				result.Add (gridBalls [ball.row] [ball.column - 1]);
		}
		//bottom
		if (ball.row - 1 >= 0) {
			if (gridBalls [ball.row - 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row - 1] [ball.column]);
		}
 
		//top
		if (ball.row + 1 < gridBalls.Count) {
			if (gridBalls [ball.row + 1] [ball.column].gameObject.activeSelf)
				result.Add (gridBalls [ball.row + 1] [ball.column]);
		}
 
 
		if (ball.column % 2 == 0) {
 
			//top-left
			if (ball.row - 1 >= 0 && ball.column - 1 >= 0) {
				if (gridBalls [ball.row - 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column - 1]);
			}
 
			//top-right
			if (ball.row - 1 >= 0 && ball.column + 1 < COLUMNS) {
				if (gridBalls [ball.row - 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row - 1] [ball.column + 1]);
			}
		} else {
			//bottom-left
			if (ball.row + 1 < gridBalls.Count && ball.column - 1 >= 0) {
				if (gridBalls [ball.row + 1] [ball.column - 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column - 1]);
			}
 
			//bottom-right
			if (ball.row + 1 < gridBalls.Count && ball.column + 1 < COLUMNS) {
				if (gridBalls [ball.row + 1] [ball.column + 1].gameObject.activeSelf)
					result.Add (gridBalls [ball.row + 1] [ball.column + 1]);
			}
 
		}
 
		return result;
	}
 
	public Ball BallCloseToPoint (Vector2 point)
	{
 
		point.y -= transform.position.y;
 
		int c = Mathf.FloorToInt ((point.x + GRID_OFFSET_X + ( TILE_SIZE * 0.5f )) / TILE_SIZE);
		if (c < 0)
			c = 0;
		if (c >= COLUMNS)
			c = COLUMNS - 1;
 
		int r =  Mathf.FloorToInt (( ( TILE_SIZE * 0.5f ) + point.y )/  TILE_SIZE);
		if (r < 0) r = 0;
		if (r >= gridBalls.Count) r = gridBalls.Count - 1;
 
 
 
		return gridBalls [r] [c];
 
	}
 
 
	void Update () {
 
		var p = transform.position;
		p.y -= Time.deltaTime * GRID_SPEED;
		transform.position = p;
 
 
		if (gridBalls [gridBalls.Count - 1] [0].transform.position.y < 6 ) {
 
			//add new line
			AddLine();
		}
 
	}
 
	private static System.Random rng = new System.Random(); 
	public static void Shuffle<T>(IList<T> list)  {  
		int n = list.Count;  
		while (n > 1) {  
			n--;  
			int k = rng.Next(n + 1);  
			T value = list[k];  
			list[k] = list[n];  
			list[n] = value;  
		}  
	}
}

The main changes are related to flipping the Grid, when building it, adding rows, and when looking for matches.

The new Update method will check the position of the last row and add a new row if necessary:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Update () {
 
	var p = transform.position;
	p.y -= Time.deltaTime * GRID_SPEED;
	transform.position = p;
 
 
	if (gridBalls [gridBalls.Count - 1] [0].transform.position.y < 6 ) {
 
		//add new line
		AddLine();
	}
 
}

And here is the AddLine method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void AddLine () {
 
	ROWS++;
 
	var rowBalls = new List<Ball>();
 
	for (int column = 0; column < COLUMNS; column++) {
 
		var item = Instantiate (gridBallGO) as GameObject;
 
		var ball = item.GetComponent<Ball>();
		ball.transform.parent = gameObject.transform;
		ball.SetBallPosition(this, column, gridBalls.Count-1);
		ball.SetType (typePool [0]);
		ball.connected = true;
 
		typePool.RemoveAt (0);
 
		rowBalls.Add (ball);
	}
	gridBalls.Add(rowBalls);
 
}

In a real game it might be best to pick the balls from a pool instead of instantiating them every time…

The SetBallPosition method in the Ball class also changes, to get rid of the Y offset:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void SetBallPosition (Grid grid, int column, int row) {
 
	this.grid = grid;
	this.column = column;
	this.row = row;
 
	ballPosition = new Vector3 ( 
		(column * grid.TILE_SIZE) - grid.GRID_OFFSET_X , 
		 (row * grid.TILE_SIZE),
 
		0);
 
	if (column % 2 == 0) {
		ballPosition.y -= grid.TILE_SIZE * 0.5f;
	}
 
	transform.localPosition = ballPosition;
 
	foreach (var go in colorsGO) {
		go.SetActive(false);
	}
}

Here is the project with the endless scrolling grid.