| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj2levelplayerframe.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 | * |
| 19 | * @par Licence: |
| 20 | * Copyright (c) 2005-2013 AJ Thomson |
| 21 | * |
| 22 | * OpenJazz is distributed under the terms of |
| 23 | * the GNU General Public License, version 2.0 |
| 24 | * |
| 25 | * @par Description: |
| 26 | * Provides the once-per-frame functions of players in levels. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | #include "event/jj2event.h" |
| 32 | #include "jj2level.h" |
| 33 | #include "jj2levelplayer.h" |
| 34 | |
| 35 | #include "io/controls.h" |
| 36 | #include "io/gfx/font.h" |
| 37 | #include "io/gfx/video.h" |
| 38 | #include "io/sound.h" |
| 39 | #include "util.h" |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Determine whether or not the area below the player is solid when travelling |
| 44 | * downwards. |
| 45 | * |
| 46 | * @param yOffset Vertical offset of the mask values to check |
| 47 | * |
| 48 | * @return Solidity |
| 49 | */ |
| 50 | bool JJ2LevelPlayer::checkMaskDown (fixed yOffset, bool drop) { |
| 51 | |
| 52 | return jj2Level->checkMaskDown(x + JJ2PXO_ML, y + yOffset, drop) || |
| 53 | jj2Level->checkMaskDown(x + JJ2PXO_MID, y + yOffset, drop) || |
| 54 | jj2Level->checkMaskDown(x + JJ2PXO_MR, y + yOffset, drop); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Determine whether or not the area above the player is solid when travelling |
| 61 | * upwards. |
| 62 | * |
| 63 | * @param yOffset Vertical offset of the mask values to check |
| 64 | * |
| 65 | * @return Solidity |
| 66 | */ |
| 67 | bool JJ2LevelPlayer::checkMaskUp (fixed yOffset) { |
| 68 | |
| 69 | return jj2Level->checkMaskUp(x + JJ2PXO_ML, y + yOffset) || |
| 70 | jj2Level->checkMaskUp(x + JJ2PXO_MID, y + yOffset) || |
| 71 | jj2Level->checkMaskUp(x + JJ2PXO_MR, y + yOffset); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Move the player to the ground's surface. |
| 78 | */ |
| 79 | void JJ2LevelPlayer::ground () { |
| 80 | |
| 81 | // If on an uphill slope, push the player upwards |
| 82 | if (checkMaskUp(0) && !checkMaskUp(-F1)) y -= F1; |
| 83 | |
| 84 | // If on a downhill slope, push the player downwards |
| 85 | if (!checkMaskUp(F1) && checkMaskUp(F2)) y += F1; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Respond to tile modifier events. |
| 92 | * |
| 93 | * @param nextMod The modifier event |
| 94 | * @param ticks Time |
| 95 | */ |
| 96 | void JJ2LevelPlayer::modify (JJ2Modifier* nextMod, unsigned int ticks) { |
| 97 | |
| 98 | switch (nextMod->type) { |
| 99 | |
| 100 | case 2: // Spikes |
| 101 | |
| 102 | if (jj2Level->checkMaskDown(x + JJ2PXO_MID, y + F1, false)) |
| 103 | hit(NULL, ticks); |
| 104 | |
| 105 | break; |
| 106 | |
| 107 | case 4: // Hook |
| 108 | |
| 109 | dx = 0; |
| 110 | |
| 111 | break; |
| 112 | |
| 113 | case 6: // H-pole |
| 114 | |
| 115 | if (!stopTime) { |
| 116 | |
| 117 | // Catch player |
| 118 | if (nextMod != mod) { |
| 119 | |
| 120 | centreX(); |
| 121 | centreY(); |
| 122 | stopTime = ticks + 1000; |
| 123 | |
| 124 | } |
| 125 | |
| 126 | } else if (ticks > stopTime) { |
| 127 | |
| 128 | dx = (dx > 0) ? JJ2PXS_POLE: -JJ2PXS_POLE; |
| 129 | stopTime = 0; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | break; |
| 134 | |
| 135 | case 7: // V-pole |
| 136 | |
| 137 | if (!stopTime) { |
| 138 | |
| 139 | // Catch player |
| 140 | if (nextMod != mod) { |
| 141 | |
| 142 | centreX(); |
| 143 | centreY(); |
| 144 | stopTime = ticks + 1000; |
| 145 | |
| 146 | } |
| 147 | |
| 148 | } else if (ticks > stopTime) { |
| 149 | |
| 150 | if (dy < 0) { |
| 151 | |
| 152 | throwY = y - TTOF(16); |
| 153 | dy = -JJ2PYS_POLE; |
| 154 | |
| 155 | } else { |
| 156 | |
| 157 | dy = JJ2PYS_POLE; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | stopTime = 0; |
| 162 | |
| 163 | } |
| 164 | |
| 165 | break; |
| 166 | |
| 167 | case 8: // Fly off |
| 168 | |
| 169 | floating = false; |
| 170 | |
| 171 | break; |
| 172 | |
| 173 | case 17: // End of level |
| 174 | case 18: // End of level |
| 175 | |
| 176 | if (!energy) return; |
| 177 | |
| 178 | if (!player->endOfLevel(FTOT(x + JJ2PXO_MID), FTOT(y + JJ2PYO_MID))) return; |
| 179 | |
| 180 | jj2Level->setStage(LS_END); |
| 181 | |
| 182 | break; |
| 183 | |
| 184 | case 206: // Sucker tube |
| 185 | |
| 186 | dx = (nextMod->properties & 0x40) ? -((nextMod->properties << 15) & 0x1F8000): ((nextMod->properties << 15) & 0x1F8000); |
| 187 | dy = (nextMod->properties & 0x2000) ? -((nextMod->properties << 8) & 0x1F8000): ((nextMod->properties << 8) & 0x1F8000); |
| 188 | |
| 189 | if (dx) centreY(); |
| 190 | if (dy) centreX(); |
| 191 | |
| 192 | break; |
| 193 | |
| 194 | case 207: // Text |
| 195 | |
| 196 | break; |
| 197 | |
| 198 | case 208: // Water level |
| 199 | |
| 200 | jj2Level->setWaterLevel(nextMod->properties & 0xFF, nextMod->properties & 0x100); |
| 201 | |
| 202 | break; |
| 203 | |
| 204 | case 230: // Warp |
| 205 | |
| 206 | if (!stopTime) { |
| 207 | |
| 208 | // Catch player |
| 209 | if (coins >= ((nextMod->properties >> 8) & 255)) stopTime = ticks + 1000; |
| 210 | |
| 211 | } else if (ticks > stopTime) { |
| 212 | |
| 213 | coins -= (nextMod->properties >> 8) & 255; |
| 214 | jj2Level->warp(this, nextMod->properties & 255); |
| 215 | |
| 216 | stopTime = 0; |
| 217 | |
| 218 | } |
| 219 | |
| 220 | break; |
| 221 | |
| 222 | default: |
| 223 | |
| 224 | stopTime = 0; |
| 225 | |
| 226 | break; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | mod = nextMod; |
| 231 | |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Respond to controls, unless the player has been killed |
| 237 | * |
| 238 | * @param ticks Time |
| 239 | * @param msps Ticks per step |
| 240 | */ |
| 241 | void JJ2LevelPlayer::control (unsigned int ticks, int msps) { |
| 242 | |
| 243 | JJ2Modifier* nextMod; |
| 244 | bool drop, platform; |
| 245 | |
| 246 | |
| 247 | // If the player has been killed, do not move |
| 248 | if (!energy) { |
| 249 | |
| 250 | dx = 0; |
| 251 | dy = 0; |
| 252 | |
| 253 | animType = JJ2PA_DIE; |
| 254 | |
| 255 | return; |
| 256 | |
| 257 | } |
| 258 | |
| 259 | |
| 260 | // Get overlapping modifier |
| 261 | nextMod = jj2Level->getModifier(FTOT(x + JJ2PXO_MID), FTOT(y + JJ2PYO_MID)); |
| 262 | |
| 263 | |
| 264 | if (stopTime) { |
| 265 | |
| 266 | // Can't control player, so just apply modifier |
| 267 | |
| 268 | modify(nextMod, ticks); |
| 269 | |
| 270 | return; |
| 271 | |
| 272 | } |
| 273 | |
| 274 | |
| 275 | if (player->pcontrols[C_RIGHT]) { |
| 276 | |
| 277 | // Walk/run right |
| 278 | |
| 279 | if (dx < 0) dx += JJ2PXA_REVERSE * msps; |
| 280 | else if (dx < JJ2PXS_WALK) dx += JJ2PXA_WALK * msps; |
| 281 | else if (dx < JJ2PXS_RUN) dx += JJ2PXA_RUN * msps; |
| 282 | |
| 283 | facing = true; |
| 284 | |
| 285 | } else if (player->pcontrols[C_LEFT]) { |
| 286 | |
| 287 | // Walk/run left |
| 288 | |
| 289 | if (dx > 0) dx -= JJ2PXA_REVERSE * msps; |
| 290 | else if (dx > -JJ2PXS_WALK) dx -= JJ2PXA_WALK * msps; |
| 291 | else if (dx > -JJ2PXS_RUN) dx -= JJ2PXA_RUN * msps; |
| 292 | |
| 293 | facing = false; |
| 294 | |
| 295 | } else if ((nextMod->type >> 1) != 3) { |
| 296 | |
| 297 | // Slow down |
| 298 | |
| 299 | if (dx > 0) { |
| 300 | |
| 301 | if (dx < JJ2PXA_STOP * msps) dx = 0; |
| 302 | else dx -= JJ2PXA_STOP * msps; |
| 303 | |
| 304 | } |
| 305 | |
| 306 | if (dx < 0) { |
| 307 | |
| 308 | if (dx > -JJ2PXA_STOP * msps) dx = 0; |
| 309 | else dx += JJ2PXA_STOP * msps; |
| 310 | |
| 311 | } |
| 312 | |
| 313 | } |
| 314 | |
| 315 | |
| 316 | drop = player->pcontrols[C_DOWN]; |
| 317 | |
| 318 | // Check for platform event, bridge or level mask below player |
| 319 | platform = (event == JJ2PE_PLATFORM) || |
| 320 | checkMaskDown(1, drop) || |
| 321 | ((dx > 0) && jj2Level->checkMaskDown(x + JJ2PXO_ML, y + F8, drop)) || |
| 322 | ((dx < 0) && jj2Level->checkMaskDown(x + JJ2PXO_MR, y + F8, drop)); |
| 323 | |
| 324 | if (floating) { |
| 325 | |
| 326 | if (player->pcontrols[C_UP]) { |
| 327 | |
| 328 | // Fly upwards |
| 329 | |
| 330 | if (dy > 0) dy -= JJ2PXA_REVERSE * msps; |
| 331 | else if (dy > -JJ2PXS_WALK) dy -= JJ2PXA_WALK * msps; |
| 332 | else if (dy > -JJ2PXS_RUN) dy -= JJ2PXA_RUN * msps; |
| 333 | |
| 334 | } else if (player->pcontrols[C_DOWN]) { |
| 335 | |
| 336 | // Fly downwards |
| 337 | |
| 338 | if (dy < 0) dy += JJ2PXA_REVERSE * msps; |
| 339 | else if (dy < JJ2PXS_WALK) dy += JJ2PXA_WALK * msps; |
| 340 | else if (dy < JJ2PXS_RUN) dy += JJ2PXA_RUN * msps; |
| 341 | |
| 342 | } else if ((nextMod->type >> 1) != 3) { |
| 343 | |
| 344 | // Slow down |
| 345 | |
| 346 | if (dy > 0) { |
| 347 | |
| 348 | if (dy < JJ2PXA_STOP * msps) dy = 0; |
| 349 | else dy -= JJ2PXA_STOP * msps; |
| 350 | |
| 351 | } |
| 352 | |
| 353 | if (dy < 0) { |
| 354 | |
| 355 | if (dy > -JJ2PXA_STOP * msps) dy = 0; |
| 356 | else dy += JJ2PXA_STOP * msps; |
| 357 | |
| 358 | } |
| 359 | |
| 360 | } |
| 361 | |
| 362 | if (event == JJ2PE_SPRING) dy = JJ2PYS_SPRING; |
| 363 | else if (event == JJ2PE_FLOAT) dy = JJ2PYS_JUMP; |
| 364 | |
| 365 | } else if (y + JJ2PYO_MID > jj2Level->getWaterLevel()) { |
| 366 | |
| 367 | if (player->pcontrols[C_SWIM]) { |
| 368 | |
| 369 | // Swim upwards |
| 370 | |
| 371 | if (dy > 0) dy -= JJ2PXA_REVERSE * msps; |
| 372 | else if (dy > -JJ2PXS_WALK) dy -= JJ2PXA_WALK * msps; |
| 373 | else if (dy > -JJ2PXS_RUN) dy -= JJ2PXA_RUN * msps; |
| 374 | |
| 375 | // Prepare to jump upon leaving the water |
| 376 | |
| 377 | if (!jj2Level->checkMaskUp(x + JJ2PXO_MID, y - F36)) { |
| 378 | |
| 379 | throwY = y - jumpHeight; |
| 380 | |
| 381 | if (dx < 0) throwY += dx >> 4; |
| 382 | else if (dx > 0) throwY -= dx >> 4; |
| 383 | |
| 384 | event = JJ2PE_NONE; |
| 385 | |
| 386 | } |
| 387 | |
| 388 | } else if (player->pcontrols[C_DOWN]) { |
| 389 | |
| 390 | // Swim downwards |
| 391 | |
| 392 | if (dy < 0) dy += JJ2PXA_REVERSE * msps; |
| 393 | else if (dy < JJ2PXS_WALK) dy += JJ2PXA_WALK * msps; |
| 394 | else if (dy < JJ2PXS_RUN) dy += JJ2PXA_RUN * msps; |
| 395 | |
| 396 | } else { |
| 397 | |
| 398 | // Sink |
| 399 | |
| 400 | dy += JJ2PYA_SINK * msps; |
| 401 | if (dy > JJ2PYS_SINK) dy = JJ2PYS_SINK; |
| 402 | |
| 403 | } |
| 404 | |
| 405 | } else { |
| 406 | |
| 407 | if (platform && player->pcontrols[C_JUMP] && |
| 408 | !jj2Level->checkMaskUp(x + JJ2PXO_MID, y - F36)) { |
| 409 | |
| 410 | // Jump |
| 411 | |
| 412 | throwY = y - jumpHeight; |
| 413 | |
| 414 | // Increase jump height if walking/running |
| 415 | if (dx < 0) throwY += dx >> 3; |
| 416 | else if (dx > 0) throwY -= dx >> 3; |
| 417 | |
| 418 | event = JJ2PE_NONE; |
| 419 | |
| 420 | playSound(SE::JUMPA); |
| 421 | |
| 422 | } |
| 423 | |
| 424 | // Stop jumping |
| 425 | if (!player->pcontrols[C_JUMP] && (event != JJ2PE_SPRING) && (event != JJ2PE_FLOAT)) |
| 426 | throwY = TTOF(256); |
| 427 | |
| 428 | if (y >= throwY) { |
| 429 | |
| 430 | // If jumping, rise |
| 431 | dy = (throwY - y - F64) * 4; |
| 432 | |
| 433 | // Avoid jumping too fast, unless caused by an event |
| 434 | if ((event == JJ2PE_NONE) && (dy < JJ2PYS_JUMP)) dy = JJ2PYS_JUMP; |
| 435 | |
| 436 | } else if (!platform) { |
| 437 | |
| 438 | // Fall under gravity |
| 439 | dy += JJ2PYA_GRAVITY * msps; |
| 440 | if (dy > JJ2PYS_FALL) dy = JJ2PYS_FALL; |
| 441 | |
| 442 | } |
| 443 | |
| 444 | // Don't descend through platforms |
| 445 | if ((dy > 0) && (event == JJ2PE_PLATFORM)) dy = 0; |
| 446 | |
| 447 | if (platform && !lookTime) { |
| 448 | |
| 449 | // If requested, look up or down |
| 450 | if (player->pcontrols[C_UP]) lookTime = -ticks; |
| 451 | else if (player->pcontrols[C_DOWN]) lookTime = ticks; |
| 452 | |
| 453 | } |
| 454 | |
| 455 | // Stop looking if there is no platform or the control has been released |
| 456 | if (!platform || |
| 457 | (!player->pcontrols[C_UP] && (lookTime < 0)) || |
| 458 | (!player->pcontrols[C_DOWN] && (lookTime > 0))) lookTime = 0; |
| 459 | |
| 460 | } |
| 461 | |
| 462 | // If there is an obstacle above and the player is not floating up, stop |
| 463 | // rising |
| 464 | if (jj2Level->checkMaskUp(x + JJ2PXO_MID, y + JJ2PYO_TOP - F1) && (throwY < y) && (event != JJ2PE_FLOAT)) { |
| 465 | |
| 466 | throwY = TTOF(256); |
| 467 | if (dy < 0) dy = 0; |
| 468 | |
| 469 | if (event != JJ2PE_PLATFORM) event = JJ2PE_NONE; |
| 470 | |
| 471 | } |
| 472 | |
| 473 | // If jump completed, stop rising |
| 474 | if (y <= throwY) { |
| 475 | |
| 476 | throwY = TTOF(256); |
| 477 | |
| 478 | if (event != JJ2PE_PLATFORM) event = JJ2PE_NONE; |
| 479 | |
| 480 | } |
| 481 | |
| 482 | |
| 483 | // Apply modifier |
| 484 | modify(nextMod, ticks); |
| 485 | |
| 486 | |
| 487 | // Limit speed |
| 488 | |
| 489 | if (dx < -JJ2PXS_LIMIT) dx = -JJ2PXS_LIMIT; |
| 490 | else if (dx > JJ2PXS_LIMIT) dx = JJ2PXS_LIMIT; |
| 491 | |
| 492 | if (dy < -JJ2PYS_LIMIT) dy = -JJ2PYS_LIMIT; |
| 493 | else if (dy > JJ2PYS_LIMIT) dy = JJ2PYS_LIMIT; |
| 494 | |
| 495 | |
| 496 | // Handle firing |
| 497 | if (player->pcontrols[C_FIRE]) { |
| 498 | |
| 499 | if (ticks > fireTime) { |
| 500 | |
| 501 | // Make sure bullet position is taken from correct animation |
| 502 | if (platform) animType = JJ2PA_STANDSHOOT; |
| 503 | |
| 504 | /// @todo Create new bullet when firing |
| 505 | |
| 506 | // Set when the next bullet can be fired |
| 507 | if (player->fireSpeed) fireTime = ticks + (1000 / player->fireSpeed); |
| 508 | else fireTime = 0x7FFFFFFF; |
| 509 | |
| 510 | // Remove the bullet from the arsenal |
| 511 | if (player->ammoType != -1) player->ammo[player->ammoType]--; |
| 512 | |
| 513 | /* If the current ammo type has been exhausted, use the previous |
| 514 | non-exhausted ammo type */ |
| 515 | while ((player->ammoType > -1) && !player->ammo[player->ammoType]) player->ammoType--; |
| 516 | |
| 517 | } |
| 518 | |
| 519 | } else fireTime = 0; |
| 520 | |
| 521 | |
| 522 | // Check for a change in ammo |
| 523 | if (player->pcontrols[C_CHANGE]) { |
| 524 | |
| 525 | if (player == localPlayer) controls.release(C_CHANGE); |
| 526 | |
| 527 | player->ammoType = ((player->ammoType + 2) % 5) - 1; |
| 528 | |
| 529 | // If there is no ammo of this type, go to the next type that has ammo |
| 530 | while ((player->ammoType > -1) && !player->ammo[player->ammoType]) |
| 531 | player->ammoType = ((player->ammoType + 2) % 5) - 1; |
| 532 | |
| 533 | } |
| 534 | |
| 535 | |
| 536 | // Deal with the bird |
| 537 | |
| 538 | //if (birds) birds = birds->step(ticks, msps); |
| 539 | |
| 540 | |
| 541 | // Choose animation |
| 542 | |
| 543 | if ((reaction == JJ2PR_HURT) && (reactionTime - ticks > JJ2PRT_HURT - JJ2PRT_HURTANIM)) |
| 544 | animType = JJ2PA_HURT; |
| 545 | |
| 546 | else if (y + JJ2PYO_MID > jj2Level->getWaterLevel()) |
| 547 | animType = JJ2PA_SWIM; |
| 548 | |
| 549 | else if (floating) animType = JJ2PA_BOARD; |
| 550 | |
| 551 | else if (dy < 0) { |
| 552 | |
| 553 | if (event == JJ2PE_SPRING) animType = JJ2PA_FLOAT1; |
| 554 | else animType = JJ2PA_JUMP; |
| 555 | |
| 556 | } else if (platform) { |
| 557 | |
| 558 | if (dx) { |
| 559 | |
| 560 | if (dx <= -JJ2PXS_RUN) animType = JJ2PA_RUN; |
| 561 | else if (dx >= JJ2PXS_RUN) animType = JJ2PA_RUN; |
| 562 | else if ((dx < 0) && facing) animType = JJ2PA_STOP1; |
| 563 | else if ((dx > 0) && !facing) animType = JJ2PA_STOP1; |
| 564 | else animType = JJ2PA_WALKSHOOT; |
| 565 | |
| 566 | } else if (!jj2Level->checkMaskDown(x + JJ2PXO_ML, y + F12, drop) && |
| 567 | !jj2Level->checkMaskDown(x + JJ2PXO_L, y + F2, drop) && |
| 568 | (event != JJ2PE_PLATFORM)) |
| 569 | animType = JJ2PA_EDGE; |
| 570 | |
| 571 | else if (!jj2Level->checkMaskDown(x + JJ2PXO_MR, y + F12, drop) && |
| 572 | !jj2Level->checkMaskDown(x + JJ2PXO_R, y + F2, drop) && |
| 573 | (event != JJ2PE_PLATFORM)) |
| 574 | animType = JJ2PA_EDGE; |
| 575 | |
| 576 | else if ((lookTime < 0) && ((int)ticks > 1000 - lookTime)) |
| 577 | animType = JJ2PA_LOOKUP; |
| 578 | |
| 579 | else if (lookTime > 0) { |
| 580 | |
| 581 | if ((int)ticks < 1000 + lookTime) animType = JJ2PA_CROUCHED; |
| 582 | else animType = JJ2PA_CROUCH1; |
| 583 | |
| 584 | } |
| 585 | |
| 586 | else if (player->pcontrols[C_FIRE]) animType = JJ2PA_STANDSHOOT; |
| 587 | |
| 588 | else animType = JJ2PA_STAND; |
| 589 | |
| 590 | } else animType = JJ2PA_FALL; |
| 591 | |
| 592 | } |
| 593 | |
| 594 | |
| 595 | /** |
| 596 | * Move the player. |
| 597 | * |
| 598 | * @param ticks Time |
| 599 | * @param msps Ticks per step |
| 600 | */ |
| 601 | void JJ2LevelPlayer::move (unsigned int ticks, int msps) { |
| 602 | |
| 603 | fixed pdx, pdy; |
| 604 | bool grounded = false; |
| 605 | int count; |
| 606 | bool drop; |
| 607 | |
| 608 | |
| 609 | if (stopTime) return; |
| 610 | |
| 611 | |
| 612 | // Apply as much of the trajectory as possible, without going into the |
| 613 | // scenery |
| 614 | |
| 615 | if (fastFeetTime > ticks) { |
| 616 | |
| 617 | pdx = (dx * msps * 3) >> 11; |
| 618 | pdy = (dy * msps * 3) >> 11; |
| 619 | |
| 620 | } else { |
| 621 | |
| 622 | pdx = (dx * msps) >> 10; |
| 623 | pdy = (dy * msps) >> 10; |
| 624 | |
| 625 | } |
| 626 | |
| 627 | |
| 628 | // First for the vertical component of the trajectory |
| 629 | |
| 630 | drop = player->pcontrols[C_DOWN]; |
| 631 | |
| 632 | if (pdy < 0) { |
| 633 | |
| 634 | // Moving up |
| 635 | |
| 636 | count = (-pdy) >> 10; |
| 637 | |
| 638 | while (count > 0) { |
| 639 | |
| 640 | if (jj2Level->checkMaskUp(x + JJ2PXO_MID, y + JJ2PYO_TOP - F1)) { |
| 641 | |
| 642 | y &= ~1023; |
| 643 | dy = 0; |
| 644 | |
| 645 | break; |
| 646 | |
| 647 | } |
| 648 | |
| 649 | y -= F1; |
| 650 | count--; |
| 651 | |
| 652 | } |
| 653 | |
| 654 | pdy = (-pdy) & 1023; |
| 655 | |
| 656 | if (jj2Level->checkMaskUp(x + JJ2PXO_MID, y + JJ2PYO_TOP - pdy)) { |
| 657 | |
| 658 | y &= ~1023; |
| 659 | dy = 0; |
| 660 | |
| 661 | } else y -= pdy; |
| 662 | |
| 663 | } else { |
| 664 | |
| 665 | if (pdy > 0) { |
| 666 | |
| 667 | // Moving down |
| 668 | |
| 669 | count = pdy >> 10; |
| 670 | |
| 671 | while (count > 0) { |
| 672 | |
| 673 | if (checkMaskDown(F1, drop)) { |
| 674 | |
| 675 | y |= 1023; |
| 676 | dy = 0; |
| 677 | |
| 678 | break; |
| 679 | |
| 680 | } |
| 681 | |
| 682 | y += F1; |
| 683 | count--; |
| 684 | |
| 685 | } |
| 686 | |
| 687 | pdy &= 1023; |
| 688 | |
| 689 | if (checkMaskDown(pdy, drop)) y |= 1023; |
| 690 | else y += pdy; |
| 691 | |
| 692 | } |
| 693 | |
| 694 | if (checkMaskDown(0, drop)) { |
| 695 | |
| 696 | // In the ground, so move up |
| 697 | if (y >= 1024) y = (y - 1024) | 1023; |
| 698 | |
| 699 | dy = 0; |
| 700 | grounded = true; |
| 701 | |
| 702 | } else if (checkMaskDown(1, drop)) { |
| 703 | |
| 704 | // On the ground |
| 705 | dy = 0; |
| 706 | grounded = true; |
| 707 | |
| 708 | } |
| 709 | |
| 710 | } |
| 711 | |
| 712 | |
| 713 | |
| 714 | // Then for the horizontal component of the trajectory |
| 715 | |
| 716 | if (pdx < 0) { |
| 717 | |
| 718 | // Moving left |
| 719 | |
| 720 | count = (-pdx) >> 10; |
| 721 | |
| 722 | while (count > 0) { |
| 723 | |
| 724 | // If there is an obstacle, stop |
| 725 | if (jj2Level->checkMaskUp(x + JJ2PXO_L - F1, y + JJ2PYO_MID)) { |
| 726 | |
| 727 | x &= ~1023; |
| 728 | dx = 0; |
| 729 | |
| 730 | break; |
| 731 | |
| 732 | } |
| 733 | |
| 734 | x -= F1; |
| 735 | count--; |
| 736 | |
| 737 | if (grounded) ground(); |
| 738 | |
| 739 | } |
| 740 | |
| 741 | pdx = (-pdx) & 1023; |
| 742 | |
| 743 | if (jj2Level->checkMaskUp(x + JJ2PXO_L - pdx, y + JJ2PYO_MID)) { |
| 744 | |
| 745 | x &= ~1023; |
| 746 | dx = 0; |
| 747 | |
| 748 | } else x -= pdx; |
| 749 | |
| 750 | if (grounded) ground(); |
| 751 | |
| 752 | } else if (pdx > 0) { |
| 753 | |
| 754 | // Moving right |
| 755 | |
| 756 | count = pdx >> 10; |
| 757 | |
| 758 | while (count > 0) { |
| 759 | |
| 760 | // If there is an obstacle, stop |
| 761 | if (jj2Level->checkMaskUp(x + JJ2PXO_R + F1, y + JJ2PYO_MID)) { |
| 762 | |
| 763 | x |= 1023; |
| 764 | dx = 0; |
| 765 | |
| 766 | break; |
| 767 | |
| 768 | } |
| 769 | |
| 770 | x += F1; |
| 771 | count--; |
| 772 | |
| 773 | if (grounded) ground(); |
| 774 | |
| 775 | } |
| 776 | |
| 777 | pdx &= 1023; |
| 778 | |
| 779 | if (jj2Level->checkMaskUp(x + JJ2PXO_R + pdx, y + JJ2PYO_MID)) { |
| 780 | |
| 781 | x |= 1023; |
| 782 | dx = 0; |
| 783 | |
| 784 | } else x += pdx; |
| 785 | |
| 786 | if (grounded) ground(); |
| 787 | |
| 788 | } |
| 789 | |
| 790 | |
| 791 | // If using a float up event and have hit a ceiling, ignore event |
| 792 | if ((event == JJ2PE_FLOAT) && jj2Level->checkMaskUp(x + JJ2PXO_MID, y + JJ2PYO_TOP - F1)) { |
| 793 | |
| 794 | throwY = TTOF(256); |
| 795 | event = JJ2PE_NONE; |
| 796 | |
| 797 | } |
| 798 | |
| 799 | |
| 800 | if (jj2Level->getStage() == LS_END) return; |
| 801 | |
| 802 | } |
| 803 | |
| 804 | |
| 805 | /** |
| 806 | * Calculate viewport. |
| 807 | * |
| 808 | * @param ticks Time |
| 809 | * @param mspf Ticks per frame |
| 810 | * @param change Time since last step |
| 811 | */ |
| 812 | void JJ2LevelPlayer::view (unsigned int ticks, int mspf, int change) { |
| 813 | |
| 814 | int oldViewX, oldViewY, speed; |
| 815 | |
| 816 | // Record old viewport position for applying lag |
| 817 | oldViewX = viewX; |
| 818 | oldViewY = viewY; |
| 819 | |
| 820 | |
| 821 | // Find new position |
| 822 | |
| 823 | viewX = x + ((dx * change) >> 10) + F8 - (canvasW << 9); |
| 824 | viewY = y + ((dy * change) >> 10) - F24 - (canvasH << 9); |
| 825 | |
| 826 | if ((lookTime > 0) && ((int)ticks > 1000 + lookTime)) { |
| 827 | |
| 828 | // Look down |
| 829 | if ((int)ticks < 2000 + lookTime) viewY += 64 * (ticks - (1000 + lookTime)); |
| 830 | else viewY += F64; |
| 831 | |
| 832 | } else if ((lookTime < 0) && ((int)ticks > 1000 - lookTime)) { |
| 833 | |
| 834 | // Look up |
| 835 | if ((int)ticks < 2000 - lookTime) viewY -= 64 * (ticks - (1000 - lookTime)); |
| 836 | else viewY -= F64; |
| 837 | |
| 838 | } |
| 839 | |
| 840 | |
| 841 | // Apply lag proportional to player "speed" |
| 842 | speed = ((dx >= 0? dx: -dx) + (dy >= 0? dy: -dy)) >> 14; |
| 843 | |
| 844 | if (speed && (mspf < speed)) { |
| 845 | |
| 846 | viewX = ((oldViewX * (speed - mspf)) + (viewX * mspf)) / speed; |
| 847 | viewY = ((oldViewY * (speed - mspf)) + (viewY * mspf)) / speed; |
| 848 | |
| 849 | } |
| 850 | |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Draw the player. |
| 855 | * |
| 856 | * @param ticks Time |
| 857 | * @param change Time since last step |
| 858 | */ |
| 859 | void JJ2LevelPlayer::draw (unsigned int ticks, int change) { |
| 860 | |
| 861 | Anim *an; |
| 862 | int frame; |
| 863 | fixed drawX, drawY; |
| 864 | |
| 865 | // The current frame for animations |
| 866 | if (reaction == JJ2PR_KILLED) frame = (ticks + JJ2PRT_KILLED - reactionTime) / 75; |
| 867 | else frame = ticks / 75; |
| 868 | |
| 869 | |
| 870 | // Get position |
| 871 | |
| 872 | drawX = getDrawX(stopTime? 0: change); |
| 873 | drawY = getDrawY(stopTime? 0: change); |
| 874 | |
| 875 | |
| 876 | // Choose sprite |
| 877 | |
| 878 | an = getAnim(); |
| 879 | an->setFrame(frame, reaction != JJ2PR_KILLED); |
| 880 | |
| 881 | |
| 882 | // Show the player |
| 883 | |
| 884 | // Use player colour |
| 885 | an->setPalette(palette, 16, 32); |
| 886 | |
| 887 | // Flash on and off if hurt |
| 888 | if ((reaction != JJ2PR_HURT) || ((ticks / 30) & 2)) { |
| 889 | |
| 890 | // Draw "motion blur" |
| 891 | if (fastFeetTime > ticks) an->draw(drawX + F16 - (dx >> 6), drawY + F16); |
| 892 | |
| 893 | // Draw player |
| 894 | an->draw(drawX + F16, drawY + F16); |
| 895 | |
| 896 | } |
| 897 | |
| 898 | |
| 899 | // Uncomment the following to see the area of the player |
| 900 | /*drawRect(FTOI(drawX + JJ2PXO_L - viewX), |
| 901 | FTOI(drawY + JJ2PYO_TOP - viewY), |
| 902 | FTOI(JJ2PXO_R - JJ2PXO_L), |
| 903 | FTOI(-JJ2PYO_TOP), 89); |
| 904 | drawRect(FTOI(drawX + JJ2PXO_ML - viewX), |
| 905 | FTOI(drawY + JJ2PYO_TOP - viewY), |
| 906 | FTOI(JJ2PXO_MR - JJ2PXO_ML), |
| 907 | FTOI(-JJ2PYO_TOP), 88);*/ |
| 908 | |
| 909 | |
| 910 | if (reaction == JJ2PR_INVINCIBLE) { |
| 911 | |
| 912 | /// @todo Show invincibility effect |
| 913 | |
| 914 | } |
| 915 | |
| 916 | switch (shield) { |
| 917 | |
| 918 | case JJ2S_NONE: |
| 919 | |
| 920 | // Do nothing |
| 921 | |
| 922 | break; |
| 923 | |
| 924 | case JJ2S_FLAME: |
| 925 | |
| 926 | /// @todo Show shield effect |
| 927 | |
| 928 | break; |
| 929 | |
| 930 | case JJ2S_BUBBLE: |
| 931 | |
| 932 | /// @todo Show shield effect |
| 933 | |
| 934 | break; |
| 935 | |
| 936 | case JJ2S_PLASMA: |
| 937 | |
| 938 | /// @todo Show shield effect |
| 939 | |
| 940 | break; |
| 941 | |
| 942 | case JJ2S_LASER: |
| 943 | |
| 944 | /// @todo Show shield effect |
| 945 | |
| 946 | break; |
| 947 | |
| 948 | } |
| 949 | |
| 950 | |
| 951 | // Show the bird |
| 952 | //if (birds) birds->draw(ticks, change); |
| 953 | |
| 954 | |
| 955 | // Show the player's name |
| 956 | if (nPlayers > 1) |
| 957 | panelBigFont->showString(player->name, |
| 958 | FTOI(drawX + JJ2PXO_MID) - (panelBigFont->getStringWidth(player->name) >> 1), |
| 959 | FTOI(drawY - F32 - F16)); |
| 960 | |
| 961 | //panelBigFont->showNumber(mod->properties, FTOI(drawX) + 24, FTOI(drawY) + 12); |
| 962 | |
| 963 | } |