A guide to source control with SourceTree for just about everybody

This past week I got a new book published. It has nothing to do with programming (although in a way it has everything to do with it). Working with artists and game designers over the past I noticed how difficult it was for them to learn how to use a source control tool, and how much their work suffered by it. They had to constantly depend on someone else, usually a programmer, to commit their changes to a project’s repo, and were completely cut out from the rest of the team as far as the workflow went.

Even through there were many GUI tools available, for Git and Mercurial, there were not that many easy to follow tutorials out there.

I use Git for everything I do. Absolutely everything: code, art, writing… I can’t imagine myself working without it. It’s like having super powers; you can control time. When something goes wrong and large sections of a project need to be reworked… there’s simply no better tool to help you out through the process.

So this book is about that, it’s about Git and about SourceTree, and how all your digital work can benefit from using source control.

So this book is a careful, step-by-step guide to all that, for coders and non-coders.

If you know an artist out there who needs this thing, let him or her know.

The book is called Learning Source Control with Git and SourceTree


Unity 5.6.x Fixes for BubbleShooter

I’ve recently updated to Unity 5.6.x and I noticed that the bubble shooter game broke. I haven’t been able to find out what the issue with Unity is, but by changing the Edge colliders with Box colliders in the wall game objects I fixed the problem.
You can download a package with that update here.

I’m now working on different types of level editors you can build in Unity and I’ll start that series of posts soon. Till then.

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.

Bubble Shooter Game in Unity: Part 10

In order to move the grid down the screen we only need to make a few adjustments to the code. Before we had a variable controlling the number of visible rows at the beginning of the game; but with a scrolling grid you don’t need that. The grid should be entirely visible now, with a fixed gap of empty rows at the bottom of the grid which will add an initial distance between visible cells and the shooters (as well as the necessary empty rows which will need to add the player’s bullets which do not create an immediate match.)

All the changes are done to the 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
434
435
436
437
438
439
440
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.12f;
 
	public float changeTypeRate = 0.5f;
 
	public int emptyLines = 16;
 
	public GameObject gridBallGO;
 
	[HideInInspector]
	public float GRID_OFFSET_X = 0;
 
	[HideInInspector]
	public  float GRID_OFFSET_Y = 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 () {
 
		//If you want to add new lines every 10 shots, uncomment the line below
		//EventManager.OnShootBall += HandleShootBall;
 
		matchList = new List<Ball> ();
		lastType = (Ball.BALL_TYPE)Random.Range (0, 5);
		typePool = new List<Ball.BALL_TYPE> ();
 
		var i = 0;
		var total = 10000;
		while (i < total) {
			typePool.Add (GetBallType ());
			i++;
		}
 
		Shuffle(typePool);
 
		BuildGrid ();
 
//		gridBalls [0] [0].gameObject.SetActive (false);
	}
 
	void AddLine () {
		//does top line have visible bubbles
		var emptyFirstRow = true;
		foreach (var b in gridBalls[0]) {
			if (b.gameObject.activeSelf) {
				emptyFirstRow = false;
				break;
			}
		}
 
		if (!emptyFirstRow) {
			var r = ROWS - 2;
			while (r >= 0) {
				foreach (var b in gridBalls[r]) {
					if (b.gameObject.activeSelf) {
						gridBalls [r + 1] [b.column].gameObject.SetActive (true);
						gridBalls [r + 1] [b.column].SetType (b.type);
					} else {
						gridBalls [r + 1] [b.column].gameObject.SetActive (false);
					}
				}
				r--;
			}
		}
 
		foreach (var b in gridBalls[0]) {
			b.SetType (typePool [0]);
			typePool.RemoveAt (0);
			b.gameObject.SetActive (true);
		}
	}
 
	void BuildGrid ()
	{
		gridBalls = new List<List<Ball>> ();
 
 
		GRID_OFFSET_X = (COLUMNS * TILE_SIZE) * 0.5f;
		GRID_OFFSET_Y = (ROWS * TILE_SIZE);
 
		GRID_OFFSET_X -= TILE_SIZE * 0.5f;
		GRID_OFFSET_Y -= 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 > ROWS - emptyLines) {
					ball.gameObject.SetActive (false);
				}
			}
 
			gridBalls.Add(rowBalls);
		}
 
		var p = transform.position;
		p.y -= 4.7f;
		transform.position = p;
 
	}
 
	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 ();
 
		for (int row = 0; row < ROWS; row++) {
			for (int column = 0; column < COLUMNS; column++) {
				gridBalls [row] [column].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 = 0;
					while (i < ROWS) {
						foreach (var b in gridBalls[i]) {
							if (!b.connected && b.gameObject.activeSelf) {
								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 first row 
		foreach (var b in gridBalls[0]) {
			if (b.gameObject.activeSelf)
				b.connected = true;
		}
 
		//now set connect property on the rest of the balls
		var i = 1;
		while (i < ROWS) {
			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 < ROWS) {
			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 < ROWS && 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 < ROWS && 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> ();
		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 < ROWS) {
			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 < ROWS && 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 < ROWS && 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;
	}
 
	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 ((GRID_OFFSET_Y + ( TILE_SIZE * 0.5f ) - point.y )/  TILE_SIZE);
		if (r < 0) r = 0;
		if (r >= ROWS) r = ROWS - 1;
 
 
 
		return gridBalls [r] [c];
 
	}
 
	void HandleShootBall () {
		bullets++;
 
		if (bullets > 10) {
			bullets = 0;
			AddLine ();
		}
	}
 
	void Update () {
 
		var p = transform.position;
		p.y -= Time.deltaTime * GRID_SPEED;
		transform.position = p;
 
	}
 
	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;  
		}  
	}
}

First of all, we change the Grid Y offset, because this grid needs to align perfectly with the bottom of the screen (the last row appears at the bottom of the screen.)

So the BuildGrid method looks like this now:

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
void BuildGrid (){
	gridBalls = new List<List<Ball>> ();
 
 
	GRID_OFFSET_X = (COLUMNS * TILE_SIZE) * 0.5f;
	GRID_OFFSET_Y = (ROWS * TILE_SIZE);
 
	GRID_OFFSET_X -= TILE_SIZE * 0.5f;
	GRID_OFFSET_Y -= 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 > ROWS - emptyLines) {
				ball.gameObject.SetActive (false);
			}
		}
 
		gridBalls.Add(rowBalls);
	}
 
	var p = transform.position;
	p.y -= 4.7f;
	transform.position = p;
 
}

