-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crumbling_crypt.p8
1649 lines (1488 loc) · 64 KB
/
crumbling_crypt.p8
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
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
-- Crumbling Crypt
-- by achiegamedev
-- Add McGuffin (later)
-- Fix Movement
-- Sniffers (?)
function add_enemy(_x, _y, _type)
local enemy = {
x = _x,
y = _y,
canStep = 0,
canSeePlayer = false,
type = _type,
bop = rnd({0.2,0.3,0.4}),
offx = 0,
offy = 0
}
if (_type == 3) or (_type == 6) then
enemy.canStep = 1
end
add(enemies, enemy)
end
function add_pickup(_x, _y, _type)
local p = {
x = _x,
y = _y,
type = _type
}
add(pickups, p)
end
function digger_showcase()
-- random walker on an 8x8 grid on map
-- set rooms to 1 if there should be something
-- move randomly on if we are good
local roomNumber = rnd({4,5,6,7,8})+flr(player.level/2)
local xStart,yStart = flr(rnd(8)),flr(rnd(8))
mset(xStart,yStart,1)
-- init the dungeon with a starer point
local diggerX, diggerY = xStart, yStart
-- select a direction to dig to
local dir = rnd({1,2,3,4})
-- offset tables for the directions, left, right, up, down
local dirTable = {{-1,0}, {1,0}, {0,-1}, {0, 1}}
-- dig
while (number_of_rooms() < roomNumber) do
-- randomly turn into a new direction
turn_chance = 0.25
if rnd() < turn_chance then
dir = rnd({1,2,3,4})
end
-- calc new position that the digger would go to
local newX, newY = diggerX+dirTable[dir][1],diggerY+dirTable[dir][2]
-- check if it is inside the map
while ((newX < 0) or (newX > 7) or (newY < 0) or (newY > 7)) do
-- if not, find a new direction, check that
dir = rnd({1,2,3,4})
newX, newY = diggerX+dirTable[dir][1],diggerY+dirTable[dir][2]
end
-- set the new room
mset(newX, newY, 1)
-- set the new digger pos
diggerX,diggerY = newX,newY
end
end
function generate_dungeon()
-- random walker on an 8x8 grid on map
-- set rooms to 1 if there should be something
-- move randomly on if we are good
local roomNumber = 4+flr(player.level/2)
local xStart,yStart = flr(rnd(8)),flr(rnd(8))
mset(xStart,yStart,1)
-- init the dungeon with a starer point
local diggerX, diggerY = xStart, yStart
-- select a direction to dig to
local dir = rnd({1,2,3,4})
-- offset tables for the directions, left, right, up, down
local dirTable = {{-1,0}, {1,0}, {0,-1}, {0, 1}}
-- dig
while (number_of_rooms() < roomNumber) do
-- randomly turn into a new direction
turn_chance = 0.25
if rnd() < turn_chance then
dir = rnd({1,2,3,4})
end
-- calc new position that the digger would go to
local newX, newY = diggerX+dirTable[dir][1],diggerY+dirTable[dir][2]
-- check if it is inside the map
while ((newX < 0) or (newX > 7) or (newY < 0) or (newY > 7)) do
-- if not, find a new direction, check that
dir = rnd({1,2,3,4})
newX, newY = diggerX+dirTable[dir][1],diggerY+dirTable[dir][2]
end
-- set the new room
mset(newX, newY, 1)
-- set the new digger pos
diggerX,diggerY = newX,newY
end
local dungeonString =""
-- gather digger data
-- dungeon is the collection for the
-- eventual dungeon data (room, not room)
-- dungeon string is debug basically
for x=0,7 do
for y=0,7 do
if (mget(x,y) == 1) then
-- if the digger has a room there, add it to the dungeon
-- +1-s so the array is indexed correctly(array from 1, map from 0)
dungeon[x+1][y+1] = 1
-- reset the map tile to empty once we added it
mset(x,y, 0)
dungeonString..="1"
else
-- if it was never set by the digger, no need to reset it
dungeonString..="0"
end
end
dungeonString..="\n"
end
-- put in the dummy filler for the dungeon
for x = 0,127 do
for y = 0,127 do
mset(x,y, 63)
end
end
-- here we buiild the actual map based on the
-- data inside dungeon. basically we blow up
-- the 8x8 "dot" array, to a 8x8, 16x16 rooms (8x8 rooms filling the whole screen)
for x=1,8 do
for y=1,8 do
-- if the dungeon has a room in positions
if (dungeon[x][y] == 1) then
-- generate the rooms on the map
-- this is done by grabbing the corners and
-- multiplying them by 16 so we start at the right
-- map coordinates
generate_room((x-1)*16, (y-1)*16)
-- try to fix room entrances
for i=1,4 do
-- we grab the neighbouring tiles from the dungeon based
-- on the direction data
local newCellX, newCellY = x+dirTable[i][1],y+dirTable[i][2]
-- if this new cell is inside the "dungeon"
if not ((newCellX > 8) or (newCellX < 1) or (newCellY > 8) or (newCellY < 1)) then
-- check if it is a room, if yes, create the
-- "door" between the current room and the new one
if dungeon[newCellX][newCellY] == 1 then
-- locally doors should be on
-- left
-- {0,7},{0,8}
-- right
-- {15, 7}, {15, 8}
-- up
-- {7,0},{8,0}
-- down
-- {7, 15}, {8,15}
-- offset to map coords (0 indexed vs 1 indexed array)
local xMap, yMap = x-1,y-1
-- set the wall tiles to doors based on direction
if i==1 then
-- mset the tile on the map, with the positions
-- and the offsets above.
mset( xMap*16, yMap*16+7, 61)
mset( xMap*16, yMap*16+8, 61)
elseif i==2 then
mset( xMap*16+15, yMap*16+7, 61)
mset( xMap*16+15, yMap*16+8, 61)
elseif i==3 then
mset( xMap*16+7, yMap*16, 61)
mset( xMap*16+8, yMap*16, 61)
elseif i==4 then
mset( xMap*16+7, yMap*16+15, 61)
mset( xMap*16+8, yMap*16+15, 61)
end
end
end
end
end
end
end
-- add the brick part to the top wall
-- if the bottom is empty
local emptySpaceCollection = {}
-- loops throught the whole map
for x = 0,127 do
for y = 0,127 do
-- if it is an empty tile add it to the emptySpace
-- collection to spawn stuff later oon
if (mget(x,y) != 1) and (mget(x,y) != 63) then
local point = {x=x,y=y}
add(emptySpaceCollection,point)
end
-- if it is a wall on top, that means it is a 1 tile
-- wall, set it to the smaller wall tile
if (mget(x,y-1) == 1) and (not (mget(x,y)==1)) then
mset(x,y-1,2)
end
end
end
-- find a spot for the player
local playerStarterPos = rnd(emptySpaceCollection)
del(emptySpaceCollection, playerStarterPos)
-- if we are on the 10th level spawn the mcguffin
if (player.level == 10) then
local mcguffinPos = rnd(emptySpaceCollection)
add_pickup(mcguffinPos.x, mcguffinPos.y, 5)
-- add guardian
local guardianSpot = rnd(emptySpaceCollection)
del(emptySpaceCollection, guardianSpot)
add_enemy(guardianSpot.x, guardianSpot.y, 7)
else
-- create random trapdoor down
local downPos = rnd(emptySpaceCollection)
mset(downPos.x, downPos.y, 55)
-- save the position so we can check against
-- runtime to see if we are on the door
doorDown.x = downPos.x
doorDown.y = downPos.y
del(emptySpaceCollection,downPos)
-- find a spot for the key
local keyPos = rnd(emptySpaceCollection)
add_pickup(keyPos.x, keyPos.y, 4)
del(emptySpaceCollection, keyPos)
end
-- add decals
for space in all(emptySpaceCollection) do
if rnd() > 0.92 then
mset(space.x, space.y, rnd({0,0,0,0,0,56,57,58,59,60}))
end
end
-- set the player's starting position
-- and set the tile under to the stairs
player.x = playerStarterPos.x
player.y = playerStarterPos.y
mset(playerStarterPos.x, playerStarterPos.y, 54)
end
-- room counter for the digger
function number_of_rooms()
-- dig until we have set number of rooms
-- so for that we just check which tiles are set
local rooms = 0
for x=0,7 do
for y=0,7 do
if mget(x,y) == 1 then
rooms+=1
end
end
end
return rooms
end
function generate_room(x, y)
-- for rooms we want to generate from a pre-build corner set
-- we choose a random preset for each corner
local corners = {0,0,0,0}
for i=1,4 do
--printh("selecting corners")
local rand = flr(rnd(4)) + ((i-1)*4)
corners[i] = rand
--printh(rand.." for corner: "..i)
end
-- then we pass the corner "collection" to the builder
-- to actually build the tiles
for i=0,3 do
build_room(corners[i+1], i, x, y)
end
end
function build_room(corner_sprite_id, corner, _x, _y)
--printh("building room at: ".._x.." ".._y)
local idString = ""
-- indicate where the given sprite starts on
-- the sprite sheet.
local xStart = 0
local yStart = 0
xStart = corner_sprite_id*8
yStart = 32
--printh("corner "..corner.." sprite "..spr)
-- gather pixel color values
--printh("gathering colors")
-- we start with y so we gather the x coords first
-- so our mset builder down below builds correctly
for y=0,7 do
for x=0,7 do
-- gather the colors from the sprite.
-- it starts from the sprite number*8, and all of them are at y=32 on the sprite sheet
-- but this could be configurated later for more
idString = idString..sget(xStart+x, yStart+y)..","
end
end
-- where to start the build based on which corner we
-- are doing at the moment. Top left corner is 0,0, top right is 8,8,
-- bottom right is 8,8, bottom left is 0,8
local mapCornerTable = {{0,0}, {8,0}, {8,8}, {0,8}}
local mapBuildStartX = mapCornerTable[corner+1][1]+_x
local mapBuildStartY = mapCornerTable[corner+1][2]+_y
local x,y = 0,0
-- now we just loop over the colors and place the
-- tiles corresponding to the color.
for val in all(split(idString)) do
--printh("setting: "..x..","..y.."to: "..val)
if val == 1 then
--dark blue - wall
mset(x+mapBuildStartX,y+mapBuildStartY, 1)
--printh("set at: "..x+mapBuildStartX.." "..y+mapBuildStartY)
elseif val == 2 then
-- dark red, enemy
if rnd() > 0.3-(player.level/10) then
--mset(x+mapBuildStartX,y+mapBuildStartY, 5)
local enemyCollectionToSpawnFrom = {1,1,1}
-- add harder enemies into the spawning chance based on
-- how deep we are into the crypt
if player.level >= 3 then add(enemyCollectionToSpawnFrom, 2) end
if player.level >= 5 then add(enemyCollectionToSpawnFrom, 3) end
if player.level >= 7 then add(enemyCollectionToSpawnFrom, 4) end
if player.level >= 8 then add(enemyCollectionToSpawnFrom, 5) end
add_enemy(x+mapBuildStartX,y+mapBuildStartY, rnd(enemyCollectionToSpawnFrom))
end
mset(x+mapBuildStartX,y+mapBuildStartY, 0)
elseif val == 3 then
-- dark green, pickup
if rnd() > 0.5 then
add_pickup(x+mapBuildStartX,y+mapBuildStartY, rnd({1,2}))
end
-- set the tile to 0 so it can be looked in
-- while running the A*
mset(x+mapBuildStartX,y+mapBuildStartY, 0)
elseif val == 0 then
-- set the tile to 0 so it can be looked in
-- while running the A*
mset(x+mapBuildStartX,y+mapBuildStartY, 0)
end
x+=1
-- this is to convert the 1D array
-- into 2D world coordinates. At every 8th tile we go
-- to the next row.
if x > 7 then
x = 0
y += 1
end
end
end
-- calc manthattan distance, which is the grid based
-- tile distance between two points
function heuristic(a, b)
return (abs(a.x - b.x)+ abs(a.y-b.y))
end
-- Insertion Sort by @impbox
function sort(array)
for i=1,#array do
local j = i
while j > 1 and array[j-1].f > array[j].f do
array[j],array[j-1] = array[j-1],array[j]
j = j - 1
end
end
end
-- scuffed sorter for an array with "holes"
function sort_holey(array)
local nonHoledArray = {}
for _,node in pairs(array) do
if (node != nil) and ( node != {}) then
add(nonHoledArray, node)
::skip::
end
end
sort(nonHoledArray)
return nonHoledArray
end
-- pop from sorted array for array with holes
-- gather non-hole elements, sort them, pop
function pop_sort(array)
local nonHoledArray = {}
for _,node in pairs(array) do
if (node != nil) and ( node != {}) then
add(nonHoledArray, node)
::skip::
end
end
sort(nonHoledArray)
local node = nonHoledArray[1]
array[node.x+node.y*128] = nil
return node
end
-- starting to get jank, but is a check for spaces
-- to see if an enemy can step/spawn there, basically
-- empty tile check
function is_legal_spot(spot, enemy, enemyPosTable)
local x = spot.x
local y = spot.y
-- is the spot already taken
if (enemyPosTable[x+y*128] == "taken") then return false end
-- is it inside the map
if (x < 0) or (x > 128) or (y < 0) or (y > 128) then
return false
end
return true
end
-- helper to check against all tiles that are walls
function wall_tile_helper(spot,canBreakWalls)
if canBreakWalls then return true end
local x = spot.x
local y = spot.y
--land = {1}
--0
local tileId = mget(x,y)
-- walls that are breakable are 1,2 and 64
if (tileId == 1) or (tileId == 2) or (tileId == 63) then
return false
end
return true
end
-- find existing neighbour tiles that are walkable (non walls)
function get_neighbours(pos, enemy, enemyPosTable)
local neighbours={}
local xPos = pos.x
local yPos = pos.y
-- classic offsets to index tiles from the center
-- tile we are calling this with
local up = {x = xPos, y=yPos-1}
local down = {x = xPos, y=yPos+1}
local left = {x = xPos-1, y=yPos}
local right = {x = xPos+1, y=yPos}
if is_legal_spot(up, enemy, enemyPosTable) and wall_tile_helper(up,enemy.type==4) then
add(neighbours,up)
end
if is_legal_spot(down, enemy, enemyPosTable) and wall_tile_helper(down,enemy.type==4) then
add(neighbours,down)
end
if is_legal_spot(left, enemy, enemyPosTable) and wall_tile_helper(left,enemy.type==4) then
add(neighbours,left)
end
if is_legal_spot(right, enemy, enemyPosTable) and wall_tile_helper(right,enemy.type==4) then
add(neighbours,right)
end
return neighbours
end
-- check if the array already has an element
-- at the indexed point
function is_node_inside_set(set, index)
if set[index] == nil then return false end
if set[index] == {} then return false end
return true
end
--- a* from colony management,
-- janked up, because sniffers are
-- my new nightmare. For more in depth
-- explanation take a look at:
-- https://www.redblobgames.com/pathfinding/a-star/introduction.html
-- enemyPositions is my jank solution to
-- not let them step on others, and help skip
-- a few checks. Also handy for slimes
function find_path(enemy, goal)
openSet = {}
openIndexedSet = {}
closedIndexedSet = {}
closedList = {}
startNode = {x=enemy.x, y=enemy.y}
startNode.g = 0
startNode.h = heuristic(startNode, goal)
startNode.f = startNode.g + startNode.h
startNode.parent = 0
endNode = goal
endNode.g = 0
endNode.h = 0
endNode.f = 0
enemy.path = {}
add(openSet, startNode)
local enemyPositions = {}
for otherEnemy in all(enemies) do
if enemy != otherEnemy then
if on_same_screen(enemy, otherEnemy) then
enemyPositions[otherEnemy.x+otherEnemy.y*128] = "taken"
end
end
end
openIndexedSet[enemy.x+enemy.y*128] = startNode
local openSetCount = 1
local iteration = 0
while (openSetCount > 0 and openSetCount < 1000) do
-- i should sort here
-- pop the least value out of the open
local current = pop_sort(openIndexedSet)
-- if we are on the goal, trace back on the parents and add those to the
-- path
if (current.x == goal.x) and (current.y == goal.y) then
while not (current == startNode) do
add(enemy.path, current)
current = current.parent
end
enemy.closedList = {}
for _,p in pairs(closedIndexedSet) do
if not (p==nil) then
add(enemy.closedList,p)
end
end
return
end
-- add node to closed
-- add(closedList, current)
closedIndexedSet[current.x+current.y*128] = current
-- get valid neighbours
local neighbours = get_neighbours(current, enemy, enemyPositions)
for node in all(neighbours) do
-- check if item is alredy in the closed
local alreadyInClosed = false
if is_node_inside_set(closedIndexedSet, node.x+node.y*128) then alreadyInClosed = true end
-- if not in closed
if not alreadyInClosed then
node.g = current.g
node.h = heuristic(goal, node)
node.f = node.g+node.h
-- check if node is already in the openList
local alreadyInOpen = false
if is_node_inside_set(openIndexedSet, node.x+node.y*128) then
alreadyInOpen = true
if openIndexedSet[node.x+node.y*128].g >= node.g then
openIndexedSet[node.x+node.y*128].parent = current
openIndexedSet[node.x+node.y*128].g = node.g
openIndexedSet[node.x+node.y*128].f = node.f
openIndexedSet[node.x+node.y*128].h = node.h
--openIndexedSet = sort_holey(openIndexedSet)
end
end
-- if the node is not in the open, the add node to the open
if (not alreadyInOpen) then
node.parent = current
--add(openSet,node)
openIndexedSet[node.x+node.y*128] = node
end
end
end
-- count elements
local count = 0
for _,node in pairs(openIndexedSet) do
if (node != nil) and (node != {}) then
count += 1
end
end
openSetCount = count
iteration += 1
end
enemy.closedList = {}
for _,p in pairs(closedIndexedSet) do
if not (p==nil) then
add(enemy.closedList,p)
end
end
end
-- line of sight with bresenham lines
-- not sure from where, thanks for whoever you are!
-- basically draw a line between two tiles, and see
-- if any tiles that are along the line are wall tiles
-- if yes, we cannot see, if no, we can see spot 1 (x0,y0) from
-- spot2 (x1,y1)
-- more info on: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
function line_of_sight(x0,y0,x1,y1)
local sx,sy,dx,dy
if x0 < x1 then
sx = 1
dx = x1 - x0
else
sx = -1
dx = x0 - x1
end
if y0 < y1 then
sy = 1
dy = y1 - y0
else
sy = -1
dy = y0 - y1
end
local err, e2 = dx-dy, nil
while not(x0 == x1 and y0 == y1) do
e2 = err + err
if e2 > -dy then
err = err - dy
x0 = x0 + sx
end
if e2 < dx then
err = err + dx
y0 = y0 + sy
end
local tile = mget(x0, y0)
if (tile == 1) or (tile == 2) then return false end
end
return true
end
-- check if to entity-s are on the same screen
function on_same_screen(entityA, entityB)
return (flr(entityA.x/16) == flr(entityB.x/16)) and (flr(entityA.y/16) == flr(entityB.y/16))
end
-- tooltip startup
-- my tooltip as an x position
-- that is where currently it is,
-- a stay timer, which decides how long
-- it is on screen
function init_tooltip(text, stay)
tooltip = {
text=text,
startX = -100,
x = -100,
stay = stay,
timer = stay+20
}
end
-- from pancelor's gamedev blog about input
-- buffering in grid games
function approach(x, target, speed)
if speed == nil then speed = 1 end
return x < target and min(x + speed,target) or max(x - speed, target)
end
-- huge init
function _init()
music(10)
player = {
x = 2,
y = 2,
sword = true,
shield = false,
potion = 0,
steps = 2,
level = 1,
screenX = 0,
screenY = 0,
offx=0,
offy=0
}
dungeon = {
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 }
}
enemies = {}
pickups = {}
state = "menu"
tooltip = {
text="",
startX = -100,
x = -100,
stay = 0,
timer = 0
}
--init_tooltip("floor 1", 20)
-- color keep
poke(0x5f2e,1)
-- poke bigger map
--reload(0x8000, 0x8000, 0x7fff)
memset(0x8000, 0, 0x4000)
poke(0x5f56, 0x80)
poke(0x5f57, 128)
-- palette
pal({[0]=129,13,2,3,4,5,6,7,8,9,10,11,12,13,14,15},1)
doorDown = {}
generate_dungeon()
--digger_showcase()
--player.x, player.y = 65,65
transition = 0
player.screenX = flr(player.x/16)*128
player.screenY = flr(player.y/16)*128
musicToggle = true
menuitem(1, "music "..tostr(musicToggle), toggle_music)
end
-- this is the function that is called
-- by the menuitem toggle music
-- basically play or stop music
function toggle_music()
musicToggle = not musicToggle
if musicToggle then
music(10)
else
music(-1,300)
end
end
-- create a new level to be played
function load_level()
-- increase level we are on
player.level += 1
-- we don't have the key, so we
-- cannot enter the trapdoor down
-- unti we find it again
player.key = false
-- reset the digger's data, basically
-- it digs on an 8x8 area an that is blow
-- up into 16x16 rooms
dungeon = {
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 },
{0, 0, 0, 0, 0, 0, 0, 0 }
}
-- reset enemies, pickups and the state
enemies = {}
pickups = {}
state = "player"
-- reset tooltip
tooltip = {
text="",
startX = -100,
x = -100,
stay = 0,
timer = 0
}
-- creates the "floor x" float-in text
init_tooltip("floor "..player.level, 20)
-- poke bigger map
--reload(0x8000, 0x8000, 0x7fff)
memset(0x8000, 0, 0x4000)
-- forget the old door position
doorDown = {}
-- generate the dungen
generate_dungeon()
end
function _update()
if state == "menu" then
update_menu()
elseif state == "player" then
-- player's turn
update_player()
elseif state == "enemies" then
-- enemy turn
update_enemies()
elseif state == "dead" then
update_dead()
elseif state == "won" then
update_won()
end
-- set the camera's position
local camPosX = player.screenX
local camPosY = player.screenY
camera(camPosX, camPosY)
-- tooltip lerping, this could be heavly
-- optimized by using a lerp library, for ex
-- from FletchMakes
if tooltip.timer > 0 then
local midpoint = player.screenX+63-(print(tooltip.text, 0, -200, 7))/2
if tooltip.x < midpoint then
tooltip.x += (midpoint - tooltip.x)/4
if abs(tooltip.x-midpoint) < 0.5 then
tooltip.stay -= 1
end
end
if tooltip.stay <= 0 then
tooltip.x += (player.screenX+200 - tooltip.x)/4
if tooltip.x > player.screenX+180 then
tooltip.timer = 0
tooltip.x = player.screenX-100
end
end
end
-- reduce the tooltip's timer so it actually goes
-- offscreen
if tooltip.timer == 0 then
tooltip.x = player.screenX-100
end
end
function update_menu()
-- start the game
if btnp(5) then
state = "player"
--_init()
end
end
function update_won()
-- return to the menu
-- and regen a first level
-- so we have a nice backdrop in the menu
if btnp(5) then
state = "menu"
player.level = 0
player.sword = false
player.shield = 0
load_level()
end
end
function update_player()
-- player lerping between two positions
if not (player.offx==0 and player.offy==0) then
player.offx = approach(player.offx, 0, 3)
player.offy = approach(player.offy, 0, 3)
else
-- direction table, that will tell us
-- which button press moves the player
-- in which direction as in x,y pairs.
-- so button 0 - left is moving us -1 on x and
-- 0 on the y. Comes in handy in a few checks
local dirTable = {{-1,0}, {1,0}, {0,-1}, {0, 1}}
-- actualy button direction we pressd
local dir = 0
-- save player position into new_x so we can
-- modifiy position runtime, do checks, and if we cannot
-- move we just don't assign new_x, new_y back to the player
local new_x, new_y = player.x, player.y
local moved = false
-- moving and saving direction
if btnp(0) then
new_x = player.x - 1
moved = true
dir = 1
elseif btnp(1) then
new_x = player.x + 1
moved = true
dir = 2
elseif btnp(2) then
new_y = player.y - 1
moved = true
dir = 3
elseif btnp(3) then
new_y = player.y + 1
moved = true
dir = 4
end
-- if we moved
if moved then
player.oldX, player.oldY = player.x, player.y
--check if can step there
-- flag 0 is walls
if not (fget(mget(new_x, new_y), 0)) then
player.x = new_x
player.y = new_y
end
-- remove one of our 2 steps
player.steps -=1
sfx(0,3)
-- check if enemy is in the way
for i=#enemies,1,-1 do
local enemy = enemies[i]
-- only check things if the enemy is
-- on the same screen as us
if on_same_screen(enemy, player) then
local fought,dead = check_for_combat(enemy, player)
-- if we fought then do not step
if fought and (not dead) then
player.x = player.oldX
player.y = player.oldY
end
end
end
player.offx = dirTable[dir][1]*-8
player.offy = dirTable[dir][2]*-8
end
-- handle all the pickup collisions
-- we init the tooltip to say what
-- we picked up, set the player according to it
-- play a sound, and reset the tile under it, so it can be
-- checked in A*
for pickup in all(pickups) do
if on_same_screen(pickup, player) then
if (pickup.x == player.x) and (pickup.y == player.y) then
if (pickup.type == 1) and (not player.shield) then
player.shield = true
init_tooltip("shield acquired", 20)
sfx(1,3)
del(pickups, pickup)
mset(pickup.x*8, pickup.y*8, 0)
elseif (pickup.type == 2) and (not player.sword) then
player.sword = true
init_tooltip("sword acquired", 20)
sfx(1,3)
del(pickups, pickup)
mset(pickup.x*8, pickup.y*8, 0)
elseif (pickup.type == 4) then
player.key = true
init_tooltip("◆-- key found --◆", 20)
sfx(5,3)
del(pickups, pickup)
mset(pickup.x*8, pickup.y*8, 0)
elseif (pickup.type == 5) then
-- this is the mcguffin
state = "won"
end
end
end
end
-- did we find the door, and do we have the key
if moved and (player.x == doorDown.x) and (player.y == doorDown.y) then
if (not player.key) then