| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1levelplayerframe.cpp |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created level.c |
| 10 | * - 1st January 2006: Created events.c from parts of level.c |
| 11 | * - 3rd February 2009: Renamed events.c to events.cpp and level.c to level.cpp, |
| 12 | * created player.cpp |
| 13 | * - 5th February 2009: Added parts of events.cpp and level.cpp to player.cpp |
| 14 | * - 18th July 2009: Created playerframe.cpp from parts of player.cpp |
| 15 | * - 24th June 2010: Renamed playerframe.cpp to levelplayerframe.cpp |
| 16 | * - 29th June 2010: Created jj2levelplayerframe.cpp from parts of |
| 17 | * levelplayerframe.cpp |
| 18 | * - 1st August 2012: Renamed levelplayerframe.cpp to jj1levelplayerframe.cpp |
| 19 | * |
| 20 | * @par Licence: |
| 21 | * Copyright (c) 2005-2013 AJ Thomson |
| 22 | * |
| 23 | * OpenJazz is distributed under the terms of |
| 24 | * the GNU General Public License, version 2.0 |
| 25 | * |
| 26 | * @par Description: |
| 27 | * Provides the once-per-frame functions of players in levels. |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "jj1bullet.h" |
| 33 | #include "event/jj1event.h" |
| 34 | #include "jj1level.h" |
| 35 | #include "jj1bird.h" |
| 36 | #include "jj1levelplayer.h" |
| 37 | |
| 38 | #include "io/controls.h" |
| 39 | #include "io/gfx/font.h" |
| 40 | #include "io/gfx/video.h" |
| 41 | #include "io/sound.h" |
| 42 | #include "util.h" |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Determine whether or not the area below the player is solid when travelling |
| 47 | * downwards. |
| 48 | * |
| 49 | * @param yOffset Vertical offset of the mask values to check |
| 50 | * |
| 51 | * @return Solidity |
| 52 | */ |
| 53 | bool JJ1LevelPlayer::checkMaskDown (fixed yOffset) { |
| 54 | |
| 55 | return level->checkMaskDown(x + PXO_ML + F1, y + yOffset) || |
| 56 | level->checkMaskDown(x + PXO_MID, y + yOffset) || |
| 57 | level->checkMaskDown(x + PXO_MR - F1, y + yOffset); |
| 58 | |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Determine whether or not the area above the player is solid when travelling |
| 64 | * upwards. |
| 65 | * |
| 66 | * @param yOffset Vertical offset of the mask values to check |
| 67 | * |
| 68 | * @return Solidity |
| 69 | */ |
| 70 | bool JJ1LevelPlayer::checkMaskUp (fixed yOffset) { |
| 71 | |
| 72 | return level->checkMaskUp(x + PXO_ML + F1, y + yOffset) || |
| 73 | level->checkMaskUp(x + PXO_MID, y + yOffset) || |
| 74 | level->checkMaskUp(x + PXO_MR - F1, y + yOffset); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Move the player to the ground's surface. |
| 81 | */ |
| 82 | void JJ1LevelPlayer::ground () { |
| 83 | |
| 84 | // If on an uphill slope, push the player upwards |
| 85 | if (checkMaskUp(0) && !checkMaskUp(-F4)) y -= F4; |
| 86 | |
| 87 | // If on a downhill slope, push the player downwards |
| 88 | if (!checkMaskUp(F4) && checkMaskUp(F8)) y += F4; |
| 89 | |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Tries to switch ammo to specific type, with fallback |
| 95 | * |
| 96 | * @param type Type |
| 97 | * @param fallback Whether to go backwards in weapon list |
| 98 | */ |
| 99 | void JJ1LevelPlayer::changeAmmo (int type, bool fallback) { |
| 100 | |
| 101 | // Type 3 is unknown, skip it and use TNT instead |
| 102 | |
| 103 | if (type == 3) player->ammoType = 4; |
| 104 | else player->ammoType = type; |
| 105 | |
| 106 | // If there is no ammo of this type, go to the next type that has ammo |
| 107 | |
| 108 | while ((player->ammoType > -1) && !player->ammo[player->ammoType]) { |
| 109 | |
| 110 | if (fallback) player->ammoType--; |
| 111 | else player->ammoType = ((player->ammoType + 2) % 6) - 1; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Respond to controls, unless the player has been killed. |
| 120 | * |
| 121 | * @param ticks Time |
| 122 | */ |
| 123 | void JJ1LevelPlayer::control (unsigned int ticks) { |
| 124 | |
| 125 | fixed speed; |
| 126 | bool platform; |
| 127 | int count; |
| 128 | |
| 129 | |
| 130 | // If the player has been killed, drop but otherwise do not move |
| 131 | if (!energy) { |
| 132 | |
| 133 | dx = 0; |
| 134 | udx = 0; |
| 135 | |
| 136 | if (flying) dy = 0; |
| 137 | else { |
| 138 | |
| 139 | if (dy < 0) dy += PYA_GRAVITY; |
| 140 | else dy = PYS_FALL; |
| 141 | |
| 142 | } |
| 143 | |
| 144 | return; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | if ((eventType == JJ1PE_FLOATH) || (eventType == JJ1PE_REPELH)) { |
| 149 | |
| 150 | speed = level->getEvent(eventX, eventY)->magnitude * |
| 151 | ((eventType == JJ1PE_FLOATH)? F10: F40); |
| 152 | |
| 153 | if (speed < -PXS_WALK || speed > PXS_WALK) { |
| 154 | |
| 155 | udx = speed; |
| 156 | |
| 157 | } else if ((speed < 0) && player->pcontrols[C_RIGHT]) { |
| 158 | |
| 159 | // Walk right, against the flow |
| 160 | |
| 161 | if (udx < speed) udx += PXA_REVERSE; |
| 162 | else if (udx < speed + PXS_WALK) udx += PXA_WALK; |
| 163 | |
| 164 | if (udx > speed + PXS_WALK) udx = speed + PXS_WALK; |
| 165 | |
| 166 | facing = true; |
| 167 | |
| 168 | } else if ((speed > 0) && player->pcontrols[C_LEFT]) { |
| 169 | |
| 170 | // Walk left, against the flow |
| 171 | |
| 172 | if (udx > 0) udx -= PXA_REVERSE; |
| 173 | else if (udx > speed - PXS_WALK) udx -= PXA_WALK; |
| 174 | |
| 175 | if (udx < speed - PXS_WALK) udx = speed - PXS_WALK; |
| 176 | |
| 177 | facing = false; |
| 178 | |
| 179 | } else { |
| 180 | |
| 181 | udx = speed; |
| 182 | |
| 183 | } |
| 184 | |
| 185 | eventType = JJ1PE_NONE; |
| 186 | |
| 187 | } else if (player->pcontrols[C_RIGHT]) { |
| 188 | |
| 189 | // Walk/run right |
| 190 | |
| 191 | if (udx < 0) udx = dx; |
| 192 | |
| 193 | if (udx < 0) udx += PXA_REVERSE; |
| 194 | else if (udx < PXS_WALK) udx += PXA_WALK; |
| 195 | else if (udx < PXS_RUN) udx += PXA_RUN; |
| 196 | |
| 197 | if (udx > PXS_RUN) udx = PXS_RUN; |
| 198 | |
| 199 | facing = true; |
| 200 | |
| 201 | } else if (player->pcontrols[C_LEFT]) { |
| 202 | |
| 203 | // Walk/run left |
| 204 | |
| 205 | if (udx > 0) udx = dx; |
| 206 | |
| 207 | if (udx > 0) udx -= PXA_REVERSE; |
| 208 | else if (udx > -PXS_WALK) udx -= PXA_WALK; |
| 209 | else if (udx > -PXS_RUN) udx -= PXA_RUN; |
| 210 | |
| 211 | if (udx < -PXS_RUN) udx = -PXS_RUN; |
| 212 | |
| 213 | facing = false; |
| 214 | |
| 215 | } else { |
| 216 | |
| 217 | // Slow down |
| 218 | |
| 219 | if (udx > 0) { |
| 220 | |
| 221 | if (udx < PXA_STOP) udx = 0; |
| 222 | else udx -= PXA_STOP; |
| 223 | |
| 224 | } else if (udx < 0) { |
| 225 | |
| 226 | if (udx > -PXA_STOP) udx = 0; |
| 227 | else udx += PXA_STOP; |
| 228 | |
| 229 | } |
| 230 | |
| 231 | } |
| 232 | |
| 233 | |
| 234 | // Check for platform event, bridge or level mask below player |
| 235 | platform = (eventType == JJ1PE_PLATFORM) || |
| 236 | checkMaskDown(F4) || |
| 237 | ((dx > 0) && level->checkMaskDown(x + PXO_ML, y + F8)) || |
| 238 | ((dx < 0) && level->checkMaskDown(x + PXO_MR, y + F8)); |
| 239 | |
| 240 | |
| 241 | if (flying) { |
| 242 | |
| 243 | if (player->pcontrols[C_UP]) { |
| 244 | |
| 245 | // Fly upwards |
| 246 | |
| 247 | if (dy > 0) dy -= PXA_REVERSE; |
| 248 | else if (dy > -PXS_WALK) dy -= PXA_WALK; |
| 249 | else if (dy > -PXS_RUN) dy -= PXA_RUN; |
| 250 | |
| 251 | } else if (player->pcontrols[C_DOWN]) { |
| 252 | |
| 253 | // Fly downwards |
| 254 | |
| 255 | if (dy < 0) dy += PXA_REVERSE; |
| 256 | else if (dy < PXS_WALK) dy += PXA_WALK; |
| 257 | else if (dy < PXS_RUN) dy += PXA_RUN; |
| 258 | |
| 259 | } else { |
| 260 | |
| 261 | // Slow down |
| 262 | |
| 263 | if (dy > 0) { |
| 264 | |
| 265 | if (dy < PXA_STOP) dy = 0; |
| 266 | else dy -= PXA_STOP; |
| 267 | |
| 268 | } else if (dy < 0) { |
| 269 | |
| 270 | if (dy > -PXA_STOP) dy = 0; |
| 271 | else dy += PXA_STOP; |
| 272 | |
| 273 | } |
| 274 | |
| 275 | } |
| 276 | |
| 277 | if (eventType == JJ1PE_SPRING) dy = level->getEvent(eventX, eventY)->multiA * -F20; |
| 278 | else if (eventType == JJ1PE_FLOAT) dy = PYS_JUMP; |
| 279 | |
| 280 | if (dy < -PXS_RUN) dy = -PXS_RUN; |
| 281 | else if (dy > PXS_RUN) dy = PXS_RUN; |
| 282 | |
| 283 | } else if (y + PYO_MID > level->getWaterLevel() + F8) { |
| 284 | |
| 285 | if (player->pcontrols[C_SWIM]) { |
| 286 | |
| 287 | // Swim upwards |
| 288 | |
| 289 | if (dy > 0) dy -= PXA_REVERSE; |
| 290 | else if (dy > -PXS_WALK) dy -= PXA_WALK; |
| 291 | else if (dy > -PXS_RUN) dy -= PXA_RUN; |
| 292 | |
| 293 | // Prepare to jump upon leaving the water |
| 294 | |
| 295 | if (!level->checkMaskUp(x + PXO_MID, y - F36)) { |
| 296 | |
| 297 | targetY = y - jumpHeight; |
| 298 | |
| 299 | if (dx < 0) targetY += dx >> 4; |
| 300 | else if (dx > 0) targetY -= dx >> 4; |
| 301 | |
| 302 | eventType = JJ1PE_NONE; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | } else if (player->pcontrols[C_DOWN]) { |
| 307 | |
| 308 | // Swim downwards |
| 309 | |
| 310 | if (dy < 0) dy += PXA_REVERSE; |
| 311 | else if (dy < PXS_WALK) dy += PXA_WALK; |
| 312 | else if (dy < PXS_RUN) dy += PXA_RUN; |
| 313 | |
| 314 | } else { |
| 315 | |
| 316 | // Sink |
| 317 | |
| 318 | dy += PYA_SINK; |
| 319 | if (dy > PYS_SINK) dy = PYS_SINK; |
| 320 | |
| 321 | } |
| 322 | |
| 323 | if (dy < -PXS_RUN) dy = -PXS_RUN; |
| 324 | else if (dy > PXS_RUN) dy = PXS_RUN; |
| 325 | |
| 326 | } else { |
| 327 | |
| 328 | if (platform && player->pcontrols[C_JUMP] && |
| 329 | !level->checkMaskUp(x + PXO_MID, y - F36)) { |
| 330 | |
| 331 | // Jump |
| 332 | |
| 333 | targetY = y - jumpHeight; |
| 334 | |
| 335 | // Increase jump height if walking/running |
| 336 | if (dx < 0) targetY += dx >> 3; |
| 337 | else if (dx > 0) targetY -= dx >> 3; |
| 338 | |
| 339 | eventType = JJ1PE_NONE; |
| 340 | |
| 341 | playSound(level->getLevelSound(LSND_JUMP)); |
| 342 | |
| 343 | } else if (((eventType == JJ1PE_NONE) || (eventType == JJ1PE_PLATFORM)) && |
| 344 | !player->pcontrols[C_JUMP]) { |
| 345 | |
| 346 | // Stop jumping |
| 347 | |
| 348 | targetY = TTOF(LH); |
| 349 | |
| 350 | } |
| 351 | |
| 352 | if ((eventType == JJ1PE_FLOAT) && |
| 353 | player->pcontrols[C_DOWN]) { |
| 354 | |
| 355 | // Prevent floating |
| 356 | |
| 357 | dy = PYS_FALL; |
| 358 | |
| 359 | } else if (y >= targetY) { |
| 360 | |
| 361 | if ((eventType == JJ1PE_FLOAT) && (dy > PYS_JUMP)) { |
| 362 | |
| 363 | dy -= F16 + F8; |
| 364 | |
| 365 | } else { |
| 366 | |
| 367 | dy = (targetY - y - F64) * 4; |
| 368 | |
| 369 | } |
| 370 | |
| 371 | // Spring/float up speed limit |
| 372 | if ((eventType == JJ1PE_SPRING) || (eventType == JJ1PE_REPELUP)) { |
| 373 | |
| 374 | speed = level->getEvent(eventX, eventY)->multiA * -F20; |
| 375 | |
| 376 | if (speed >= 0) speed = PYS_JUMP; |
| 377 | |
| 378 | if (dy < speed) dy = speed; |
| 379 | |
| 380 | } |
| 381 | |
| 382 | // Avoid jumping too fast, unless caused by an event |
| 383 | if ((eventType == JJ1PE_NONE) && (dy < PYS_JUMP)) dy = PYS_JUMP; |
| 384 | |
| 385 | } else if (eventType != JJ1PE_PLATFORM) { |
| 386 | |
| 387 | // Fall under gravity |
| 388 | if (dy < 0) dy += PYA_GRAVITY; |
| 389 | else dy = PYS_FALL; |
| 390 | |
| 391 | } |
| 392 | |
| 393 | // Don't descend through platforms |
| 394 | if ((dy > 0) && (eventType == JJ1PE_PLATFORM)) dy = 0; |
| 395 | |
| 396 | if (platform && !lookTime) { |
| 397 | |
| 398 | // If requested, look up or down |
| 399 | if (player->pcontrols[C_UP]) lookTime = -ticks; |
| 400 | else if (player->pcontrols[C_DOWN]) lookTime = ticks; |
| 401 | |
| 402 | } |
| 403 | |
| 404 | // Stop looking if there is no platform or the control has been released |
| 405 | if (!platform || |
| 406 | (!player->pcontrols[C_UP] && (lookTime < 0)) || |
| 407 | (!player->pcontrols[C_DOWN] && (lookTime > 0))) lookTime = 0; |
| 408 | |
| 409 | } |
| 410 | |
| 411 | // If there is an obstacle above, stop rising |
| 412 | if ((targetY < y) && checkMaskUp(PYO_TOP - F4)) { |
| 413 | |
| 414 | targetY = TTOF(LH); |
| 415 | if (dy < 0) dy = 0; |
| 416 | |
| 417 | if (eventType != JJ1PE_PLATFORM) eventType = JJ1PE_NONE; |
| 418 | |
| 419 | } |
| 420 | |
| 421 | |
| 422 | // Handle firing |
| 423 | if (player->pcontrols[C_FIRE]) { |
| 424 | |
| 425 | if (ticks > fireTime) { |
| 426 | |
| 427 | if (player->ammoType == 4) { |
| 428 | |
| 429 | JJ1Event* event; |
| 430 | |
| 431 | // TNT |
| 432 | |
| 433 | event = level->getEvents(); |
| 434 | |
| 435 | while (event) { |
| 436 | |
| 437 | // If the event is within range, hit it |
| 438 | if (event->overlap(x - F160, y - F100, 2 * F160, 2 * F100)) { |
| 439 | |
| 440 | event->hit(this, 2, ticks); |
| 441 | |
| 442 | } |
| 443 | |
| 444 | event = event->getNext(); |
| 445 | |
| 446 | } |
| 447 | |
| 448 | // Red flash |
| 449 | level->flash(255, 0, 0, T_TNT); |
| 450 | |
| 451 | } else { |
| 452 | |
| 453 | // Horizontal bullet position is taken from the shooting animation |
| 454 | animType = facing? PA_RSHOOT: PA_LSHOOT; |
| 455 | |
| 456 | level->createBullet(this, |
| 457 | 0, |
| 458 | 0, |
| 459 | x + anims[animType]->getShootX(), |
| 460 | y - ITOF(lookTime? 5: 10), |
| 461 | player->getAmmoType() + 1, |
| 462 | facing, |
| 463 | ticks); |
| 464 | |
| 465 | } |
| 466 | |
| 467 | // Set when the next bullet can be fired and length of shoot animation |
| 468 | if (player->fireSpeed) { |
| 469 | |
| 470 | fireTime = ticks + (1000 / player->fireSpeed); |
| 471 | fireAnimTime = ticks + 1000; |
| 472 | |
| 473 | } else { |
| 474 | |
| 475 | fireTime = 0x7FFFFFFF; |
| 476 | fireAnimTime = ticks + T_FIRING; |
| 477 | |
| 478 | } |
| 479 | |
| 480 | // Remove the bullet from the arsenal |
| 481 | if (player->ammoType != -1) player->ammo[player->ammoType]--; |
| 482 | |
| 483 | /* If the current ammo type has been exhausted or TNT has been used, |
| 484 | use the previous non-exhausted ammo type */ |
| 485 | while (((player->ammoType > -1) && !player->ammo[player->ammoType]) || (player->ammoType == 4)) player->ammoType--; |
| 486 | |
| 487 | } |
| 488 | |
| 489 | } else fireTime = 0; |
| 490 | |
| 491 | |
| 492 | // Check for a change in ammo |
| 493 | if (player->pcontrols[C_CHANGE]) { |
| 494 | |
| 495 | if (player == localPlayer) controls.release(C_CHANGE); |
| 496 | |
| 497 | changeAmmo(((player->ammoType + 2) % 6) - 1); |
| 498 | |
| 499 | } |
| 500 | for (count = 0; count < 5; count++) { |
| 501 | |
| 502 | if (controls.getState(count + C_BLASTER)) { |
| 503 | |
| 504 | if (player == localPlayer) controls.release(count + C_BLASTER); |
| 505 | |
| 506 | changeAmmo(count - 1, true); |
| 507 | |
| 508 | } |
| 509 | |
| 510 | } |
| 511 | |
| 512 | |
| 513 | // Deal with the bird |
| 514 | |
| 515 | if (birds) birds = birds->step(ticks); |
| 516 | |
| 517 | // Replay sound effect before invincibility wears off |
| 518 | |
| 519 | if ((reaction == PR_INVINCIBLE) && (reactionTime < ticks + 2200)) { |
| 520 | // FIXME: This is hardcoded for now |
| 521 | if (!isSoundPlaying(SE::INVULN)) playSound(SE::INVULN); |
| 522 | } |
| 523 | |
| 524 | } |
| 525 | |
| 526 | |
| 527 | /** |
| 528 | * Move the player. |
| 529 | * |
| 530 | * @param ticks Time |
| 531 | */ |
| 532 | void JJ1LevelPlayer::move (unsigned int ticks) { |
| 533 | |
| 534 | fixed pdx, pdy; |
| 535 | bool grounded = false; |
| 536 | int count; |
| 537 | |
| 538 | if (warpTime && (ticks > warpTime)) { |
| 539 | |
| 540 | x = TTOF(warpX); |
| 541 | y = TTOF(warpY + 1); |
| 542 | warpTime = 0; |
| 543 | |
| 544 | } |
| 545 | |
| 546 | // Apply as much of the trajectory as possible, without going into the |
| 547 | // scenery |
| 548 | |
| 549 | if (fastFeetTime > ticks) { |
| 550 | |
| 551 | pdx = (udx * 3) >> 7; |
| 552 | pdy = (dy * 3) >> 7; |
| 553 | |
| 554 | // reset music speed before running shoes wear off |
| 555 | |
| 556 | if (fastFeetTime - ticks < T_FASTFEET >> 2) |
| 557 | setMusicTempo(MusicTempo::NORMAL); |
| 558 | |
| 559 | } else { |
| 560 | |
| 561 | pdx = udx >> 6; |
| 562 | pdy = dy >> 6; |
| 563 | |
| 564 | } |
| 565 | |
| 566 | // First for the vertical component of the trajectory |
| 567 | |
| 568 | if (pdy < 0) { |
| 569 | |
| 570 | // Moving up |
| 571 | |
| 572 | count = (-pdy) >> 12; |
| 573 | |
| 574 | while (count > 0) { |
| 575 | |
| 576 | if (checkMaskUp(PYO_TOP - F4)) { |
| 577 | |
| 578 | y &= ~4095; |
| 579 | dy = 0; |
| 580 | |
| 581 | break; |
| 582 | |
| 583 | } |
| 584 | |
| 585 | y -= F4; |
| 586 | count--; |
| 587 | |
| 588 | } |
| 589 | |
| 590 | pdy = (-pdy) & 4095; |
| 591 | |
| 592 | if (checkMaskUp(PYO_TOP - pdy)) { |
| 593 | |
| 594 | y &= ~4095; |
| 595 | dy = 0; |
| 596 | |
| 597 | } else y -= pdy; |
| 598 | |
| 599 | } else { |
| 600 | |
| 601 | if (pdy > 0) { |
| 602 | |
| 603 | // Moving down |
| 604 | |
| 605 | count = pdy >> 12; |
| 606 | |
| 607 | while (count > 0) { |
| 608 | |
| 609 | if (checkMaskDown(F4)) { |
| 610 | |
| 611 | y |= 4095; |
| 612 | dy = 0; |
| 613 | |
| 614 | break; |
| 615 | |
| 616 | } |
| 617 | |
| 618 | y += F4; |
| 619 | count--; |
| 620 | |
| 621 | } |
| 622 | |
| 623 | pdy &= 4095; |
| 624 | |
| 625 | if (checkMaskDown(pdy)) { |
| 626 | |
| 627 | y |= 4095; |
| 628 | dy = 0; |
| 629 | |
| 630 | } else y += pdy; |
| 631 | |
| 632 | } |
| 633 | |
| 634 | if (!flying) { |
| 635 | |
| 636 | if (checkMaskDown(0)) { |
| 637 | |
| 638 | // In the ground, so move up |
| 639 | if (y >= 4096) y = (y - 4096) | 4095; |
| 640 | grounded = true; |
| 641 | |
| 642 | } else if (checkMaskDown(1)) { |
| 643 | |
| 644 | // On the ground |
| 645 | grounded = true; |
| 646 | |
| 647 | } |
| 648 | |
| 649 | } |
| 650 | |
| 651 | } |
| 652 | |
| 653 | |
| 654 | |
| 655 | // Then for the horizontal component of the trajectory |
| 656 | |
| 657 | dx = udx; |
| 658 | |
| 659 | if (pdx < 0) { |
| 660 | |
| 661 | // Moving left |
| 662 | |
| 663 | count = (-pdx) >> 12; |
| 664 | |
| 665 | while (count > 0) { |
| 666 | |
| 667 | // If there is an obstacle, stop |
| 668 | if (level->checkMaskUp(x + PXO_L - F4, y + PYO_MID)) { |
| 669 | |
| 670 | x &= ~4095; |
| 671 | dx = 0; |
| 672 | |
| 673 | if (udx < -PXS_RUN) udx = -PXS_RUN; |
| 674 | |
| 675 | break; |
| 676 | |
| 677 | } |
| 678 | |
| 679 | x -= F4; |
| 680 | count--; |
| 681 | |
| 682 | if (grounded) ground(); |
| 683 | |
| 684 | } |
| 685 | |
| 686 | pdx = (-pdx) & 4095; |
| 687 | |
| 688 | if (level->checkMaskUp(x + PXO_L - pdx, y + PYO_MID)) { |
| 689 | |
| 690 | x &= ~4095; |
| 691 | dx = 0; |
| 692 | |
| 693 | if (udx < -PXS_RUN) udx = -PXS_RUN; |
| 694 | |
| 695 | } else x -= pdx; |
| 696 | |
| 697 | if (grounded) ground(); |
| 698 | |
| 699 | } else if (pdx > 0) { |
| 700 | |
| 701 | // Moving right |
| 702 | |
| 703 | count = pdx >> 12; |
| 704 | |
| 705 | while (count > 0) { |
| 706 | |
| 707 | // If there is an obstacle, stop |
| 708 | if (level->checkMaskUp(x + PXO_R + F4, y + PYO_MID)) { |
| 709 | |
| 710 | x |= 4095; |
| 711 | dx = 0; |
| 712 | |
| 713 | if (udx > PXS_RUN) udx = PXS_RUN; |
| 714 | |
| 715 | break; |
| 716 | |
| 717 | } |
| 718 | |
| 719 | x += F4; |
| 720 | count--; |
| 721 | |
| 722 | if (grounded) ground(); |
| 723 | |
| 724 | } |
| 725 | |
| 726 | pdx &= 4095; |
| 727 | |
| 728 | if (level->checkMaskUp(x + PXO_R + pdx, y + PYO_MID)) { |
| 729 | |
| 730 | x |= 4095; |
| 731 | dx = 0; |
| 732 | |
| 733 | if (udx > PXS_RUN) udx = PXS_RUN; |
| 734 | |
| 735 | } else x += pdx; |
| 736 | |
| 737 | if (grounded) ground(); |
| 738 | |
| 739 | } |
| 740 | |
| 741 | |
| 742 | // If the target has been reached, stop rising |
| 743 | if (y <= targetY) { |
| 744 | |
| 745 | targetY = TTOF(LH); |
| 746 | |
| 747 | if ((eventType != JJ1PE_PLATFORM) && |
| 748 | (eventType != JJ1PE_FLOATH) && |
| 749 | (eventType != JJ1PE_REPELH)) eventType = JJ1PE_NONE; |
| 750 | |
| 751 | } |
| 752 | |
| 753 | |
| 754 | if (level->getStage() != LS_END) { |
| 755 | |
| 756 | // If the player has hit the bottom of the level, kill |
| 757 | if (y + F4 > TTOF(LH)) kill(NULL, ticks); |
| 758 | |
| 759 | // Handle spikes |
| 760 | if (level->checkSpikes(x + PXO_MID, y + PYO_TOP - F4) || |
| 761 | level->checkSpikes(x + PXO_MID, y + F4) || |
| 762 | level->checkSpikes(x + PXO_L - F4, y + PYO_MID) || |
| 763 | level->checkSpikes(x + PXO_R + F4, y + PYO_MID)) hit(NULL, ticks); |
| 764 | |
| 765 | } |
| 766 | |
| 767 | |
| 768 | // Choose animation |
| 769 | |
| 770 | if (!energy) |
| 771 | animType = facing? PA_RDIE: PA_LDIE; |
| 772 | |
| 773 | else if ((reaction == PR_HURT) && (reactionTime - ticks > PRT_HURT - PRT_HURTANIM)) |
| 774 | animType = facing? PA_RHURT: PA_LHURT; |
| 775 | |
| 776 | else if (y + PYO_MID > level->getWaterLevel() + F8) |
| 777 | animType = facing? PA_RSWIM: PA_LSWIM; |
| 778 | |
| 779 | else if (flying) animType = facing? PA_RBOARD: PA_LBOARD; |
| 780 | |
| 781 | else if (dy < 0) { |
| 782 | |
| 783 | if (eventType == JJ1PE_SPRING) animType = facing? PA_RSPRING: PA_LSPRING; |
| 784 | else animType = facing? PA_RJUMP: PA_LJUMP; |
| 785 | |
| 786 | } else if (checkMaskDown(F4) || (eventType == JJ1PE_PLATFORM)) { |
| 787 | |
| 788 | if (udx) { |
| 789 | |
| 790 | if (udx <= -PXS_RUN) animType = PA_LRUN; |
| 791 | else if (udx >= PXS_RUN) animType = PA_RRUN; |
| 792 | else if ((udx < 0) && facing) animType = PA_LSTOP; |
| 793 | else if ((udx > 0) && !facing) animType = PA_RSTOP; |
| 794 | else animType = facing? PA_RWALK: PA_LWALK; |
| 795 | |
| 796 | } else if ((player->pcontrols[C_FIRE]) && (ticks < fireAnimTime)) |
| 797 | animType = facing? PA_RSHOOT: PA_LSHOOT; |
| 798 | |
| 799 | else if ((lookTime < 0) && ((int)ticks > 1000 - lookTime)) |
| 800 | animType = PA_LOOKUP; |
| 801 | |
| 802 | else if (lookTime > 0) { |
| 803 | |
| 804 | if ((int)ticks < 1000 + lookTime) animType = facing? PA_RCROUCH: PA_LCROUCH; |
| 805 | else animType = PA_LOOKDOWN; |
| 806 | |
| 807 | } |
| 808 | |
| 809 | else if (!level->checkMaskDown(x + PXO_ML, y + F20) && |
| 810 | !level->checkMaskDown(x + PXO_L, y + F2) && |
| 811 | (eventType != JJ1PE_PLATFORM)) |
| 812 | animType = facing? PA_RSTAND: PA_LEDGE; |
| 813 | |
| 814 | else if (!level->checkMaskDown(x + PXO_MR, y + F20) && |
| 815 | !level->checkMaskDown(x + PXO_R, y + F2) && |
| 816 | (eventType != JJ1PE_PLATFORM)) |
| 817 | animType = facing? PA_REDGE: PA_LSTAND; |
| 818 | |
| 819 | else |
| 820 | animType = facing? PA_RSTAND: PA_LSTAND; |
| 821 | |
| 822 | } else animType = facing? PA_RFALL: PA_LFALL; |
| 823 | |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /** |
| 828 | * Calculate viewport. |
| 829 | * |
| 830 | * @param ticks Time |
| 831 | * @param mspf Ticks per frame |
| 832 | * @param change Time since last step |
| 833 | */ |
| 834 | void JJ1LevelPlayer::view (unsigned int ticks, int mspf, int change) { |
| 835 | |
| 836 | int oldViewX, oldViewY, speed; |
| 837 | |
| 838 | // Record old viewport position for applying lag |
| 839 | oldViewX = viewX; |
| 840 | oldViewY = viewY; |
| 841 | |
| 842 | // Find new position |
| 843 | |
| 844 | viewX = x + ((dx * change) >> 10) + F8 - (canvasW << 9); |
| 845 | viewY = y + ((dy * change) >> 10) - F24 - ((canvasH - 33) << 9); |
| 846 | |
| 847 | if ((lookTime > 0) && ((int)ticks > 1000 + lookTime)) { |
| 848 | |
| 849 | // Look down |
| 850 | if ((int)ticks < 2000 + lookTime) viewY += 64 * (ticks - (1000 + lookTime)); |
| 851 | else viewY += F64; |
| 852 | |
| 853 | } else if ((lookTime < 0) && ((int)ticks > 1000 - lookTime)) { |
| 854 | |
| 855 | // Look up |
| 856 | if ((int)ticks < 2000 - lookTime) viewY -= 64 * (ticks - (1000 - lookTime)); |
| 857 | else viewY -= F64; |
| 858 | |
| 859 | } |
| 860 | |
| 861 | |
| 862 | // Apply lag proportional to player "speed" |
| 863 | speed = ((dx >= 0? dx: -dx) + (dy >= 0? dy: -dy)) >> 14; |
| 864 | |
| 865 | if (speed && (mspf < speed)) { |
| 866 | |
| 867 | viewX = ((oldViewX * (speed - mspf)) + (viewX * mspf)) / speed; |
| 868 | viewY = ((oldViewY * (speed - mspf)) + (viewY * mspf)) / speed; |
| 869 | |
| 870 | } |
| 871 | |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Draw the player. |
| 876 | * |
| 877 | * @param ticks Time |
| 878 | * @param change Time since last step |
| 879 | */ |
| 880 | void JJ1LevelPlayer::draw (unsigned int ticks, int change) { |
| 881 | |
| 882 | Anim *an; |
| 883 | int frame; |
| 884 | fixed drawX, drawY; |
| 885 | fixed xOffset, yOffset; |
| 886 | fixed angle; |
| 887 | |
| 888 | // The current frame for animations |
| 889 | if (reaction == PR_KILLED) frame = (ticks + PRT_KILLED - reactionTime) / 75; |
| 890 | else frame = ticks / 75; |
| 891 | |
| 892 | |
| 893 | // Get position |
| 894 | |
| 895 | drawX = getDrawX(change); |
| 896 | drawY = getDrawY(change); |
| 897 | |
| 898 | |
| 899 | // Choose sprite |
| 900 | |
| 901 | an = anims[animType]; |
| 902 | an->setFrame(frame, reaction != PR_KILLED); |
| 903 | |
| 904 | |
| 905 | // Show the player |
| 906 | |
| 907 | // Flash red if hurt, otherwise use player colour |
| 908 | if ((reaction == PR_HURT) && (!((ticks / 30) & 3))) |
| 909 | an->flashPalette(36); |
| 910 | |
| 911 | else { |
| 912 | |
| 913 | an->setPalette(palette, 23, 41); |
| 914 | an->setPalette(palette, 88, 8); |
| 915 | |
| 916 | } |
| 917 | |
| 918 | |
| 919 | // Draw "motion blur" |
| 920 | if (fastFeetTime > ticks) an->draw(drawX - (dx >> 6), drawY); |
| 921 | |
| 922 | // Draw player |
| 923 | an->draw(drawX, drawY); |
| 924 | |
| 925 | |
| 926 | // Remove red flash or player colour from sprite |
| 927 | an->restorePalette(); |
| 928 | |
| 929 | |
| 930 | // Uncomment the following to see the area of the player |
| 931 | /*drawRect(FTOI(drawX + PXO_L), |
| 932 | FTOI(drawY + PYO_TOP), |
| 933 | FTOI(PXO_R - PXO_L), |
| 934 | FTOI(-PYO_TOP), 89); |
| 935 | drawRect(FTOI(drawX + PXO_ML), |
| 936 | FTOI(drawY + PYO_TOP), |
| 937 | FTOI(PXO_MR - PXO_ML), |
| 938 | FTOI(-PYO_TOP), 88);*/ |
| 939 | |
| 940 | // Uncomment the following to show the tile containing the player's base |
| 941 | //drawRect(FTOI(TTOF(FTOT(x + PXO_MID)) - viewX), FTOI(TTOF(FTOT(y)) - viewY), 32, 32, 48); |
| 942 | |
| 943 | // Uncomment the following to show the player's event tile |
| 944 | // if (eventType != JJ1PE_NONE) drawRect(FTOI(TTOF(eventX) - viewX), FTOI(TTOF(eventY) - viewY), 32, 32, 89); |
| 945 | |
| 946 | |
| 947 | if (flying) { |
| 948 | |
| 949 | an = level->getLevelAnim(facing? LA_RBOARD: LA_LBOARD); |
| 950 | |
| 951 | an->setFrame(ticks >> 4, true); |
| 952 | an->draw(drawX, drawY + F10); |
| 953 | |
| 954 | } |
| 955 | |
| 956 | if (reaction == PR_INVINCIBLE) { |
| 957 | |
| 958 | // Show invincibility stars |
| 959 | |
| 960 | xOffset = fSin(ticks * 2) * 12; |
| 961 | yOffset = fCos(ticks * 2) * 12; |
| 962 | |
| 963 | an = level->getMiscAnim(MA_SPARKLE); |
| 964 | |
| 965 | an->setFrame(frame, true); |
| 966 | an->draw(drawX + PXO_MID + xOffset, drawY + PYO_MID + yOffset); |
| 967 | |
| 968 | an->setFrame(frame + 1, true); |
| 969 | an->draw(drawX + PXO_MID - xOffset, drawY + PYO_MID - yOffset); |
| 970 | |
| 971 | an->setFrame(frame + 2, true); |
| 972 | an->draw(drawX + PXO_MID + yOffset, drawY + PYO_MID + xOffset); |
| 973 | |
| 974 | an->setFrame(frame + 3, true); |
| 975 | an->draw(drawX + PXO_MID - yOffset, drawY + PYO_MID - xOffset); |
| 976 | |
| 977 | } else if (shield > 1) { |
| 978 | |
| 979 | // Show the 4-hit shield |
| 980 | |
| 981 | an = level->getLevelAnim(LA_4SHIELD); |
| 982 | |
| 983 | if (shield == 4) { |
| 984 | |
| 985 | // triangle based |
| 986 | |
| 987 | for (int i = 0; i < 3; i++) { |
| 988 | |
| 989 | angle = -(i * 341 + ticks); |
| 990 | |
| 991 | xOffset = fSin(angle) * 20; |
| 992 | yOffset = fCos(angle) * 20; |
| 993 | |
| 994 | if (!facing) yOffset = -yOffset; |
| 995 | |
| 996 | an->draw(drawX + xOffset, drawY + PYO_TOP + yOffset); |
| 997 | |
| 998 | } |
| 999 | |
| 1000 | } else { |
| 1001 | |
| 1002 | // rectangle based |
| 1003 | |
| 1004 | xOffset = fCos(ticks) * 20; |
| 1005 | yOffset = fSin(ticks) * 20; |
| 1006 | |
| 1007 | if (!facing) yOffset = -yOffset; |
| 1008 | |
| 1009 | an->draw(drawX + xOffset, drawY + PYO_TOP + yOffset); |
| 1010 | |
| 1011 | if (shield > 2) an->draw(drawX - xOffset, drawY + PYO_TOP - yOffset); |
| 1012 | |
| 1013 | if (shield > 4) { |
| 1014 | |
| 1015 | an->draw(drawX + yOffset, drawY + PYO_TOP - xOffset); |
| 1016 | an->draw(drawX - yOffset, drawY + PYO_TOP + xOffset); |
| 1017 | |
| 1018 | } |
| 1019 | |
| 1020 | } |
| 1021 | |
| 1022 | } else if (shield) { |
| 1023 | |
| 1024 | // Show the 1-hit shield |
| 1025 | |
| 1026 | xOffset = fCos(ticks) * 20; |
| 1027 | yOffset = fSin(ticks) * 20; |
| 1028 | |
| 1029 | an = level->getLevelAnim(LA_1SHIELD); |
| 1030 | |
| 1031 | if (!facing) yOffset = -yOffset; |
| 1032 | |
| 1033 | an->draw(drawX + xOffset, drawY + PYO_TOP + yOffset); |
| 1034 | an->draw(drawX - xOffset, drawY + PYO_TOP - yOffset); |
| 1035 | |
| 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | // Show the bird |
| 1040 | if (birds) birds->draw(ticks, change); |
| 1041 | |
| 1042 | |
| 1043 | // Show the player's name |
| 1044 | if (nPlayers > 1) |
| 1045 | panelBigFont->showString(player->name, |
| 1046 | FTOI(drawX + PXO_MID) - (panelBigFont->getStringWidth(player->name) >> 1), |
| 1047 | FTOI(drawY - F32 - F16)); |
| 1048 | |
| 1049 | } |