First GRID_OFFSET_Y is the full height of the grid, and at the end of the method I shift the grid container, which is what I scroll down the screen.

The logic identifying the closest cell to a given point changes too (although I have not been using this logic, it’s good to know how to handle it.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 ((GRID_OFFSET_Y + ( TILE_SIZE * 0.5f ) - point.y )/  TILE_SIZE);
	if (r < 0) r = 0;
	if (r >= ROWS) r = ROWS - 1;
 
 
 
	return gridBalls [r] [c];
 
}

All I need to do is offset the current Y position of the grid container. I do that in the very first line of the method.

And then finally, the update method that scrolls the grid.

1
2
3
4
5
6
7
void Update () {
 
	var p = transform.position;
	p.y -= Time.deltaTime * GRID_SPEED;
	transform.position = p;
 
}

And that’s it.

I didn’t add any checks for game over conditions, like the balls hitting the bottom of the screen. But that’s simple to do, if you want.

Here is the project with the logic to scroll down the grid.

Bubble Shooter Game in Unity: Part 9

So what if you want to use multiple shooters? The class I wrote for the ray cast shooter can be easily updated so we can use as many shooters as we want in the game.
I’ll place five shooters at the bottom of the screen, and for the sake of this tutorial, each shooter will now have a fixed color type, so the color won’t change after a shot.
(Although you could easily keep the existing logic that handles that in the touch up handler, who knows, it might make things interesting to have each shooter select its color randomly and keep changing its color randomly after every shot.)

The first main changes are to the game view:

I created a shooter prefab, and placed the five shooters on the screen. Each shooter prefab will have its own bullet now.

I moved the input control to a new class, called GameController, attached to the GameScreen gameobject.

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
using UnityEngine;
using System.Collections;
 
public class GameController : MonoBehaviour {
 
	public RayCastShooter[] shooters;
 
	private RayCastShooter selectedShooter;
 
