-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdoom-nano.ino
855 lines (739 loc) · 23.2 KB
/
doom-nano.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
#include "constants.h"
#include "level.h"
#include "sprites.h"
#include "input.h"
#include "entities.h"
#include "types.h"
#include "display.h"
#include "sound.h"
// Useful macros
#define swap(a, b) do { typeof(a) temp = a; a = b; b = temp; } while (0)
#define sign(a, b) (double) (a > b ? 1 : (b > a ? -1 : 0))
// general
uint8_t scene = INTRO;
bool exit_scene = false;
bool invert_screen = false;
uint8_t flash_screen = 0;
// game
// player and entities
Player player;
Entity entity[MAX_ENTITIES];
StaticEntity static_entity[MAX_STATIC_ENTITIES];
uint8_t num_entities = 0;
uint8_t num_static_entities = 0;
void setup(void) {
setupDisplay();
input_setup();
sound_init();
}
// Jump to another scene
void jumpTo(uint8_t target_scene) {
scene = target_scene;
exit_scene = true;
}
// Finds the player in the map
void initializeLevel(const uint8_t level[]) {
for (uint8_t y = LEVEL_HEIGHT - 1; y >= 0; y--) {
for (uint8_t x = 0; x < LEVEL_WIDTH; x++) {
uint8_t block = getBlockAt(level, x, y);
if (block == E_PLAYER) {
player = create_player(x, y);
return;
}
// todo create other static entities
}
}
}
uint8_t getBlockAt(const uint8_t level[], uint8_t x, uint8_t y) {
if (x < 0 || x >= LEVEL_WIDTH || y < 0 || y >= LEVEL_HEIGHT) {
return E_FLOOR;
}
// y is read in inverse order
return pgm_read_byte(level + (((LEVEL_HEIGHT - 1 - y) * LEVEL_WIDTH + x) / 2))
>> (!(x % 2) * 4) // displace part of wanted bits
& 0b1111; // mask wanted bits
}
bool isSpawned(UID uid) {
for (uint8_t i = 0; i < num_entities; i++) {
if (entity[i].uid == uid) return true;
}
return false;
}
bool isStatic(UID uid) {
for (uint8_t i = 0; i < num_static_entities; i++) {
if (static_entity[i].uid == uid) return true;
}
return false;
}
void spawnEntity(uint8_t type, uint8_t x, uint8_t y) {
// Limit the number of spawned entities
if (num_entities >= MAX_ENTITIES) {
return;
}
// todo: read static entity status
switch (type) {
case E_ENEMY:
entity[num_entities] = create_enemy(x, y);
num_entities++;
break;
case E_KEY:
entity[num_entities] = create_key(x, y);
num_entities++;
break;
case E_MEDIKIT:
entity[num_entities] = create_medikit(x, y);
num_entities++;
break;
}
}
void spawnFireball(double x, double y) {
// Limit the number of spawned entities
if (num_entities >= MAX_ENTITIES) {
return;
}
UID uid = create_uid(E_FIREBALL, x, y);
// Remove if already exists, don't throw anything. Not the best, but shouldn't happen too often
if (isSpawned(uid)) return;
// Calculate direction. 32 angles
int16_t dir = FIREBALL_ANGLES + atan2(y - player.pos.y, x - player.pos.x) / PI * FIREBALL_ANGLES;
if (dir < 0) dir += FIREBALL_ANGLES * 2;
entity[num_entities] = create_fireball(x, y, dir);
num_entities++;
}
void removeEntity(UID uid, bool makeStatic = false) {
uint8_t i = 0;
bool found = false;
while (i < num_entities) {
if (!found && entity[i].uid == uid) {
// todo: doze it
found = true;
num_entities--;
}
// displace entities
if (found) {
entity[i] = entity[i + 1];
}
i++;
}
}
void removeStaticEntity(UID uid) {
uint8_t i = 0;
bool found = false;
while (i < num_static_entities) {
if (!found && static_entity[i].uid == uid) {
found = true;
num_static_entities--;
}
// displace entities
if (found) {
static_entity[i] = static_entity[i + 1];
}
i++;
}
}
UID detectCollision(const uint8_t level[], Coords *pos, double relative_x, double relative_y, bool only_walls = false) {
// Wall collision
uint8_t round_x = int(pos->x + relative_x);
uint8_t round_y = int(pos->y + relative_y);
uint8_t block = getBlockAt(level, round_x, round_y);
if (block == E_WALL) {
playSound(hit_wall_snd, HIT_WALL_SND_LEN);
return create_uid(block, round_x, round_y);
}
if (only_walls) {
return UID_null;
}
// Entity collision
for (uint8_t i=0; i < num_entities; i++) {
// Don't collide with itself
if (&(entity[i].pos) == pos) {
continue;
}
uint8_t type = uid_get_type(entity[i].uid);
// Only ALIVE enemy collision
if (type != E_ENEMY || entity[i].state == S_DEAD || entity[i].state == S_HIDDEN) {
continue;
}
Coords new_coords = { entity[i].pos.x - relative_x, entity[i].pos.y - relative_y };
uint8_t distance = coords_distance(pos, &new_coords);
// Check distance and if it's getting closer
if (distance < ENEMY_COLLIDER_DIST && distance < entity[i].distance) {
return entity[i].uid;
}
}
return UID_null;
}
// Shoot
void fire() {
playSound(shoot_snd, SHOOT_SND_LEN);
for (uint8_t i = 0; i < num_entities; i++) {
// Shoot only ALIVE enemies
if (uid_get_type(entity[i].uid) != E_ENEMY || entity[i].state == S_DEAD || entity[i].state == S_HIDDEN) {
continue;
}
Coords transform = translateIntoView(&(entity[i].pos));
if (abs(transform.x) < 20 && transform.y > 0) {
uint8_t damage = (double) min(GUN_MAX_DAMAGE, GUN_MAX_DAMAGE / (abs(transform.x) * entity[i].distance) / 5);
if (damage > 0) {
entity[i].health = max(0, entity[i].health - damage);
entity[i].state = S_HIT;
entity[i].timer = 4;
}
}
}
}
// Update coords if possible. Return the collided uid, if any
UID updatePosition(const uint8_t level[], Coords *pos, double relative_x, double relative_y, bool only_walls = false) {
UID collide_x = detectCollision(level, pos, relative_x, 0, only_walls);
UID collide_y = detectCollision(level, pos, 0, relative_y, only_walls);
if (!collide_x) pos->x += relative_x;
if (!collide_y) pos->y += relative_y;
return collide_x || collide_y || UID_null;
}
void updateEntities(const uint8_t level[]) {
uint8_t i = 0;
while (i < num_entities) {
// update distance
entity[i].distance = coords_distance(&(player.pos), &(entity[i].pos));
// Run the timer. Works with actual frames.
// Todo: use delta here. But needs double type and more memory
if (entity[i].timer > 0) entity[i].timer--;
// too far away. put it in doze mode
if (entity[i].distance > MAX_ENTITY_DISTANCE) {
removeEntity(entity[i].uid);
// don't increase 'i', since current one has been removed
continue;
}
// bypass render if hidden
if (entity[i].state == S_HIDDEN) {
i++;
continue;
}
uint8_t type = uid_get_type(entity[i].uid);
switch (type) {
case E_ENEMY: {
// Enemy "IA"
if (entity[i].health == 0) {
if (entity[i].state != S_DEAD) {
entity[i].state = S_DEAD;
entity[i].timer = 6;
}
} else if (entity[i].state == S_HIT) {
if (entity[i].timer == 0) {
// Back to alert state
entity[i].state = S_ALERT;
entity[i].timer = 40; // delay next fireball thrown
}
} else if (entity[i].state == S_FIRING) {
if (entity[i].timer == 0) {
// Back to alert state
entity[i].state = S_ALERT;
entity[i].timer = 40; // delay next fireball throwm
}
} else {
// ALERT STATE
if (entity[i].distance > ENEMY_MELEE_DIST && entity[i].distance < MAX_ENEMY_VIEW) {
if (entity[i].state != S_ALERT) {
entity[i].state = S_ALERT;
entity[i].timer = 20; // used to throw fireballs
} else {
if (entity[i].timer == 0) {
// Throw a fireball
spawnFireball(entity[i].pos.x, entity[i].pos.y);
entity[i].state = S_FIRING;
entity[i].timer = 6;
} else {
// move towards to the player.
updatePosition(
level,
&(entity[i].pos),
sign(player.pos.x, entity[i].pos.x) * ENEMY_SPEED * delta,
sign(player.pos.y, entity[i].pos.y) * ENEMY_SPEED * delta,
true
);
}
}
} else if (entity[i].distance <= ENEMY_MELEE_DIST) {
if (entity[i].state != S_MELEE) {
// Preparing the melee attack
entity[i].state = S_MELEE;
entity[i].timer = 10;
} else if (entity[i].timer == 0) {
// Melee attack
player.health = max(0, player.health - ENEMY_MELEE_DAMAGE);
entity[i].timer = 14;
flash_screen = 1;
updateHud();
}
} else {
// stand
entity[i].state = S_STAND;
}
}
break;
}
case E_FIREBALL: {
if (entity[i].distance < FIREBALL_COLLIDER_DIST) {
// Hit the player and disappear
player.health = max(0, player.health - ENEMY_FIREBALL_DAMAGE);
flash_screen = 1;
updateHud();
removeEntity(entity[i].uid);
continue; // continue in the loop
} else {
// Move. Only collide with walls.
// Note: using health to store the angle of the movement
UID collided = updatePosition(
level,
&(entity[i].pos),
cos((double) entity[i].health / FIREBALL_ANGLES * PI) * FIREBALL_SPEED,
sin((double) entity[i].health / FIREBALL_ANGLES * PI) * FIREBALL_SPEED,
true
);
if (collided) {
removeEntity(entity[i].uid);
continue; // continue in the entity check loop
}
}
break;
}
case E_MEDIKIT: {
if (entity[i].distance < ITEM_COLLIDER_DIST) {
// pickup
playSound(medkit_snd, MEDKIT_SND_LEN);
entity[i].state = S_HIDDEN;
player.health = min(100, player.health + 50);
updateHud();
flash_screen = 1;
}
break;
}
case E_KEY: {
if (entity[i].distance < ITEM_COLLIDER_DIST) {
// pickup
playSound(get_key_snd, GET_KEY_SND_LEN);
entity[i].state = S_HIDDEN;
player.keys++;
updateHud();
flash_screen = 1;
}
break;
}
}
i++;
}
}
// The map raycaster. Based on https://lodev.org/cgtutor/raycasting.html
void renderMap(const uint8_t level[], double view_height) {
UID last_uid;
for (uint8_t x = 0; x < SCREEN_WIDTH; x += RES_DIVIDER) {
double camera_x = 2 * (double) x / SCREEN_WIDTH - 1;
double ray_x = player.dir.x + player.plane.x * camera_x;
double ray_y = player.dir.y + player.plane.y * camera_x;
uint8_t map_x = uint8_t(player.pos.x);
uint8_t map_y = uint8_t(player.pos.y);
Coords map_coords = { player.pos.x, player.pos.y };
double delta_x = abs(1 / ray_x);
double delta_y = abs(1 / ray_y);
int8_t step_x;
int8_t step_y;
double side_x;
double side_y;
if (ray_x < 0) {
step_x = -1;
side_x = (player.pos.x - map_x) * delta_x;
} else {
step_x = 1;
side_x = (map_x + 1.0 - player.pos.x) * delta_x;
}
if (ray_y < 0) {
step_y = -1;
side_y = (player.pos.y - map_y) * delta_y;
} else {
step_y = 1;
side_y = (map_y + 1.0 - player.pos.y) * delta_y;
}
// Wall detection
uint8_t depth = 0;
bool hit = 0;
bool side;
while (!hit && depth < MAX_RENDER_DEPTH) {
if (side_x < side_y) {
side_x += delta_x;
map_x += step_x;
side = 0;
} else {
side_y += delta_y;
map_y += step_y;
side = 1;
}
uint8_t block = getBlockAt(level, map_x, map_y);
if (block == E_WALL) {
hit = 1;
} else {
// Spawning entities here, as soon they are visible for the
// player. Not the best place, but would be a very performance
// cost scan for them in another loop
if (block == E_ENEMY || (block & 0b00001000) /* all collectable items */) {
// Check that it's close to the player
if (coords_distance(&(player.pos), &map_coords) < MAX_ENTITY_DISTANCE) {
UID uid = create_uid(block, map_x, map_y);
if (last_uid != uid && !isSpawned(uid)) {
spawnEntity(block, map_x, map_y);
last_uid = uid;
}
}
}
}
depth++;
}
if (hit) {
double distance;
if (side == 0) {
distance = max(1, (map_x - player.pos.x + (1 - step_x) / 2) / ray_x);
} else {
distance = max(1, (map_y - player.pos.y + (1 - step_y) / 2) / ray_y);
}
// store zbuffer value for the column
zbuffer[x / Z_RES_DIVIDER] = min(distance * DISTANCE_MULTIPLIER, 255);
// rendered line height
uint8_t line_height = RENDER_HEIGHT / distance;
drawVLine(
x,
view_height / distance - line_height / 2 + RENDER_HEIGHT / 2,
view_height / distance + line_height / 2 + RENDER_HEIGHT / 2,
GRADIENT_COUNT - int(distance / MAX_RENDER_DEPTH * GRADIENT_COUNT) - side * 2
);
}
}
}
// Sort entities from far to close
uint8_t sortEntities() {
uint8_t gap = num_entities;
bool swapped = false;
while (gap > 1 || swapped) {
//shrink factor 1.3
gap = (gap * 10) / 13;
if (gap == 9 || gap == 10) gap = 11;
if (gap < 1) gap = 1;
swapped = false;
for (uint8_t i = 0; i < num_entities - gap; i++)
{
uint8_t j = i + gap;
if (entity[i].distance < entity[j].distance)
{
swap(entity[i], entity[j]);
swapped = true;
}
}
}
}
Coords translateIntoView(Coords *pos) {
//translate sprite position to relative to camera
double sprite_x = pos->x - player.pos.x;
double sprite_y = pos->y - player.pos.y;
//required for correct matrix multiplication
double inv_det = 1.0 / (player.plane.x * player.dir.y - player.dir.x * player.plane.y);
double transform_x = inv_det * (player.dir.y * sprite_x - player.dir.x * sprite_y);
double transform_y = inv_det * (- player.plane.y * sprite_x + player.plane.x * sprite_y); // Z in screen
return { transform_x, transform_y };
}
void renderEntities(double view_height) {
sortEntities();
for (uint8_t i = 0; i < num_entities; i++) {
if (entity[i].state == S_HIDDEN) continue;
Coords transform = translateIntoView(&(entity[i].pos));
// don´t render if behind the player or too far away
if (transform.y <= 0.1 || transform.y > MAX_SPRITE_DEPTH) {
continue;
}
int16_t sprite_screen_x = HALF_WIDTH * (1.0 + transform.x / transform.y);
int8_t sprite_screen_y = RENDER_HEIGHT / 2 + view_height / transform.y;
uint8_t type = uid_get_type(entity[i].uid);
// don´t try to render if outside of screen
// doing this pre-shortcut due int16 -> int8 conversion makes out-of-screen
// values fit into the screen space
if (sprite_screen_x < - HALF_WIDTH || sprite_screen_x > SCREEN_WIDTH + HALF_WIDTH) {
continue;
}
switch (type) {
case E_ENEMY: {
uint8_t sprite;
if (entity[i].state == S_ALERT) {
// walking
sprite = int(millis() / 500) % 2;
} else if (entity[i].state == S_FIRING) {
// fireball
sprite = 2;
} else if (entity[i].state == S_HIT) {
// hit
sprite = 3;
} else if (entity[i].state == S_MELEE) {
// melee atack
sprite = entity[i].timer > 10 ? 2 : 1;
} else if (entity[i].state == S_DEAD) {
// dying
sprite = entity[i].timer > 0 ? 3 : 4;
} else {
// stand
sprite = 0;
}
drawSprite(
sprite_screen_x - BMP_IMP_WIDTH * .5 / transform.y,
sprite_screen_y - 8 / transform.y,
bmp_imp_bits,
bmp_imp_mask,
BMP_IMP_WIDTH,
BMP_IMP_HEIGHT,
sprite,
transform.y
);
break;
}
case E_FIREBALL: {
drawSprite(
sprite_screen_x - BMP_FIREBALL_WIDTH / 2 / transform.y,
sprite_screen_y - BMP_FIREBALL_HEIGHT / 2 / transform.y,
bmp_fireball_bits,
bmp_fireball_mask,
BMP_FIREBALL_WIDTH,
BMP_FIREBALL_HEIGHT,
0,
transform.y
);
break;
}
case E_MEDIKIT: {
drawSprite(
sprite_screen_x - BMP_ITEMS_WIDTH / 2 / transform.y,
sprite_screen_y + 5 / transform.y,
bmp_items_bits,
bmp_items_mask,
BMP_ITEMS_WIDTH,
BMP_ITEMS_HEIGHT,
0,
transform.y
);
break;
}
case E_KEY: {
drawSprite(
sprite_screen_x - BMP_ITEMS_WIDTH / 2 / transform.y,
sprite_screen_y + 5 / transform.y,
bmp_items_bits,
bmp_items_mask,
BMP_ITEMS_WIDTH,
BMP_ITEMS_HEIGHT,
1,
transform.y
);
break;
}
}
}
}
void renderGun(uint8_t gun_pos, double amount_jogging) {
// jogging
char x = 48 + sin((double) millis() * JOGGING_SPEED) * 10 * amount_jogging;
char y = RENDER_HEIGHT - gun_pos + abs(cos((double) millis() * JOGGING_SPEED)) * 8 * amount_jogging;
if (gun_pos > GUN_SHOT_POS - 2) {
// Gun fire
display.drawBitmap(x + 6, y - 11, bmp_fire_bits, BMP_FIRE_WIDTH, BMP_FIRE_HEIGHT, 1);
}
// Don't draw over the hud!
uint8_t clip_height = max(0, min(y + BMP_GUN_HEIGHT, RENDER_HEIGHT) - y);
// Draw the gun (black mask + actual sprite).
display.drawBitmap(x, y, bmp_gun_mask, BMP_GUN_WIDTH, clip_height, 0);
display.drawBitmap(x, y, bmp_gun_bits, BMP_GUN_WIDTH, clip_height, 1);
}
// Only needed first time
void renderHud() {
drawText(2, 58, F("{}"), 0); // Health symbol
drawText(40, 58, F("[]"), 0); // Keys symbol
updateHud();
}
// Render values for the HUD
void updateHud() {
display.clearRect(12, 58, 15, 6);
display.clearRect(50, 58, 5, 6);
drawText(12, 58, player.health);
drawText(50, 58, player.keys);
}
// Debug stats
void renderStats() {
display.clearRect(58, 58, 70, 6);
drawText(114, 58, int(getActualFps()));
drawText(82, 58, num_entities);
// drawText(94, 58, freeMemory());
}
// Intro screen
void loopIntro() {
display.drawBitmap(
(SCREEN_WIDTH - BMP_LOGO_WIDTH) / 2,
(SCREEN_HEIGHT - BMP_LOGO_HEIGHT) / 3,
bmp_logo_bits,
BMP_LOGO_WIDTH,
BMP_LOGO_HEIGHT,
1
);
delay(1000);
drawText(SCREEN_WIDTH / 2 - 25, SCREEN_HEIGHT * .8, F("PRESS FIRE"));
display.display();
// wait for fire
while (!exit_scene) {
#ifdef SNES_CONTROLLER
getControllerData();
#endif
if (input_fire()) jumpTo(GAME_PLAY);
};
}
void loopGamePlay() {
bool gun_fired = false;
bool walkSoundToggle = false;
uint8_t gun_pos = 0;
double rot_speed;
double old_dir_x;
double old_plane_x;
double view_height;
double jogging;
uint8_t fade = GRADIENT_COUNT - 1;
initializeLevel(sto_level_1);
do {
fps();
// Clear only the 3d view
memset(display_buf, 0, SCREEN_WIDTH * (RENDER_HEIGHT / 8));
#ifdef SNES_CONTROLLER
getControllerData();
#endif
// If the player is alive
if (player.health > 0) {
// Player speed
if (input_up()) {
player.velocity += (MOV_SPEED - player.velocity) * .4;
jogging = abs(player.velocity) * MOV_SPEED_INV;
} else if (input_down()) {
player.velocity += (- MOV_SPEED - player.velocity) * .4;
jogging = abs(player.velocity) * MOV_SPEED_INV;
} else {
player.velocity *= .5;
jogging = abs(player.velocity) * MOV_SPEED_INV;
}
// Player rotation
if (input_right()) {
rot_speed = ROT_SPEED * delta;
old_dir_x = player.dir.x;
player.dir.x = player.dir.x * cos(-rot_speed) - player.dir.y * sin(-rot_speed);
player.dir.y = old_dir_x * sin(-rot_speed) + player.dir.y * cos(-rot_speed);
old_plane_x = player.plane.x;
player.plane.x = player.plane.x * cos(-rot_speed) - player.plane.y * sin(-rot_speed);
player.plane.y = old_plane_x * sin(-rot_speed) + player.plane.y * cos(-rot_speed);
} else if (input_left()) {
rot_speed = ROT_SPEED * delta;
old_dir_x = player.dir.x;
player.dir.x = player.dir.x * cos(rot_speed) - player.dir.y * sin(rot_speed);
player.dir.y = old_dir_x * sin(rot_speed) + player.dir.y * cos(rot_speed);
old_plane_x = player.plane.x;
player.plane.x = player.plane.x * cos(rot_speed) - player.plane.y * sin(rot_speed);
player.plane.y = old_plane_x * sin(rot_speed) + player.plane.y * cos(rot_speed);
}
view_height = abs(sin((double) millis() * JOGGING_SPEED)) * 6 * jogging;
if(view_height > 5.9) {
if(sound == false) {
if(walkSoundToggle) {
playSound(walk1_snd, WALK1_SND_LEN);
walkSoundToggle = false;
} else {
playSound(walk2_snd, WALK2_SND_LEN);
walkSoundToggle = true;
}
}
}
// Update gun
if (gun_pos > GUN_TARGET_POS) {
// Right after fire
gun_pos -= 1;
} else if (gun_pos < GUN_TARGET_POS) {
// Showing up
gun_pos += 2;
} else if (!gun_fired && input_fire()) {
// ready to fire and fire pressed
gun_pos = GUN_SHOT_POS;
gun_fired = true;
fire();
} else if (gun_fired && !input_fire()) {
// just fired and restored position
gun_fired = false;
}
} else {
// The player is dead
if (view_height > -10) view_height--;
else if (input_fire()) jumpTo(INTRO);
if (gun_pos > 1) gun_pos -= 2;
}
// Player movement
if (abs(player.velocity) > 0.003) {
updatePosition(
sto_level_1,
&(player.pos),
player.dir.x * player.velocity * delta,
player.dir.y * player.velocity * delta
);
} else {
player.velocity = 0;
}
// Update things
updateEntities(sto_level_1);
// Render stuff
renderMap(sto_level_1, view_height);
renderEntities(view_height);
renderGun(gun_pos, jogging);
// Fade in effect
if (fade > 0) {
fadeScreen(fade);
fade--;
if (fade == 0) {
// Only draw the hud after fade in effect
renderHud();
}
} else {
renderStats();
}
// flash screen
if (flash_screen > 0) {
invert_screen = !invert_screen;
flash_screen--;
} else if (invert_screen) {
invert_screen = 0;
}
// Draw the frame
display.invertDisplay(invert_screen);
display.display();
// Exit routine
#ifdef SNES_CONTROLLER
if (input_start()) {
#else
if (input_left() && input_right()) {
#endif
jumpTo(INTRO);
}
} while (!exit_scene);
}
void loop(void) {
switch (scene) {
case INTRO: {
loopIntro();
break;
}
case GAME_PLAY: {
loopGamePlay();
break;
}
}
// fade out effect
for (uint8_t i=0; i<GRADIENT_COUNT; i++) {
fadeScreen(i, 0);
display.display();
delay(40);
}
exit_scene = false;
}