1 // Brain Party
2 // Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)
3 
4 // Brain Party is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 
18 #include "bombhunt.h"
19 #include "Minigame.h"
20 #include "BPList.h"
21 
BPMiniGame_BombHunt(BPGame * game)22 BPMiniGame_BombHunt::BPMiniGame_BombHunt(BPGame* game) : BPMiniGame(game) {
23 	SuccessTime = -1;
24 	GuessesLeft = 12;
25 	LastNumCorrect = 0;
26 	LastGuessTime = 0;
27 	TimeStarted = 0;
28 
29 	sfcLastNumCorrect = sfcGuessesLeft = NULL;
30 
31 	Selected1 = Selected2 = Selected3 = NULL;
32 
33 	GameTitle = "Bomb Hunt";
34 	GameHelp = "There's a bomb, and you have to diffuse it! You have 12 tries to figure out which three gems make up the bomb, and I'll tell you how many of your gem guesses were correct. All three gems in the bomb are different!";
35 	GameHelp2 = "To make a guess, just tap any three bomb components. If you get it wrong, you'll be told how many of those gems were correct. For example, if you tap red, green and blue gems and Brain Party says that you got 1 correct, it means that two of those gems were wrong.";
36 
37 	MiniGameType = PUZZLE;
38 
39 	sfcBackground = TheGame->LoadBitmap("bombhunt", 320, 480);
40 
41 	Components.Add(TheGame->LoadBitmap("gem1", 64, 64));
42 	Components.Add(TheGame->LoadBitmap("gem2", 64, 64));
43 	Components.Add(TheGame->LoadBitmap("gem3", 64, 64));
44 	Components.Add(TheGame->LoadBitmap("gem4", 64, 64));
45 	Components.Add(TheGame->LoadBitmap("gem5", 64, 64));
46 	Components.Add(TheGame->LoadBitmap("gem6", 64, 64));
47 	Components.Add(TheGame->LoadBitmap("gem7", 64, 64));
48 	Components.Add(TheGame->LoadBitmap("gem8", 64, 64));
49 
50 	Components.Shuffle();
51 
52 	BombItems.Add(Components[0]);
53 	BombItems.Add(Components[1]);
54 	BombItems.Add(Components[2]);
55 
56 	Components.Shuffle();
57 }
58 
~BPMiniGame_BombHunt()59 BPMiniGame_BombHunt::~BPMiniGame_BombHunt() {
60 	SAFE_DELETE(sfcBackground);
61 
62 	SAFE_DELETE(sfcLastNumCorrect);
63 	SAFE_DELETE(sfcGuessesLeft);
64 
65 	BombItems.Clear(false);
66 	Components.Clear();
67 }
68 
Start()69 void BPMiniGame_BombHunt::Start() {
70 	TimeStarted = TheGame->TickCount;
71 }
72 
GetWeight()73 int BPMiniGame_BombHunt::GetWeight() {
74 	int time = floor((TheGame->TickCount - TimeStarted) / 1000);
75 	return MinMax(600 - round(time * 20));
76 }
77 
Render()78 void BPMiniGame_BombHunt::Render() {
79 	TheGame->DrawImage(sfcBackground, 0, 0);
80 
81 	for (int i = 0; i < 8; ++i) {
82 		if (i < 4) {
83 			TheGame->DrawImage(Components[i], 23 + (i * 70), 77);
84 		} else {
85 			TheGame->DrawImage(Components[i], 23 + ((i - 4) * 70), 147);
86 		}
87 	}
88 
89 	if (Selected1 != NULL) TheGame->DrawImage(Selected1, 48, 257);
90 	if (Selected2 != NULL) TheGame->DrawImage(Selected2, 128, 257);
91 	if (Selected3 != NULL) TheGame->DrawImage(Selected3, 208, 257);
92 
93 	if (SuccessTime == -1) {
94 		if (LastGuessTime != 0) {
95 			TheGame->DrawString(sfcLastNumCorrect, WHITE, 0, 350);
96 		} else {
97 			TheGame->DrawString(sfcGuessesLeft, WHITE, 0, 350);
98 		}
99 	}
100 }
101 
Tick()102 void BPMiniGame_BombHunt::Tick() {
103 	if (SuccessTime != -1) {
104 		if (SuccessTime + 250 < TheGame->TickCount) {
105 			Success();
106 		}
107 	} else {
108 		// this player has used all their chances!
109 		if (GuessesLeft == 0) {
110 			// short delay so the Guesses Left text can be updated
111 			if (LastGuessTime + 250 < TheGame->TickCount) {
112 				Failure();
113 			}
114 		}
115 	}
116 
117 	if (LastGuessTime != 0) {
118 		if (LastGuessTime + 500 < TheGame->TickCount) {
119 			ClearForNextTry();
120 		}
121 	}
122 }
123 
ClearForNextTry()124 void BPMiniGame_BombHunt::ClearForNextTry() {
125 	Selected1 = NULL;
126 	Selected2 = NULL;
127 	Selected3 = NULL;
128 
129 	LastGuessTime = 0;
130 }
131 
OnMouseUp()132 void BPMiniGame_BombHunt::OnMouseUp() {
133 	if (LastGuessTime != 0) ClearForNextTry(); // speed up the guessing time when you tap the screen
134 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 23, 77, 64, 64)) SelectItem(Components[0]);
135 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 93, 77, 64, 64)) SelectItem(Components[1]);
136 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 163, 77, 64, 64)) SelectItem(Components[2]);
137 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 233, 77, 64, 64)) SelectItem(Components[3]);
138 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 23, 147, 64, 64)) SelectItem(Components[4]);
139 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 93, 147, 64, 64)) SelectItem(Components[5]);
140 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 163, 147, 64, 64)) SelectItem(Components[6]);
141 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 233, 147, 64, 64)) SelectItem(Components[7]);
142 
143 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 0, 231, 240, 20)) {
144 		if (GuessesLeft > 1) {
145 			//MessageBox.Show("You only have " + GuessesLeft + " more tries to figure out which three items make up the bomb - good luck!", GameTitle);
146 		} else {
147 			//MessageBox.Show("You only have one more try to figure out which three items make up the bomb - you'd better guess correctly!", GameTitle);
148 		}
149 	}
150 
151 	if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 28, 243, 263, 90)) {
152 		MessageBox::Show("This is your guessing area. As you tap on the gems above, they'll be placed here so you can see which ones you have chosen.", GameTitle);
153 	}
154 }
155 
OnMouseDown()156 void BPMiniGame_BombHunt::OnMouseDown() {
157 
158 }
159 
OnMouseMove()160 void BPMiniGame_BombHunt::OnMouseMove() {
161 
162 }
163 
SelectItem(Texture * item)164 void BPMiniGame_BombHunt::SelectItem(Texture* item) {
165 	if (Selected1 == NULL) {
166 		TheGame->PlaySound("gem_select");
167 		Selected1 = item;
168 		return;
169 	} else if (Selected2 == NULL && Selected1 != item) {
170 		TheGame->PlaySound("gem_select");
171 		Selected2 = item;
172 		return;
173 	} else if (Selected3 == NULL && Selected1 != item && Selected2 != item) {
174 		Selected3 = item;
175 		CheckAnswer();
176 		return;
177 	}
178 
179 	if (Selected1 == item || Selected2 == item || Selected3 == item) {
180 		MessageBox::Show("You've chosen that item already! Remember, the bomb is made up of three different components.", "Oops!");
181 	}
182 }
183 
CheckAnswer()184 void BPMiniGame_BombHunt::CheckAnswer() {
185 	--GuessesLeft;
186 
187 	LastNumCorrect = 0;
188 
189 	if (BombItems.Contains(Selected1)) ++LastNumCorrect;
190 	if (BombItems.Contains(Selected2)) ++LastNumCorrect;
191 	if (BombItems.Contains(Selected3)) ++LastNumCorrect;
192 
193 	if (LastNumCorrect == 3) {
194 		TheGame->PlaySound("gem_select");
195 		SuccessTime = TheGame->TickCount;
196 	} else {
197 		TheGame->PlaySound("wrong2");
198 		LastGuessTime = TheGame->TickCount;
199 	}
200 
201 	ostringstream numcorrect;
202 	numcorrect << "You got " << LastNumCorrect << " correct.";
203 	TheGame->AllocString(&sfcLastNumCorrect, numcorrect.str().c_str(), NORMAL, 320, 50, CENTRED);
204 
205 	ostringstream guesses;
206 
207 	if (GuessesLeft <= 1) {
208 		TheGame->AllocString(&sfcGuessesLeft, "1 guess remaining.", NORMAL, 320, 50, CENTRED);
209 	} else {
210 		guesses << GuessesLeft << " guesses remaining.";
211 		TheGame->AllocString(&sfcGuessesLeft, guesses.str().c_str(), NORMAL, 320, 50, CENTRED);
212 	}
213 
214 }
215