	private bool mouseDown = false;
 
 
 
	// Update is called once per frame
	void Update () {
		if (Input.touches.Length > 0) {
 
			Touch touch = Input.touches [0];
 
			if (touch.phase == TouchPhase.Began) {
				TouchDown (touch.position);
			} else if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended) {
				TouchUp (Input.mousePosition);
			} else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) {
				TouchMove (touch.position);
			}
			TouchMove (touch.position);	
			return;
		} else if (Input.GetMouseButtonDown (0)) {
			mouseDown = true;
			TouchDown (Input.mousePosition);
		} else if (Input.GetMouseButtonUp (0)) {
			mouseDown = false;
			TouchUp (Input.mousePosition);
		} else if (mouseDown) {
			TouchMove (Input.mousePosition);
		}
	}
 
 
	void TouchDown (Vector2 touch) {
 
		selectedShooter = null;
		Vector2 point = Camera.main.ScreenToWorldPoint (touch);
 
		Debug.Log (point.y);
 
		if (point.y < -1f) {
 
 
			var minDistance = 100000.0f;
			RayCastShooter shooter = null;
 
			//look for closest shooter
			foreach (var s in shooters) {
				var d = Vector2.Distance (point, s.transform.position);
				if (d < minDistance) {
					minDistance = d;
					shooter = s;
				}
			}
			selectedShooter = shooter;
 
		}
	}
 
	void TouchUp (Vector2 touch) {
		if (selectedShooter == null)
			return;
		Vector2 point = Camera.main.ScreenToWorldPoint (touch);
		if (Vector2.Distance (point, selectedShooter.transform.position) < 0.2f) {
			selectedShooter.ClearShotPath ();
		} else {
			selectedShooter.HandleTouchUp (touch);
		}
	}
 
	void TouchMove (Vector2 touch) {
		if (selectedShooter == null)
			return;
		selectedShooter.HandleTouchMove (touch);
	}
}

I tried to make the logic selecting the closest shooter to the player’s touch as relaxed as I could manage. There is a minimum Y threshold, so bellow that line every touch will end up selecting a shooter, the closest to the touch point. So I’m not using a minimum distance between touch and shooter, so the player has more space to interact with the game.

And on touch up, if the touch is too close to the shooter, I treat it as a cancelled shot.

