Changeset 303
- Timestamp:
- Jan 29, 2011, 1:04:12 AM (10 years ago)
- Location:
- trunk/monsterz
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/monsterz/board.cpp
r302 r303 36 36 int screen, board, tiles; 37 37 int click, whip; 38 Piece *pieces[8][8]; 39 Piece *grabbed; 38 39 struct Pair 40 { 41 int id; 42 Piece *piece; 43 } 44 pairs[8][8], grabbed; 45 40 46 Mash *mashes; 41 47 Emitter *emitter; … … 75 81 srand(rand() ^ time(NULL)); 76 82 77 restart: 83 /* Fill the board with a correct position */ 84 int list[8][8]; 85 do 86 { 87 for (int j = 0; j < 8; j++) 88 for (int i = 0; i < 8; i++) 89 data->pairs[i][j].id = 1 + rand() % 7; 90 } while (ListMashes(list)); 91 92 /* Spawn pieces */ 78 93 for (int j = 0; j < 8; j++) 79 94 for (int i = 0; i < 8; i++) 80 95 { 81 int id = 100 + 20 * (rand() % 7); 82 data->pieces[i][j] = new Piece(data->emitter, int2(i, j), id); 96 int2 newpos = int2(i, j + 8) * 48; 97 int id = 80 + 20 * data->pairs[i][j].id; 98 data->pairs[i][j].piece = new Piece(data->emitter, int2(i, j), id); 99 data->pairs[i][j].piece->SetPos(newpos); 100 data->pairs[i][j].piece->Move(int2(i, j) * 48); 83 101 if (j) 84 data->pieces[i][j]->SetBelow(data->pieces[i][j - 1]); 85 Ticker::Ref(data->pieces[i][j]); 86 } 87 88 int list[8][8]; 89 if (ListMashes(list)) 90 { 91 for (int j = 0; j < 8; j++) 92 for (int i = 0; i < 8; i++) 93 { 94 data->pieces[i][j]->SetBelow(NULL); 95 Ticker::Unref(data->pieces[i][j]); 96 } 97 goto restart; 98 } 102 data->pairs[i][j].piece->SetBelow(data->pairs[i][j - 1].piece); 103 Ticker::Ref(data->pairs[i][j].piece); 104 } 99 105 100 106 data->mashes = NULL; … … 114 120 if ((data->nextblink -= deltams) < 0.0f) 115 121 { 116 data->p ieces[rand() % 8][rand() % 8]->Blink();122 data->pairs[rand() % 8][rand() % 8].piece->Blink(); 117 123 data->nextblink = (float)(200 + rand() % 500); 118 124 } … … 143 149 if (x >= 0 && x < 8 * 48 && y >= 0 && y < 8 * 48) 144 150 { 145 if (data->p ieces[x / 48][y / 48]->Grab(int2(0, 0)))151 if (data->pairs[x / 48][y / 48].piece->Grab(int2(0, 0))) 146 152 { 147 153 Sampler::PlaySample(data->click); 148 data->grabbed = data->p ieces[x / 48][y / 48];154 data->grabbed = data->pairs[x / 48][y / 48]; 149 155 data->src_cell = int2(x / 48, y / 48); 150 156 data->dst_cell = int2(-1); … … 162 168 { 163 169 /* Mouse is still in the window, keep grabbing */ 164 data->grabbed ->Grab(mouse - data->mouse);165 int2 cur_pos = data->grabbed ->GetPos();170 data->grabbed.piece->Grab(mouse - data->mouse); 171 int2 cur_pos = data->grabbed.piece->GetPos(); 166 172 int2 cur_cell = (cur_pos + 24) / 48; 167 173 if (cur_cell.i < 0 || cur_cell.i >= 8 … … 179 185 } 180 186 if (data->dst_cell != int2(-1)) 181 data->p ieces[data->dst_cell.i]182 [data->dst_cell.j]->Ungrab(data->dst_cell * 48);187 data->pairs[data->dst_cell.i] 188 [data->dst_cell.j].piece->Ungrab(data->dst_cell * 48); 183 189 if (cur_cell != int2(-1)) 184 data->p ieces[cur_cell.i]185 [cur_cell.j]->Ungrab(data->src_cell * 48);190 data->pairs[cur_cell.i] 191 [cur_cell.j].piece->Ungrab(data->src_cell * 48); 186 192 data->dst_cell = cur_cell; 187 193 } … … 189 195 if (!buttons[0] || mouse.x < 0 || mouse.y < 0 190 196 || (data->src_cell * 48 191 - data->grabbed ->GetPos()).sqlen() > 100 * 100)197 - data->grabbed.piece->GetPos()).sqlen() > 100 * 100) 192 198 { 193 199 /* Mouse released, or exited window, or dragged too far. */ 194 data->grabbed ->Ungrab(data->grabbed->GetCell() * 48);200 data->grabbed.piece->Ungrab(data->grabbed.piece->GetCell() * 48); 195 201 if (data->dst_cell != int2(-1)) 196 202 Switch(data->src_cell, data->dst_cell); … … 225 231 void Board::Switch(int2 cell_a, int2 cell_b) 226 232 { 227 Piece *a = data->pieces[cell_a.i][cell_a.j];228 Piece *b = data->pieces[cell_b.i][cell_b.j];229 data->p ieces[cell_a.i][cell_a.j] = b;230 data->p ieces[cell_b.i][cell_b.j] = a;233 BoardData::Pair a = data->pairs[cell_a.i][cell_a.j]; 234 BoardData::Pair b = data->pairs[cell_b.i][cell_b.j]; 235 data->pairs[cell_a.i][cell_a.j] = b; 236 data->pairs[cell_b.i][cell_b.j] = a; 231 237 232 238 /* Check whether this is a valid move by testing all patterns. … … 236 242 if (!ListMashes(list)) 237 243 { 238 data->p ieces[cell_a.i][cell_a.j] = a;239 data->p ieces[cell_b.i][cell_b.j] = b;240 a ->Ungrab(cell_a * 48);241 b ->Ungrab(cell_b * 48);244 data->pairs[cell_a.i][cell_a.j] = a; 245 data->pairs[cell_b.i][cell_b.j] = b; 246 a.piece->Ungrab(cell_a * 48); 247 b.piece->Ungrab(cell_b * 48); 242 248 return; 243 249 } 244 250 245 251 /* Perform the swap */ 246 a ->SetCell(cell_b);247 a ->Ungrab(cell_b * 48);248 b ->SetCell(cell_a);249 b ->Ungrab(cell_a * 48);252 a.piece->SetCell(cell_b); 253 a.piece->Ungrab(cell_b * 48); 254 b.piece->SetCell(cell_a); 255 b.piece->Ungrab(cell_a * 48); 250 256 251 257 /* Swap above and below cells */ 252 258 if (cell_a.i == cell_b.i) 253 259 { 254 Piece *tmpa = a ->GetAbove();255 Piece *tmpb = b ->GetAbove();256 if (tmpb == a )257 { 258 tmpb = b ->GetBelow();259 b ->SetAbove(tmpa);260 b ->SetBelow(a);261 a ->SetBelow(tmpb);262 } 263 else /* tmpa == b */264 { 265 tmpa = a ->GetBelow();266 a ->SetAbove(tmpb);267 a ->SetBelow(b);268 b ->SetBelow(tmpa);260 Piece *tmpa = a.piece->GetAbove(); 261 Piece *tmpb = b.piece->GetAbove(); 262 if (tmpb == a.piece) 263 { 264 tmpb = b.piece->GetBelow(); 265 b.piece->SetAbove(tmpa); 266 b.piece->SetBelow(a.piece); 267 a.piece->SetBelow(tmpb); 268 } 269 else /* tmpa == b.piece */ 270 { 271 tmpa = a.piece->GetBelow(); 272 a.piece->SetAbove(tmpb); 273 a.piece->SetBelow(b.piece); 274 b.piece->SetBelow(tmpa); 269 275 } 270 276 } 271 277 else 272 278 { 273 Piece *tmpa = a ->GetAbove();274 Piece *tmpb = b ->GetAbove();275 a ->SetAbove(tmpb);276 b ->SetAbove(tmpa);277 tmpa = a ->GetBelow();278 tmpb = b ->GetBelow();279 a ->SetBelow(tmpb);280 b ->SetBelow(tmpa);279 Piece *tmpa = a.piece->GetAbove(); 280 Piece *tmpb = b.piece->GetAbove(); 281 a.piece->SetAbove(tmpb); 282 b.piece->SetAbove(tmpa); 283 tmpa = a.piece->GetBelow(); 284 tmpb = b.piece->GetBelow(); 285 a.piece->SetBelow(tmpb); 286 b.piece->SetBelow(tmpa); 281 287 } 282 288 … … 293 299 294 300 /* The mash becomes the new owner of the disappearing piece */ 295 mash->AddPiece(data->p ieces[i][j]);301 mash->AddPiece(data->pairs[i][j].piece); 296 302 297 303 #if 0 // Test for piece creation 298 304 if (list[i][j] >= 2) 299 305 { 300 Piece *old = data->pieces[i][j]; 301 int id = 100 + 20 * (rand() % 7); 302 data->pieces[i][j] = new Piece(data->emitter, int2(i, j), id); 303 data->pieces[i][j]->SetBelow(old->GetBelow()); 304 data->pieces[i][j]->SetAbove(old->GetAbove()); 305 Ticker::Ref(data->pieces[i][j]); 306 Piece *old = data->pairs[i][j].piece; 307 int id = 1 + rand() % 7; 308 data->pairs[i][j].id = id; 309 data->pairs[i][j].piece = new Piece(data->emitter, int2(i, j), 80 + 20 * id); 310 data->pairs[i][j].piece->SetBelow(old->GetBelow()); 311 data->pairs[i][j].piece->SetAbove(old->GetAbove()); 312 Ticker::Ref(data->pieces[i][j].piece); 306 313 list[i][j] = 0; 307 314 } … … 309 316 #endif 310 317 { 311 Piece *below = data->p ieces[i][7];318 Piece *below = data->pairs[i][7].piece; 312 319 313 320 /* Change coordinates for the whole column above */ 314 321 for (int j2 = j + 1; j2 < 8; j2++) 315 322 { 316 data->p ieces[i][j2 - 1] = data->pieces[i][j2];317 data->p ieces[i][j2 - 1]->SetCell(int2(i, j2 - 1));318 data->p ieces[i][j2 - 1]->Move(int2(i, j2 - 1) * 48);323 data->pairs[i][j2 - 1] = data->pairs[i][j2]; 324 data->pairs[i][j2 - 1].piece->SetCell(int2(i, j2 - 1)); 325 data->pairs[i][j2 - 1].piece->Move(int2(i, j2 - 1) * 48); 319 326 list[i][j2 - 1] = list[i][j2]; 320 327 } … … 323 330 * the board. */ 324 331 int2 newpos = int2(i * 48, below->GetPos().y + 48); 325 int id = 100 + 20 * (rand() % 7); 326 data->pieces[i][7] = new Piece(data->emitter, int2(i, 7), id); 327 data->pieces[i][7]->SetBelow(below); 328 data->pieces[i][7]->SetPos(newpos); 329 data->pieces[i][7]->Move(int2(i, 7) * 48); 330 Ticker::Ref(data->pieces[i][7]); 332 int id = 1 + rand() % 7; 333 data->pairs[i][7].id = id; 334 data->pairs[i][7].piece = new Piece(data->emitter, int2(i, 7), 80 + 20 * id); 335 data->pairs[i][7].piece->SetBelow(below); 336 data->pairs[i][7].piece->SetPos(newpos); 337 data->pairs[i][7].piece->Move(int2(i, 7) * 48); 338 Ticker::Ref(data->pairs[i][7].piece); 331 339 list[i][7] = 0; 332 340 } … … 350 358 for (int i = 0; i < 8; i++) 351 359 { 352 int id = data->p ieces[i][j]->GetId();353 354 if (i + 2 < 8 && data->p ieces[i + 1][j]->GetId()== id355 && data->p ieces[i + 2][j]->GetId()== id)360 int id = data->pairs[i][j].id; 361 362 if (i + 2 < 8 && data->pairs[i + 1][j].id == id 363 && data->pairs[i + 2][j].id == id) 356 364 { 357 365 list[i][j]++; … … 361 369 } 362 370 363 if (j + 2 < 8 && data->p ieces[i][j + 1]->GetId()== id364 && data->p ieces[i][j + 2]->GetId()== id)371 if (j + 2 < 8 && data->pairs[i][j + 1].id == id 372 && data->pairs[i][j + 2].id == id) 365 373 { 366 374 list[i][j]++; … … 380 388 for (int i = 0; i < 8; i++) 381 389 { 382 data->p ieces[i][j]->SetBelow(NULL);383 Ticker::Unref(data->p ieces[i][j]);390 data->pairs[i][j].piece->SetBelow(NULL); 391 Ticker::Unref(data->pairs[i][j].piece); 384 392 } 385 393 while (data->mashes) -
trunk/monsterz/piece.cpp
r297 r303 85 85 { 86 86 return data->pos; 87 }88 89 int Piece::GetId() const90 {91 return data->id;92 87 } 93 88 -
trunk/monsterz/piece.h
r297 r303 26 26 void SetPos(int2 pos); 27 27 int2 GetPos() const; 28 int GetId() const;29 28 30 29 void SetAbove(Piece *below);
Note: See TracChangeset
for help on using the changeset viewer.