| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1bridge.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 |
| 12 | * - 19th July 2009: Created eventframe.cpp from parts of events.cpp |
| 13 | * - 19th July 2009: Renamed events.cpp to event.cpp |
| 14 | * - 2nd March 2010: Created bridge.cpp from parts of event.cpp and eventframe.cpp |
| 15 | * - 1st August 2012: Renamed bridge.cpp to jj1bridge.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2012 AJ Thomson |
| 19 | * |
| 20 | * OpenJazz is distributed under the terms of |
| 21 | * the GNU General Public License, version 2.0 |
| 22 | * |
| 23 | * @par Description: |
| 24 | * Provides the functions of bridge events. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include "../jj1level.h" |
| 30 | #include "../jj1levelplayer.h" |
| 31 | #include "jj1event.h" |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Create bridge. |
| 36 | * |
| 37 | * @param gX X-coordinate |
| 38 | * @param gY Y-coordinate |
| 39 | */ |
| 40 | JJ1Bridge::JJ1Bridge (unsigned char gX, unsigned char gY) : JJ1Event(gX, gY) { |
| 41 | |
| 42 | y = TTOF(gY) + ITOF(set->multiB); |
| 43 | |
| 44 | setAnimType(E_LEFTANIM); |
| 45 | |
| 46 | // Bridges should ignore the default yOffsets |
| 47 | noAnimOffset = true; |
| 48 | |
| 49 | // leftDipX and rightDipX used to store leftmost and rightmost player on bridge |
| 50 | // Start with minimum values |
| 51 | leftDipX = set->multiA * set->pieceSize * F4; |
| 52 | rightDipX = 0; |
| 53 | |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Bridge iteration. |
| 59 | * |
| 60 | * @param ticks Time |
| 61 | * |
| 62 | * @return Remaining event |
| 63 | */ |
| 64 | JJ1Event* JJ1Bridge::step (unsigned int ticks) { |
| 65 | |
| 66 | set = prepareStep(ticks); |
| 67 | if (!set) return remove(false); |
| 68 | |
| 69 | fixed bridgeLength = set->multiA * set->pieceSize * F4; |
| 70 | |
| 71 | // Gradually stop the bridge sagging |
| 72 | if (leftDipX < bridgeLength) leftDipX += 5120; |
| 73 | if (leftDipX > bridgeLength) leftDipX = bridgeLength; |
| 74 | if (rightDipX > 0) rightDipX -= 5120; |
| 75 | if (rightDipX < 0) rightDipX = 0; |
| 76 | |
| 77 | for (int i = 0; i < nPlayers; i++) { |
| 78 | |
| 79 | JJ1LevelPlayer* levelPlayer = players[i].getJJ1LevelPlayer(); |
| 80 | |
| 81 | fixed playerDipX = levelPlayer->getX() + PXO_MID - x; |
| 82 | fixed playerDipY; |
| 83 | |
| 84 | if (playerDipX < bridgeLength >> 1) playerDipY = playerDipX >> 3; |
| 85 | else playerDipY = (bridgeLength - playerDipX) >> 3; |
| 86 | |
| 87 | if (levelPlayer->overlap(x, y - F8 + playerDipY - F4, bridgeLength, F8) && |
| 88 | !level->checkMaskDown(x + playerDipX, y - F8 + playerDipY - F32)) { |
| 89 | |
| 90 | // Player is on the bridge |
| 91 | |
| 92 | if (playerDipX < leftDipX) leftDipX = playerDipX; |
| 93 | |
| 94 | if (playerDipX > rightDipX) rightDipX = playerDipX; |
| 95 | |
| 96 | levelPlayer->setPlatform(gridX, gridY, 0, y - F8 - F1 + playerDipY); |
| 97 | |
| 98 | } else levelPlayer->clearEvent(gridX, gridY); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | return this; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Draw bridge. |
| 109 | * |
| 110 | * @param ticks Time |
| 111 | * @param change Time since last iteration |
| 112 | */ |
| 113 | void JJ1Bridge::draw (unsigned int ticks, int change) { |
| 114 | |
| 115 | int count; |
| 116 | |
| 117 | if (next) next->draw(ticks, change); |
| 118 | |
| 119 | // If the event has been removed from the grid, do not show it |
| 120 | if (!set) return; |
| 121 | |
| 122 | // Check if the event has anything to draw |
| 123 | if ((animType == E_NOANIM) || ((set->anims[animType] & 0x7F) == 0)) return; |
| 124 | |
| 125 | unsigned char frame = ticks / (set->animSpeed << 5); |
| 126 | setAnimFrame(frame + gridX + gridY, true); |
| 127 | |
| 128 | // Draw the bridge |
| 129 | fixed leftDipY, rightDipY; |
| 130 | fixed bridgeLength = set->multiA * set->pieceSize * F4; |
| 131 | fixed anchorY = getDrawY(change) - F10 - anim->getOffset(); |
| 132 | |
| 133 | if (rightDipX >= leftDipX) { |
| 134 | |
| 135 | leftDipY = (leftDipX <= (bridgeLength >> 1)) ? leftDipX >> 3: (bridgeLength - leftDipX) >> 3; |
| 136 | rightDipY = (rightDipX <= (bridgeLength >> 1)) ? rightDipX >> 3: (bridgeLength - rightDipX) >> 3; |
| 137 | |
| 138 | for (count = 0; count < bridgeLength; count += F4 * set->pieceSize) { |
| 139 | |
| 140 | if (count < leftDipX) |
| 141 | anim->draw(getDrawX(change) + count, anchorY + (count * leftDipY / leftDipX)); |
| 142 | else if (count < rightDipX) |
| 143 | anim->draw(getDrawX(change) + count, anchorY + leftDipY + ((count - leftDipX) * (rightDipY - leftDipY) / (rightDipX - leftDipX))); |
| 144 | else |
| 145 | anim->draw(getDrawX(change) + count, anchorY + ((bridgeLength - count) * rightDipY / (bridgeLength - rightDipX))); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | } else { |
| 150 | |
| 151 | // No players on the bridge, de-sagging in progress |
| 152 | |
| 153 | // Midpoint |
| 154 | leftDipY = (leftDipX + rightDipX) >> 1; |
| 155 | |
| 156 | // Dip |
| 157 | rightDipY = (rightDipX < bridgeLength - leftDipX) ? rightDipX >> 3: (bridgeLength - leftDipX) >> 3; |
| 158 | |
| 159 | for (count = 0; count < bridgeLength; count += F4 * set->pieceSize) { |
| 160 | |
| 161 | if (count < leftDipY) |
| 162 | anim->draw(getDrawX(change) + count, anchorY + (count * rightDipY / leftDipY)); |
| 163 | else |
| 164 | anim->draw(getDrawX(change) + count, anchorY + ((bridgeLength - count) * rightDipY / (bridgeLength - leftDipY))); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | } |