Debian Codegraph @

src/jj2/level/event/jj2eventframe.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file jj2eventframe.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created level.c
10 * - 22nd July 2008: Created levelload.c from parts of level.c
11 * - 3rd February 2009: Renamed level.c to level.cpp
12 * - 19th July 2009: Added parts of levelload.cpp to level.cpp
13 * - 29th June 2010: Created jj2level.cpp from parts of level.cpp
14 * - 2nd July 2010: Created jj2eventframe.cpp from parts of jj2level.cpp
15 *
16 * @par Licence:
17 * Copyright (c) 2005-2013 AJ Thomson
18 *
19 * OpenJazz is distributed under the terms of
20 * the GNU General Public License, version 2.0
21 *
22 * @par Description:
23 * Deals with the playing of JJ2 levels.
24 *
25 */
26
27
28 #include "jj2event.h"
29 #include "../jj2level.h"
30 #include "../jj2levelplayer.h"
31
32 #include "io/gfx/anim.h"
33 #include "io/gfx/font.h"
34 #include "io/gfx/video.h"
35
36
37 /// Look-up table for ammo animations (in animSet 0)
38 const unsigned char ammoAnims[] = {
39 29, // Ice
40 25, // Bouncer
41 34, // Seeker
42 49, // RF
43 57, // Toaster
44 59, // TNT
45 62, // Pellets
46 69 // Sparks
47 };
48
49 /// Look-up table for food etc. animations (in animSet 67/71)
50 const unsigned char pickupAnims[] = {
51 0, // 0
52 0, // 1
53 0, // 2
54 0, // 3
55 0, // 4
56 0, // 5
57 0, // 6
58 0, // 7
59 0, // 8
60 0, // 9
61 0, // 10
62 0, // 11
63 0, // 12
64 0, // 13
65 0, // 14
66 0, // 15
67 0, // 16
68 0, // 17
69 0, // 18
70 0, // 19
71 0, // 20
72 0, // 21
73 0, // 22
74 0, // 23
75 0, // 24
76 0, // 25
77 0, // 26
78 0, // 27
79 0, // 28
80 0, // 29
81 0, // 30
82 0, // 31
83 0, // 32
84 0, // 33
85 0, // 34
86 0, // 35
87 0, // 36
88 0, // 37
89 0, // 38
90 0, // 39
91 0, // 40
92 0, // 41
93 0, // 42
94 0, // 43
95 84, // Silver coin
96 37, // Gold coin
97 0, // 46
98 0, // 47
99 0, // 48
100 0, // 49
101 0, // 50
102 0, // 51
103 0, // 52
104 55, // Ice crate
105 54, // Bouncer crate
106 56, // Seeker crate
107 57, // RF crate
108 58, // Toaster crate
109 90, // Armed TNT
110 36, // Board
111 0, // 60
112 29, // Rapid fire (can also be 30)
113 0, // 62
114 35, // Red gem
115 35, // Green gem
116 35, // Blue gem
117 35, // Purple gem
118 34, // Large red gem
119 0, // 68
120 3, // Ammo barrel
121 0, // 70
122 0, // 71
123 82, // Energy
124 72, // Full energy
125 31, // Fire shield
126 10, // Bubble shield
127 51, // Plasma shield
128 0, // 77
129 0, // 78
130 33, // High jump
131 0, // 1-up
132 28, // exit signpost
133 0, // 82
134 14, // Checkpoint
135 0, // 84
136 0, // 85
137 0, // 86
138 0, // 87
139 0, // 88
140 87, // Extra time
141 42, // Freeze
142 0, // 91
143 0, // 92
144 0, // 93
145 0, // 94
146 52, // Trigger crate
147 0, // 96
148 0, // 97
149 0, // 98
150 0, // 99
151 0, // 100
152 0, // 101
153 0, // 102
154 0, // 103
155 0, // 104
156 0, // 105
157 0, // 106
158 0, // 107
159 0, // 108
160 0, // 109
161 0, // 110
162 0, // 111
163 0, // 112
164 0, // 113
165 0, // 114
166 0, // 115
167 0, // 116
168 0, // 117
169 0, // 118
170 0, // 119
171 0, // 120
172 0, // 121
173 0, // 122
174 0, // 123
175 0, // 124
176 0, // 125
177 0, // 126
178 0, // 127
179 0, // 128
180 0, // 129
181 0, // 130
182 60, // Blaster PU (can also be 83)
183 61, // Bouncer PU
184 62, // Ice PU
185 63, // Seeker PU
186 64, // RF PU
187 65, // Toaster PU
188 0, // 137
189 0, // 138
190 0, // 139
191 0, // 140
192 1, // Apple
193 2, // Banana
194 16, // Cherry
195 71, // Orange
196 74, // Pear
197 79, // Pretzel
198 81, // Strawberry
199 0, // 148
200 0, // 149
201 0, // 150
202 0, // 151
203 0, // 152
204 0, // 153
205 48, // Lemon
206 50, // Lime
207 89, // Thing
208 92, // Watermelon
209 73, // Peach
210 38, // Grapes
211 49, // Lettuce
212 26, // Aubergine
213 23, // Cucumber
214 75, // Jazzade
215 20, // Cola
216 53, // Milk
217 76, // Tart
218 12, // Cake
219 25, // Doughnut
220 24, // Cupcake
221 18, // Crisps
222 13, // Sweet
223 19, // Chocolate
224 43, // Ice cream
225 11, // Burger
226 77, // Pizza
227 32, // Chips
228 17, // Chicken drumstick
229 80, // Sandwich
230 88, // Taco
231 91, // Hot dog
232 39, // Ham
233 15, // Cheese
234 0, // 183
235 0, // 184
236 0, // 185
237 0, // 186
238 0, // 187
239 0, // 188
240 0, // 189
241 0, // 190
242 0, // 191
243 0, // 192
244 0, // 193
245 0, // 194
246 0, // 195
247 0, // 196
248 0, // 197
249 0, // 198
250 0, // 199
251 0, // 200
252 0, // 201
253 0, // 202
254 0, // 203
255 0, // 204
256 0, // 205
257 0, // 206
258 0, // 207
259 0, // 208
260 0, // 209
261 0, // 210
262 0, // 211
263 0, // 212
264 0, // 213
265 0, // 214
266 0, // 215
267 0, // 216
268 0, // 217
269 0, // 218
270 0, // 219
271 66, // Pellet PU
272 67, // Sparks PU
273 };
274
275
276 /**
277 * Functionality required by all event types on each iteration
278 *
279 * @param ticks Time
280 * @param msps Ticks per step
281 *
282 * @return Whether or not the event should be deleted
283 */
284 bool JJ2Event::prepareStep (unsigned int ticks, int msps) {
285
286 int count;
287
288
289 // Process next event(s)
290 if (next) next = next->step(ticks, msps);
291
292
293 // If the reaction time has expired
294 if (endTime && (ticks > endTime)) {
295
296 return true;
297
298 }
299
300
301 if (endTime) return false;
302
303
304 // Handle contact with player
305
306 for (count = 0; count < nPlayers; count++) {
307
308 JJ2LevelPlayer *levelPlayer = players[count].getJJ2LevelPlayer();
309
310 // Check if the player is touching the event
311 if (levelPlayer->overlap(x, y, F32, F32)) {
312
313 // If the player picks up the event, destroy it
314 if (levelPlayer->touchEvent(this, ticks, msps)) destroy(ticks);
315
316 }
317
318 }
319
320 return false;
321
322 }
323
324
325 /**
326 * Functionality required by all event types on each draw
327 *
328 * @param ticks Time
329 * @param change Time since last iteration
330 *
331 * @return Whether or not the event shouldn't be drawn
332 */
333 bool JJ2Event::prepareDraw (unsigned int ticks, int change) {
334
335 // Draw next event(s)
336 if (next) next->draw(ticks, change);
337
338 // Don't draw if too far off-screen
339 if ((x < viewX - F64) || (y < viewY - F64) ||
340 (x > viewX + ITOF(canvasW) + F64) || (y > viewY + ITOF(canvasH) + F64)) return true;
341
342 return false;
343
344 }
345
346
347 /**
348 * Pickup event iteration.
349 *
350 * @param ticks Time
351 * @param msps Ticks per step
352 *
353 * @return Remaining event
354 */
355 JJ2Event* PickupJJ2Event::step (unsigned int ticks, int msps) {
356
357 if (prepareStep(ticks, msps)) return remove();
358
359 if (!floating) {
360
361 if (jj2Level->checkMaskDown(x, y + F4, false)) {
362
363 dy += 1000 / msps;
364
365 } else {
366
367 dy = 0;
368
369 }
370
371 } else {
372
373 /// @todo Check for bullet overlap
374 // floating = false;
375
376 }
377
378 return this;
379
380 }
381
382
383 /**
384 * Draw ammo pickup event.
385 *
386 * @param ticks Time
387 * @param change Time since last iteration
388 */
389 void AmmoJJ2Event::draw (unsigned int ticks, int change) {
390
391 Anim* an;
392 int drawX, drawY;
393
394 if (prepareDraw(ticks, change)) return;
395
396 drawX = getDrawX(change);
397 drawY = getDrawY(change);
398
399 /// @todo Check if ammo is powered up
400 if (!endTime) an = jj2Level->getAnim(0, ammoAnims[type - 33], flipped);
401 else an = jj2Level->getAnim(animSet, 86, flipped);
402
403 an->setFrame((int)ticks / 60, true);
404 an->draw(drawX + F16, drawY + F16 + F32);
405
406 }
407
408
409 /**
410 * Map gem animation's palette.
411 *
412 * @param anim Gem animation
413 * @param start Index of first colour
414 */
415 void CoinGemJJ2Event::mapPalette (Anim* anim, int start) {
416
417 SDL_Color palette[MAX_PALETTE_COLORS];
418 int count;
419
420 for (count = 0; count < 112; count++)
421 palette[count + 128].r = palette[count + 128].g = palette[count + 128].b = start + 6 - (count >> 4);
422 for (; count < 128; count++)
423 palette[count + 128].r = palette[count + 128].g = palette[count + 128].b = 255;
424
425 anim->setPalette(palette, 128, 128);
426
427 }
428
429
430 /**
431 * Draw coin/gem pickup event.
432 *
433 * @param ticks Time
434 * @param change Time since last iteration
435 */
436 void CoinGemJJ2Event::draw (unsigned int ticks, int change) {
437
438 Anim* an;
439 int drawX, drawY;
440
441 if (prepareDraw(ticks, change)) return;
442
443 drawX = getDrawX(change);
444 drawY = getDrawY(change);
445
446 if (endTime) {
447
448 an = jj2Level->getAnim(animSet, 86, flipped);
449 an->setFrame((int)ticks / 60, true);
450 an->draw(drawX + F16, drawY + F16 + F32);
451
452 }
453
454 if (type == 44) an = jj2Level->getAnim(animSet, 84, flipped); // Silver coin
455 else if (type == 45) an = jj2Level->getAnim(animSet, 37, flipped); // Gold coin
456 else an = jj2Level->getAnim(animSet, 35, flipped); // Gem
457
458 an->setFrame((int)ticks / 60, true);
459
460 switch (type) {
461
462 case 63: // Red gem
463
464 mapPalette(an, 48);
465
466 break;
467
468 case 64: // Green gem
469
470 mapPalette(an, 80);
471
472 break;
473
474 case 65: // Blue gem
475
476 mapPalette(an, 32);
477
478 break;
479
480 case 66: // Purple gem
481
482 mapPalette(an, 88);
483
484 break;
485
486 }
487
488 if (endTime) {
489
490 drawX += (ticks + 500 - endTime) << 8;
491 drawY += (ticks + 500 - endTime) << 10;
492
493 }
494
495 an->draw(drawX + F16, drawY + F16 + F32);
496
497 }
498
499
500 /**
501 * Draw food pickup event.
502 *
503 * @param ticks Time
504 * @param change Time since last iteration
505 */
506 void FoodJJ2Event::draw (unsigned int ticks, int change) {
507
508 Anim* an;
509 int drawX, drawY;
510
511 if (prepareDraw(ticks, change)) return;
512
513 drawX = getDrawX(change);
514 drawY = getDrawY(change);
515
516 // Use look-up table
517 if (!endTime) an = jj2Level->getAnim(animSet, pickupAnims[type], flipped);
518 else an = jj2Level->getAnim(animSet, 86, flipped);
519
520 an->setFrame((int)ticks / 60, true);
521 an->draw(drawX + F16, drawY + F16 + F32);
522
523 }
524
525
526 /**
527 * Spring event iteration.
528 *
529 * @param ticks Time
530 * @param msps Ticks per step
531 *
532 * @return Remaining event
533 */
534 JJ2Event* SpringJJ2Event::step (unsigned int ticks, int msps) {
535
536 if (prepareStep(ticks, msps)) return remove();
537
538 if (!jj2Level->checkMaskDown(x, y + F1, true)) y += F1;
539
540 return this;
541
542 }
543
544
545 /**
546 * Draw spring event.
547 *
548 * @param ticks Time
549 * @param change Time since last iteration
550 */
551 void SpringJJ2Event::draw (unsigned int ticks, int change) {
552
553 Anim* an;
554 int drawX, drawY;
555
556 if (prepareDraw(ticks, change)) return;
557
558 drawX = getDrawX(change);
559 drawY = getDrawY(change);
560
561 switch (type) {
562
563 case 60: // Frozen green spring
564
565 an = jj2Level->getAnim(animSet, 5, flipped);
566
567 break;
568
569 case 62: // Spring crate
570
571 an = jj2Level->getAnim(animSet, 0, flipped);
572
573 break;
574
575 case 85: // Red spring
576
577 an = jj2Level->getAnim(animSet, 7, flipped);
578
579 break;
580
581 case 86: // Green spring
582
583 an = jj2Level->getAnim(animSet, 5, flipped);
584
585 break;
586
587 case 87: // Blue spring
588
589 an = jj2Level->getAnim(animSet, 0, flipped);
590
591 break;
592
593 default:
594
595 return;
596
597 }
598
599 an->setFrame(0, true);
600 an->draw(drawX + F16, drawY + F16);
601
602 }
603
604
605 /**
606 * Placeholder event iteration.
607 *
608 * @param ticks Time
609 * @param msps Ticks per step
610 *
611 * @return Remaining event
612 */
613 JJ2Event* OtherJJ2Event::step (unsigned int ticks, int msps) {
614
615 if (prepareStep(ticks, msps)) return remove();
616
617 return this;
618
619 }
620
621
622 /**
623 * Draw placeholder event.
624 *
625 * @param ticks Time
626 * @param change Time since last iteration
627 */
628 void OtherJJ2Event::draw (unsigned int ticks, int change) {
629
630 Anim* an;
631 int drawX, drawY;
632
633 if (prepareDraw(ticks, change)) return;
634
635 drawX = getDrawX(change);
636 drawY = getDrawY(change);
637
638 switch (type) {
639
640 default:
641
642 if ((type <= 221) && pickupAnims[type]) {
643
644 an = jj2Level->getAnim(animSet, pickupAnims[type], flipped);
645 an->setFrame((int)ticks / 60, true);
646 an->draw(drawX + F16, drawY + F16 + F32);
647
648 } else if (!endTime) {
649
650 drawRect(FTOI(drawX) + 8, FTOI(drawY) + 8, 16, 16, type);
651
652 }
653
654 panelBigFont->showNumber(type, FTOI(drawX) + 24, FTOI(drawY) + 12);
655
656 return;
657
658 }
659
660 an->draw(drawX + F16, drawY + F16);
661
662 }