The new RayCastShooter looks like this:

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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class RayCastShooter : MonoBehaviour {
 
	public GameObject[] colorsGO;
	public Ball.BALL_TYPE type;
 
	public GameObject dotPrefab;
	public Bullet bullet;
	public Grid grid;
 
 
	private List<Vector2> dots;
	private List<GameObject> dotsPool;
	private int maxDots = 26;
 
	private float dotGap = 0.32f;
	private float bulletProgress = 0.0f;
	private float bulletIncrement = 0.0f;
	private bool mouseDown = false;
	private bool selected = false;
 
 
 
	// Use this for initialization
	void Start () {
 
		dots = new List<Vector2> ();
		dotsPool = new List<GameObject> ();
 
		var i = 0;
		var alpha = 1.0f / maxDots;
		var startAlpha = 1.0f;
		while (i < maxDots) {
			var dot = Instantiate (dotPrefab) as GameObject;
			var sp = dot.GetComponent<SpriteRenderer> ();
			var c = sp.color;
 
			c.a = startAlpha - alpha;
			startAlpha -= alpha;
			sp.color = c;
 
			dot.SetActive (false);
			dotsPool.Add (dot);
			i++;
		}
 
		//select initial type
		foreach (var go in colorsGO) {
			go.SetActive(false);
		}
 
		colorsGO [(int) type].SetActive (true);
 
	}
 
	public void HandleTouchUp (Vector2 touch) {
 
 
		if (bullet.gameObject.activeSelf)
			return;
 
		if (dots == null || dots.Count < 2)
			return;
 
		ClearShotPath ();
 
		bulletProgress = 0.0f;
		bullet.SetType (type);
		bullet.gameObject.SetActive (true);
		bullet.transform.position = transform.position;
		InitPath ();
 
		EventManager.ShootBall ();
 
	}
 
 
	public void HandleTouchMove (Vector2 touch) {
 
		if (bullet.gameObject.activeSelf)
			return;
 
		if (dots == null) {
			return;
		}
 
		dots.Clear ();
 
		foreach (var d in dotsPool)
			d.SetActive (false);
 
		Vector2 point = Camera.main.ScreenToWorldPoint (touch);
		var direction = new Vector2 (point.x - transform.position.x, point.y - transform.position.y);
 
		RaycastHit2D hit = Physics2D.Raycast(transform.position, direction);
		if (hit.collider != null) {
 
			dots.Add (transform.position);
 
			if (hit.collider.tag == "SideWall") {
				DoRayCast (hit, direction);
			} else {
				dots.Add (hit.point);
				DrawPaths ();
			}
		}
	}
 
	public void ClearShotPath () {
		foreach (var d in dotsPool)
			d.SetActive (false);
 
	}
 
	void DoRayCast (RaycastHit2D previousHit, Vector2 directionIn) {
 
		dots.Add (previousHit.point);
 
		var normal = Mathf.Atan2 (previousHit.normal.y, previousHit.normal.x);
		var newDirection = normal + (  normal - Mathf.Atan2(directionIn.y, directionIn.x) );
		var reflection = new Vector2 (-Mathf.Cos (newDirection), -Mathf.Sin (newDirection));
		var newCastPoint = previousHit.point + (2 * reflection);
 
//		directionIn.Normalize ();
//		newCastPoint = new Vector2(previousHit.point.x + 2 * (-directionIn.x), previousHit.point.y + 2 * (directionIn.y));
//		reflection = new Vector2 (-directionIn.x, directionIn.y);
 
		var hit2 = Physics2D.Raycast(newCastPoint, reflection);
		if (hit2.collider != null) {
			if (hit2.collider.tag == "SideWall") {
				//shoot another cast
				DoRayCast (hit2, reflection);
			} else {
				dots.Add (hit2.point);
				DrawPaths ();
			}
		} else {
			DrawPaths ();
		}
	}
 
 
	// Update is called once per frame
	void Update () {
 
		if (bullet.gameObject.activeSelf) {
 
			bulletProgress += bulletIncrement;
 
			if (bulletProgress > 1) {
				dots.RemoveAt (0);
				if (dots.Count < 2) {
					bullet.gameObject.SetActive (false);
					dots.Clear ();
					return;
				} else {
					InitPath ();
				}
			}
 
			var px = dots [0].x + bulletProgress * (dots [1].x - dots [0].x);
			var py = dots [0].y + bulletProgress * (dots [1].y - dots [0].y);
 
			bullet.transform.position = new Vector2 (px, py);
		}
 
	}
 
	void DrawPaths () {
 
		if (dots.Count > 1) {
 
			foreach (var d in dotsPool)
				d.SetActive (false);
 
			int index = 0;
 
			for (var i = 1; i < dots.Count; i++) {
				DrawSubPath (i - 1, i, ref index);
			}
		}
	}
 
	void DrawSubPath (int start, int end, ref int index) {
		var pathLength = Vector2.Distance (dots [start], dots [end]);
 
		int numDots = Mathf.RoundToInt ( (float)pathLength / dotGap );
		float dotProgress = 1.0f / numDots;
 
		var p = 0.0f;
 
		while (p < 1) {
			var px = dots [start].x + p * (dots [end].x - dots [start].x);
			var py = dots [start].y + p * (dots [end].y - dots [start].y);
 
			if (index < maxDots) {
				var d = dotsPool [index];
				d.transform.position = new Vector2 (px, py);
				d.SetActive (true);
				index++;
			}
 
			p += dotProgress;
		}
	}
 
	void InitPath () {
		var start = dots [0];
		var end = dots [1];
		var length = Vector2.Distance (start, end);
		var iterations = length / 0.15f;
		bulletProgress = 0.0f;
		bulletIncrement = 1.0f / iterations;
	}
 
 
}

The main difference here is that I don’t count the number of shots from inside the shooter. Instead I dispatch an event that the Grid will pick up in this line inside the HandleTouchUp method:

1
EventManager.ShootBall ();

If you prefer, you can call a public method in Grid to update the shot count, I just wanted to show how to do it with events.

The EventManager class looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using UnityEngine;
using System.Collections;
 
public class EventManager {
 
