| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1bullet.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 | * - 11th February 2009: Created bullet.cpp from parts of events.cpp |
| 13 | * - 1st August 2012: Renamed bullet.cpp to jj1bullet.cpp |
| 14 | * |
| 15 | * @par Licence: |
| 16 | * Copyright (c) 2005-2012 AJ Thomson |
| 17 | * |
| 18 | * OpenJazz is distributed under the terms of |
| 19 | * the GNU General Public License, version 2.0 |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include "jj1bullet.h" |
| 25 | #include "event/jj1event.h" |
| 26 | #include "jj1level.h" |
| 27 | #include "jj1bird.h" |
| 28 | #include "jj1levelplayer.h" |
| 29 | |
| 30 | #include "game/game.h" |
| 31 | #include "io/gfx/sprite.h" |
| 32 | #include "io/gfx/video.h" |
| 33 | #include "io/sound.h" |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Generic bullet constructor. |
| 40 | * |
| 41 | * @param nextBullet The next bullet |
| 42 | * @param sourcePlayer The player that fired the bullet (if any) |
| 43 | * @param startX The starting x-coordinate of the bullet |
| 44 | * @param startY The starting y-coordinate of the bullet |
| 45 | * @param bullet Type |
| 46 | * @param newDirection The direction of the bullet |
| 47 | * @param ticks Time |
| 48 | */ |
| 49 | JJ1Bullet::JJ1Bullet (JJ1Bullet* nextBullet, JJ1LevelPlayer* sourcePlayer, fixed startX, fixed startY, signed char* bullet, int newDirection, unsigned int ticks) { |
| 50 | |
| 51 | next = nextBullet; |
| 52 | source = sourcePlayer; |
| 53 | set = bullet; |
| 54 | direction = newDirection; |
| 55 | |
| 56 | x = startX; |
| 57 | y = startY; |
| 58 | dx = set[B_XSPEED + direction] << 18; |
| 59 | dy = set[B_YSPEED + direction] << 16; |
| 60 | |
| 61 | if (source) { |
| 62 | |
| 63 | if ((newDirection & 1) == 0) { |
| 64 | |
| 65 | if (dx > 0) dx = -dx; |
| 66 | |
| 67 | } else { |
| 68 | |
| 69 | if (dx < 0) dx = -dx; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | if (set[B_BEHAVIOUR] == 4) dx += source->getXSpeed(); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | sprite = level->getSprite(reinterpret_cast<unsigned char*>(set)[B_SPRITE + direction]); |
| 78 | |
| 79 | time = ticks + T_BULLET; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Delete all bullets. |
| 86 | */ |
| 87 | JJ1Bullet::~JJ1Bullet () { |
| 88 | |
| 89 | if (next) delete next; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Delete this bullet. |
| 96 | * |
| 97 | * @return The next bullet |
| 98 | */ |
| 99 | JJ1Bullet* JJ1Bullet::remove () { |
| 100 | |
| 101 | JJ1Bullet* oldNext; |
| 102 | |
| 103 | oldNext = next; |
| 104 | next = NULL; |
| 105 | delete this; |
| 106 | |
| 107 | return oldNext; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Get the player responsible for this bullet. |
| 114 | * |
| 115 | * @return The player (NULL if fired by an event) |
| 116 | */ |
| 117 | JJ1LevelPlayer* JJ1Bullet::getSource () { |
| 118 | |
| 119 | return source; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Bullet iteration. |
| 126 | * |
| 127 | * @param ticks Time |
| 128 | * |
| 129 | * @return Remaining bullet |
| 130 | */ |
| 131 | JJ1Bullet* JJ1Bullet::step (unsigned int ticks) { |
| 132 | |
| 133 | // Process the next bullet |
| 134 | if (next) next = next->step(ticks); |
| 135 | |
| 136 | |
| 137 | if (level->getStage() != LS_END) { |
| 138 | |
| 139 | // If the time has expired, destroy the bullet |
| 140 | if (ticks > time) return remove(); |
| 141 | |
| 142 | |
| 143 | // Check if a player has been hit |
| 144 | for (int i = 0; i < nPlayers; i++) { |
| 145 | |
| 146 | if (players[i].getJJ1LevelPlayer()->overlap(x, y, |
| 147 | ITOF(sprite->getWidth()), ITOF(sprite->getHeight()))) { |
| 148 | |
| 149 | // If the hit was successful, destroy the bullet |
| 150 | if (players[i].getJJ1LevelPlayer()->hit(source? source->player: NULL, ticks)) return remove(); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | |
| 157 | if (source) { |
| 158 | |
| 159 | // Check if an event has been hit |
| 160 | |
| 161 | JJ1Event* event = level->getEvents(); |
| 162 | |
| 163 | while (event) { |
| 164 | |
| 165 | // Check if the event has been hit |
| 166 | if (event->overlap(x, y, |
| 167 | ITOF(sprite->getWidth()), ITOF(sprite->getHeight()))) { |
| 168 | |
| 169 | // If the event is hittable, hit it and destroy the bullet |
| 170 | if (event->hit(source, 1, ticks)) return remove(); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | event = event->getNext(); |
| 175 | |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | |
| 181 | } |
| 182 | |
| 183 | |
| 184 | // If the scenery has been hit and this is not a bouncer, destroy the bullet |
| 185 | if (level->checkMaskUp(x, y) && (set[B_BEHAVIOUR] != 4)) { |
| 186 | |
| 187 | // FIXME: rewrite "set" |
| 188 | auto se = static_cast<SE::Type>(set[B_FINISHSOUND]); |
| 189 | playSound(se); |
| 190 | |
| 191 | return remove(); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | |
| 196 | // Calculate trajectory |
| 197 | if (set[B_BEHAVIOUR] == 4) { |
| 198 | |
| 199 | if (level->checkMaskDown(x, y - F1)) { |
| 200 | |
| 201 | // Bounce the bullet away from a vertical surface |
| 202 | if (dx < 0) direction |= 1; |
| 203 | else direction &= ~1; |
| 204 | |
| 205 | dx = -dx; |
| 206 | dy = 0; |
| 207 | |
| 208 | } |
| 209 | |
| 210 | if (level->checkMaskDown(x, y + (dy >> 6) - F1)) { |
| 211 | |
| 212 | // Bounce the bullet against a horizontal surface |
| 213 | if (dy < 0) dy = 0; |
| 214 | else dy = -dy - (abs(dx - (set[B_XSPEED + direction] << 18)) >> 1); |
| 215 | |
| 216 | } else { |
| 217 | |
| 218 | // Respond to gravity |
| 219 | dy += F32 * set[B_GRAVITY]; |
| 220 | |
| 221 | } |
| 222 | |
| 223 | } else { |
| 224 | |
| 225 | dy += F32 * set[B_GRAVITY]; |
| 226 | |
| 227 | if (source && (abs(source->getXSpeed() + dx) > abs(dx))) { |
| 228 | |
| 229 | x += source->getXSpeed() >> 6; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | } |
| 234 | |
| 235 | |
| 236 | // Apply trajectory |
| 237 | x += dx >> 6; |
| 238 | y += dy >> 6; |
| 239 | |
| 240 | |
| 241 | // Do not destroy the bullet |
| 242 | return this; |
| 243 | |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * Draw the bullet. |
| 249 | * |
| 250 | * @param change Time since last iteration |
| 251 | */ |
| 252 | void JJ1Bullet::draw (int change) { |
| 253 | |
| 254 | if (next) next->draw(change); |
| 255 | |
| 256 | // Show the bullet |
| 257 | sprite->draw(FTOI(getDrawX(change)), FTOI(getDrawY(change)), false); |
| 258 | |
| 259 | } |