File: balloonblaster.cpp

package info (click to toggle)
brainparty 0.61%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,640 kB
  • sloc: cpp: 12,991; objc: 252; makefile: 61
file content (361 lines) | stat: -rw-r--r-- 11,366 bytes parent folder | download | duplicates (6)
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
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)

// Brain Party is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "Minigame.h"
#include "balloonblaster.h"

BPMiniGame_BalloonBlaster::BPMiniGame_BalloonBlaster(BPGame* game) : BPMiniGame(game) {
	sfcBackground = TheGame->LoadBitmap("clouds", 320, 480);
	
	Balloons = new vector<vector<BPMiniGame_BalloonBlaster_Balloon*>* >();
	BalloonTypes = new BPPList<Texture*>();
	sfcBalloonBlack = TheGame->LoadBitmap("balloon_black", 32, 45);
	sfcPuff = TheGame->LoadBitmap("puff", 61, 61);
	
	TimeStarted = 0;
	TimePassed = 0;
	SmartBombTime = -1;
	SuccessTime = -1;
	
	DisappearTime = 200;
	
	Score = 0;
	sfcScoreStr = sfcClock = NULL;
	SetScore();
	
	GameTitle = "Balloon Blaster";
	GameHelp = "Pop balloons by tapping them - get three or more of the same colour to score. But watch out: pop one and you lose straight away, pop two and you lose lots of points. What's more, you have only two minutes!";
	GameHelp2 = "The key to scoring high is to get as many similar-coloured balloons together as possible before popping them - even if that means popping one or two smaller groups first. Remember: if you're going to score lots of points then losing a few thousand first is a good investment!";
	
	MiniGameType = LIVELY;
		
	BalloonTypes->Add(TheGame->LoadBitmap("balloon_blue", 32, 45));
	BalloonTypes->Add(TheGame->LoadBitmap("balloon_red", 32, 45));
	BalloonTypes->Add(TheGame->LoadBitmap("balloon_purple", 32, 45));
	BalloonTypes->Add(TheGame->LoadBitmap("balloon_green", 32, 45));
	
	for (int j = 0; j < 7; ++j) {
		vector<BPMiniGame_BalloonBlaster_Balloon*>* row = new vector<BPMiniGame_BalloonBlaster_Balloon*>();
		
		for (int i = 0; i < 9; ++i) {
			BPMiniGame_BalloonBlaster_Balloon* balloon = new BPMiniGame_BalloonBlaster_Balloon();
			balloon->X = 4 + (i * 35);
			balloon->Y = MiniGameHeight + (j * 50);
			balloon->DestY = 10 + (j * 50);
			
			balloon->Colour = TheGame->RandomRange(0, BalloonTypes->Count - 1);
			row->push_back(balloon);
		}
		
		Balloons->push_back(row);
	}
}

BPMiniGame_BalloonBlaster::~BPMiniGame_BalloonBlaster() {
	SAFE_DELETE(sfcBackground);
	SAFE_DELETE(sfcBalloonBlack);
	SAFE_DELETE(sfcPuff);
		
	vector<BPMiniGame_BalloonBlaster_Balloon*>* row;
	BPMiniGame_BalloonBlaster_Balloon* balloon;
	
	for (int i = 0; i < Balloons->size(); ++i) {
		row = (*Balloons)[i];
		
		for (int j = 0; j < row->size(); ++j) {
			balloon = (*row)[j];
			SAFE_DELETE(balloon);
		}
		
		row->clear();
		SAFE_DELETE(row);
	};
	
	Balloons->clear();
	SAFE_DELETE(Balloons);
	
	SAFE_DELETE(sfcScoreStr);
	SAFE_DELETE(sfcClock);
	
	BalloonTypes->Clear();
	SAFE_DELETE(BalloonTypes);
}

void BPMiniGame_BalloonBlaster::OnMouseUp() {
	
}

void BPMiniGame_BalloonBlaster::OnMouseMove() {
	
}
	
void BPMiniGame_BalloonBlaster::OnMouseDown() {
	vector<BPMiniGame_BalloonBlaster_Balloon*>* row;
	BPMiniGame_BalloonBlaster_Balloon* balloon;
		
	for (int i = 0; i < Balloons->size(); ++i) {
		row = (*Balloons)[i];
			
		for (int j = 0; j < row->size(); ++j) {
			balloon = (*row)[j];
				
			if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, balloon->X, balloon->Y, 32, 45)) {
				if (balloon->Colour == -1) {
					TheGame->PlaySound("explosion");
					int ScoreAdd = SmartBomb(balloon, i, j);
					ScoreAdd = floor(pow(2.0f, min(16, ScoreAdd)));
					ModifyScore(ScoreAdd);
				} else {
					int ScoreAdd = MatchBalloon(balloon, i, j);
						
					switch (ScoreAdd) {
						case 0:
							break;
						case 1:
							Failure();
							break;
						case 2:
							TheGame->PlaySound("balloon_pop");
							ModifyScore(-100);
							break;
						default:
							TheGame->PlaySound("balloon_pop");
							ModifyScore(floor(pow(2.0f, min(16, ScoreAdd))));
							break;
					}
				}
				
				return;
			}
		}
	}
}
	
