| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1bonuslevelplayer.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: Created bonusplayer.cpp from parts of player.cpp and |
| 16 | * playerframe.cpp |
| 17 | * - 1st August 2012: Renamed bonusplayer.cpp to jj1bonuslevelplayer.cpp |
| 18 | * |
| 19 | * @par Licence: |
| 20 | * Copyright (c) 2005-2012 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 | * Deals with players in bonus levels. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | #include "jj1bonuslevel.h" |
| 32 | #include "jj1bonuslevelplayer.h" |
| 33 | |
| 34 | #include "game/game.h" |
| 35 | #include "io/controls.h" |
| 36 | #include "io/gfx/sprite.h" |
| 37 | #include "io/gfx/video.h" |
| 38 | #include "util.h" |
| 39 | |
| 40 | #include <string.h> |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Create a JJ1 bonus level player. |
| 45 | * |
| 46 | * @param parent The game player corresponding to this level player. |
| 47 | * @param newAnims Animations |
| 48 | * @param startX Starting position x-coordinate |
| 49 | * @param startY Starting position y-coordinate |
| 50 | * @param flockSize The number of birds accompanying the player |
| 51 | */ |
| 52 | JJ1BonusLevelPlayer::JJ1BonusLevelPlayer (Player* parent, Anim **newAnims, unsigned char startX, unsigned char startY, int flockSize) { |
| 53 | |
| 54 | player = parent; |
| 55 | birds = flockSize; |
| 56 | memcpy(anims, newAnims, BPANIMS * sizeof(Anim*)); |
| 57 | reset(startX, startY); |
| 58 | |
| 59 | // Create the player's palette |
| 60 | |
| 61 | for (int i = 0; i < MAX_PALETTE_COLORS; i++) |
| 62 | palette[i].r = palette[i].g = palette[i].b = i; |
| 63 | |
| 64 | /// @todo Custom colours |
| 65 | |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Delete the JJ1 bonus level player. |
| 71 | */ |
| 72 | JJ1BonusLevelPlayer::~JJ1BonusLevelPlayer () { |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Reset the player's position, energy etc. |
| 79 | * |
| 80 | * @param startX New x-coordinate |
| 81 | * @param startY New y-coordinate |
| 82 | */ |
| 83 | void JJ1BonusLevelPlayer::reset (int startX, int startY) { |
| 84 | |
| 85 | x = TTOF(startX) + F16; |
| 86 | y = TTOF(startY) + F16; |
| 87 | z = 0; |
| 88 | dx = 0; |
| 89 | dy = 0; |
| 90 | dz = 0; |
| 91 | |
| 92 | direction = FQ; |
| 93 | dr = 0; |
| 94 | da = 0; |
| 95 | |
| 96 | gems = 0; |
| 97 | |
| 98 | animType = PA_WALK; |
| 99 | running = false; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Add to the player's gem tally. |
| 106 | */ |
| 107 | void JJ1BonusLevelPlayer::addGem () { |
| 108 | |
| 109 | gems++; |
| 110 | |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Determine the direction the player is facing. |
| 116 | * |
| 117 | * @return The player's direction |
| 118 | */ |
| 119 | fixed JJ1BonusLevelPlayer::getDirection () { |
| 120 | |
| 121 | return direction; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Determine the number of gems the player has collected. |
| 128 | * |
| 129 | * @return Number of gems collected |
| 130 | */ |
| 131 | int JJ1BonusLevelPlayer::getGems () { |
| 132 | |
| 133 | return gems; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Determine the player's Z-coordinate (altitude). |
| 140 | * |
| 141 | * @return Player's Z-coordinate |
| 142 | */ |
| 143 | fixed JJ1BonusLevelPlayer::getZ () { |
| 144 | |
| 145 | return z; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Determine how many birds are accompanying the player. |
| 152 | * |
| 153 | * @return The number of birds accompanying the player |
| 154 | */ |
| 155 | int JJ1BonusLevelPlayer::countBirds () { |
| 156 | |
| 157 | return birds; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * Player iteration. |
| 164 | * |
| 165 | * @param ticks Time |
| 166 | * @param msps Ticks per step |
| 167 | * @param bonus Bonus level |
| 168 | */ |
| 169 | void JJ1BonusLevelPlayer::step (unsigned int ticks, int msps, JJ1BonusLevel* bonus) { |
| 170 | |
| 171 | (void)ticks; |
| 172 | |
| 173 | fixed cdx, cdy; |
| 174 | |
| 175 | // Bonus stages use polar coordinates for movement (but not position) |
| 176 | |
| 177 | if (animType == PA_CRASH) { |
| 178 | |
| 179 | if (dr < 0) dr += PRA_REBOUND * msps; |
| 180 | else { |
| 181 | |
| 182 | dr = 0; |
| 183 | |
| 184 | animType = PA_OTHER; |
| 185 | |
| 186 | } |
| 187 | |
| 188 | } else { |
| 189 | |
| 190 | if (player->getControl(C_FIRE)) { |
| 191 | |
| 192 | running = true; |
| 193 | dr = PRS_RUN; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | if (player->getControl(C_UP)) { |
| 198 | |
| 199 | // Walk/run forwards |
| 200 | |
| 201 | if (dr < 0) dr += PRA_REVERSE * msps; |
| 202 | else if (dr < PRS_WALK) dr += PRA_WALK * msps; |
| 203 | |
| 204 | animType = PA_WALK; |
| 205 | |
| 206 | } else if (player->getControl(C_DOWN)) { |
| 207 | |
| 208 | // Walk/run back |
| 209 | |
| 210 | running = false; |
| 211 | |
| 212 | if (dr > 0) dr -= PRA_REVERSE * msps; |
| 213 | else if (dr > PRS_REVERSE) dr -= PRA_WALK * msps; |
| 214 | |
| 215 | animType = PA_WALK; |
| 216 | |
| 217 | } else { |
| 218 | |
| 219 | // Slow down |
| 220 | |
| 221 | if (!running && (dr > 0)) { |
| 222 | |
| 223 | if (dr < PRA_STOP * msps) dr = 0; |
| 224 | else dr -= PRA_STOP * msps; |
| 225 | |
| 226 | } |
| 227 | |
| 228 | if (dr < 0) { |
| 229 | |
| 230 | if (dr > -PRA_STOP * msps) dr = 0; |
| 231 | else dr += PRA_STOP * msps; |
| 232 | |
| 233 | } |
| 234 | |
| 235 | if (dr == 0) running = false; |
| 236 | |
| 237 | animType = PA_OTHER; |
| 238 | |
| 239 | } |
| 240 | |
| 241 | if (player->getControl(C_LEFT)) { |
| 242 | |
| 243 | if (da > -PAS_TURN) da -= PAA_TURN * msps; |
| 244 | |
| 245 | animType = PA_LEFT; |
| 246 | |
| 247 | } else if (player->getControl(C_RIGHT)) { |
| 248 | |
| 249 | if (da < PAS_TURN) da += PAA_TURN * msps; |
| 250 | |
| 251 | animType = PA_RIGHT; |
| 252 | |
| 253 | } else { |
| 254 | |
| 255 | // Slow down rotation |
| 256 | |
| 257 | if (da > 0) { |
| 258 | |
| 259 | if (da < PAA_STOP * msps) da = 0; |
| 260 | else da -= PAA_STOP * msps; |
| 261 | |
| 262 | } |
| 263 | |
| 264 | if (da < 0) { |
| 265 | |
| 266 | if (da > -PAA_STOP * msps) da = 0; |
| 267 | else da += PAA_STOP * msps; |
| 268 | |
| 269 | } |
| 270 | |
| 271 | } |
| 272 | |
| 273 | if ((z == 0) && player->getControl(C_JUMP)) { |
| 274 | |
| 275 | // Jump |
| 276 | |
| 277 | dz = PZS_JUMP; |
| 278 | |
| 279 | if (dr < PRS_JUMP) dr = PRS_JUMP; |
| 280 | |
| 281 | } |
| 282 | |
| 283 | if (dz > PZS_FALL) dz += PZA_GRAVITY * msps; |
| 284 | |
| 285 | if (z > 0) animType = PA_JUMP; |
| 286 | |
| 287 | } |
| 288 | |
| 289 | |
| 290 | // Apply trajectory |
| 291 | |
| 292 | direction += (da * msps) >> 10; |
| 293 | |
| 294 | cdx = (MUL(fSin(direction), dr) * msps) >> 10; |
| 295 | cdy = (MUL(-fCos(direction), dr) * msps) >> 10; |
| 296 | |
| 297 | if (!bonus->checkMask(x + cdx, y)) x += cdx; |
| 298 | if (!bonus->checkMask(x, y + cdy)) y += cdy; |
| 299 | |
| 300 | z += (dz * msps) >> 10; |
| 301 | if (z > F1) z = F1; |
| 302 | if (z < 0) z = 0; |
| 303 | |
| 304 | |
| 305 | // React to running collision |
| 306 | |
| 307 | if (running && bonus->checkMask(x + cdx, y + cdy)) { |
| 308 | |
| 309 | running = false; |
| 310 | dr = PRS_CRASH; |
| 311 | da = 0; |
| 312 | |
| 313 | animType = PA_CRASH; |
| 314 | |
| 315 | } |
| 316 | |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /** |
| 321 | * Draw the player. |
| 322 | * |
| 323 | * @param ticks Time |
| 324 | */ |
| 325 | void JJ1BonusLevelPlayer::draw (unsigned int ticks) { |
| 326 | |
| 327 | Anim* anim; |
| 328 | |
| 329 | anim = anims[animType]; |
| 330 | anim->setFrame(ticks / 75, true); |
| 331 | if (canvasW <= SW) anim->draw(ITOF((canvasW - anim->getWidth()) >> 1), ITOF(canvasH - anim->getHeight() - 16 - FTOI(z * 80))); |
| 332 | else anim->drawScaled(ITOF(canvasW >> 1), ITOF(canvasH - ((((anim->getHeight() >> 1) + 16 + FTOI(z * 80)) * canvasW) / SW)), ITOF(canvasW) / SW); |
| 333 | |
| 334 | } |
| 335 | |
| 336 | |
| 337 | /** |
| 338 | * Fill a buffer with player data. |
| 339 | * |
| 340 | * @param buffer The buffer |
| 341 | */ |
| 342 | void JJ1BonusLevelPlayer::send (unsigned char *buffer) { |
| 343 | |
| 344 | // Copy data to be sent to clients/server |
| 345 | |
| 346 | buffer[9] = birds; |
| 347 | buffer[23] = 0; |
| 348 | buffer[25] = 0; |
| 349 | buffer[26] = 0; |
| 350 | buffer[27] = direction >> 2; |
| 351 | buffer[29] = 0; |
| 352 | buffer[30] = 0; |
| 353 | buffer[31] = 0; |
| 354 | buffer[32] = 0; |
| 355 | buffer[33] = z >> 24; |
| 356 | buffer[34] = (z >> 16) & 255; |
| 357 | buffer[35] = (z >> 8) & 255; |
| 358 | buffer[36] = z & 255; |
| 359 | buffer[37] = x >> 24; |
| 360 | buffer[38] = (x >> 16) & 255; |
| 361 | buffer[39] = (x >> 8) & 255; |
| 362 | buffer[40] = x & 255; |
| 363 | buffer[41] = y >> 24; |
| 364 | buffer[42] = (y >> 16) & 255; |
| 365 | buffer[43] = (y >> 8) & 255; |
| 366 | buffer[44] = y & 255; |
| 367 | |
| 368 | } |
| 369 | |
| 370 | |
| 371 | /** |
| 372 | * Adjust player data based on the contents of a given buffer. |
| 373 | * |
| 374 | * @param buffer The buffer |
| 375 | */ |
| 376 | void JJ1BonusLevelPlayer::receive (unsigned char *buffer) { |
| 377 | |
| 378 | // Interpret data received from client/server |
| 379 | |
| 380 | switch (buffer[1]) { |
| 381 | |
| 382 | case MT_P_TEMP: |
| 383 | |
| 384 | birds = buffer[9]; |
| 385 | direction = buffer[27] << 2; |
| 386 | z = (buffer[33] << 24) + (buffer[34] << 16) + (buffer[35] << 8) + buffer[36]; |
| 387 | x = (buffer[37] << 24) + (buffer[38] << 16) + (buffer[39] << 8) + buffer[40]; |
| 388 | y = (buffer[41] << 24) + (buffer[42] << 16) + (buffer[43] << 8) + buffer[44]; |
| 389 | |
| 390 | break; |
| 391 | |
| 392 | } |
| 393 | |
| 394 | } |