| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file jj1bird.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 | * - 1st March 2009: Created bird.cpp from parts of events.cpp |
| 13 | * - 1st August 2012: Renamed bird.cpp to jj1bird.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 "io/gfx/video.h" |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Create a bird for the specified player. |
| 35 | * |
| 36 | * @param birds The player's existing birds (NULL if none) |
| 37 | * @param rescuer The player that freed the bird |
| 38 | * @param gX The new bird's grid x-coordinate |
| 39 | * @param gY The new bird's grid y-coordinate |
| 40 | */ |
| 41 | JJ1Bird::JJ1Bird (JJ1Bird* birds, JJ1LevelPlayer *rescuer, unsigned char gX, unsigned char gY) { |
| 42 | |
| 43 | next = birds; |
| 44 | player = rescuer; |
| 45 | x = TTOF(gX); |
| 46 | y = TTOF(gY); |
| 47 | dx = 0; |
| 48 | dy = 0; |
| 49 | fleeing = false; |
| 50 | fireTime = 0; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Delete all birds. |
| 57 | */ |
| 58 | JJ1Bird::~JJ1Bird () { |
| 59 | |
| 60 | if (next) delete next; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Delete this bird. |
| 67 | * |
| 68 | * @return The next bird |
| 69 | */ |
| 70 | JJ1Bird* JJ1Bird::remove () { |
| 71 | |
| 72 | JJ1Bird* oldNext; |
| 73 | |
| 74 | oldNext = next; |
| 75 | next = NULL; |
| 76 | delete this; |
| 77 | |
| 78 | return oldNext; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * Get the player that freed the bird. |
| 85 | * |
| 86 | * @return The player |
| 87 | */ |
| 88 | JJ1LevelPlayer * JJ1Bird::getPlayer () { |
| 89 | |
| 90 | return player; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Notify the bird that the player has been hit. |
| 97 | */ |
| 98 | void JJ1Bird::hit () { |
| 99 | |
| 100 | fleeing = true; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Recursively count the number of birds. |
| 107 | * |
| 108 | * @return The number of birds ahead of this bird, plus one |
| 109 | */ |
| 110 | int JJ1Bird::getFlockSize () { |
| 111 | |
| 112 | if (next) return next->getFlockSize() + 1; |
| 113 | |
| 114 | return 1; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * Recursively set the number of birds. |
| 121 | * |
| 122 | * @param size The number of birds not already counted |
| 123 | * |
| 124 | * @return Remaining bird (NULL if none) |
| 125 | */ |
| 126 | JJ1Bird* JJ1Bird::setFlockSize (int size) { |
| 127 | |
| 128 | if (size <= 0) { |
| 129 | |
| 130 | delete this; |
| 131 | return NULL; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | if (size > 1) { |
| 136 | |
| 137 | if (!next) next = new JJ1Bird(NULL, player, FTOT(x), FTOT(y)); |
| 138 | |
| 139 | next = next->setFlockSize(size - 1); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | return this; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * JJ1Bird iteration. |
| 150 | * |
| 151 | * @param ticks Time |
| 152 | * |
| 153 | * @return Remaining bird (NULL if none) |
| 154 | */ |
| 155 | JJ1Bird* JJ1Bird::step (unsigned int ticks) { |
| 156 | |
| 157 | Movable* leader; |
| 158 | JJ1Event* event; |
| 159 | |
| 160 | // Process the next bird |
| 161 | if (next) next = next->step(ticks); |
| 162 | |
| 163 | if (next) leader = next; |
| 164 | else leader = player; |
| 165 | |
| 166 | if (fleeing) { |
| 167 | |
| 168 | // Trajectory for flying away |
| 169 | dx = F80; |
| 170 | dy = -F80; |
| 171 | |
| 172 | // If the bird has flown off-screen, remove it |
| 173 | if (y < viewY - F160) return remove(); |
| 174 | |
| 175 | } else { |
| 176 | |
| 177 | // Trajectory for flying towards the leader |
| 178 | |
| 179 | if ((x < leader->getX() - F160) || (x > leader->getX() + F160)) { |
| 180 | |
| 181 | // Far away from the leader |
| 182 | // Approach the leader at a speed proportional to the distance |
| 183 | |
| 184 | dx = leader->getX() - x; |
| 185 | |
| 186 | } else if (x < leader->getX()) { |
| 187 | |
| 188 | // To the left of the leader, so move right |
| 189 | |
| 190 | if (dx < F80) dx += 6400; |
| 191 | |
| 192 | } else { |
| 193 | |
| 194 | // To the right of the leader, so move left |
| 195 | |
| 196 | if (dx > -F80) dx -= 6400; |
| 197 | |
| 198 | } |
| 199 | |
| 200 | if (y > level->getWaterLevel() - F24) { |
| 201 | |
| 202 | // Always stay above water |
| 203 | |
| 204 | y = level->getWaterLevel() - F24; |
| 205 | dy = 0; |
| 206 | |
| 207 | } else { |
| 208 | |
| 209 | if ((y < leader->getY() - F100) || (y > leader->getY() + F100)) { |
| 210 | |
| 211 | // Far away from the leader |
| 212 | // Approach the leader at a speed proportional to the distance |
| 213 | |
| 214 | dy = (leader->getY() - F64) - y; |
| 215 | |
| 216 | } else if (y < leader->getY() - F64) { |
| 217 | |
| 218 | // Above the leader, so move downwards |
| 219 | |
| 220 | if (dy < F80) dy += 6400; |
| 221 | |
| 222 | } else { |
| 223 | |
| 224 | // Below the leader, so move upwards |
| 225 | |
| 226 | if (dy > -F80) dy -= 6400; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | |
| 231 | } |
| 232 | |
| 233 | |
| 234 | if (ticks > fireTime) { |
| 235 | |
| 236 | // Check for nearby targets |
| 237 | |
| 238 | bool target = false; |
| 239 | event = level->getEvents(); |
| 240 | |
| 241 | if (player->getFacing()) { |
| 242 | |
| 243 | while (event && !target) { |
| 244 | |
| 245 | target = event->isEnemy() && event->overlap(x, y, F160, F100); |
| 246 | |
| 247 | event = event->getNext(); |
| 248 | |
| 249 | } |
| 250 | |
| 251 | } else { |
| 252 | |
| 253 | while (event && !target) { |
| 254 | |
| 255 | target = event->isEnemy() && event->overlap(x - F160, y, F160, F100); |
| 256 | |
| 257 | event = event->getNext(); |
| 258 | |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | // If there is a target in the vicinity, generate bullets |
| 264 | if (target) { |
| 265 | |
| 266 | level->createBullet(player, |
| 267 | 0, |
| 268 | 0, |
| 269 | x + (player->getFacing()? PXO_R: PXO_L), |
| 270 | y, |
| 271 | 30, |
| 272 | player->getFacing(), |
| 273 | ticks); |
| 274 | |
| 275 | fireTime = ticks + T_BIRD_FIRE; |
| 276 | |
| 277 | } |
| 278 | |
| 279 | } |
| 280 | |
| 281 | } |
| 282 | |
| 283 | // Apply trajectory |
| 284 | x += dx >> 6; |
| 285 | y += dy >> 6; |
| 286 | |
| 287 | return this; |
| 288 | |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /** |
| 293 | * Draw the bird. |
| 294 | * |
| 295 | * @param ticks Time |
| 296 | * @param change Time since last step |
| 297 | */ |
| 298 | void JJ1Bird::draw (unsigned int ticks, int change) { |
| 299 | |
| 300 | Anim *anim; |
| 301 | |
| 302 | if (next) next->draw(ticks, change); |
| 303 | |
| 304 | anim = level->getLevelAnim((player->getFacing() || fleeing)? LA_RBIRD: LA_LBIRD); |
| 305 | anim->setFrame(ticks / 80, true); |
| 306 | |
| 307 | anim->draw(getDrawX(change), getDrawY(change)); |
| 308 | |
| 309 | } |