int BPMiniGame_BalloonBlaster::SmartBomb(BPMiniGame_BalloonBlaster_Balloon* balloon, int row, int col) {
	// this matches all adjacent balloons
	int ThisScore = 0;
		
	if (balloon->MatchTime == -1) {
		balloon->MatchTime = TheGame->TickCount;
			
		if (row > 0) {
			if (col > 0) ThisScore += MatchBalloon((*(*Balloons)[row - 1])[col - 1], row - 1, col - 1);
			ThisScore += MatchBalloon((*(*Balloons)[row - 1])[col], row - 1, col);
			if (col < 8) ThisScore += MatchBalloon((*(*Balloons)[row - 1])[col + 1], row - 1, col + 1);
		}
			
		if (col > 0) ThisScore += MatchBalloon((*(*Balloons)[row])[col - 1], row, col - 1);
		if (col < 8) ThisScore += MatchBalloon((*(*Balloons)[row])[col + 1], row, col + 1);
			
		if (row < 5) {
			if (col > 0) ThisScore += MatchBalloon((*(*Balloons)[row + 1])[col - 1], row + 1, col - 1);
			ThisScore += MatchBalloon((*(*Balloons)[row + 1])[col], row + 1, col);
			if (col < 8) ThisScore += MatchBalloon((*(*Balloons)[row + 1])[col + 1], row + 1, col + 1);
		}
	}
		
	// used to give a quick white flash on the screen
	SmartBombTime = TheGame->TickCount + 100;
		
	return ThisScore;
}
	
void BPMiniGame_BalloonBlaster::ModifyScore(int adjust) {
	Score += adjust;
		
	if (Score < 0) Score = 0;
	
	SetScore();
}
	
void BPMiniGame_BalloonBlaster::Start() {
	TimeStarted = TheGame->TickCount;
}
	
int BPMiniGame_BalloonBlaster::GetWeight() {
	return MinMax(Score / 490);
}
	
void BPMiniGame_BalloonBlaster::Render() {
	if (SmartBombTime > TheGame->TickCount) {
		// give a quick white flash for the smart bomb
		TheGame->Clear(TheGame->White);
		return;
	}
		
	TheGame->DrawImage(sfcBackground, 0, 0);
		
	BPMiniGame_BalloonBlaster_Balloon* balloon;
		
	for (int b = 0; b < Balloons->size(); ++b) {
		vector<BPMiniGame_BalloonBlaster_Balloon*>* row = (*Balloons)[b];
		
		for (int i = row->size() - 1; i >= 0; --i) {
			balloon = (*row)[i];
								
			if (balloon->MatchTime != -1) {
				float diff = TheGame->TickCount - balloon->MatchTime;
				
				if (diff <= DisappearTime) {
					float step = diff / DisappearTime; // get a value between 0 and 1
					Colour col = Colour(1.0f, 1.0f, 1.0f, 1 - step);

					if (balloon->Colour == -1) {
						TheGame->DrawImage(sfcBalloonBlack, balloon->X + sfcBalloonBlack->HalfWidth, balloon->Y + sfcBalloonBlack->HalfHeight, 0.0f, 1.0f + step, col);
					} else {
						TheGame->DrawImage((*BalloonTypes)[balloon->Colour], balloon->X + sfcBalloonBlack->HalfWidth, balloon->Y + sfcBalloonBlack->HalfHeight, 0.0f, 1.0f + step, col);
					}			
					
					TheGame->DrawImage(sfcPuff, balloon->X + sfcBalloonBlack->HalfWidth, balloon->Y + sfcBalloonBlack->HalfHeight, 0.0f, 1.0f, col);
				}
			} else {
				if (balloon->Colour == -1) {
					TheGame->DrawImage(sfcBalloonBlack, balloon->X +  + sfcBalloonBlack->HalfWidth, balloon->Y + sfcBalloonBlack->HalfHeight, 0.0f, 1.0f, (*TheGame->White));
				} else {
					TheGame->DrawImage((*BalloonTypes)[balloon->Colour], balloon->X +  sfcBalloonBlack->HalfWidth, balloon->Y + sfcBalloonBlack->HalfHeight, 0.0f, 1.0f, (*TheGame->White));
				}				
			}
		}
	}
	
	if (!MarathonMode) {
		TimePassed = TheGame->TickCount - TimeStarted;
		TimePassed = 120000 - TimePassed;
			
		if (TimePassed <= 0) {
			if (SuccessTime == -1) {
				SuccessTime = TheGame->TickCount;
				Success();
			}
		}
		
		if (sfcClock == NULL || RedrawClock()) {
			TheGame->AllocString(&sfcClock, TheGame->TicksToTime(TimePassed)->c_str(), LARGE, 80, 47, LEFT);
		}
		
		TheGame->DrawString(sfcClock, BLACK, 235, 366);
	}

	TheGame->DrawString(sfcScoreStr, BLACK, 15, 367);
}
	
