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
|
// 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 "jewelflip.h"
#include "Minigame.h"
BPMiniGame_JewelFlip::BPMiniGame_JewelFlip(BPGame* game) : BPMiniGame(game) {
sfcBackground = TheGame->LoadBitmap("jewelflip", 320, 416);
sfcBottomBar = TheGame->LoadBitmap("jewelflip_bottombar", 320, 48);
sfcSelected = TheGame->LoadBitmap("selected48", 48, 48);
SelectedGem = NULL;
sfcTotalScore = NULL;
TheGame->AllocString(&sfcTotalScore, "Score: 0", LARGE, 320, 37, CENTRED, true);
GameOverCounter = GameOverTimer = TotalScore = TimeStarted = 0;
GameOver = false;
GameTitle = "Jewel Flip";
GameHelp = "Tap adjacent gems to switch them, but only if one of the gems ends next to another of the same type. Try to group gems as best you can, then tap \"Ready\" once you're done - bigger groups get more points!";
GameHelp2 = "To swap a gem, select it then select another gem above, below, left or right of it. You can swap gems only if one of them ends up next to another one of its own type. For example, if you want to swap a white gem and a red gem, then either the white gem must end up next to another white gem or the red gem must end up next to another red gem.";
MiniGameType = PUZZLE;
GemTypes.Add(TheGame->LoadBitmap("gem1_small", 48, 48));
GemTypes.Add(TheGame->LoadBitmap("gem2_small", 48, 48));
GemTypes.Add(TheGame->LoadBitmap("gem3_small", 48, 48));
GemTypes.Add(TheGame->LoadBitmap("gem4_small", 48, 48));
GemTypes.Add(TheGame->LoadBitmap("gem5_small", 48, 48));
GemTypes.Add(TheGame->LoadBitmap("gem6_small", 48, 48));
GemTypes.Add(TheGame->LoadBitmap("gem7_small", 48, 48));
GemTypes.Add(TheGame->LoadBitmap("gem8_small", 48, 48));
BPList<int> JewelList;
// generate an equal distribution of gem types so that the scores aren't too uneven
for (int i = 0; i < 42; ++i) {
JewelList.Add(i % GemTypes.Count);
}
JewelList.Shuffle();
int n = 0;
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 7; ++j) {
BPMiniGame_JewelFlip_Jewel* gem = new BPMiniGame_JewelFlip_Jewel();
gem->X = 11 + (i * 50);
gem->Y = 11 + (j * 50);
gem->GemType = JewelList[n];
gem->DestX = gem->X;
gem->DestY = gem->Y;
Gems.Add(gem);
++n;
}
}
}
BPMiniGame_JewelFlip::~BPMiniGame_JewelFlip() {
SAFE_DELETE(sfcBackground);
SAFE_DELETE(sfcBottomBar);
SAFE_DELETE(sfcSelected);
GemTypes.Clear();
Gems.Clear();
SAFE_DELETE(SelectedGem);
SAFE_DELETE(sfcTotalScore);
}
void BPMiniGame_JewelFlip::Start() {
TimeStarted = TheGame->TickCount;
}
int BPMiniGame_JewelFlip::GetWeight() {
float TimePassed = (TheGame->TickCount - TimeStarted) / 1000.0f;
return MinMax((TotalScore + TotalScore + TotalScore) - round(TimePassed / 4.0f));
}
void BPMiniGame_JewelFlip::Render() {
TheGame->DrawImage(sfcBackground, 0, 0);
for (int i = 0; i < Gems.Count; ++i) {
BPMiniGame_JewelFlip_Jewel* gem = Gems[i];
if (gem->Removed) continue;
TheGame->DrawImage(GemTypes[gem->GemType], gem->X, gem->Y);
if (SelectedGem == gem) {
TheGame->DrawImage(sfcSelected, gem->X, gem->Y);
}
}
if (GameOver) {
TheGame->DrawImage(sfcBottomBar, 0, 368);
TheGame->DrawString(sfcTotalScore, WHITE, 0, 374);
}
}
void BPMiniGame_JewelFlip::Tick() {
if (GameOver && GameOverTimer + 250 < TheGame->TickCount) {
GameOverTimer = TheGame->TickCount;
if (GameOverCounter == Gems.Count) {
Success();
return;
}
while (Gems[GameOverCounter]->Removed) {
++GameOverCounter;
if (GameOverCounter == Gems.Count) {
Success();
return;
}
}
TotalScore += (int)pow(2.0f, RemoveGem(Gems[GameOverCounter]));
ostringstream totalscore;
totalscore << "Score: " << TotalScore;
TheGame->AllocString(&sfcTotalScore, totalscore.str().c_str(), LARGE, 320, 37, CENTRED, true);
} else {
for (int i = 0; i < Gems.Count; ++i) {
BPMiniGame_JewelFlip_Jewel* gem = Gems[i];
if (gem->Speed != 0) {
// this gem needs to move
if (gem->Y != gem->DestY) {
if (gem->Y < gem->DestY) {
++gem->Speed;
} else {
--gem->Speed;
}
gem->Y += gem->Speed;
if (gem->Speed > 0 && gem->Y >= gem->DestY) {
gem->Y = gem->DestY;
gem->Speed = 0;
} else if (gem->Speed < 0 && gem->Y <= gem->DestY) {
gem->Y = gem->DestY;
gem->Speed = 0;
}
} else if (gem->X != gem->DestX) {
if (gem->X < gem->DestX) {
++gem->Speed;
} else {
--gem->Speed;
}
gem->X += gem->Speed;
if (gem->Speed > 0 && gem->X >= gem->DestX) {
gem->X = gem->DestX;
gem->Speed = 0;
} else if (gem->Speed < 0 && gem->X <= gem->DestX) {
gem->X = gem->DestX;
gem->Speed = 0;
}
}
}
}
}
}
bool BPMiniGame_JewelFlip::CanSwitch(BPMiniGame_JewelFlip_Jewel* gem1, BPMiniGame_JewelFlip_Jewel* gem2) {
// if these two gems were swapped, would they match anything?
if (GemMatches(gem1, gem2)) return true;
if (GemMatches(gem2, gem1)) return true;
MessageBox::Show("You can't swap these two gems, because at least one of them must end up next to a gem of the same type.", GameTitle);
SelectedGem = NULL;
return false;
}
BPMiniGame_JewelFlip_Jewel* BPMiniGame_JewelFlip::AutoSubstituteGem(int position, BPMiniGame_JewelFlip_Jewel* match, BPMiniGame_JewelFlip_Jewel* replace) {
BPMiniGame_JewelFlip_Jewel* gem = Gems[position];
if (gem == match) return replace;
return gem;
}
bool BPMiniGame_JewelFlip::GemMatches(BPMiniGame_JewelFlip_Jewel* gem1, BPMiniGame_JewelFlip_Jewel* gem2) {
int type = gem1->GemType;
int position = Gems.IndexOf(gem2);
int Row;
int Col = DivRem(position, 7, Row);
// try searching horizontally
if (Col > 0) {
// gem to the left
if (AutoSubstituteGem(position - 7, gem1, gem2)->GemType == type) return true;
}
if (Col < 5) {
// one to the right
if (AutoSubstituteGem(position + 7, gem1, gem2)->GemType == type) return true;
}
// try searching vertically
if (Row > 0) {
// gem above
if (AutoSubstituteGem(position - 1, gem1, gem2)->GemType == type) return true;
}
if (Row < 6) {
// gem below
if (AutoSubstituteGem(position + 1, gem1, gem2)->GemType == type) return true;
}
return false;
}
void BPMiniGame_JewelFlip::OnMouseDown() {
}
void BPMiniGame_JewelFlip::OnMouseMove() {
}
void BPMiniGame_JewelFlip::OnMouseUp() {
if (GameOver == true) return;
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, 110, 370, 102, 47)) {
GameOver = true;
GameOverTimer = TheGame->TickCount;
return;
}
for (int i = 0; i < Gems.Count; ++i) {
BPMiniGame_JewelFlip_Jewel* gem = Gems[i];
if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, gem->X, gem->DestY, 48, 48)) {
// we've clicked this gem!
if (gem->Removed) break;
if (SelectedGem == gem) {
// if we've selected it already, deselect it
SelectedGem = NULL;
} else {
if (SelectedGem == NULL) {
SelectedGem = gem;
} else if (GemsAreAdjacent(SelectedGem, gem)) {
SwapGems(SelectedGem, gem);
} else {
MessageBox::Show("These two gems can't be swapped because they are not next to each other.", GameTitle);
SelectedGem = NULL;
}
}
break;
}
}
}
bool BPMiniGame_JewelFlip::GemsAreAdjacent(BPMiniGame_JewelFlip_Jewel* gem1, BPMiniGame_JewelFlip_Jewel* gem2) {
return (abs(gem1->DestX - gem2->DestX) + abs(gem1->DestY - gem2->DestY)) == 50;
}
void BPMiniGame_JewelFlip::SwapGems(BPMiniGame_JewelFlip_Jewel* gem1, BPMiniGame_JewelFlip_Jewel* gem2) {
if (!CanSwitch(gem1, gem2)) return;
TheGame->PlaySound("slide");
SelectedGem = NULL;
gem1->DestX = gem2->X;
gem1->DestY = gem2->Y;
gem2->DestX = gem1->X;
gem2->DestY = gem1->Y;
if (gem1->DestX < gem1->X || gem1->DestY < gem1->Y) {
gem1->Speed = -1;
} else {
gem1->Speed = 1;
}
if (gem2->DestX < gem2->X || gem2->DestY < gem2->Y) {
gem2->Speed = -1;
} else {
gem2->Speed = 1;
}
// we need to swap positions in the Gems array, otherwise CanSwap() won't work
int Gem1Pos = Gems.IndexOf(gem1);
int Gem2Pos = Gems.IndexOf(gem2);
// remove and insert the higher-positioned gem first
if (Gem1Pos > Gem2Pos) {
Gems.RemoveAt(Gem1Pos);
Gems.RemoveAt(Gem2Pos);
Gems.Insert(Gem2Pos, gem1);
Gems.Insert(Gem1Pos, gem2);
} else {
Gems.RemoveAt(Gem2Pos);
Gems.RemoveAt(Gem1Pos);
Gems.Insert(Gem1Pos, gem2);
Gems.Insert(Gem2Pos, gem1);
}
}
int BPMiniGame_JewelFlip::RemoveGem(BPMiniGame_JewelFlip_Jewel* gem) {
// remove this gem, then remove any others that touch it
if (gem->Removed) return 0; // this has been removed already!
gem->Removed = true;
if (SelectedGem == gem) SelectedGem = NULL;
int position = Gems.IndexOf(gem);
int Row;
int Col = DivRem(position, 7, Row);
int num_removed = 1; // we've removed ourselves
// remove the one above
if (Row > 0) {
if (Gems[position - 1]->GemType == gem->GemType) num_removed += RemoveGem(Gems[position - 1]);
}
// remove the one below
if (Row < 6) {
if (Gems[position + 1]->GemType == gem->GemType) num_removed += RemoveGem(Gems[position + 1]);
}
// remove the one to the left
if (Col > 0) {
if (Gems[position - 7]->GemType == gem->GemType) num_removed += RemoveGem(Gems[position - 7]);
}
// remove the one to the right
if (Col < 5) {
if (Gems[position + 7]->GemType == gem->GemType) num_removed += RemoveGem(Gems[position + 7]);
}
return num_removed;
}
|