-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
2081 lines (2027 loc) · 50.6 KB
/
main.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
// 3DSage GBA template
//---#defines---
#include "gba.h"
//---Math functions---
#include <math.h>
#include <stdlib.h>
//---Global variables---
#define GBA_SW 160 // actual gba screen width
#define SW 120 // game screen width
#define SH 80 // game screen height
#define RGB(r,g,b) ((r)+((g)<<5)+((b)<<10)) // 15 bit, 0-31, 5bit=r, 5bit=g, 5bit=b
int lastFr = 0, FPS = 0; // for frames per second
int8_t terrainSeed[TERRAIN_SIZE];
uint8_t biomeSeed[BIOME_SIZE];
__attribute__((section (".sbss"))) uint8_t oreSeed[ORE_SIZE][33];
// the above creates an array of 0s and stores it in ewram instead of iwram
uint8_t currentChunk[30][40];
int32_t curChunkOffset = 0;
uint8_t curChanges = 0;
uint8_t nextChunk[30][40];
int32_t nextChunkOffset = -30;
uint8_t nextChanges = 0;
uint16_t blockGraphic[16];
uint32_t saveMemOffset = 0;
uint16_t GDS = 72+240; // General Data Size (in bytes(also edited in the init function))
uint8_t resetCounter = 0; // amount of frames until world is reset if buttons pressed
// player variables
int32_t viewX = 0; // i had these as unsigned for the longest tiiiiimmmmmeeee
int32_t viewY = -4; // it broke everythiiiinnnnggggggg... knew it was something dumb it always is
uint8_t keyRReleased = 1;
uint8_t keyLReleased = 1;
uint8_t keyUReleased = 1;
uint8_t keyDReleased = 1;
uint8_t keySTReleased = 1;
uint8_t keySLReleased = 1;
uint8_t aKey = 0;
uint8_t bKey = 0;
uint8_t rKey = 0;
uint8_t lKey = 0;
uint8_t uKey = 0;
uint8_t dKey = 0;
uint8_t slKey = 0;
uint8_t stKey = 0;
uint8_t lsKey = 0;
uint8_t rsKey = 0;
uint8_t keyLSPressed = 0;
uint8_t keyRSPressed = 0;
int8_t lastMovement = 0;
uint8_t gravityDelay = 0;
int16_t hForce = 0;
int16_t vForce = 0;
int8_t viewXChange = 0;
int8_t viewYChange = 0;
uint8_t playerMode = 0;
uint8_t inventory[15][2];
uint8_t invenSel = 0;
int8_t playerHP = 20;
int32_t telerocks[30][2];
struct Entity {
uint8_t id;
uint16_t color;
int16_t hp;
int32_t x;
int32_t y;
int8_t hF;
int8_t vF;
};
struct Entity entities[6];
uint8_t numEntities = sizeof(entities)/sizeof(struct Entity);
void savePlayer()
{
// DONT EXCEED GDS
// seems to get angry if not done in 4 byte increments
// save viewX
saveMemory[5] = viewX;
saveMemory[6] = viewX>>8;
saveMemory[7] = viewX>>16;
saveMemory[8] = viewX>>24;
// save viewY
saveMemory[9] = viewY;
saveMemory[10] = viewY>>8;
saveMemory[11] = viewY>>16;
saveMemory[12] = viewY>>24;
}
void saveHealth() // save player health
{
uint8_t invenSize = sizeof(inventory)/sizeof(inventory[0]);
saveMemory[32+numEntities*13+invenSize*2] = playerHP;
saveMemory[33+numEntities*13+invenSize*2] = playerHP>>8;
}
void saveEntities()
{
// save entity data
for(uint8_t e = 0; e < numEntities; e++)
{
saveMemory[17+e*13] = entities[e].id;
saveMemory[18+e*13] = entities[e].color;
saveMemory[19+e*13] = entities[e].color>>8;
saveMemory[20+e*13] = entities[e].hp;
saveMemory[21+e*13] = entities[e].hp>>8;
saveMemory[22+e*13] = entities[e].x;
saveMemory[23+e*13] = entities[e].x>>8;
saveMemory[24+e*13] = entities[e].x>>16;
saveMemory[25+e*13] = entities[e].x>>24;
saveMemory[26+e*13] = entities[e].y;
saveMemory[27+e*13] = entities[e].y>>8;
saveMemory[28+e*13] = entities[e].y>>16;
saveMemory[29+e*13] = entities[e].y>>24;
}
}
void saveChunk(uint8_t chunk[30][40], int32_t chunkOffset)
{
// check if chunk has already been saved previously
uint16_t preSaveLocation = 0xFFFF;
for(uint8_t c = 0; c < (31999-GDS)/CHUNK_SIZE; c++) // game pak sram is 64000B
{
// retrieve chunk location
int32_t chOff = saveMemory[GDS+c*CHUNK_SIZE] + (saveMemory[GDS+c*CHUNK_SIZE+1]<<8) + (saveMemory[GDS+c*CHUNK_SIZE+2]<<16) + (saveMemory[GDS+c*CHUNK_SIZE+3]<<24);
if(chOff == chunkOffset) // if this is the chunk we want to save
{
preSaveLocation = GDS+c*CHUNK_SIZE;
break;
}
}
// create new chunk save
if(preSaveLocation == 0xFFFF)
{
// persistent memory works a byte at a time >:I
saveMemory[GDS+saveMemOffset] = chunkOffset;
saveMemory[GDS+saveMemOffset+1] = chunkOffset>>8;
saveMemory[GDS+saveMemOffset+2] = chunkOffset>>16;
saveMemory[GDS+saveMemOffset+3] = chunkOffset>>24;
uint16_t index = 0;
for(int x = 0; x < 30; x++)
{
for(int y = 0; y < 40; y++)
{
saveMemory[GDS+4+saveMemOffset+index] = chunk[x][y];
index++;
}
}
saveMemOffset += CHUNK_SIZE;
// check if game pak sram memory exceeded
if(saveMemOffset > 31999 - GDS - CHUNK_SIZE)
{
playNote(0xF4DF, 0x0C); // debug sound
saveMemOffset = 0; // reset pointer
// now the next new chunk saved will overwrite the first chunk saved
}
saveMemory[1] = saveMemOffset;
saveMemory[2] = saveMemOffset>>8;
saveMemory[3] = saveMemOffset>>16;
saveMemory[4] = saveMemOffset>>24;
}
else // replace chunk data in previously created chunk save
{
uint16_t index = 0;
for(int x = 0; x < 30; x++)
{
for(int y = 0; y < 40; y++)
{
saveMemory[4+preSaveLocation+index] = chunk[x][y];
index++;
}
}
}
}
// save player inventory
void saveInven()
{
for(uint8_t i = 0; i < sizeof(inventory)/sizeof(inventory[0]); i++)
{
saveMemory[30+numEntities*13+i*2] = inventory[i][0];
saveMemory[31+numEntities*13+i*2] = inventory[i][1];
}
}
// save telerock locations
void saveTelerock()
{
uint8_t invenSize = sizeof(inventory)/sizeof(inventory[0]);
for(uint8_t i = 0; i < 30; i++)
{
saveMemory[34+numEntities*13+invenSize*2+i*8] = telerocks[i][0];
saveMemory[35+numEntities*13+invenSize*2+i*8] = telerocks[i][0]>>8;
saveMemory[36+numEntities*13+invenSize*2+i*8] = telerocks[i][0]>>16;
saveMemory[37+numEntities*13+invenSize*2+i*8] = telerocks[i][0]>>24;
saveMemory[38+numEntities*13+invenSize*2+i*8] = telerocks[i][1];
saveMemory[39+numEntities*13+invenSize*2+i*8] = telerocks[i][1]>>8;
saveMemory[40+numEntities*13+invenSize*2+i*8] = telerocks[i][1]>>16;
saveMemory[41+numEntities*13+invenSize*2+i*8] = telerocks[i][1]>>24;
}
}
inline int32_t arrayLoop(int32_t index, uint16_t size)
{
while(index < 0)
{
index += size*3;
}
return index;
}
void generateChunk(uint8_t chunk[30][40], int32_t chunkOffset)
{
// generate chunk
int32_t skyArea = 2;
int32_t biome, index, terIndex;
// select biome
index = arrayLoop(chunkOffset/30, BIOME_SIZE);
biome = biomeSeed[(index % BIOME_SIZE) / BIOME_CHUNKS];
for(int x = 0; x < 30; x++)
{
// allow infinite negative array wrap around
terIndex = arrayLoop(chunkOffset+x, TERRAIN_SIZE);
terIndex %= TERRAIN_SIZE;
// alter terrain based on biome
switch(biome)
{
case 0:
// default terrain
skyArea = 7 + terrainSeed[terIndex] * 10 / 16;
break;
case 1:
// flatter terrain
skyArea = 7 + terrainSeed[terIndex] * 10 / 18;
break;
case 2:
// more jagged terrain
skyArea = 7 + terrainSeed[terIndex];
break;
case 3:
// more jagged terrain
skyArea = 7 + terrainSeed[terIndex];
break;
}
for(int y = 0; y < 40; y++)
{
if(y < skyArea)
{
// sky
chunk[x][y] = 4;
}
else if(y == skyArea)
{
// surface
switch(biome)
{
case 0:
chunk[x][y] = 2;
break;
case 1:
chunk[x][y] = 7; // bouncy land
break;
case 2:
chunk[x][y] = 2;
break;
case 3:
chunk[x][y] = 13; // volcano land
break;
}
if(y >= 11 && chunk[x][y] == 13)
{
chunk[x][y] = 14;
}
}
else if(y == 39)
{
// bedrock
chunk[x][y] = 3;
}
else if(y > skyArea + 3)
{
index = arrayLoop(chunkOffset+x, ORE_SIZE);
// adjacent empty spaces increase the chance of empty spaces
uint8_t caveChance = 0;
if(chunk[x-1][y] == 10)
{
caveChance += 64;
}
if(chunk[x][y-1] == 10)
{
caveChance += 60;
}
if(oreSeed[index % ORE_SIZE][y-7] > 0 + caveChance)
{
// stone
chunk[x][y] = 1;
if(y > 10)
{ // y-7 because oreSeed y range is 7 less then chunk y range
uint8_t oreChance = 0;
if(chunk[x-1][y] == 7)
{
oreChance += 18;
}
if(chunk[x][y-1] == 7)
{
oreChance += 18;
}
if(oreSeed[index % ORE_SIZE][y-7] + oreChance > 124)
{
// push
chunk[x][y] = 7;
}
}
if(y > 28)
{
uint8_t oreChance = 0;
if(chunk[x-1][y] == 9)
{
oreChance += 16;
}
if(chunk[x][y-1] == 9)
{
oreChance += 16;
}
if(oreSeed[index % ORE_SIZE][y-7] + oreChance > 125)
{
// gold
chunk[x][y] = 8;
}
}
if(y > 35)
{
uint8_t oreChance = 0;
if(chunk[x-1][y] == 9)
{
oreChance += 14;
}
if(chunk[x][y-1] == 9)
{
oreChance += 14;
}
if(oreSeed[index % ORE_SIZE][y-7] + oreChance > 126)
{
// telerock
chunk[x][y] = 9;
}
}
}
else
{
// cave
chunk[x][y] = 10;
}
}
else
{
if(biome == 3)
{
// magma
chunk[x][y] = 14;
}
else
{
// dirt
chunk[x][y] = 0;
}
}
}
}
}
uint8_t loadChunk(uint8_t chunk[30][40], int32_t chunkOffset)
{
for(uint8_t c = 0; c < (31999-GDS)/CHUNK_SIZE; c++)
{
// retrieve chunk location
int32_t chOff = saveMemory[GDS+c*CHUNK_SIZE] + (saveMemory[GDS+c*CHUNK_SIZE+1]<<8) + (saveMemory[GDS+c*CHUNK_SIZE+2]<<16) + (saveMemory[GDS+c*CHUNK_SIZE+3]<<24);
if(chOff == chunkOffset) // if this is the chunk we want to load
{
// load in chunk data
uint16_t index = 0;
for(int x = 0; x < 30; x++)
{
for(int y = 0; y < 40; y++)
{
chunk[x][y] = saveMemory[GDS+4+c*CHUNK_SIZE+index];
index++;
}
}
return 0;
}
}
return 1;
}
void drawTerrain()
{
int relCurOffset = viewX - curChunkOffset;
int relNextOffset = viewX - nextChunkOffset;
uint16_t g, blk;
// variables to preload biome grass color
// doing it in the loop was very slow
uint16_t gPlusCur = 0;
uint16_t gPlusNext = 0;
// get biome grass color offset for both chunks
uint8_t index = arrayLoop(curChunkOffset/30, BIOME_SIZE);
switch(biomeSeed[(index % BIOME_SIZE) / BIOME_CHUNKS])
{
case 1:
gPlusCur = RGB(7,0,0);
break;
case 2:
gPlusCur = RGB(0,0,6);
break;
}
index = arrayLoop(nextChunkOffset/30, BIOME_SIZE);
switch(biomeSeed[(index % BIOME_SIZE) / BIOME_CHUNKS])
{
case 1:
gPlusNext = RGB(7,0,0);
break;
case 2:
gPlusNext = RGB(0,0,6);
break;
}
// draw the blocks
for(int x = 0; x < SW; x++)
{
// originally i wanted to make it so the blocks had texture but didnt
// want to overwhelm myself and it might end up being too slow
int relX = x/8;
for(int y = 8; y < SH; y++) // start at 8 because HUD covers it anyway
{
if(relX == 7 && (y/8 == 5 || y/8 == 4))
{
VRAM[y*GBA_SW+x] = blockGraphic[6]; // draw player
}
else if(y/8+viewY >= 0 && y/8+viewY < 40)
{
if(relX+relCurOffset >= 0 && relX+relCurOffset < 30)
{
blk = currentChunk[relX+relCurOffset][y/8+viewY];
g = blockGraphic[blk];
if(blk == 2)
{
g += gPlusCur;
}
VRAM[y*GBA_SW+x] = g;
}
else if(relX+relNextOffset >= 0 && relX+relNextOffset < 30)
{
blk = nextChunk[relX+relNextOffset][y/8+viewY];
g = blockGraphic[blk];
if(blk == 2)
{
g += gPlusNext;
}
VRAM[y*GBA_SW+x] = g;
}
else
{
VRAM[y*GBA_SW+x] = blockGraphic[5]; // draw void
}
}
else
{
VRAM[y*GBA_SW+x] = blockGraphic[5]; // draw void
}
}
}
}
void drawHUD()
{
// draw inventory
uint8_t invenSize = sizeof(inventory)/sizeof(inventory[0]);
for(uint8_t i = 0; i < invenSize*8; i++)
{
for(uint8_t y = 0; y < 8; y++)
{
if(y == 0 || y == 7 || i%8 == 0 || i == 0 || (i+1)%8 == 0)
{
if(i/8 == invenSel)
{
// highlight selected inventory slot
VRAM[y*GBA_SW+i] = RGB(29,29,29);
}
else
{
// inventory slot borders
VRAM[y*GBA_SW+i] = RGB(10,10,10);
}
}
else if(inventory[i/8][0] != 255)
{
// contents of inventory slot
VRAM[y*GBA_SW+i] = blockGraphic[inventory[i/8][0]];
}
else
{
// empty inventory slot color
VRAM[y*GBA_SW+i] = RGB(22,22,22);
}
}
}
// draw health bar
for(uint8_t i = 0; i < playerHP*SW/20; i++)
{
for(uint8_t y = 8; y < 11; y++)
{
VRAM[y*GBA_SW+i] = RGB(28,0,0);
}
}
}
void teleport(int32_t x, int32_t y)
{
viewX = x;
viewY = y;
savePlayer();
// generate spawn chunks
curChunkOffset = x/30*30; // round down to nearest 30
if(loadChunk(currentChunk, curChunkOffset))
{
generateChunk(currentChunk, curChunkOffset);
curChanges = 0;
}
else
{
curChanges = CHANGE_CAP;
}
if((x+7)%30 < 15) // check which side of the current chunk the player is
{
x -= 30;
}
else
{
x += 30;
}
nextChunkOffset = x/30*30;
if(loadChunk(nextChunk, nextChunkOffset))
{
generateChunk(nextChunk, nextChunkOffset);
nextChanges = 0;
}
else
{
nextChanges = CHANGE_CAP;
}
}
// player die
void playerDie()
{
// DIE
playerHP = 20;
hForce = 0;
vForce = 0;
// clear inventory
for(uint8_t i = 0; i < sizeof(inventory)/sizeof(inventory[0]); i++)
{
inventory[i][0] = 255;
inventory[i][1] = 0;
}
saveInven();
// reset player position
teleport(0, -1);
}
// handle entities
void updateEntities()
{
for(uint8_t e = 0; e < numEntities; e++)
{
// skip deactivated entites
if(entities[e].id == 0)
{
continue;
}
// despawn entities
// if the entity is not in a rendered chunk it is deactivated
if(nextChunkOffset < curChunkOffset)
{
if(entities[e].x < nextChunkOffset || entities[e].x > curChunkOffset+30)
{
entities[e].id = 0;
continue;
}
}
else
{
if(entities[e].x < curChunkOffset || entities[e].x > nextChunkOffset+30)
{
entities[e].id = 0;
continue;
}
}
int32_t chunkOffset = 0;
uint8_t(* chunk)[40];
if(entities[e].x >= curChunkOffset && entities[e].x < curChunkOffset+30)
{
chunk = currentChunk;
chunkOffset = curChunkOffset;
}
else
{
chunk = nextChunk;
chunkOffset = nextChunkOffset;
}
// get enemy location relative to chunk
int32_t enOffsetX = (entities[e].x - chunkOffset);
int32_t enOffsetY = entities[e].y;
// update entities
if(entities[e].id == 1) // mine creatures... sleepers?
{
if(entities[e].hF == 0 && entities[e].vF == 0 && abs(viewX + 7 - entities[e].x) < 2) // check distance to player
{
// check what chunk the enemy is in
int32_t chunkOffset = 0;
uint8_t(* chunk)[40];
if(entities[e].x >= curChunkOffset && entities[e].x < curChunkOffset+30)
{
chunk = currentChunk;
chunkOffset = curChunkOffset;
}
else
{
chunk = nextChunk;
chunkOffset = nextChunkOffset;
}
// get enemy location relative to chunk
int32_t enOffsetX = (entities[e].x - chunkOffset);
int32_t enOffsetY = entities[e].y;
if(abs(viewY + 5 - entities[e].y) < 3) // explode
{
// deactivate enemy
entities[e].id = 0;
saveEntities();
// set surrounding blocks to sky/wall
uint8_t radius = 2;
for(uint8_t bx = 0; bx < radius*2+1; bx++)
{ // make sure only the chunk memory is altered
if(enOffsetX - radius + bx < 0 || enOffsetX - radius + bx > 29) { continue; }
for(uint8_t by = 0; by < radius*2+1; by++)
{
if(enOffsetY - radius + by < 0 || enOffsetY - radius + by > 39) { continue; }
// properly remove active telerock
if(chunk[enOffsetX - radius + bx][enOffsetY - radius + by] == 12)
{
int32_t realX = enOffsetX - radius + bx + chunkOffset;
for(uint8_t t = 0; t < sizeof(telerocks)/sizeof(telerocks[0]); t++)
{
if(realX == telerocks[t][0] && enOffsetY - radius + by == telerocks[t][1])
{
telerocks[t][1] = -1;
playNote(0xF6, 0x5);
break;
}
}
saveTelerock();
}
if(enOffsetY - radius + by > UNDERGROUND)
{
chunk[enOffsetX - radius + bx][enOffsetY - radius + by] = 10;
}
else
{
chunk[enOffsetX - radius + bx][enOffsetY - radius + by] = 4;
}
}
}
playNote(0xF00F, 0x20);
playerHP -= (3 - abs(viewX + 7 - entities[e].x)) * 7;
if(playerHP <= 0)
{
playerDie();
}
saveHealth();
}
else if(entities[e].hF == 0 && entities[e].vF == 0 && viewY + 5 - entities[e].y < 0 && viewY + 5 - entities[e].y > -13)
{
// bounce
if(chunk[enOffsetX][enOffsetY+1] == 7)
{
entities[e].vF -= 8;
}
if(chunk[enOffsetX][enOffsetY-1] == 7)
{
entities[e].vF += 8;
}
if(chunk[enOffsetX+1][enOffsetY] == 7)
{
entities[e].hF -= 8;
}
if(chunk[enOffsetX-1][enOffsetY] == 7)
{
entities[e].hF += 8;
}
entities[e].y -= 1;
}
}
}
else if(entities[e].id == 2)
{
// gravity
if((entities[e].hF == 0 && entities[e].vF == 0)
&& (chunk[enOffsetX][enOffsetY+1] == 4 || chunk[enOffsetX][enOffsetY+1] == 10))
{
entities[e].y++;
saveEntities();
}
if(rand() % 12 == 0) // 10 percent chance to do something
{
if(entities[e].hF == 0 && entities[e].vF == 0)
{
if(viewX+7 - entities[e].x < -1 || viewX+7 - entities[e].x > 1) // enemy move
{
// get direction of player in the x axis
int8_t dirMod = (viewX+7 - entities[e].x)/abs(viewX+7 - entities[e].x);
// check what chunk the enemy would move into
int32_t chO = 0;
uint8_t(* ch)[40];
if(entities[e].x+dirMod >= curChunkOffset && entities[e].x+dirMod < curChunkOffset+30)
{
ch = currentChunk;
chO = curChunkOffset;
}
else
{
ch = nextChunk;
chO = nextChunkOffset;
}
// get enemy location relative to chunk
int32_t enX = (entities[e].x - chO);
int32_t enY = entities[e].y;
if(ch[enX+dirMod][enY] == 4 || ch[enX+dirMod][enY] == 10)
{
entities[e].x+=dirMod;
saveEntities();
}
else if((ch[enX+dirMod][enY-1] == 4 || ch[enX+dirMod][enY-1] == 10)
&& (chunk[enOffsetX][enOffsetY-1] == 4 || chunk[enOffsetX][enOffsetY-1] == 10)
&& (chunk[enOffsetX][enOffsetY+1] != 4 && chunk[enOffsetX][enOffsetY+1] != 10))
{
entities[e].y--;
entities[e].x+=dirMod;
saveEntities();
}
}
else if(entities[e].y == viewY+5 || entities[e].y == viewY+4) // enemy in attack range of player
{
playerHP --;
if(playerHP <= 0)
{
playerDie();
}
saveHealth();
playNote(0xF10F, 0x0F);
}
else if((chunk[enOffsetX][enOffsetY-1] == 4 || chunk[enOffsetX][enOffsetY-1] == 10)
&& (chunk[enOffsetX][enOffsetY+1] != 4 && chunk[enOffsetX][enOffsetY+1] != 10))
{
entities[e].y--;
saveEntities();
if(entities[e].y == viewY+5 || entities[e].y == viewY+4) // enemy in attack range of player
{
playerHP --;
if(playerHP <= 0)
{
playerDie();
}
saveHealth();
playNote(0xF10F, 0x0F);
}
}
}
// burn
if(chunk[enOffsetX][enOffsetY+1] == 14)
{
entities[e].hp--;
if(entities[e].hp <= 0)
{
entities[e].id = 0;
}
saveEntities();
}
// bounce
if(chunk[enOffsetX][enOffsetY+1] == 7)
{
entities[e].vF -= 8;
playNote(0xF3FF, 0x0D);
}
if(chunk[enOffsetX][enOffsetY-1] == 7)
{
entities[e].vF += 8;
playNote(0xF3FF, 0x0D);
}
if(chunk[enOffsetX+1][enOffsetY] == 7)
{
entities[e].hF -= 8;
playNote(0xF3FF, 0x0D);
}
if(chunk[enOffsetX-1][enOffsetY] == 7)
{
entities[e].hF += 8;
playNote(0xF3FF, 0x0D);
}
}
}
if(entities[e].vF != 0)
{
// push vertically
int8_t vPush = 1*entities[e].vF/abs(entities[e].vF);
// makes sure you don't go through blocks or outside the world
if(entities[e].vF > 0)
{
if(chunk[enOffsetX][enOffsetY+1] == 4 || chunk[enOffsetX][enOffsetY+1] == 10)
{
goto VerticalPushE;
}
}
else
{
if(chunk[enOffsetX][enOffsetY-1] == 4 || chunk[enOffsetX][enOffsetY-1] == 10)
{
goto VerticalPushE;
}
}
// take damage if movement blocked
entities[e].hp -= abs(entities[e].vF/7);
if(entities[e].hp <= 0)
{
entities[e].id = 0;
}
saveEntities();
goto SkipVPushE;
VerticalPushE:
entities[e].y += vPush;
SkipVPushE:
entities[e].vF -= vPush;
}
// push horizontally
if(entities[e].hF != 0)
{
int8_t hPush = 1*entities[e].hF/abs(entities[e].hF);
entities[e].hF -= hPush;
// check what chunk the enemy would move into
int32_t chO = 0;
uint8_t(* ch)[40];
if(entities[e].x+hPush >= curChunkOffset && entities[e].x+hPush < curChunkOffset+30)
{
ch = currentChunk;
chO = curChunkOffset;
}
else
{
ch = nextChunk;
chO = nextChunkOffset;
}
// get enemy location relative to chunk
int32_t enX = (entities[e].x - chO);
int32_t enY = entities[e].y;
if(ch[enX+hPush][enY] == 4 || ch[enX+hPush][enY] == 10)
{
entities[e].x += hPush;
}
else
{
entities[e].hp -= abs(entities[e].hF/7);
if(entities[e].hp <= 0)
{
entities[e].id = 0;
}
saveEntities();
}
}
// draw entities
if(entities[e].id != 0 && entities[e].x >= viewX && entities[e].x < viewX+15
&& entities[e].y >= viewY && entities[e].y < viewY+10)
{
for(uint8_t x = 0; x < 8; x++)
{
for(uint8_t y = 0; y < 8; y++)
{
int32_t enOffsetX = (entities[e].x - viewX) * 8 + x;
int32_t enOffsetY = (entities[e].y - viewY) * 8 + y;
VRAM[enOffsetY*GBA_SW+enOffsetX] = entities[e].color;
}
}
}
}
}
void createEntity(uint8_t id, uint16_t color, int16_t hp, int32_t x, int32_t y)
{
for(uint8_t e = 0; e < numEntities; e++)
{
if(entities[e].id != 0) { continue; }
entities[e].id = id;
entities[e].color = color;
entities[e].hp = hp;
entities[e].x = x;
entities[e].y = y;
saveEntities();
// get enemy location relative to chunk
int32_t chunkOffset = 0;
uint8_t(* chunk)[40];
if(entities[e].x >= curChunkOffset && entities[e].x < curChunkOffset+30)
{
chunk = currentChunk;
chunkOffset = curChunkOffset;
}
else
{
chunk = nextChunk;
chunkOffset = nextChunkOffset;
}
if(id == 1)
{
int32_t enOffsetX = (entities[e].x - chunkOffset);
int32_t enOffsetY = entities[e].y;
// create hole
for(int8_t y = 0; y < enOffsetY; y++)
{
if(enOffsetY-y > UNDERGROUND)
{
chunk[enOffsetX][enOffsetY-y] = 10;
}
else
{
chunk[enOffsetX][enOffsetY-y] = 4;
}
}
}
else
{
while(chunk[entities[e].x - chunkOffset][entities[e].y] != 4 && chunk[entities[e].x - chunkOffset][entities[e].y] != 10
&& chunk[entities[e].x - chunkOffset][entities[e].y] != 5)
{
entities[e].y--;
}
while(chunk[entities[e].x - chunkOffset][entities[e].y] == 5)
{
int8_t dist = viewX - entities[e].x;
int8_t dirMod = (dist)/abs(dist);
entities[e].x += dirMod;
}
}
break;
}
}
void spawnSleeper(int32_t x)
{
uint8_t r = rand() % 4;
if(r == 0)
{
createEntity(1, RGB(0,27,0), 6, x, 18);
}
}
void spawnSlime(int32_t x, int32_t y)
{
uint8_t r = rand() % 270;
if(r == 0 || r == 269)
{
uint16_t col;
if(r > 134)
{
col = RGB(28,7,7);
}
else
{
col = RGB(4,12,28);
}
createEntity(2, col, 10, x, y);
}
}
uint8_t checkBreak(uint8_t block, uint8_t relX, uint8_t relY, uint8_t target, int32_t chunkOff)
{
// resource collection
if(block == 4) // if sky
{
uint8_t invenSize = sizeof(inventory)/sizeof(inventory[0]);