	public delegate void Event_ShootBall ();
 
	public static event Event_ShootBall  OnShootBall;
 
	public static void ShootBall () {
		if (OnShootBall != null)
			OnShootBall ();
	}
}

And the new Grid class keeps track of the shots and adds a new line to the grid every 10 shots:

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
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 changeTypeRate = 0.5f;
 
	public int lines = 20;
 
	public GameObject gridBallGO;
 
	[HideInInspector]
	public float GRID_OFFSET_X = 0;
 
	[HideInInspector]
	public  float GRID_OFFSET_Y = 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 () {
 
 
		EventManager.OnShootBall += HandleShootBall;
 
		matchList = new List<Ball> ();
		lastType = (Ball.BALL_TYPE)Random.Range (0, 5);
		typePool = new List<Ball.BALL_TYPE> ();
 
		var i = 0;
		var total = 10000;
		while (i < total) {
			typePool.Add (GetBallType ());
			i++;
		}
 
		Shuffle(typePool);
 
		BuildGrid ();
 
//		gridBalls [0] [0].gameObject.SetActive (false);
	}
 
	void AddLine () {
		//does top line have visible bubbles
		var emptyFirstRow = true;
		foreach (var b in gridBalls[0]) {
			if (b.gameObject.activeSelf) {
				emptyFirstRow = false;
				break;
			}
		}
 
		if (!emptyFirstRow) {
			var r = ROWS - 2;
			while (r >= 0) {
				foreach (var b in gridBalls[r]) {
					if (b.gameObject.activeSelf) {
						gridBalls [r + 1] [b.column].gameObject.SetActive (true);
						gridBalls [r + 1] [b.column].SetType (b.type);
					} else {
						gridBalls [r + 1] [b.column].gameObject.SetActive (false);
					}
				}
				r--;
			}
		}
 
		foreach (var b in gridBalls[0]) {
			b.SetType (typePool [0]);
			typePool.RemoveAt (0);
			b.gameObject.SetActive (true);
		}
	}
 
	void BuildGrid ()
	{
		gridBalls = new List<List<Ball>> ();
 
		GRID_OFFSET_X = (COLUMNS * TILE_SIZE) * 0.5f;
		GRID_OFFSET_Y = (ROWS * TILE_SIZE) * 0.5f;
 
		GRID_OFFSET_X -= TILE_SIZE * 0.5f;
		GRID_OFFSET_Y -= 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 > lines) {
					ball.gameObject.SetActive (false);
				}
			}
 
			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 ();
 
		for (int row = 0; row < ROWS; row++) {
			for (int column = 0; column < COLUMNS; column++) {
				gridBalls [row] [column].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 = 0;
					while (i < ROWS) {
						foreach (var b in gridBalls[i]) {
							if (!b.connected && b.gameObject.activeSelf) {
								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 first row 
		foreach (var b in gridBalls[0]) {
			if (b.gameObject.activeSelf)
				b.connected = true;
		}
 
		//now set connect property on the rest of the balls
		var i = 1;
		while (i < ROWS) {
			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 < ROWS) {
			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 < ROWS && 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 < ROWS && 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> ();
		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 < ROWS) {
			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 < ROWS && 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 < ROWS && 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;
	}
 
	public Ball BallCloseToPoint (Vector2 point)
	{
 
 
		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 ((GRID_OFFSET_Y + ( TILE_SIZE * 0.5f ) - point.y )/  TILE_SIZE);
		if (r < 0) r = 0;
		if (r >= ROWS) r = ROWS - 1;
 
		return gridBalls [r] [c];
 
	}
 
	void HandleShootBall () {
		bullets++;
 
		if (bullets > 10) {
			bullets = 0;
			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;  
		}  
	}
 
}

In the next post I’ll show you how to scroll the grid instead of adding new lines.

Here is the project with multiple shooters.

Bubble Shooter Game in Unity: Part 8

As I mentioned before, a bubble shooter game looks better with a hex grid, specially when the targets are circles. A square grid populated with circles should not run matches diagonally, because it looks weird. So with a square grid you can have a maximum of four neighbors for each cell (diagonal neighbors look too far away when using circles). But with a hex grid you can have up to six neighbors.

Note: If you are building the type of bubble shooter where the “grid” responds to the impact of each shot, meaning there is some physics applied to it, you might consider dropping a grid altogether and use a graph tree instead.

Converting the square grid to a hex grid is super easy though.

I decided to offset the columns, because I thought it looked better, but you have the choice of offsetting either the columns or the rows.

The first change is in the Ball class.

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 , grid.GRID_OFFSET_Y + (-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);
}
}

That condition checking the column index... It shifts the Y of each cell by half its size.

Then we change the logic in the Grid class which collects a cell's neighbors:

List BallActiveNeighbors (Ball ball) {
var result = new List ();
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 < ROWS) { 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 < ROWS && 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 < ROWS && 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; }

If we are collecting the neighbors from a cell located in the shifted column we collect TOP, BOTTOM, LEFT, RIGHT, BOTTOM LEFT and BOTTOM RIGHT.

If the cell is in a regular column, we collect TOP LEFT and TOP RIGHT instead.

The same update in the logic is added to the method collecting the empty neighbors around a cell (used when we add a new Ball to the grid).

And that's it. I'm sure I'm forgetting something major in order to make this a proper game (like identifying when the grid is clear!) but the main chunk of the game logic is here. Have fun!

In the next few posts I'll cover some odd options and changes to the main engine.

Here is the final project.

Bubble Shooter Game in Unity: Part 7

We need to remove all balls (bubbles) which are no longer connected to the main body of the grid.

I do this with a new method in the Grid class, called right after I remove matches from the grid.

void CheckForDisconnected () {
//set all balls as disconnected
foreach (var r in gridBalls) {
foreach (var b in r) {
b.connected = false;
}
}
//connect visible balls in first row
foreach (var b in gridBalls[0]) {
if (b.gameObject.activeSelf)
b.connected = true;
}

//now set connect property on the rest of the balls
var i = 1;
while (i < ROWS) { 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++; } }

The Ball now has a property called connected. I set it to false initially, and then I make every visible ball in the first row connected. The first row is "anchored" to the screen, so any visible ball in that row, is "connected."

Then I check ever subsequent row and check if a ball is touching a connected cell. If it is, that ball and all its neighbors are set to be connected.

Next I remove the disconnected balls:

public void CheckMatchesForBall (Ball ball) {

matchList.Clear ();

for (int row = 0; row < ROWS; row++) { for (int column = 0; column < COLUMNS; column++) { gridBalls [row] [column].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 = 0;
while (i < ROWS) { foreach (var b in gridBalls[i]) { if (!b.connected && b.gameObject.activeSelf) { b.gameObject.SetActive (false); } } i++; } } return; } } }

So when I run the logic that removes matches, I re-check for disconnected cells and then just remove them.

You might want to add a nice delay here between matches removed and disconnected balls being removed, or make the disconnected balls fall off the screen. However you want to handle it, it would start here.

Here is the project so far.

Next, I'll convert the grid to a Hex grid.

Bubble Shooter Game in Unity: Part 6

Finding matches in this grid is very similar to the logic I used in the match three game, particularly the “Tap to Match” variation.

Here’s the new logic in the Grid class:

public void CheckMatchesForBall (Ball ball) {

matchList.Clear ();

for (int row = 0; row < ROWS; row++) { for (int column = 0; column < COLUMNS; column++) { gridBalls [row] [column].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);
}
}
return;
}
}
}

List GetMatches (Ball ball) {
ball.visited = true;
var result = new List () { ball };
var n = BallActiveNeighbors (ball);

foreach (var b in n) {
if (b.type == ball.type) {
result.Add (b);
}
}

return result;
}

void AddMatches (List matches) {
foreach (var b in matches) {
if (!matchList.Contains (b))
matchList.Add (b);
}
}

List BallActiveNeighbors (Ball ball) {
var result = new List ();
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 < ROWS) { if (gridBalls [ball.row + 1] [ball.column].gameObject.activeSelf) result.Add (gridBalls [ball.row + 1] [ball.column]); } return result; }

In this game however, the CheckMatchesForBall call is made from the Grid class the moment we add a new ball to the grid.

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); }

And that's it. In the next tutorial I'll take out any bubbles which are disconnected to the main grid after matches are removed.

Here is the project so far.