-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacebox.lua
791 lines (701 loc) · 14 KB
/
spacebox.lua
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
-- title: game title
-- author: game developer
-- desc: short description
-- script: lua
T=8
W=240
H=136
EMPTY_TILE_ID=1
DEBUG_TILES=false
SAVE_C=2
SAVE_R=2
LIVES=3
JMP_IMP=2.8
ACCEL=0.2
OVERJMP_ACC=0.1
ST={
SL=1,
SR=2,
RL=3,
RR=4,
JL=5,
JR=6
}
ANIM_TICK=0
ANIM_SPEED=0.1
PL_ANIM={
[ST.SL]={{{259},{275}}},
[ST.SR]={{{256},{272}}},
[ST.RL]={{{260},{276}},{{261},{277}}},
[ST.RR]={{{257},{273}},{{258},{274}}},
[ST.JL]={{{263},{279}}},
[ST.JR]={{{262},{278}}}
}
function faceRight(state)
return state == ST.SR or state == ST.JR or state == ST.RR
end
Player = {
x=0,
y=0,
cr={x=1,y=0,w=6,h=16},
vx=0,
vy=0,
rigid=true,
mass=true,
state=ST.SR,
sp=PL_ANIM[ST.SR][1]
}
cam={x=W//2,y=H//2}
SpikeTex = {
C=304,
L=305,
U=306,
D=307,
R=308
}
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
local function has_value (tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
LOGO1={
x=0,
y=0,
sp={{1,1,1,312,313,314,315,316,317,318},
{1,1,327,328,329,330,331,332,333,334},
{341,342,343,344,345,346,347,348,349,1},
{357,358,359,360,361,362,363,364,365,366},
{373,374,375,376,377,378,379,380,381,382},
{389,390,391,392,393,394,395,396,397,398},
{405,406,407,408,409,410,411,412,413,1}}
}
LOGO2={
x=0,
y=0,
sp={{421,422,423,424,425,426,427},
{437,438,439,440,441,442,1},
{453,454,455,456,457,1,1},
{469,470,471,472,473,474,1}}
}
LOGO3={
x=0,
y=0,
sp={{486,487,488,1,490,491}}
}
LOGO4={
x=0,
y=0,
sp={{368,369,370,1,1},
{384,385,386,387,388},
{400,401,402,1,1},
{416,417,418,419,420},
{432,433,434,1,1},
{448,449,450,451,452},
{464,465,466,467,1},
{480,481,482,483,484},
{496,497,498,499,1}}
}
LSpikes = {
x=0,
y=0,
vx=0,
vy=0,
cr={x=0,y=0,w=240,h=136},
rigid=false,
mass=false,
sp={}
}
RSpikes = deepcopy(LSpikes)
USpikes = deepcopy(LSpikes)
DSpikes = deepcopy(LSpikes)
Flag = {
x=16,
y=192,
cr={x=0,y=0,w=8,h=48},
sp={{6,1},{22,23},{38,39},{54,1},{54,1},{54,1}}
}
function drawEnt(e,cam)
local i=1
for i,t in ipairs(e.sp) do
for j,v in ipairs(t) do
spr(v, e.x+(j-1)*T+cam.x, e.y+(i-1)*T+cam.y, 0)
end
end
end
function vec2(xV, yV)
return {x=xV,y=yV}
end
function v2mul(v, s)
return vec2(v.x*s, v.y*s)
end
function v2div(v,s)
return vec2(v.x/s, v.y/s)
end
function v2add(v1, v2)
return vec2(v1.x+v2.x,v1.y+v2.y)
end
function v2len(v)
return math.ceil(math.sqrt(v.x*v.x + v.y*v.y))
end
function sign(x) return x>0 and 1 or x<0 and -1 or 0 end
function lerp(a,b,t) return (1-t)*a + t*b end
BTN_UP=0
BTN_LEFT=2
BTN_RIGHT=3
BTN_Z=4
btn_st={
[BTN_UP]=false,
[BTN_LEFT]=false,
[BTN_RIGHT]=false,
[BTN_Z]=false
}
function btni(id)
if btn(id) then
btn_st[id]=true
return false
elseif btn_st[id] then
btn_st[id]=false
return true
end
end
function btno(id)
if btn(id) then
if not btn_st[id] then
btn_st[id]=true
return true
else
return false
end
else
btn_st[id]=false
return false
end
end
transparent_sprites_index = 0
solid_sprites_index = 80
bonus_index = 112
bonusExpired=116
spikeFirst=48
spikeLast=51
saveTile=16
savedTile=17
tileSky=1
tileBonusCommon=117
-- tile direction
TD={
U=1,
D=2,
L=3,
R=4
}
SPIKE_WALL_DIRS={
[2]=TD.D,
[3]=TD.L,
[4]=TD.U,
[5]=TD.R,
[96]=TD.D,
[97]=TD.L,
[98]=TD.U,
[99]=TD.R
}
SP_WALL_DIR_TILE_OFFSET = vec2(2,2)
function isTileSpikeDirFlag(tileId)
return (tileId >= 2 and tileId <= 5) or (tileId >= 96 and tileId <= 99)
end
function getCurrentRoom(x,y)
return x//W*30,y//H*17
end
function getEntRoom(e)
return getCurrentRoom(e.x,e.y)
end
function getSpWallDirInRoom(cx,cy)
local off=SP_WALL_DIR_TILE_OFFSET
local tile = mget(cx+off.x,cy+off.y)
return isTileSpikeDirFlag(tile) and SPIKE_WALL_DIRS[tile] or 0
end
SPIKES={
[TD.U]={e=DSpikes,x=0,y=H,vx=0,vy=-1},
[TD.D]={e=USpikes,x=0,y=-H-16,vx=0,vy=1},
[TD.L]={e=RSpikes,x=W+16,y=0,vx=-1,vy=0},
[TD.R]={e=LSpikes,x=-W-16,y=0,vx=1,vy=0}
}
spVxMul=0.5
spVyMul=0.25
RoomCount=0
function resetSP(sp,x,y)
sp.e.x=sp.x+x*8
sp.e.y=sp.y+y*8
sp.e.vx=sp.vx*spVxMul
sp.e.vy=sp.vy*spVyMul
end
crx,cry=-1,-1
function onChangeRoom(e,x,y)
bonusDir=0
crx,cry=x,y
for i,v in ipairs(SPIKES) do
resetSP(v,x,y)
end
end
function diffRoom(x,y)
return x ~= crx or y ~= cry
end
function checkChangeRoom(e)
local x1,y1=getCurrentRoom(e.x,e.y)
local x2,y2=getCurrentRoom(e.x+e.cr.w,e.y)
local x3,y3=getCurrentRoom(e.x,e.y+e.cr.h-1)
local x4,y4=getCurrentRoom(e.x+e.cr.w,e.y+e.cr.h-1)
if diffRoom(x1,y1) and diffRoom(x2,y2) and diffRoom(x3,y3) and diffRoom(x4,y4) then
onChangeRoom(e,x1,y1)
RoomCount=RoomCount+1
end
end
BONUS_DIRS={
[112]=TD.D,
[113]=TD.L,
[114]=TD.U,
[115]=TD.R
}
bonusDir=0
function getBonusDir(c,r)
return BONUS_DIRS[mget(c,r)]
end
REM_TILE_DIRS={
[TD.D]={82,83},
[TD.L]={84,85},
[TD.U]={86,87},
[TD.R]={88,89}
}
function isTileRemovable(tileId)
for i,v in ipairs(REM_TILE_DIRS) do
for j,w in ipairs(REM_TILE_DIRS[i]) do
if w == tileId then return true end
end
end
return false
end
function isTileRemoved(tileId)
return bonusDir ~= 0 and has_value(REM_TILE_DIRS[bonusDir], tileId)
end
TAKEN_HEARTS={}
tileHeart=18
function isTileHeart(c,r)
return mget(c,r)==tileHeart
end
function isHeartTaken(c,r)
for i,v in ipairs(TAKEN_HEARTS) do
if v.x == c and v.y == r then return true end
end
return false
end
function takeHeart(c,r)
if not isHeartTaken(c,r) then
table.insert(TAKEN_HEARTS, (vec2(c,r)))
end
end
function isTileSave(c,r)
return mget(c,r)==saveTile
end
function isTileBonus(x,y)
return bonus_index <= mget(x,y) and mget(x,y) < bonusExpired
end
function isTileSpike(c,r)
id=mget(c,r)
return id>=spikeFirst and id <=spikeLast
end
function IsTileSolid(x, y)
tileId = mget(x, y)
if isTileRemoved(tileId) then
return false
end
return (tileId >= solid_sprites_index)
end
function collide(e1,e2)
return (e1.x < e2.x+e2.cr.w and e2.x < e1.x + e1.cr.w) and
(e1.y < e2.y+e2.cr.h and e2.y < e1.y+e1.cr.h)
end
function handleInput()
local iv={pos=vec2(0,0), jump=false}
if btn(BTN_LEFT) then
iv.pos.x = -1
elseif btn(BTN_RIGHT) then
iv.pos.x = 1
end
if btno(BTN_UP) then
iv.jump=true
end
return iv
end
--callback(c,r)
function collideTile(dp,cr,callback)
local x1 = dp.x + cr.x
local y1 = dp.y + cr.y
local x2 = x1 + cr.w - 1
local y2 = y1 + cr.h - 1
-- check all tiles touched by the rect
local startC = x1 // T
local endC = x2 // T
local startR = y1 // T
local endR = y2 // T
for c = startC, endC do
for r = startR, endR do
callback(c,r)
end
end
end
function CanMove(dp,cr)
local cm=true
collideTile(dp,cr, function(c,r)
if IsTileSolid(c, r) then
cm=false
return
end
end)
return cm
end
function checkHitBonus(e)
collideTile(vec2(e.x,e.y-1),e.cr,function(c,r)
if isTileBonus(c,r) and bonusDir == 0 then
bonusDir = getBonusDir(c,r)
end
end)
end
function touchSaveTile(e)
local tsc,tsr=-1,-1
collideTile(vec2(e.x,e.y),e.cr,function (c,r)
if isTileSave(c,r) then
tsc,tsr=c,r
end
end)
return tsc,tsr
end
function touchTile(e, id)
local tc,tr=-1,-1
collideTile(vec2(e.x,e.y),e.cr,function (c,r)
if mget(c,r) == id then
tc,tr=c,r
end
end)
return tc,tr
end
function isOnFloor(e)
return not CanMove(vec2(e.x,e.y+1),e.cr) and e.vy >= 0
end
function isUnderCeiling(e)
return not CanMove(vec2(e.x,e.y-1),e.cr)
end
function isTouchSpikeTiles(e)
ts=false
collideTile(vec2(e.x,e.y),e.cr,function(c,r)
if isTileSpike(c,r) then
ts=true
end
end)
return ts
end
function TryMoveBy(e,dp)
local pos=vec2(e.x, e.y)
if (e.rigid) then
local dx,dy=0,0
for i=0,math.ceil(dp.y),sign(dp.y) do
if dx == 0 then
for j=0,math.ceil(dp.x),sign(dp.x) do
if CanMove(vec2(e.x+j,e.y+i),e.cr) and dp.x~=0 then
dx=j
else
break
end
end
end
if CanMove(vec2(e.x+dx,e.y+i),e.cr) and dp.y ~= 0 then
dy=i
end
if dp.y==0 then break end
end
e.x=e.x+dx
e.y=e.y+dy
else
e.x=e.x+dp.x
e.y=e.y+dp.y
end
end
function update(e)
local iv=handleInput()
if (e.mass) then
if isOnFloor(e) then
if iv.jump then
e.vy=-1*JMP_IMP
else
e.vy=0
end
elseif isUnderCeiling(e) and e.vy < 0 then
e.vy=0
elseif btn(BTN_UP) and e.vy < 0 then
e.vy=e.vy+OVERJMP_ACC
else
e.vy = e.vy+ACCEL
end
end
if e.vx ~= 0 then
end
e.vx=iv.pos.x
local dp=vec2(e.vx, e.vy)
checkHitBonus(e)
TryMoveBy(e,dp)
end
function fillRect(e,x0,y0,w,h,tex)
if e.sp == nil then e.sp = {} end
for i=y0,h do
if e.sp[i] == nil then e.sp[i]={} end
for j=x0,w do
e.sp[i][j]=tex
end
end
end
function updateCam(cam,e)
cam.x=math.min(W//2,W//2-e.x)
cam.y=math.min(H//2,H//2-e.y)
end
function updSpikeWall(dir,cam)
if dir ~= 0 then
local s=SPIKES[dir].e
s.x=s.x+s.vx
s.y=s.y+s.vy
drawEnt(s,cam)
return s
end
end
-- FFFUUUUU
function updateState(e)
if e.vx > 0 then
if isOnFloor(e) then
e.state=ST.RR
else
e.state=ST.JR
end
elseif e.vx < 0 then
if isOnFloor(e) then
e.state=ST.RL
else
e.state=ST.JL
end
elseif faceRight(e.state) then
if isOnFloor(e) then
e.state=ST.SR
else
e.state=ST.JR
end
else
if isOnFloor(e) then
e.state=ST.SL
else
e.state=ST.JL
end
end
end
function init()
local cw = W//T
local ch = H//T
fillRect(LSpikes,1,1,cw,ch,SpikeTex.C)
fillRect(RSpikes,1,1,cw,ch,SpikeTex.C)
fillRect(USpikes,1,1,cw,ch,SpikeTex.C)
fillRect(DSpikes,1,1,cw,ch,SpikeTex.C)
fillRect(LSpikes,cw,1,cw,ch,SpikeTex.L)
fillRect(RSpikes,1,1,1,ch,SpikeTex.R)
fillRect(USpikes,1,ch,cw,ch,SpikeTex.U)
fillRect(DSpikes,1,1,cw,1,SpikeTex.D)
mode=MOD_LOGO
end
function initGame()
Player.x=SAVE_C*T
Player.y=SAVE_R*T
Player.vx=0
Player.vy=0
local x,y=getCurrentRoom(Player.x,Player.y)
onChangeRoom(Player,x,y)
end
function initFail()
LIVES=LIVES-1
end
LOGO_TO=0
function TICLogo()
cls()
spr(336, 88, 24, -1, 8)
print("CAT_IN_THE_DARK", 72, 108, 4)
LOGO_TO=LOGO_TO+1
if btni(BTN_Z) then mode=MOD_INTRO end
if LOGO_TO > 60 then mode=MOD_INTRO end
end
function TICFail()
mode=MOD_START
end
function TICStart()
cls()
if LIVES < 0 then
mode=MOD_GAMEOVER
return
end
spr(256, 108, 64)
spr(272, 108, 72)
print(string.format("x %d", LIVES), 120, 68)
print("-Press Z to start!-", 68, 96, 4)
if btni(BTN_Z) then mode=MOD_GAME end
end
function TICGameOver()
cls()
print("Game over!", W/2-40, H/2, 4)
print("-Press Z to restart!-", W/2-64, H/2+16, 4)
if btni(BTN_Z) then reset() end
end
function TICWin()
cls()
print("You win!", W/2-32,H/2,4)
print("-Press Z to restart-", W/2-64, H/2+16, 4)
if btni(BTN_Z) then reset() end
end
function drawMap(e,cam)
map(crx,cry,30,17,crx*8+cam.x,cry*8+cam.y,-1,1, function(tile, x, y)
if isTileRemoved(tile) then
return EMPTY_TILE_ID
end
if isTileSave(x,y) then
if SAVE_C==x and SAVE_R==y then
return savedTile
end
end
if isTileHeart(x,y) then
if isHeartTaken(x,y) then
return EMPTY_TILE_ID
end
end
if (bonusDir ~= 0) and isTileBonus(x,y) then
return bonusExpired
end
if not DEBUG_TILES then
if isTileSpikeDirFlag(tile) then
return tile < 80 and tileSky or 80
end
if isTileBonus(x,y) then
return tileBonusCommon
end
if isTileRemovable(tile) then
-- EXTREMELY WTF!!!
return 80 + (tile % 2)
end
end
return tile
end)
end
function drawHud()
printOut(string.format("Room %d-%d", crx/30, cry/17), 4, 4, 9, 1, 1)
end
function animate(e,tex)
local anim=tex[e.state]
e.sp=anim[(math.floor(ANIM_TICK)%#anim)+1]
ANIM_TICK = ANIM_TICK + ANIM_SPEED
end
function printOut(text,x,y,clr,oclr,s)
print(text,x-1,y,oclr,false,s)
print(text,x+1,y,oclr,false,s)
print(text,x,y-1,oclr,false,s)
print(text,x,y+1,oclr,false,s)
print(text,x,y,clr,false,s)
end
interval=10
ct=0
st=false
function TICIntro()
cls()
map(210, 119, 30, 17)
drawEnt(LOGO1,vec2(112,32))
drawEnt(LOGO2,vec2(112,88))
drawEnt(LOGO3,vec2(120,120))
drawEnt(LOGO4,vec2(24,64))
printOut("SPACE", 24, 10, 9, 1, 3)
printOut("BOX", 24, 34, 9, 1, 5)
if st then
printOut("Press Z to start", 80, 120, 4, 1, 1)
end
ct=ct+1
if ct % interval == 0 then
st = not st
end
if btni(BTN_Z) then mode=MOD_START end
end
GT=0
function TICGame()
GT=GT+1
cls()
updateCam(cam,Player)
update(Player)
updateState(Player)
drawMap(Player,cam)
animate(Player,PL_ANIM)
checkChangeRoom(Player)
local dir=getSpWallDirInRoom(crx,cry)
local spWall=updSpikeWall(dir,cam)
drawEnt(Player,cam)
drawHud()
local stc,str=touchSaveTile(Player)
if stc > 0 and str > 0 then
SAVE_C,SAVE_R=stc,str
end
local sc,sr=touchTile(Player, tileHeart)
if sc > 0 and sr > 0 then
if not isHeartTaken(sc,sr) then
LIVES = LIVES + 1
takeHeart(sc,sr)
end
end
if isTouchSpikeTiles(Player) then mode=MOD_FAIL end
if collide(Player, Flag) then mode=MOD_WIN end
if spWall ~= nil and collide(Player, spWall) then mode=MOD_FAIL
end
end
MOD_GAME = 0
MOD_FAIL = 1
MOD_WIN=2
MOD_LOGO=3
MOD_GAMEOVER=4
MOD_START=5
MOD_INTRO=6
TICMode={
[MOD_GAME]=TICGame,
[MOD_FAIL]=TICFail,
[MOD_WIN]=TICWin,
[MOD_LOGO]=TICLogo,
[MOD_GAMEOVER]=TICGameOver,
[MOD_START]=TICStart,
[MOD_INTRO]=TICIntro
}
inits={
[MOD_GAME]=initGame,
[MOD_FAIL]=initFail
}
init()
oldMode=mode
function TIC()
if oldMode ~= mode then
if inits[mode] ~= nil then
inits[mode]()
end
oldMode=mode
end
TICMode[mode]()
end