forked from harmattan/brainparty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatchmatch.cpp
312 lines (233 loc) · 8.17 KB
/
patchmatch.cpp
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
// 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 "patchmatch.h"
#include "Minigame.h"
BPMiniGame_PatchMatch::BPMiniGame_PatchMatch(BPGame* game) : BPMiniGame(game) {
sfcBackground = TheGame->LoadBitmap("patchmatch", 512, 512);
Locked = false; // when true disallow movement
TimeStarted = 0;
SuccessTime = -1;
sfcClock = sfcScoreStr = NULL;
SelectedBox = NULL;
DisappearTime = 400;
Score = 0;
SetScore();
GameTitle = "Patch Match";
GameHelp = "Tap on any shape, then drag it onto a square with the same picture to score points. It sounds easy enough, but can you stay focused for two minutes?";
GameHelp2 = "Not every box will have a match on the screen at any given time, so you need to look hard and find matches wherever you can.";
MiniGameType = PUZZLE;
for (int i = 1; i <= 59; ++i) {
ostringstream filename;
filename << "patch" << i;
PatchPictures.Add(TheGame->LoadBitmap(filename.str().c_str(), 48, 48));
}
for (int j = 0; j < 7; ++j) {
vector<BPMiniGame_PatchMatch_Box*>* row = new vector<BPMiniGame_PatchMatch_Box*>();
for (int i = 0; i < 6; ++i) {
BPMiniGame_PatchMatch_Box* box = new BPMiniGame_PatchMatch_Box();
box->X = 36 + (i * 50);
box->Y = 36 + (j * 50);
box->DestY = 36 + (j * 50);
box->DrawX = box->X;
box->DrawY = box->Y;
box->Colour = TheGame->RandomRange(0, PatchPictures.Count - 1);
row->push_back(box);
}
Boxes.push_back(row);
}
}
BPMiniGame_PatchMatch::~BPMiniGame_PatchMatch() {
SAFE_DELETE(sfcBackground);
BPMiniGame_PatchMatch_Box* box;
for (int i = 0; i < Boxes.size(); ++i) {
vector<BPMiniGame_PatchMatch_Box*>* row = Boxes[i];
for (int i = row->size() - 1; i >= 0; --i) {
box = (*row)[i];
SAFE_DELETE(box);
}
row->clear();
SAFE_DELETE(row);
}
Boxes.clear();
PatchPictures.Clear();
SAFE_DELETE(sfcScoreStr);
SAFE_DELETE(sfcClock);
}
void BPMiniGame_PatchMatch::OnMouseMove() {
if (Locked) return;
if (SelectedBox == NULL) return;
Locked = true;
SelectedBox->DrawX = TouchEvent.X;
SelectedBox->DrawY = TouchEvent.Y;
LastPos = TouchEvent;
Locked = false;
}
void BPMiniGame_PatchMatch::OnMouseDown() {
if (Locked) return;
Locked = true;
LastPos = TouchEvent;
for (int i = 0; i < Boxes.size(); ++i) {
vector<BPMiniGame_PatchMatch_Box*>* row = Boxes[i];
for (int j = 0; j < row->size(); ++j) {
BPMiniGame_PatchMatch_Box* box = (*row)[j];
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, box->X - HalfBoxSize, box->Y - HalfBoxSize, BoxSize, BoxSize)) {
SelectedBox = (*row)[j];
TheGame->PlaySound("card_flip");
return;
}
}
}
Locked = false;
}
void BPMiniGame_PatchMatch::OnMouseUp() {
if (SelectedBox == NULL) return;
if (Locked) return;
for (int i = 0; i < Boxes.size(); ++i) {
vector<BPMiniGame_PatchMatch_Box*>* row = Boxes[i];
for (int j = 0; j < row->size(); ++j) {
BPMiniGame_PatchMatch_Box* box = (*row)[j];
if (box == SelectedBox) continue;
if (box->Colour != SelectedBox->Colour) continue;
if (TheGame->RectOverRect(SelectedBox->DrawX, SelectedBox->DrawY, BoxSize, BoxSize, box->X, box->Y, BoxSize, BoxSize)) {
box->MatchTime = TheGame->TickCount;
SelectedBox->MatchTime = TheGame->TickCount;
Score += 1;
SetScore();
TheGame->PlaySound("gem_select");
SelectedBox = NULL;
return;
}
}
}
SelectedBox->DrawX = SelectedBox->X;
SelectedBox->DrawY = SelectedBox->Y;
SelectedBox = NULL;
}
void BPMiniGame_PatchMatch::Start() {
TimeStarted = TheGame->TickCount;
}
int BPMiniGame_PatchMatch::GetWeight() {
return MinMax(round(Score * 17));
}
void BPMiniGame_PatchMatch::Render() {
TheGame->Clear(TheGame->Black);
int time_passed = TheGame->TickCount - TimeStarted;
TheGame->DrawImage(sfcBackground, 160, 208, time_passed / 500.0f, 1.1f, (*TheGame->White));
Colour col = Colour(0.0f, 0.0f, 0.0f, 0.6f);
TheGame->FillRectangle(col, 0, 369, 320, 40);
if (!MarathonMode) {
int 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, 160, 50, RIGHT);
}
TheGame->DrawString(sfcClock, WHITE, 145, 370);
}
TheGame->DrawString(sfcScoreStr, WHITE, 14, 370);
BPMiniGame_PatchMatch_Box* box;
for (int i = 0; i < Boxes.size(); ++i) {
vector<BPMiniGame_PatchMatch_Box*>* row = Boxes[i];
for (int i = row->size() - 1; i >= 0; --i) {
box = (*row)[i];
if (box->MatchTime == -1) {
TheGame->DrawImage(PatchPictures[box->Colour], box->DrawX, box->DrawY, 0.0f, 1.0f, (*TheGame->White));
}
}
}
for (int i = 0; i < Boxes.size(); ++i) {
vector<BPMiniGame_PatchMatch_Box*>* row = Boxes[i];
for (int i = row->size() - 1; i >= 0; --i) {
box = (*row)[i];
if (box->MatchTime != -1) {
float diff = TheGame->TickCount - box->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);
TheGame->DrawImage(PatchPictures[box->Colour], box->DrawX, box->DrawY, TheGame->Lerp(0, 360, step), TheGame->SmoothStep(1.0f, 6.0f, step), col);
}
}
}
}
if (SelectedBox != NULL) {
TheGame->DrawImage(PatchPictures[SelectedBox->Colour], SelectedBox->DrawX, SelectedBox->DrawY, 0.0f, 1.0f, (*TheGame->White));
}
}
void BPMiniGame_PatchMatch::Tick() {
vector<BPMiniGame_PatchMatch_Box*>* row;
BPMiniGame_PatchMatch_Box* box;
BPMiniGame_PatchMatch_Box* copybox;
int MatchTimeout = TheGame->TickCount - 400;
bool AllStill = true;
for (int i = Boxes.size() - 1; i >= 0; --i) {
row = Boxes[i];
for (int j = row->size() - 1; j >= 0; --j) {
box = (*row)[j];
if (box->MatchTime != -1) AllStill = false;
if (box->Y != box->DestY) {
AllStill = false;
box->YSpeed += 2.0f;
box->Y += box->YSpeed;
if (box->Y > box->DestY) {
box->Y = box->DestY;
box->YSpeed = 0;
}
box->DrawY = box->Y;
}
if (box->MatchTime != -1 && box->MatchTime < MatchTimeout) {
SAFE_DELETE(box);
row->erase(row->begin() + j);
// move all boxes down above me
for (int k = i - 1; k >= 0; --k) {
copybox = (*Boxes[k])[j];
Boxes[k]->erase(Boxes[k]->begin() + j);
Boxes[k + 1]->insert(Boxes[k + 1]->begin() + j, copybox);
copybox->DestY = 36 + ((k + 1) * BoxSize);
}
BPMiniGame_PatchMatch_Box* newbox = new BPMiniGame_PatchMatch_Box();
newbox->X = 36 + (j * BoxSize);
newbox->Y = -((9 - j) * BoxSize);
newbox->DrawX = newbox->X;
newbox->DrawY = newbox->Y;
newbox->DestY = 36;
newbox->Colour = TheGame->RandomRange(0, PatchPictures.Count - 1);
Boxes[0]->insert(Boxes[0]->begin() + j, newbox);
}
}
}
if (AllStill) {
Locked = false;
} else {
Locked = true;
}
}
void BPMiniGame_PatchMatch::SetMarathon() {
MarathonMode = true;
GameHelp = "Drag rows of jewels left and right, matching three or more to score points; the more you match, the more you score.";
}
void BPMiniGame_PatchMatch::SetScore() {
if (Score > 0) {
ostringstream score;
string scorestr = TheGame->SeparateThousands(Score);
score << "Matches: " << scorestr;
TheGame->AllocString(&sfcScoreStr, score.str().c_str(), LARGE, 270, 50, LEFT);
} else {
TheGame->AllocString(&sfcScoreStr, "Matches: 0", LARGE, 270, 50, LEFT);
}
}