void BPMiniGame_BalloonBlaster::Tick() {
	vector<BPMiniGame_BalloonBlaster_Balloon*>* row;
	BPMiniGame_BalloonBlaster_Balloon* balloon;
	BPMiniGame_BalloonBlaster_Balloon* copyballoon;
			
	int MatchTimeout = TheGame->TickCount - DisappearTime;
			
	for (int i = 0; i < Balloons->size(); ++i) {
		row = (*Balloons)[i];
				
		for (int j = row->size() - 1; j >= 0; --j) {
			balloon = (*row)[j];
			
			if (balloon->Y != balloon->DestY) {
				++balloon->YSpeed;
				balloon->Y -= balloon->YSpeed;
				if (balloon->Y < balloon->DestY) {
					balloon->Y = balloon->DestY;
					balloon->YSpeed = 0;
				}
			}
					
			if (balloon->MatchTime != -1 && balloon->MatchTime < MatchTimeout) {
				SAFE_DELETE((*row)[j]);
				
				row->erase(row->begin() + j);
				
				// move all balloons up below me
				
				for (int k = i + 1; k < Balloons->size(); ++k) {
					copyballoon = (*(*Balloons)[k])[j];
					(*Balloons)[k]->erase((*Balloons)[k]->begin() + j);
										
					(*Balloons)[k - 1]->insert((*Balloons)[k - 1]->begin() + j, copyballoon);
					copyballoon->DestY = 10 + ((k - 1) * 50);
				}
						
				BPMiniGame_BalloonBlaster_Balloon* newballoon = new BPMiniGame_BalloonBlaster_Balloon();
				newballoon->X = 4 + (j * 35);
				newballoon->Y = MiniGameHeight;
				newballoon->DestY = 310;
						
				if (TheGame->RandomRange(0, 100) == 56) {
					newballoon->Colour = -1;
				} else {
					newballoon->Colour = TheGame->RandomRange(0, BalloonTypes->Count - 1);
				}
						
				(*Balloons)[Balloons->size() - 1]->insert((*Balloons)[Balloons->size() - 1]->begin() + j, newballoon);
			}
		}
	}
}
	
int BPMiniGame_BalloonBlaster::MatchBalloon(BPMiniGame_BalloonBlaster_Balloon* balloon, int row, int col) {
	int ThisScore = 0;
		
	if (balloon->MatchTime == -1) {
		balloon->MatchTime = TheGame->TickCount;
			
		ThisScore = 1;
			
		// now match all other identical balloons around it
		if (row > 0) {
			if ((*(*Balloons)[row - 1])[col]->Colour == balloon->Colour && (*(*Balloons)[row - 1])[col]->MatchTime == -1) {
				ThisScore += MatchBalloon((*(*Balloons)[row - 1])[col], row - 1, col);
			}
		}
	
		if (row < 6) {
			if ((*(*Balloons)[row + 1])[col]->Colour == balloon->Colour && (*(*Balloons)[row + 1])[col]->MatchTime == -1) {
				ThisScore += MatchBalloon((*(*Balloons)[row + 1])[col], row + 1, col);
			}
		}
		
		if (col > 0) {
			if ((*(*Balloons)[row])[col - 1]->Colour == balloon->Colour && (*(*Balloons)[row])[col - 1]->MatchTime == -1) {
				ThisScore += MatchBalloon((*(*Balloons)[row])[col - 1], row, col - 1);
			}
		}
			
		if (col < 8) {
			if ((*(*Balloons)[row])[col + 1]->Colour == balloon->Colour && (*(*Balloons)[row])[col + 1]->MatchTime == -1) {
				ThisScore += MatchBalloon((*(*Balloons)[row])[col + 1], row, col + 1);
			}
		}
	}
		
	return ThisScore;
}
	
void BPMiniGame_BalloonBlaster::SetMarathon() {
	MarathonMode = true;
	GameHelp = "Pop balloons by tapping them - get three or more of the same colour to score. But watch out: popping one or two is bad!";
}

void BPMiniGame_BalloonBlaster::SetScore() {
	if (Score > 0) {
		ostringstream str;
		string score_str = TheGame->SeparateThousands(Score);
		str << "Score: " << score_str;
		TheGame->AllocString(&sfcScoreStr, str.str().c_str(), LARGE, 250, 50, LEFT);
	} else {
		TheGame->AllocString(&sfcScoreStr, "Score: 0", LARGE, 250, 50, LEFT);
	}
}