| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1guardians.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 guardians.cpp from parts of event.cpp and eventframe.cpp |
| 15 | * - 1st August 2012: Renamed guardians.cpp to jj1guardians.cpp |
| 16 | * |
| 17 | * @par Licence: |
| 18 | * Copyright (c) 2005-2013 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 guardian events. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | #include "../jj1bullet.h" |
| 30 | #include "../jj1level.h" |
| 31 | #include "jj1guardians.h" |
| 32 | |
| 33 | #include "io/gfx/video.h" |
| 34 | #include "util.h" |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Create guardian. |
| 41 | * |
| 42 | * @param gX X-coordinate |
| 43 | * @param gY Y-coordinate |
| 44 | */ |
| 45 | Guardian::Guardian(unsigned char gX, unsigned char gY) : JJ1Event(gX, gY) { |
| 46 | |
| 47 | setAnimType(E_LEFTANIM); |
| 48 | |
| 49 | stage = 0; |
| 50 | |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Create episode B guardian. |
| 56 | * |
| 57 | * @param gX X-coordinate |
| 58 | * @param gY Y-coordinate |
| 59 | */ |
| 60 | DeckGuardian::DeckGuardian (unsigned char gX, unsigned char gY) : Guardian(gX, gY) { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Episode B guardian iteration. |
| 67 | * |
| 68 | * @param ticks Time |
| 69 | * |
| 70 | * @return Remaining event |
| 71 | */ |
| 72 | JJ1Event* DeckGuardian::step (unsigned int ticks) { |
| 73 | |
| 74 | set = prepareStep(ticks); |
| 75 | if (!set) return remove(false); |
| 76 | |
| 77 | int count = level->getEventHits(gridX, gridY); |
| 78 | |
| 79 | if (count < 8) stage = 0; |
| 80 | else if (count < 16) stage = 1; |
| 81 | else if (count < 24) stage = 2; |
| 82 | else stage = 3; |
| 83 | |
| 84 | // If the event has been destroyed, play its finishing animation and set its |
| 85 | // reaction time |
| 86 | if (set->strength && |
| 87 | (level->getEventHits(gridX, gridY) >= set->strength) && |
| 88 | ((animType & ~1) != E_LFINISHANIM)) { |
| 89 | |
| 90 | destroy(ticks); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | // If the reaction time has expired |
| 95 | if (level->getEventTime(gridX, gridY) && |
| 96 | (ticks > level->getEventTime(gridX, gridY))) { |
| 97 | |
| 98 | if ((animType & ~1) != E_LFINISHANIM) { |
| 99 | |
| 100 | level->setEventTime(gridX, gridY, 0); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | return this; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Draw episode B guardian. |
| 113 | * |
| 114 | * @param ticks Time |
| 115 | * @param change Time since last iteration |
| 116 | */ |
| 117 | void DeckGuardian::draw (unsigned int ticks, int change) { |
| 118 | |
| 119 | if (next) next->draw(ticks, change); |
| 120 | |
| 121 | // If the event has been removed from the grid, do not show it |
| 122 | if (!set) return; |
| 123 | |
| 124 | // Draw the boss |
| 125 | |
| 126 | if (stage < 3) { |
| 127 | |
| 128 | // Draw unit |
| 129 | |
| 130 | Anim* unitAnim = level->getAnim(29 + stage); |
| 131 | |
| 132 | if (stage == 0) { |
| 133 | |
| 134 | width = F8; |
| 135 | drawnX = x - F64; |
| 136 | |
| 137 | } else if (stage == 1) { |
| 138 | |
| 139 | width = F8; |
| 140 | drawnX = x + F32 - F8; |
| 141 | |
| 142 | } else if (stage == 2) { |
| 143 | |
| 144 | width = F64 + F32; |
| 145 | drawnX = x - F64; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | drawnY = y + F32; |
| 150 | height = F32; |
| 151 | |
| 152 | if (ticks < flashTime) unitAnim->flashPalette(0); |
| 153 | |
| 154 | if (stage == 0) unitAnim->draw(getDrawX(change) - F64, getDrawY(change) + F32); |
| 155 | else if (stage == 1) unitAnim->draw(getDrawX(change) + F32 - F8 - F4, getDrawY(change) + F32); |
| 156 | else unitAnim->draw(getDrawX(change) + F8 - F64, getDrawY(change) + F32); |
| 157 | |
| 158 | if (ticks < flashTime) unitAnim->restorePalette(); |
| 159 | |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * Create episode 1 guardian. |
| 167 | * |
| 168 | * @param gX X-coordinate |
| 169 | * @param gY Y-coordinate |
| 170 | */ |
| 171 | MedGuardian::MedGuardian(unsigned char gX, unsigned char gY) : Guardian(gX, gY) { |
| 172 | |
| 173 | direction = 1; |
| 174 | shoot = false; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /** |
| 180 | * Episode 1 guardian iteration. |
| 181 | * |
| 182 | * @param ticks Time |
| 183 | * |
| 184 | * @return Remaining event |
| 185 | */ |
| 186 | JJ1Event* MedGuardian::step(unsigned int ticks) { |
| 187 | |
| 188 | fixed sin = fSin(ticks / 2); |
| 189 | fixed cos = fCos(ticks / 2); |
| 190 | |
| 191 | set = prepareStep(ticks); |
| 192 | |
| 193 | if (!set) return remove(false); |
| 194 | |
| 195 | |
| 196 | if (level->getEventHits(gridX, gridY) >= set->strength / 2) |
| 197 | stage = 1; |
| 198 | if (level->getEventHits(gridX, gridY) >= set->strength) |
| 199 | stage = 2; |
| 200 | |
| 201 | // Stage 0: Move in an eight shape and fire the occasional shot |
| 202 | |
| 203 | if (stage == 0) { |
| 204 | |
| 205 | if (direction == 1) { |
| 206 | |
| 207 | // Lower right part of the eight |
| 208 | setAnimType(E_LEFTANIM); |
| 209 | |
| 210 | dx = TTOF(gridX) + (sin * 96) - x + ITOF(96); |
| 211 | dy = TTOF(gridY) - (cos * 64) - y; |
| 212 | |
| 213 | if (cos > 0) direction = 2; |
| 214 | |
| 215 | } |
| 216 | |
| 217 | if (direction == 2) { |
| 218 | |
| 219 | // Upper left part of the eight |
| 220 | setAnimType(E_LEFTANIM); |
| 221 | |
| 222 | dx = TTOF(gridX) - (sin * 96) - x - ITOF(96); |
| 223 | dy = TTOF(gridY) - (cos * 64) - y; |
| 224 | |
| 225 | if (cos < 0) direction = 3; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | if (direction == 3) { |
| 230 | |
| 231 | // Lower left part of the eight |
| 232 | setAnimType(E_RIGHTANIM); |
| 233 | |
| 234 | dx = TTOF(gridX) - (sin * 96) - x - ITOF(96); |
| 235 | dy = TTOF(gridY) - (cos * 64) - y; |
| 236 | |
| 237 | if (cos > 0) direction = 4; |
| 238 | |
| 239 | } |
| 240 | |
| 241 | if (direction == 4) { |
| 242 | |
| 243 | // Upper right part of the eight |
| 244 | setAnimType(E_RIGHTANIM); |
| 245 | |
| 246 | dx = TTOF(gridX) + (sin * 96) - x + ITOF(96); |
| 247 | dy = TTOF(gridY) - (cos * 64) - y; |
| 248 | |
| 249 | if (cos < 0) direction = 1; |
| 250 | |
| 251 | } |
| 252 | |
| 253 | // Decide if there should be a shot |
| 254 | if ((ticks % (set->bulletPeriod * 25) > |
| 255 | (unsigned int)(set->bulletPeriod * 25) - 300)) { |
| 256 | |
| 257 | level->setEventTime(gridX, gridY, ticks + 300); |
| 258 | shoot = true; |
| 259 | |
| 260 | } |
| 261 | |
| 262 | // Shoot if there is a shot |
| 263 | if (level->getEventTime(gridX, gridY) && |
| 264 | (ticks > level->getEventTime(gridX, gridY)) && |
| 265 | shoot) { |
| 266 | |
| 267 | if (set->bullet < 32) |
| 268 | level->createBullet(NULL, |
| 269 | gridX, |
| 270 | gridY, |
| 271 | x + anim->getAccessoryShootX(), |
| 272 | y + anim->getAccessoryShootY(), |
| 273 | set->bullet, |
| 274 | (animType != E_LEFTANIM), |
| 275 | ticks); |
| 276 | |
| 277 | shoot = false; |
| 278 | |
| 279 | } |
| 280 | |
| 281 | } |
| 282 | |
| 283 | // Stage 1: Hop back and forth destroying the bottom row of tiles |
| 284 | |
| 285 | if (stage == 1) { |
| 286 | |
| 287 | fixed startPos = TTOF(gridY) + ITOF(40); |
| 288 | |
| 289 | if (direction < 5) { |
| 290 | |
| 291 | // Move up or down towards the starting position for hopping |
| 292 | direction = (y > startPos) ? 5 : 6; |
| 293 | |
| 294 | } |
| 295 | |
| 296 | // Move up to the correct height |
| 297 | if (direction == 5) { |
| 298 | |
| 299 | if (y > startPos) { |
| 300 | |
| 301 | dx = 0; |
| 302 | dy = -ITOF(2); |
| 303 | |
| 304 | } |
| 305 | else direction = 7; |
| 306 | |
| 307 | } |
| 308 | |
| 309 | // Move down to the correct height |
| 310 | if (direction == 6) { |
| 311 | |
| 312 | if (y < startPos) { |
| 313 | |
| 314 | dx = 0; |
| 315 | dy = ITOF(2); |
| 316 | |
| 317 | } else direction = 7; |
| 318 | |
| 319 | } |
| 320 | |
| 321 | // Cosinus should be near zero before we start hopping. |
| 322 | if (direction == 7) { |
| 323 | |
| 324 | dx = 0; |
| 325 | dy = 0; |
| 326 | |
| 327 | if (cos > -100 && cos < 100) direction = 8; |
| 328 | |
| 329 | } |
| 330 | |
| 331 | // Start hopping |
| 332 | if (direction == 8) { |
| 333 | |
| 334 | if (level->checkMaskUp(x, y) || |
| 335 | level->checkMaskUp(x + width, y)) |
| 336 | setAnimType((animType == E_LEFTANIM) ? E_RIGHTANIM : E_LEFTANIM); |
| 337 | |
| 338 | dy = startPos - abs(cos * 96) - y; |
| 339 | dx = abs(cos * 6); |
| 340 | |
| 341 | if (animType == E_LEFTANIM) |
| 342 | dx *= -1; |
| 343 | |
| 344 | if (cos < 0 && |
| 345 | level->checkMaskDown(x + ITOF(anim->getWidth() / 2), y + TTOF(1))) |
| 346 | direction = 9; |
| 347 | |
| 348 | } |
| 349 | |
| 350 | // Destroy the block underneath |
| 351 | if (direction == 9) { |
| 352 | |
| 353 | // Shake a bit |
| 354 | dx = (FTOI(x) % 2) ? ITOF(1) : -ITOF(1); |
| 355 | dy = 0; |
| 356 | |
| 357 | // Remove the tile |
| 358 | if (cos > 0 && cos < 100) { |
| 359 | |
| 360 | level->setTile( |
| 361 | FTOT(x + ITOF((anim->getWidth() / 2))), |
| 362 | FTOT(y) + 1, |
| 363 | set->magnitude); |
| 364 | |
| 365 | direction = 8; |
| 366 | |
| 367 | } |
| 368 | |
| 369 | } |
| 370 | |
| 371 | } |
| 372 | |
| 373 | // Stage 2: End of behavior |
| 374 | |
| 375 | if (stage == 2) { |
| 376 | |
| 377 | dx = 0; |
| 378 | dy = ITOF(4); |
| 379 | |
| 380 | } |
| 381 | |
| 382 | x += dx; |
| 383 | y += dy; |
| 384 | dx = dx << 6; |
| 385 | dy = dy << 6; |
| 386 | |
| 387 | return this; |
| 388 | |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /** |
| 393 | * Draw episode 1 guardian. |
| 394 | * |
| 395 | * @param ticks Time |
| 396 | * @param change Time since last iteration |
| 397 | */ |
| 398 | void MedGuardian::draw(unsigned int ticks, int change) { |
| 399 | |
| 400 | Anim *stageAnim; |
| 401 | unsigned char frame; |
| 402 | |
| 403 | if (next) next->draw(ticks, change); |
| 404 | |
| 405 | fixed xChange = getDrawX(change); |
| 406 | fixed yChange = getDrawY(change); |
| 407 | |
| 408 | |
| 409 | frame = ticks / (set->animSpeed << 5); |
| 410 | |
| 411 | |
| 412 | if (stage == 0) |
| 413 | stageAnim = anim; |
| 414 | else |
| 415 | stageAnim = level->getAnim(set->anims[E_LFINISHANIM | (animType & 1)] & 0x7F); |
| 416 | |
| 417 | |
| 418 | stageAnim->setFrame(frame + gridX + gridY, true); |
| 419 | |
| 420 | if (ticks < flashTime) stageAnim->flashPalette(0); |
| 421 | |
| 422 | drawnX = x + anim->getXOffset(); |
| 423 | drawnY = y + anim->getYOffset() + stageAnim->getOffset(); |
| 424 | |
| 425 | stageAnim->draw(xChange, yChange); |
| 426 | |
| 427 | if (ticks < flashTime) stageAnim->restorePalette(); |
| 428 | |
| 429 | } |