-
Notifications
You must be signed in to change notification settings - Fork 34
/
player.c
1304 lines (1244 loc) · 38 KB
/
player.c
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
#include "audio.h"
#include "res/system.h"
#include "res/local.h"
#include "camera.h"
#include "md/dma.h"
#include "effect.h"
#include "entity.h"
#include "hud.h"
#include "md/joy.h"
#include "cjk.h"
#include "math.h"
#include "md/stdlib.h"
#include "npc.h"
#include "resources.h"
#include "sheet.h"
#include "stage.h"
#include "system.h"
#include "tables.h"
#include "md/comp.h"
#include "tsc.h"
#include "md/sys.h"
#include "md/vdp.h"
#include "weapon.h"
#include "player.h"
#include "res/tiles.h"
#define PLAYER_SPRITE_TILES_QUEUE() ({ \
uint8_t f = player.frame + ((playerEquipment & EQUIP_MIMIMASK) ? 10 : 0); \
TILES_QUEUE(SPR_TILES(&SPR_Quote,0,f),TILE_PLAYERINDEX,4); \
})
uint8_t currentWeapon;
Entity player;
Sprite playerSprite;
uint8_t playerIFrames;
uint8_t playerMoveMode;
uint8_t lookingDown;
uint16_t playerMaxHealth;
uint8_t controlsLocked;
uint16_t playerEquipment;
uint8_t playerInventory[MAX_ITEMS];
uint8_t shoot_cooldown, mgun_chargetime, playerNoBump;
Entity *playerPlatform;
uint8_t playerPlatformTime;
uint8_t playerBoosterFuel, playerBoostState, lastBoostState;
uint16_t mapNameTTL;
uint8_t iSuckAtThisGameSHIT;
uint8_t missileEmptyFlag;
const uint8_t spur_time[2][4] = {
{ 0, 40, 60, 200 }, // NTSC
{ 0, 33, 50, 166 }, // PAL
};
Sprite weaponSprite;
uint8_t mapNameSpriteNum;
Sprite mapNameSprite[4];
Sprite airSprite[2];
uint8_t airPercent;
uint8_t airTick;
uint8_t airDisplayTime;
uint8_t blockl, blocku, blockr, blockd;
uint8_t ledge_time;
Sprite airTankSprite;
static void player_update_booster();
static void player_update_interaction();
static void player_update_air_display();
static void player_update_movement();
static void player_update_walk();
static void player_update_jump();
static void player_update_float();
static void player_prev_weapon();
static void player_next_weapon();
// Default values for player
void player_init() {
controlsLocked = FALSE;
player.hidden = FALSE;
player.dir = 0;
player.flags = NPC_IGNORE44|NPC_SHOWDAMAGE;
playerMaxHealth = 3;
player.health = 3;
player.x = block_to_sub(10) + pixel_to_sub(8);
player.y = block_to_sub(8) + pixel_to_sub(8);
player.x_next = player.x;
player.y_next = player.y;
player.x_speed = 0;
player.y_speed = 0;
player.enableSlopes = TRUE;
player.damage_time = 0;
player.damage_value = 0;
player.dir = 1;
player.hit_box = (bounding_box) {{ 6, 6, 5, 8 }};
player.frame = 255;
ledge_time = 0;
lookingDown = FALSE;
shoot_cooldown = 0;
mgun_chargetime = 0;
playerEquipment = 0; // Nothing equipped
for(uint8_t i = 0; i < MAX_ITEMS; i++) playerInventory[i] = 0; // Empty inventory
for(uint8_t i = 0; i < MAX_WEAPONS; i++) playerWeapon[i].type = 0; // No Weapons
for(uint8_t i = 0; i < MAX_BULLETS; i++) playerBullet[i].extent.x1 = 0xFFFF;
playerMoveMode = 0;
currentWeapon = 0;
airPercent = 100;
airTick = 0;
airDisplayTime = 0;
playerIFrames = 0;
mapNameTTL = 0;
missileEmptyFlag = FALSE;
// Booster trail sprite tiles
vdp_tiles_load(SPR_TILES(&SPR_Boost, 0, 0), 12, 4);
// AIR Sprite
//const SpriteDefinition *spr = cfg_language == LANG_JA ? &SPR_J_Air : &SPR_Air;
//vdp_tiles_load(SPR_TILES(spr, 0, 0), TILE_AIRINDEX, 4);
vdp_tiles_load(SPR_AIR->tiles, TILE_AIRINDEX, 4);
airSprite[0] = (Sprite) {
.x = ScreenHalfW - 28 + 128, .y = ScreenHalfH - 24 + 128,
.size = SPRITE_SIZE(4, 1), .attr = TILE_ATTR(PAL0,1,0,0,TILE_AIRINDEX)
};
airSprite[1] = (Sprite) {
.x = ScreenHalfW + 8 + 128, .y = ScreenHalfH - 24 + 128,
.size = SPRITE_SIZE(3, 1), .attr = TILE_ATTR(PAL0,1,0,0,TILE_AIRINDEX+4)
};
// Air Tank sprite
vdp_tiles_load(SPR_TILES(&SPR_Bubble, 0, 0), TILE_AIRTANKINDEX, 9);
airTankSprite = (Sprite) {
.size = SPRITE_SIZE(3,3),
.attr = TILE_ATTR(PAL0,0,0,0,TILE_AIRTANKINDEX)
};
// Player sprite
playerSprite = (Sprite) {
.size = SPRITE_SIZE(2,2),
.attr = TILE_ATTR(PAL0,0,0,1,TILE_PLAYERINDEX)
};
}
void player_update() {
uint8_t tile = stage_get_block_type(sub_to_block(player.x), sub_to_block(player.y));
if(!playerMoveMode) { // Normal movement
// Wind/Water current
if(tile & 0x80) {
// This stops us fron getting stuck in ledges
if((blk(player.x, 0, player.y, 6) & 0x83) == 0x81) {
player.y_speed -= SPEED_8(0x80);
} else {
switch(tile & 0x03) {
case 0: player.x_speed -= SPEED_8(0x88); break;
case 1: player.y_speed -= SPEED_8(0x80); break;
case 2: player.x_speed += SPEED_8(0x88); break;
case 3: player.y_speed += SPEED_8(0x50); break;
}
}
if(player.underwater) {
if(player.x_speed > SPEED_12(0x4E0)) player.x_speed = SPEED_12(0x4E0);
if(player.x_speed < -SPEED_12(0x4E0)) player.x_speed = -SPEED_12(0x4E0);
if(player.y_speed > SPEED_12(0x420)) player.y_speed = SPEED_12(0x420);
if(player.y_speed < -SPEED_12(0x4FF)) player.y_speed = -SPEED_12(0x4FF);
} else {
if(player.x_speed > SPEED_12(0x5FF)) player.x_speed = SPEED_12(0x5FF);
if(player.x_speed < -SPEED_12(0x5FF)) player.x_speed = -SPEED_12(0x5FF);
if(player.y_speed > SPEED_12(0x5FF)) player.y_speed = SPEED_12(0x5FF);
if(player.y_speed < -SPEED_12(0x5FF)) player.y_speed = -SPEED_12(0x5FF);
}
}
player_update_movement();
// Updates the booster while it is enabled
if(playerBoostState != BOOST_OFF) {
player_update_booster();
}
// Slow down the player when we stop the Booster 2.0.
// Has to run before collision since it recalculates our next position
if (playerBoostState != lastBoostState) {
if (playerBoostState == BOOST_OFF && (playerEquipment & EQUIP_BOOSTER20)) {
switch(lastBoostState) {
case BOOST_HOZ:
player.x_speed >>= 1;
player.x_next = player.x + player.x_speed;
break;
case BOOST_UP:
player.y_speed >>= 1;
player.y_next = player.y + player.y_speed;
break;
}
}
}
lastBoostState = playerBoostState;
// The above code to slow down the booster needs to always run.
// As such we check again whether the booster is enabled.
uint16_t px = player.x_next >> CSF, py = player.y_next >> CSF;
uint16_t bx = px >> 4, by = py >> 4;
if(playerBoostState == BOOST_OFF && bx > 0 && bx < stageWidth - 1 && by > 0 && by < stageHeight - 1) {
uint8_t blockl_next, blocku_next, blockr_next, blockd_next;
// Ok so, making the collision with ceiling <= 0 pushes the player out of
// the ceiling during the opening scene with Kazuma on the computer.
// Hi Kazuma!
blocku_next = player.y_speed < 0 ? collide_stage_ceiling(&player) : FALSE;
blockl_next = (player.x_speed <= 0 || playerPlatform) ? collide_stage_leftwall(&player) : FALSE;
blockr_next = (player.x_speed >= 0 || playerPlatform) ? collide_stage_rightwall(&player) : FALSE;
if(ledge_time == 0) {
if(player.grounded) {
player.grounded = collide_stage_floor_grounded(&player);
} else if(player.y_speed >= 0) {
player.grounded = collide_stage_floor(&player);
}
}
blockd_next = player.grounded;
// Here I do something weird to emulate the way the game pushes quote
// into small gaps.
if(!blockl_next && blockl && joy_down(JOY_LEFT)) {
player.x_speed -= 0xE0;
player.x_next = player.x + player.x_speed;
}
if(!blockr_next && blockr && joy_down(JOY_RIGHT)) {
player.x_speed += 0xE0;
player.x_next = player.x + player.x_speed;
}
if(ledge_time > 0) {
ledge_time--;
player.y_next += 0x600;
blockl_next = player.x_speed < 0 ? collide_stage_leftwall(&player) : FALSE;
blockr_next = player.x_speed > 0 ? collide_stage_rightwall(&player) : FALSE;
player.y_next -= 0x600;
} else if(!joy_down(btn[cfg_btn_jump]) && !blockd_next && blockd && !playerPlatform) {
player.y_speed += 0x80;
ledge_time = 4;
}
blockl = blockl_next;
blocku = blocku_next;
blockr = blockr_next;
blockd = blockd_next;
if(playerPlatform) {
player.x_next += playerPlatform->x_speed;
player.y_next += playerPlatform->y_speed;
player.hit_box.bottom++;
bounding_box box = entity_react_to_collision(&player, playerPlatform);
player.hit_box.bottom--;
// playerPlatformTime prevents the player from being too "loose" with platforms.
// They would slip off if the platform moves diagonally.
if(box.bottom == 0) {
if(++playerPlatformTime > 1) playerPlatform = NULL;
} else {
player.grounded = TRUE;
player.y_next += pixel_to_sub(1);
playerPlatformTime = 0;
}
}
}
} else { // Move mode 1 - for ironhead
player_update_float();
player.x_next = player.x + player.x_speed;
player.y_next = player.y + player.y_speed;
if(player.x_speed < 0) {
collide_stage_leftwall(&player);
// Check special noplayer blocks in main artery
if(blk(player.x, -6, player.y, 0) == 0x46) {
player.x_speed = 0;
player.x_next += 2 << CSF;
}
} else if (player.x_speed > 0) {
collide_stage_rightwall(&player);
// Check special noplayer blocks in main artery
if(blk(player.x, 6, player.y, 0) == 0x46) {
player.x_speed = 0;
player.x_next -= 2 << CSF;
}
}
// No "grounded" when floating, but don't go through the floor
if(player.y_speed > 0) {
collide_stage_floor(&player);
} else if(player.y_speed < 0) {
collide_stage_ceiling(&player);
}
}
// If somehow the player gets stuck in a wall, push them out
if(blk(player.x, 0, player.y, 0) == 0x41) {
if (blk(player.x, -16, player.y, 0) != 0x41) player.x -= 0x2000;
else if(blk(player.x, 16, player.y, 0) != 0x41) player.x += 0x2000;
else if(blk(player.x, 0, player.y, -16) != 0x41) player.y -= 0x2000;
else if(blk(player.x, 0, player.y, 16) != 0x41) player.y += 0x2000;
}
player.x = player.x_next;
player.y = player.y_next;
// Damage Tiles / Death check / IFrames
if(!playerIFrames && player.health > 0) {
// Match foreground (0x40) and fore+water (0x60) but not wind (0x80) or slope (0x10)
if((tile & 0xDF) == 0x42) {
// To reduce the amount objects on screen, the spikes that
// Ballos creates in the last phase merge with the stage once
// they rise fully. They should only do 2 damage though, so I
// added this check.
if(!tscState) player_inflict_damage((stageID == STAGE_SEAL_CHAMBER) ? 2 : 10);
}
if(player.health == 0) return;
} else {
playerIFrames--;
}
// This "damage_time" block is for weapon energy numbers.
// When the player gets hurt, create damage numbers instantly.
if(player.damage_time > 0) {
player.damage_time--;
if(player.damage_time == 0) {
effect_create_damage(player.damage_value, &player, -8, -4);
player.damage_value = 0;
}
}
// Handle air when underwater, unless a script is running
if(!tscState) {
if(player.underwater && !(playerEquipment & EQUIP_AIRTANK)) {
if(airTick == 0) {
airTick = TIME_8(9);
airPercent--;
if(airPercent == 0) {
// Spoilers
if(system_get_flag(ALMOND_DROWN_FLAG)) {
tsc_call_event(ALMOND_DROWN_EVENT);
} else {
player.health = 0;
player.frame = 8;
PLAYER_SPRITE_TILES_QUEUE();
tsc_call_event(PLAYER_DROWN_EVENT);
return;
}
}
} else {
airTick--;
}
} else if(airPercent < 100) {
airPercent = 100;
airTick = 0;
airDisplayTime = 60;
} else if(airDisplayTime > 0) {
airDisplayTime--;
}
player_update_air_display();
}
// Weapon switching
if(joytype == JOY_TYPE_PAD3) {
// 3 button controller cycles with A
if(joy_pressed(btn[cfg_btn_ffwd])) {
player_next_weapon();
}
} else {
// 6 button cycles with Y/Z
if(joy_pressed(btn[cfg_btn_lswap])) {
player_prev_weapon();
} else if(joy_pressed(btn[cfg_btn_rswap])) {
player_next_weapon();
}
}
// Bullets before shooting, fixes enemies in the wall not getting hit
player_update_bullets();
// Shooting
Weapon *w = &playerWeapon[currentWeapon];
if(iSuckAtThisGameSHIT) w->ammo = w->maxammo;
if(w->type == WEAPON_MACHINEGUN) {
if(shoot_cooldown > 0) shoot_cooldown--;
if(joy_down(btn[cfg_btn_shoot])) {
if(shoot_cooldown == 0) {
if(w->ammo > 0) {
missileEmptyFlag = FALSE;
weapon_fire(*w);
w->ammo--;
} else {
sound_play(SND_GUN_CLICK, 5);
if(!missileEmptyFlag) {
missileEmptyFlag = TRUE;
entity_create(player.x, player.y, OBJ_EMPTY, 0);
}
}
shoot_cooldown = (pal_mode || cfg_60fps) ? 7 : 8;
}
} else {
shoot_cooldown = 0;
if(w->ammo < 100) {
if(mgun_chargetime > 0) {
mgun_chargetime--;
} else {
w->ammo++;
mgun_chargetime = (playerEquipment & EQUIP_TURBOCHARGE) ? 2 : 4;
}
}
}
} else if(w->type == WEAPON_BUBBLER) { // mgun_shoottime and mgun_chargetime reused here
w->maxammo = 100;
uint8_t chargespeed;
if(w->level == 1) {
chargespeed = TIME_8(25); // Twice per second
if(joy_pressed(btn[cfg_btn_shoot])) weapon_fire(*w);
} else {
chargespeed = 3; // Around 12-15 per second
if(shoot_cooldown > 0) shoot_cooldown--;
if(joy_down(btn[cfg_btn_shoot]) && shoot_cooldown == 0) {
weapon_fire(*w);
shoot_cooldown = (pal_mode || cfg_60fps) ? 8 : 9;
}
}
if(!joy_down(btn[cfg_btn_shoot]) && w->ammo < 100) {
if(mgun_chargetime > 0) {
mgun_chargetime--;
} else {
w->ammo++;
mgun_chargetime = chargespeed;
}
}
} else if(w->type == WEAPON_SPUR) {
if(joy_pressed(btn[cfg_btn_shoot])) {
weapon_fire(*w);
shoot_cooldown = 4;
} else if(joystate & btn[cfg_btn_shoot]) {
uint8_t maxenergy = spur_time[pal_mode||cfg_60fps][w->level];
if(!(w->level == 3 && w->energy == maxenergy)) {
w->energy += (playerEquipment & EQUIP_TURBOCHARGE) ? 3 : 2;
if(w->energy >= maxenergy) {
if(w->level == 3) {
w->energy = maxenergy;
sound_play(SND_SPUR_MAXED, 5);
} else {
w->level++;
w->energy = 0;
}
} else {
mgun_chargetime++;
if((mgun_chargetime & 3) == 1) {
sound_play(SND_SPUR_CHARGE_1 + w->level - 1, 2);
}
}
}
} else {
if(mgun_chargetime) {
if(w->level > 1) {
if(w->energy == spur_time[pal_mode||cfg_60fps][3]) w->level = 4;
weapon_fire(*w);
}
mgun_chargetime = 0;
}
w->level = 1;
w->energy = 0;
hud_force_energy();
}
} else if(shoot_cooldown) {
shoot_cooldown--;
} else {
if(joy_pressed(btn[cfg_btn_shoot])) {
weapon_fire(*w);
shoot_cooldown = 4;
}
}
if(player.grounded) {
playerBoosterFuel = (pal_mode || cfg_60fps) ? 51 : 61;
player_update_interaction();
}
player_draw();
if(mapNameTTL > 0) {
mapNameTTL--;
vdp_sprites_add(mapNameSprite, mapNameSpriteNum);
}
}
static void player_update_movement() {
player_update_walk();
player_update_jump();
player.x_next = player.x + player.x_speed;
player.y_next = player.y + player.y_speed;
}
static void player_update_walk() {
int16_t acc;
int16_t fric;
int16_t max_speed;
if(pal_mode || cfg_60fps) {
max_speed = 810;
if(player.grounded) {
acc = 85;
fric = 51;
} else {
acc = 32;
fric = 0;
}
} else {
max_speed = 675;
if(player.grounded) {
acc = 71;
fric = 42;
} else {
acc = 27;
fric = 0;
}
}
// 2 kinds of water, actual water blocks & background water in Core
if((blk(player.x, 0, player.y, 0) & BLOCK_WATER) ||
(stageBackgroundType == 4 && water_entity && player.y > water_entity->y)) {
if(!player.underwater) {
player.underwater = TRUE;
sound_play(SND_SPLASH, 5);
effect_create_misc(EFF_SPLASH, player.x >> CSF, player.y >> CSF, FALSE);
effect_create_misc(EFF_SPLASH, player.x >> CSF, player.y >> CSF, FALSE);
effect_create_misc(EFF_SPLASH, player.x >> CSF, player.y >> CSF, FALSE);
}
// Half everything, maybe inaccurate?
acc >>= 1;
max_speed >>= 1;
fric >>= 1;
} else {
player.underwater = FALSE;
}
// Stop player from moving faster if they exceed max speed
if(joy_down(JOY_LEFT)) {
if(player.x_speed >= -max_speed) player.x_speed -= acc;
}
if(joy_down(JOY_RIGHT)) {
if(player.x_speed <= max_speed) player.x_speed += acc;
}
// But only slow them down on the ground
if(player.grounded) {
if(abs(player.x_speed) <= fric) player.x_speed = 0;
else if(player.x_speed < 0) player.x_speed += fric;
else if(player.x_speed > 0) player.x_speed -= fric;
}
}
static void player_update_jump() {
int16_t jumpSpeed;
int16_t gravity;
int16_t gravityJump;
int16_t maxFallSpeed;
if(pal_mode || cfg_60fps) {
// See issue #270 for the reason these values are off
jumpSpeed = 0x4E0; // 0x500
gravity = 0x50;
gravityJump = 0x21; // 0x20
maxFallSpeed = 0x5FF;
} else {
jumpSpeed = 0x42A;
gravity = 0x42;
gravityJump = 0x1A;
maxFallSpeed = 0x4FF;
}
if(player.underwater) {
jumpSpeed >>= 1;
gravity >>= 1;
gravityJump >>= 1;
maxFallSpeed >>= 1;
}
if(player.jump_time > 0) {
if(joy_down(btn[cfg_btn_jump])) {
player.jump_time--;
} else {
player.jump_time = 0;
}
}
if(player.jump_time > 0) return;
if(player.grounded) {
if(joy_pressed(btn[cfg_btn_jump])) {
player.grounded = FALSE;
player.y_speed = -jumpSpeed;
// Maybe possibly fix jump height?
player.jump_time = (pal_mode || cfg_60fps) ? 0 : 3;
player.jump_time += player.underwater ? 2 : 0;
sound_play(SND_PLAYER_JUMP, 3);
}
} else if((playerEquipment & (EQUIP_BOOSTER08 | EQUIP_BOOSTER20)) &&
joy_pressed(btn[cfg_btn_jump])) {
player_start_booster();
} else if(playerBoostState == BOOST_OFF) {
if(joy_down(btn[cfg_btn_jump]) && (player.y_speed < 0 || player.underwater)) {
player.y_speed += gravityJump;
} else {
player.y_speed += gravity;
}
if(player.y_speed > maxFallSpeed) player.y_speed = maxFallSpeed;
}
}
static void player_update_float() {
int16_t acc;
int16_t fric;
int16_t max_speed;
if(pal_mode || cfg_60fps) {
acc = 256;
fric = 128;
max_speed = 1024;
} else {
acc = 213;
fric = 106;
max_speed = 853;
}
if (joy_down(JOY_LEFT)) {
player.x_speed -= acc;
if (player.x_speed < -max_speed) player.x_speed = -max_speed;
} else if (joy_down(JOY_RIGHT)) {
player.x_speed += acc;
if (player.x_speed > max_speed) player.x_speed = max_speed;
} else {
if (player.x_speed < fric && player.x_speed > -fric) player.x_speed = 0;
else if (player.x_speed < 0) player.x_speed += fric;
else if (player.x_speed > 0) player.x_speed -= fric;
}
if (joy_down(JOY_UP)) {
player.y_speed -= acc;
if (player.y_speed < -max_speed) player.y_speed = -max_speed;
} else if (joy_down(JOY_DOWN)) {
player.y_speed += acc;
if (player.y_speed > max_speed) player.y_speed = max_speed;
} else {
if(player.y_speed < fric && player.y_speed > -fric) player.y_speed = 0;
else if (player.y_speed < 0) player.y_speed += fric;
else if (player.y_speed > 0) player.y_speed -= fric;
}
}
void player_update_bullets() {
for(uint8_t i = 0; i < MAX_BULLETS; i++) {
if(playerBullet[i].ttl) {
bullet_update(playerBullet[i]);
if(playerBullet[i].ttl) {
playerBullet[i].extent = (extent_box) {
.x1 = (playerBullet[i].x >> CSF) - (playerBullet[i].hit_box.left),
.y1 = (playerBullet[i].y >> CSF) - (playerBullet[i].hit_box.top),
.x2 = (playerBullet[i].x >> CSF) + (playerBullet[i].hit_box.right),
.y2 = (playerBullet[i].y >> CSF) + (playerBullet[i].hit_box.bottom),
};
} else {
playerBullet[i].extent = (extent_box) {};
}
}
}
}
static void player_update_interaction() {
// Interaction with entities when pressing down
if(cfg_updoor ? joy_pressed(JOY_UP) : joy_pressed(JOY_DOWN)) {
Entity *e = entityList;
while(e) {
if((e->flags & NPC_INTERACTIVE) && entity_overlapping(&player, e)) {
// To avoid triggering it twice
joystate_old |= cfg_updoor ? JOY_UP : JOY_DOWN;
if(e->event > 0) {
// Quote should look down while the game logic is frozen
// Manually send sprite frame since draw() is not called
lookingDown = TRUE;
player.frame = 6;
PLAYER_SPRITE_TILES_QUEUE();
tsc_call_event(e->event);
return;
}
}
e = e->next;
}
// Question mark above head
effect_create_misc(EFF_QMARK, (player.x >> CSF), (player.y >> CSF) - 12, TRUE);
}
}
void player_start_booster() {
if(playerBoosterFuel == 0) return;
player.jump_time = 0;
player.grounded = FALSE;
playerPlatform = NULL;
playerPlatformTime = 0;
// Pick a direction with Booster 2.0, default up
if ((playerEquipment & EQUIP_BOOSTER20)) {
playerBoostState = BOOST_UP;
// in order of precedence
if (joy_down(JOY_LEFT)) {
player.dir = 0;
playerBoostState = BOOST_HOZ;
} else if (joy_down(JOY_RIGHT)) {
player.dir = 1;
playerBoostState = BOOST_HOZ;
} else if (joy_down(JOY_DOWN)) {
playerBoostState = BOOST_DOWN;
} else if (joy_down(JOY_UP)) {
playerBoostState = BOOST_UP;
}
if (playerBoostState == BOOST_UP || playerBoostState == BOOST_DOWN)
player.x_speed = 0;
switch(playerBoostState) {
case BOOST_UP:
player.y_speed = -SPEED_12(0x600);
break;
case BOOST_DOWN:
player.y_speed = SPEED_12(0x600);
break;
case BOOST_HOZ:
player.y_speed = 0;
if (joy_down(JOY_LEFT)) {
player.x_speed = -SPEED_12(0x600);
// Little hack to prevent clipping against left wall
player.x += 0x100;
} else {
player.x_speed = SPEED_12(0x600);
}
break;
}
} else {
playerBoostState = BOOST_08;
// help it overcome gravity
if (player.y_speed > SPEED_8(0xFF))
player.y_speed >>= 1;
}
sound_play(SND_BOOSTER, 3);
}
static void player_update_booster() {
if(!(playerEquipment & (EQUIP_BOOSTER08 | EQUIP_BOOSTER20))) playerBoostState = BOOST_OFF;
if(!joy_down(btn[cfg_btn_jump])) {
// Decelerate after boosting down
//if(playerBoostState == BOOST_DOWN) player.y_speed >>= 2;
playerBoostState = BOOST_OFF;
}
if(playerBoostState == BOOST_OFF) return;
// player seems to want it active...check the fuel
if(playerBoosterFuel == 0) {
playerBoostState = BOOST_OFF;
return;
} else {
playerBoosterFuel--;
}
// ok so then, booster is active right now
uint8_t sputtering = FALSE;
if (joy_down(JOY_LEFT)) player.dir = 0;
else if (joy_down(JOY_RIGHT)) player.dir = 1;
// Don't bump in the opposite direction when hitting the ceiling
if(player.y_speed <= 0 && collide_stage_ceiling(&player)) player.y_speed = 0;
uint8_t nblockl = collide_stage_leftwall(&player),
nblockr = collide_stage_rightwall(&player);
collide_stage_floor(&player);
player.grounded = FALSE;
playerPlatform = NULL;
playerPlatformTime = 0;
switch(playerBoostState) {
case BOOST_HOZ:
{
if(!player.dir) { // Left
if(nblockl) {
player.y_speed = -SPEED_8(0xFF);
player.x += 0x100;
}
} else { // Right
if(nblockr) {
player.y_speed = -SPEED_8(0xFF);
player.x -= 0x100;
}
}
}
break;
case BOOST_UP:
{
player.y_speed -= SPEED_8(0x20);
if(player.y_speed < -SPEED_12(0x600)) player.y_speed = -SPEED_12(0x600);
}
break;
case BOOST_DOWN:
{
player.y_speed += SPEED_8(0x20);
}
break;
case BOOST_08:
{
// top speed and sputtering
if (player.y_speed < -SPEED_10(0x3FF)) {
player.y_speed += SPEED_8(0x20);
sputtering = TRUE; // no sound/smoke this frame
} else {
player.y_speed -= SPEED_8(0x20);
}
}
break;
}
// smoke and sound effects
if((++player.timer2 >= TIME_8(5)) && !sputtering) {
player.timer2 = 0;
sound_play(SND_BOOSTER, 3);
switch(playerBoostState) {
case BOOST_08: effect_create_misc(EFF_BOOST8, player.x >> CSF, (player.y >> CSF) + 6, 0); break;
case BOOST_HOZ:
if(player.dir) effect_create_misc(EFF_BOOST2, (player.x >> CSF) - 0, (player.y >> CSF) + 2, 0);
else effect_create_misc(EFF_BOOST2, (player.x >> CSF) + 0, (player.y >> CSF) + 2, 0);
break;
case BOOST_UP: effect_create_misc(EFF_BOOST2, player.x >> CSF, (player.y >> CSF) + 6, 0); break;
case BOOST_DOWN: effect_create_misc(EFF_BOOST2, player.x >> CSF, (player.y >> CSF) - 4, 0); break;
}
}
}
static uint16_t GetNextChar(uint8_t index) {
const uint8_t *names = (const uint8_t*)STAGE_NAMES;
uint16_t chr = names[stageID * 16 + index];
if(chr >= 0xE0 && chr < 0xFF) {
return (chr - 0xE0) * 0x60 + (names[stageID * 16 + index + 1] - 0x20) + 0x100;
} else {
return chr;
}
}
static void show_map_jname(uint8_t ttl) {
static const uint8_t mul6[20] = {
0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102, 108, 114
};
uint16_t len = 0;
uint16_t i = 0;
mapNameSpriteNum = 0;
while(i < 16) {
uint16_t chr1, chr2;
chr1 = GetNextChar(i++);
if(chr1 == 0) break; // End of string
if(chr1 > 0xFF) i++;
chr2 = GetNextChar(i++);
if (chr2 > 0xFF) i++;
cjk_drawsprite(mul6[mapNameSpriteNum], chr1, chr2);
len += chr2 ? 2 : 1;
mapNameSpriteNum++;
}
if(!len) return;
uint16_t x = ScreenHalfW - mul6[len] + 128;
uint16_t y = ScreenHalfH - 32 + 128;
for(i = 0; i < len; i += 2) {
uint16_t tind = mul6[i >> 1];
mapNameSprite[i >> 1] = (Sprite) {
.x = x, .y = y, .size = SPRITE_SIZE(min(3,(len-i)<<1), 2),
.attr = TILE_ATTR(PAL0,1,0,0,TILE_FACEINDEX+tind)
};
x += 24;
}
mapNameTTL = ttl;
}
void player_show_map_name(uint8_t ttl) {
// Boss bar overwrites the name
if(stageID == STAGE_WATERWAY_BOSS) return;
// Show kanji name
if(cfg_language >= LANG_JA && cfg_language <= LANG_KO) {
show_map_jname(ttl);
return;
}
// English name
const uint8_t *str = (uint8_t*) stage_info[stageID].name;
if(cfg_language > 0) {
str = ((const uint8_t*)STAGE_NAMES) + (stageID << 5);
}
const uint32_t *font = cfg_language >= LANG_RU ? UFTC_SysFontRU : UFTC_SysFont;
uint16_t len = 0;
uint16_t pos = 0;
while(len < 16) {
uint8_t chr = str[pos++];
if(chr == 0x01) {
chr = str[pos++];
chr += 0x5F;
len++;
} else {
chr -= 0x20;
if (chr < 0x60) len++;
else break;
}
vdp_tiles_load_uftc(font, TILE_NAMEINDEX + len-1, chr, 1);
}
if(!len) return;
// Transfer tile array to VRAM
mapNameSpriteNum = 0;
uint16_t x = ScreenHalfW - (len << 2) + 128;
uint16_t y = ScreenHalfH - 32 + 128;
for(uint16_t i = 0; i < len; i += 4) {
uint8_t sind = i>>2;
mapNameSprite[sind] = (Sprite) {
.x = x, .y = y, .size = SPRITE_SIZE(min(4,len-i), 1)
};
mapNameSprite[sind].attr = TILE_ATTR(PAL0,1,0,0,TILE_NAMEINDEX+i);
x += 32;
mapNameSpriteNum++;
}
mapNameTTL = ttl;
}
static void draw_air_percent() {
uint8_t airTemp = airPercent;
uint32_t numberTiles[3][8];
if(airTemp >= 100) {
airTemp -= 100;
memcpy(numberTiles[0], &TS_Numbers[8], 32);
} else {
memcpy(numberTiles[0], BlankData, 32);
}
memcpy(numberTiles[1], &TS_Numbers[div10[airTemp] * 8], 32);
memcpy(numberTiles[2], &TS_Numbers[mod10[airTemp] * 8], 32);
vdp_tiles_load(numberTiles[0], TILE_AIRINDEX + 4, 3);
}
static void player_update_air_display() {
// Blink for a second after getting out of the water
if(airPercent == 100) {
if(airDisplayTime == TIME_8(50)) {
draw_air_percent();
} else if(airDisplayTime & 4) {
vdp_sprites_add(airSprite, 2);
}
} else {
airDisplayTime++;
if((airDisplayTime & 31) == 0) {
vdp_tiles_load(BlankData, TILE_AIRINDEX, 1);
} else if((airDisplayTime & 31) == 15) {
//const SpriteDefinition *spr = cfg_language == LANG_JA ? &SPR_J_Air : &SPR_Air;
//vdp_tiles_load(SPR_TILES(spr, 0, 0), TILE_AIRINDEX, 1);
vdp_tiles_load(SPR_AIR->tiles, TILE_AIRINDEX, 1);
}
// Calculate air percent and display the value
if(airTick == TIME_8(9)) draw_air_percent();
vdp_sprites_add(airSprite, 2);
}
}
void player_draw() {
// Special case when player drowns
if(!airPercent) {
sprite_pos(&playerSprite,
sub_to_pixel(player.x) - sub_to_pixel(camera.x) + ScreenHalfW - 8,
sub_to_pixel(player.y) - sub_to_pixel(camera.y) + ScreenHalfH - 8);
vdp_sprite_add(&playerSprite);
return;
} else if(!player.health) {
return; // Don't draw the player if we died in a way that is not drowning
}
enum { STAND, WALK1, WALK2, LOOKUP, UPWALK1, UPWALK2, LOOKDN, JUMPDN };
// Sprite Animation
player.oframe = player.frame;
if(playerMoveMode) {
player.frame = WALK2;
} else {
if(player.grounded) {
if(joy_down(JOY_UP) && !controlsLocked) {
lookingDown = FALSE;
if(joystate&(JOY_LEFT | JOY_RIGHT)) {
static const uint8_t f[] = { UPWALK1, LOOKUP, UPWALK2, LOOKUP };
if(++player.timer2 > TIME_8(5)) {
player.timer2 = 0;
if(++player.animtime > 3) player.animtime = 0;
}
player.frame = f[player.animtime];
} else {
player.frame = LOOKUP;
player.animtime = 0;
}
} else if(joystate&(JOY_LEFT | JOY_RIGHT) && !controlsLocked) {
lookingDown = FALSE;
static const uint8_t f[] = { WALK1, STAND, WALK2, STAND };
if(++player.timer2 > TIME_8(5)) {
player.timer2 = 0;
if(++player.animtime > 3) player.animtime = 0;
}
player.frame = f[player.animtime];
} else if(joy_pressed(JOY_DOWN) && !controlsLocked) {
lookingDown = TRUE;
player.frame = LOOKDN;
player.animtime = 0;
} else if(!lookingDown) {
player.frame = STAND;
player.animtime = 0;
}
} else {
lookingDown = FALSE;
player.animtime = 0;
if(joy_down(JOY_UP) && !controlsLocked) {
player.frame = (player.y_speed > 0) ? UPWALK2 : UPWALK1;
} else if(joy_down(JOY_DOWN) && !controlsLocked) {
player.frame = JUMPDN;
} else {
player.frame = (player.y_speed > 0) ? WALK2 : WALK1;
}
}
if((player.animtime & 1) && player.timer2 == 1) sound_play(SND_PLAYER_WALK, 2);
}
// Set frame if it changed
if(player.frame != player.oframe) PLAYER_SPRITE_TILES_QUEUE();
// Blink during invincibility frames
if(!player.hidden && !(playerIFrames & 2)) {
// Change direction if pressing left or right
if(playerMoveMode) {
player.dir = RIGHT;