| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1event.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 | * - 5th February 2009: Added parts of events.cpp and level.cpp to player.cpp |
| 13 | * - 11th February 2009: Created bullet.cpp from parts of events.cpp |
| 14 | * - 1st March 2009: Created bird.cpp from parts of events.cpp |
| 15 | * - 19th March 2009: Created sprite.cpp from parts of event.cpp and player.cpp |
| 16 | * - 19th July 2009: Created eventframe.cpp from parts of events.cpp |
| 17 | * - 19th July 2009: Renamed events.cpp to event.cpp |
| 18 | * - 2nd March 2010: Created guardians.cpp from parts of event.cpp and |
| 19 | * eventframe.cpp |
| 20 | * - 2nd March 2010: Created bridge.cpp from parts of event.cpp and eventframe.cpp |
| 21 | * - 5th February 2011: Moved parts of eventframe.cpp to event.cpp |
| 22 | * - 1st August 2012: Renamed event.cpp to jj1event.cpp |
| 23 | * |
| 24 | * @par Licence: |
| 25 | * Copyright (c) 2005-2013 AJ Thomson |
| 26 | * |
| 27 | * OpenJazz is distributed under the terms of |
| 28 | * the GNU General Public License, version 2.0 |
| 29 | * |
| 30 | * @par Description: |
| 31 | * Deals with events in ordinary levels. |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | |
| 36 | #include "../jj1level.h" |
| 37 | #include "jj1event.h" |
| 38 | |
| 39 | #include "io/gfx/video.h" |
| 40 | #include "io/sound.h" |
| 41 | #include "util.h" |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Create event |
| 46 | * |
| 47 | * @param gX X-coordinate |
| 48 | * @param gY Y-coordinate |
| 49 | */ |
| 50 | JJ1Event::JJ1Event (unsigned char gX, unsigned char gY) { |
| 51 | |
| 52 | set = level->getEvent(gX, gY); |
| 53 | |
| 54 | x = TTOF(gX); |
| 55 | y = TTOF(gY + 1); |
| 56 | dx = 0; |
| 57 | dy = 0; |
| 58 | |
| 59 | next = level->getEvents(); |
| 60 | gridX = gX; |
| 61 | gridY = gY; |
| 62 | flashTime = 0; |
| 63 | |
| 64 | animType = E_NOANIM; |
| 65 | anim = NULL; |
| 66 | noAnimOffset = false; |
| 67 | |
| 68 | drawnX = x; |
| 69 | drawnY = y; |
| 70 | width = F32; |
| 71 | height = F32; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Delete all events |
| 78 | */ |
| 79 | JJ1Event::~JJ1Event () { |
| 80 | |
| 81 | if (next) delete next; |
| 82 | |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Delete this event |
| 88 | * |
| 89 | * @param permanently Whether or not to delete the event from the level |
| 90 | * |
| 91 | * @return The next event |
| 92 | */ |
| 93 | JJ1Event* JJ1Event::remove (bool permanently) { |
| 94 | |
| 95 | JJ1Event *oldNext; |
| 96 | |
| 97 | if (permanently) level->clearEvent(gridX, gridY); |
| 98 | |
| 99 | oldNext = next; |
| 100 | next = NULL; |
| 101 | delete this; |
| 102 | |
| 103 | return oldNext; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Get the next event |
| 110 | * |
| 111 | * @return The next event |
| 112 | */ |
| 113 | JJ1Event * JJ1Event::getNext () { |
| 114 | |
| 115 | return next; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Initiate the destruction of the event |
| 122 | * |
| 123 | * @param ticks Time |
| 124 | */ |
| 125 | void JJ1Event::destroy (unsigned int ticks) { |
| 126 | |
| 127 | setAnimType(E_LFINISHANIM | (animType & 1)); |
| 128 | |
| 129 | level->setEventTime(gridX, gridY, ticks); |
| 130 | |
| 131 | playSound(set->sound); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Deal with bullet collisions |
| 138 | * |
| 139 | * @param source Source of the hit(s) |
| 140 | * @param hits Number of hits to inflict |
| 141 | * @param ticks Current time |
| 142 | * |
| 143 | * @return Whether or not the hit was successful |
| 144 | */ |
| 145 | bool JJ1Event::hit (JJ1LevelPlayer *source, int hits, unsigned int ticks) { |
| 146 | |
| 147 | // Check if event has already been destroyed |
| 148 | if (((animType & ~1) == E_LFINISHANIM) || (ticks < flashTime)) return false; |
| 149 | |
| 150 | int hitsRemaining = level->hitEvent(gridX, gridY, hits, source, ticks); |
| 151 | |
| 152 | // If the event cannot be hit, do not register hit |
| 153 | if (hitsRemaining < 0) return false; |
| 154 | |
| 155 | // Check if the hit has destroyed the event |
| 156 | if (hitsRemaining == 0) destroy(ticks); |
| 157 | |
| 158 | // The event has been hit, so it should flash |
| 159 | flashTime = ticks + T_FLASH; |
| 160 | |
| 161 | // Register hit |
| 162 | return true; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Determine whether or not the event is an enemy |
| 169 | * |
| 170 | * @return Whether or not the event is an enemy |
| 171 | */ |
| 172 | bool JJ1Event::isEnemy () { |
| 173 | |
| 174 | return set->strength && (set->modifier == 0); |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /** |
| 180 | * Determine whether or not the event is from the given position |
| 181 | * |
| 182 | * @return Whether or not the event is from the given position |
| 183 | */ |
| 184 | bool JJ1Event::isFrom (unsigned char gX, unsigned char gY) { |
| 185 | |
| 186 | return (gX == gridX) && (gY == gridY); |
| 187 | |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Calculate the width and height of the event |
| 193 | */ |
| 194 | void JJ1Event::calcDimensions () { |
| 195 | |
| 196 | if (animType == E_NOANIM) { |
| 197 | |
| 198 | width = F32; |
| 199 | height = F32; |
| 200 | |
| 201 | } else { |
| 202 | |
| 203 | width = ITOF(anim->getWidth()); |
| 204 | height = ITOF(anim->getHeight()); |
| 205 | |
| 206 | // Blank sprites for e.g. invisible springs |
| 207 | if ((width == F1) && (height == F1)) { |
| 208 | |
| 209 | width = F32; |
| 210 | height = F32; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * Determine whether or not the event is overlapping the given area |
| 221 | * |
| 222 | * @param areaX The x-coordinate of the left of the area |
| 223 | * @param areaY The y-coordinate of the top of the area |
| 224 | * @param areaWidth The width of the area |
| 225 | * @param areaHeight The height of the area |
| 226 | * |
| 227 | * @return Whether or not there is an overlap |
| 228 | */ |
| 229 | bool JJ1Event::overlap (fixed areaX, fixed areaY, fixed areaWidth, fixed areaHeight) { |
| 230 | |
| 231 | return (drawnX + width >= areaX) && |
| 232 | (drawnX < areaX + areaWidth) && |
| 233 | (drawnY + height >= areaY) && |
| 234 | (drawnY < areaY + areaHeight); |
| 235 | |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /** |
| 240 | * Sets the animation type and updates the current animation and dimensions |
| 241 | * |
| 242 | * @param type The new animation type |
| 243 | */ |
| 244 | void JJ1Event::setAnimType(unsigned char type) { |
| 245 | |
| 246 | if (type == animType) return; |
| 247 | |
| 248 | animType = type; |
| 249 | |
| 250 | if (animType == E_NOANIM) anim = NULL; |
| 251 | |
| 252 | // If there is no shooting animation, use the normal animation instead |
| 253 | else if (((animType & ~1) == E_LSHOOTANIM) && (set->anims[animType] == 0)) |
| 254 | anim = level->getAnim(set->anims[animType & 1] & 0x7F); |
| 255 | |
| 256 | else anim = level->getAnim(set->anims[animType] & 0x7F); |
| 257 | |
| 258 | calcDimensions(); |
| 259 | |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | * Sets the animation frame and updates the current dimensions |
| 265 | */ |
| 266 | void JJ1Event::setAnimFrame (int frame, bool looping) { |
| 267 | |
| 268 | if (!anim) return; |
| 269 | |
| 270 | anim->setFrame(frame, looping); |
| 271 | calcDimensions(); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /** |
| 277 | * Functionality required by all event types on each iteration |
| 278 | * |
| 279 | * @param ticks Time |
| 280 | * |
| 281 | * @return Animation |
| 282 | */ |
| 283 | JJ1EventType* JJ1Event::prepareStep (unsigned int ticks) { |
| 284 | |
| 285 | // Process the next event |
| 286 | if (next) next = next->step(ticks); |
| 287 | |
| 288 | // If the event has been removed from the grid, destroy it |
| 289 | if (!set) return NULL; |
| 290 | |
| 291 | // If the event and its origin are off-screen, the event is not in the |
| 292 | // process of self-destruction, remove it |
| 293 | if (((animType & ~1) != E_LFINISHANIM) && |
| 294 | ((x < viewX - F192) || (x > viewX + ITOF(canvasW) + F192) || |
| 295 | (y < viewY - F160) || (y > viewY + ITOF(canvasH) + F160)) && |
| 296 | ((gridX < FTOT(viewX) - 1) || |
| 297 | (gridX > ITOT(FTOI(viewX) + canvasW) + 1) || |
| 298 | (gridY < FTOT(viewY) - 1) || |
| 299 | (gridY > ITOT(FTOI(viewY) + canvasH) + 1))) return NULL; |
| 300 | |
| 301 | return set; |
| 302 | |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /** |
| 307 | * Draw the event's energy bar |
| 308 | * |
| 309 | * @param ticks Time |
| 310 | */ |
| 311 | void JJ1Event::drawEnergy (unsigned int ticks) { |
| 312 | |
| 313 | Anim* miscAnim; |
| 314 | int hits; |
| 315 | |
| 316 | if (!set || set->modifier != 8) { |
| 317 | |
| 318 | if (next) next->drawEnergy(ticks); |
| 319 | |
| 320 | return; |
| 321 | |
| 322 | } else if (set->strength) { |
| 323 | |
| 324 | // Draw boss energy bar |
| 325 | |
| 326 | hits = level->getEventHits(gridX, gridY) * 100 / set->strength; |
| 327 | |
| 328 | |
| 329 | // Devan head |
| 330 | |
| 331 | miscAnim = level->getMiscAnim(MA_DEVHEAD); |
| 332 | miscAnim->setFrame(0, true); |
| 333 | |
| 334 | if (ticks < flashTime) miscAnim->flashPalette(0); |
| 335 | |
| 336 | miscAnim->draw(ITOF(canvasW - 44), ITOF(hits + 48)); |
| 337 | |
| 338 | if (ticks < flashTime) miscAnim->restorePalette(); |
| 339 | |
| 340 | |
| 341 | // Bar |
| 342 | video.drawRect(canvasW - 40, hits + 40, 12, 100 - hits, (ticks < flashTime)? 0: 32); |
| 343 | |
| 344 | } |
| 345 | |
| 346 | } |