-
Notifications
You must be signed in to change notification settings - Fork 0
/
actop.p8
594 lines (563 loc) · 18 KB
/
actop.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
pico-8 cartridge // http://www.pico-8.com
version 38
__lua__
-- actop
-- by ymao
-- sfx:
-- 0: attack
-- 1: player hit by enemy
-- 2: pickup item
-- 3: attack ranged
-- global variables
g_frames=0
g_difficulty=0
g_player={}
g_player_bullets={}
g_enemies={}
g_killcnt=0
g_items={}
g_itemtypes={}
g_alive=true
-- global constants
k_atk=5
k_atkr=4
function _init()
_initmenu()
_update=_updatemenu
_draw=_drawmenu
end
function _startlevel()
_initlevel()
_update=_updatelevel
_draw=_drawlevel
end
function _initlevel()
g_frames=0
g_difficulty=0
g_alive=true
item_manager.init()
enemy_manager.init()
player.init(g_player)
bullet_manager.init()
end
function _updatelevel()
g_frames=(g_frames+1)%30
if (g_alive) then
item_manager.update()
enemy_manager.update()
player.update(g_player)
bullet_manager.update()
else -- not g_alive
if (btnp(5)) then
_initlevel()
end
end
end
function _drawlevel()
cls()
item_manager.draw()
enemy_manager.draw()
player.draw(g_player)
bullet_manager.draw()
-- draw ui
for i=1,g_player.hpmax do
if (g_player.hp >= i) then
spr(11,i*8-6,0)
else
spr(10,i*8-6,0)
end
end
for i=1,g_player.mpmax do
if (g_player.mp >= i) then
pset(i*2,8,12)
else
pset(i*2,8,5)
end
end
local killpos=print(g_killcnt,0,-20)
print("\f7score: "..g_killcnt,100-killpos,0)
-- draw dead message
if (not g_alive) then
local titlex=32
local titley=24
print("\#2\^w\^tyou died!",titlex,titley)
print("\#2press ❎ to restart",titlex-2,titlex+64)
end
end
-->8
-- player
player={
init=function(this)
this.hpmax=3 -- const
this.hp=3
this.mpmax=20-- const
this.mp=20
this.x=64
this.y=64
this.dx=0
this.dy=0
this.face=3
this.frame=0
this.hitbox={x=1,y=1,w=6,h=6} -- const
this.invulnframe=0
this.cooldown=0 -- action cooldown
-- melee attack
this.atk={}
this.atk.x=0
this.atk.y=0
this.atk.face=0
this.atk.frame=0
this.atk.hitbox={x=0,y=0,w=8,h=8} -- const
this.atk.pow=1
-- ranged attack manage by bullet_manager
end,
update=function(this)
if (this.invulnframe > 0) this.invulnframe-=1
if (this.cooldown > 0) this.cooldown-=1
-- input
this.dx=0
this.dy=0
if (btn(0)) then
this.face=0
this.dx=-1
end
if (btn(1)) then
this.face=1
this.dx=1
end
if (btn(2)) then
this.face=2
this.dy=-1
end
if (btn(3)) then
this.face=3
this.dy=1
end
-- move
this.x+=this.dx
this.y+=this.dy
forceinsidescreen(this)
item_manager.consumeall(this)
-- melee attack
if (btn(k_atk) and (this.cooldown == 0)) then
sfx(0)
this.atk.frame=3
this.cooldown=10
this.atk.face=this.face
local atkpos=face2pos(this.atk.face)
this.atk.x=this.x+atkpos.x*8
this.atk.y=this.y+atkpos.y*8
end
if (this.atk.frame > 0) then
enemy_manager.hitall(this.atk)
end
-- ranged attack (use mana)
if (btn(k_atkr) and (this.cooldown == 0)
and (this.mp > 0)) then
sfx(3)
this.mp-=1
this.cooldown=10
local atkpos=face2pos(this.face)
bullet_manager.fire(this.x+atkpos.x*8+4,
this.y+atkpos.y*8+4,
this.face)
end
end,
draw=function(this)
-- draw player body
if (this.invulnframe > 0) pal(8,9)
if ((this.dx == 0) and (this.dy == 0)) then -- stop
spr(1,this.x,this.y)
else -- move
spr(2+this.frame,this.x,this.y)
if ((g_frames%5) == 0) this.frame=(this.frame+1)%2
end
pal()
-- draw player eye
spr(this.face+12,this.x,this.y)
-- draw atk
if (this.atk.frame > 0) then
local atkpos=face2pos(this.atk.face)
spr(this.atk.face*3+this.atk.frame+15,
this.x+atkpos.x*8,
this.y+atkpos.y*8)
this.atk.frame-=1
end
end,
hit=function(this,other)
if (this.invulnframe == 0) then
if (collidebox(this,other)) then
sfx(1)
this.hp-=other.pow
this.invulnframe=30
if (this.hp <= 0) player.kill(this)
end
end
end,
kill=function(this)
g_alive=false
end,
}
bullet={
init=function(this,x,y,face)
this.x=x
this.y=y
this.speed=3
this.size=1
local bulletpos=face2pos(face)
this.dx=bulletpos.x*this.speed
this.dy=bulletpos.y*this.speed
this.hitbox={x=-1,y=-1,w=this.size+2,h=this.size+2}
this.pow=1
end,
update=function(this)
-- move
this.x+=this.dx
this.y+=this.dy
-- attack
enemy_manager.hitall(this)
end,
draw=function(this)
rectfill(this.x,this.y,
this.x+this.size,this.y+this.size,
12)
end,
dead=function(this)
return isoutofscreen(this)
end,
}
-- manage function
-- manipulate global var
-- g_player_bullets
bullet_manager={
init=function()
g_player_bullets={}
end,
update=function()
foreach(g_player_bullets,bullet.update)
-- dead objs detection after update
for obj in all(g_player_bullets) do
if bullet.dead(obj) then
bullet_manager.remove(obj)
end
end
end,
draw=function()
foreach(g_player_bullets,bullet.draw)
end,
fire=function(x,y,face)
local obj={}
bullet.init(obj,x,y,face)
add(g_player_bullets,obj)
end,
remove=function(obj)
del(g_player_bullets,obj)
end,
}
-->8
-- enemy
enemy_slime={
init=function(this,x,y)
this.type=enemy_slime
this.hp=1
this.x=x
this.y=y
this.dx=0
this.dy=0
this.speed=0.1
this.flipx=false
this.frame=0
this.hitbox={x=0,y=3,w=8,h=5} -- const
this.invulnframe=0
this.pow=1
end,
update=function(this)
if (this.invulnframe > 0) this.invulnframe-=1
-- update speed and face
if ((g_frames%30) == 0) then
this.dx=cmp(g_player.x,this.x)*this.speed
this.dy=cmp(g_player.y,this.y)*this.speed
this.flipx=(this.dx > 0)
end
-- move
this.x+=this.dx
this.y+=this.dy
-- attack
player.hit(g_player,this)
end,
draw=function(this)
spr(this.frame+32,this.x,this.y,1,1,this.flipx,false)
if ((g_frames%5) == 0) this.frame=(this.frame+1)%2
end,
hit=function(this,other)
if (this.invulnframe == 0) then
if (collidebox(this,other)) then
this.hp-=other.pow
this.invulnframe=3
if (this.hp <= 0) enemy_manager.kill(this)
end
end
end,
}
-- manage function
-- manipulate global var
-- g_enemies, g_killcnt, g_difficulty
enemy_manager={
init=function()
g_killcnt=0
g_enemies={}
end,
update=function()
for e in all(g_enemies) do
e.type.update(e)
end
-- spawn if not enough enemy
if (enemy_manager.count() < g_difficulty+2) then
enemy_manager.spawnrnd()
end
end,
draw=function()
for e in all(g_enemies) do
e.type.draw(e)
end
end,
spawn=function(type,x,y)
local obj={}
type.init(obj,x,y)
add(g_enemies,obj)
end,
spawnrnd=function()
local spawnpos=rndspawnpos()
local type=enemy_slime
enemy_manager.spawn(type,spawnpos.x,spawnpos.y)
end,
count=function()
return #g_enemies
end,
hitall=function(other)
for e in all(g_enemies) do
e.type.hit(e,other)
end
end,
kill=function(obj)
g_killcnt+=1
if ((g_killcnt % 5) == 0) item_manager.spawnrnd()
if ((g_killcnt % 10) == 0) g_difficulty+=1
del(g_enemies,obj)
end,
}
-->8
-- item
item_hp={
init=function(this,x,y)
this.type=item_hp
this.x=x
this.y=y
this.hitbox={x=1,y=1,w=6,h=6}
end,
update=function(this)
end,
draw=function(this)
spr(48,this.x,this.y)
end,
consume=function(this,other)
if (collidebox(this,other)) then
sfx(2)
if (other.hp < other.hpmax) other.hp+=1
item_manager.remove(this)
end
end,
}
item_mp={
init=function(this,x,y)
this.type=item_mp
this.x=x
this.y=y
this.hitbox={x=1,y=1,w=6,h=6}
end,
update=function(this)
end,
draw=function(this)
spr(49,this.x,this.y)
end,
consume=function(this,other)
if (collidebox(this,other)) then
sfx(2)
other.mp=other.mpmax
item_manager.remove(this)
end
end,
}
-- manage function
-- manipulate global var
-- g_items, g_itemtypes
item_manager={
init=function()
g_items={}
g_itemtypes={item_hp, item_mp}
item_manager.spawn(item_hp,50,50)
end,
update=function()
for i in all(g_items) do
i.type.update(i)
end
end,
draw=function()
for i in all(g_items) do
i.type.draw(i)
end
end,
spawn=function(type,x,y)
local obj={}
type.init(obj,x,y)
add(g_items,obj)
end,
spawnrnd=function()
local type=rnd(g_itemtypes)
local pos=rndspawnpos()
item_manager.spawn(type,pos.x,pos.y)
end,
count=function()
return #g_items
end,
consumeall=function(other)
for i in all(g_items) do
(i.type).consume(i,other)
end
end,
remove=function(obj)
del(g_items,obj)
end,
}
-->8
-- util
-- helper function
face2pos=function(face)
local x=0
local y=0
if (face == 0) x=-1
if (face == 1) x=1
if (face == 2) y=-1
if (face == 3) y=1
return {x=x,y=y}
end
cmp=function(x1,x2)
local res=0
if (x1 < x2) res=-1
if (x1 > x2) res=1
return res
end
collidebox=function(obj1,obj2)
if ((obj1.x+obj1.hitbox.x < obj2.x+obj2.hitbox.x+obj2.hitbox.w) and
(obj1.x+obj1.hitbox.x+obj1.hitbox.w > obj2.x+obj2.hitbox.x) and
(obj1.y+obj1.hitbox.y < obj2.y+obj2.hitbox.y+obj2.hitbox.h) and
(obj1.y+obj1.hitbox.y+obj1.hitbox.h > obj2.y+obj2.hitbox.y)) then
return true
else
return false
end
end
-- random spawn location away from player
rndspawnpos=function()
-- spawn only away from player
local x=0
local y=0
repeat
x=flr(rnd(120))
y=flr(rnd(120))
until ((abs(x-g_player.x)>20)
or (abs(y-g_player.y)>20))
return {x=x,y=y}
end
isoutofscreen=function(this)
return ((this.x < -8) or
(this.x > 128) or
(this.y < -8) or
(this.y > 128))
end
forceinsidescreen=function(this)
if (this.x < 0) this.x=0
if (this.x > 120) this.x=120
if (this.y < 0) this.y=0
if (this.y > 120) this.y=120
end
-->8
-- menu
function _initmenu()
end
function _updatemenu()
if (btnp(5)) then
_startlevel()
end
end
function _drawmenu()
cls()
local titlex=32
local titley=24
-- draw title
sspr(96,32,32,16,titlex,titley,64,32)
sspr(112,48,16,8,titlex+48,titley+32)
-- draw menu item
print("press ❎ to start",titlex-2,titley+64)
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000088888800888888008888880000000000000000000000000000000000000000000000000055055000880880000000000000000000000000000000000
00000000088888800888888008888880000000000000000000000000000000000000000000000000500500508888878000000000000000000010010000000000
00000000088888800888888008888880000000000000000000000000000000000000000000000000500000508888888000101000000101000010010000100100
00000000088888800888888008888880000000000000000000000000000000000000000000000000050005000888880000101000000101000000000000100100
00000000088888800888888008888880000000000000000000000000000000000000000000000000005050000088800000000000000000000000000000000000
00000000008008000080080000800800000000000000000000000000000000000000000000000000000500000008000000000000000000000000000000000000
00000000008008000080000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000006000000076000000000000000000000000000000000000000000000055000000500000005500000000000000000000000000000000000
00000000000000000007600000760000000000000000000000000000000000000000000000056700000550000006500000000000000000000000000000000000
00000000000000000000760007600000000000000000000000000000000060000000000000000670000670000067000000000000000000000000000000000000
00000055006666550000076556000000057770005500000060000000000760000000007600000067000670000670000000000000000000000000000000000000
00000065000777500000005555000000556666005670000076000000000760000000076000000006000670006700000000000000000000000000000000000000
00000670000000000000000000000000000000000067000007600000000760000000760000000000000600000000000000000000000000000000000000000000
00006700000000000000000000000000000000000006700000765000000550000005600000000000000000000000000000000000000000000000000000000000
00067000000000000000000000000000000000000000600000055000000050000005500000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
333b0000333b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
3323b0003323b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
33333300333333000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
33333300033333300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
03333330003333330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000088000000cc000000076000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00088880000cccc00000767000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
007688800076ccc00057670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0777680007776c000005700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
07777000077770000060500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00770000007700000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008888880000000050000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008188180000000555000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008188180888805555508888808888800
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008888880800000767008000808000800
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008888880800000767008000808000800
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800800800000767008000808000800
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800800888800767008888808888800
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000767000000008000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000767000000008000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000008000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090909990
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090909990
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000099099909090
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000909090
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000099909090
__sfx__
000600002435027250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
001000003a6703f6703c6500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000a0000350503c050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0001000023550285502c5503155035550395500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000