-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdarkest-moon.p8
1955 lines (1681 loc) · 71.8 KB
/
darkest-moon.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 8
__lua__
function round(x)
return flr(x+0.5)
end
-- copies props to obj
-- if obj is nil, a new object will be created, so set(nil,{...}) copies the object
function set(obj,props)
obj=obj or {}
for k,v in pairs(props) do
obj[k]=v
end
return obj
end
-- used for callbacks into entities that might or might not have a method to handle an event
function event(ob,name,...)
local cb=ob[name]
return type(cb)=="function"
and cb(ob,...)
or cb
end
-- returns smallest element of seq, according to key function
function min_of(seq,key)
local me,mk=nil,32767
for e in all(seq) do
local k=key(e)
if k<mk then
me,mk=e,k
end
end
return me
end
-- creates a "class" object with support for basic inheritance/initialization
function kind(kob)
kob=kob or {}
setmetatable(kob,{__index=kob.extends})
kob.new=function(self,ob)
ob=set(ob,{kind=kob})
setmetatable(ob,{__index=kob})
if (kob.create) ob:create()
return ob
end
return kob
end
-- for some stuff, we want vector math - so we make a vector type with all the usual operations
vec={}
function vec.__add(v1,v2)
return v(v1.x+v2.x,v1.y+v2.y)
end
function vec.__sub(v1,v2)
return v(v1.x-v2.x,v1.y-v2.y)
end
function vec.__mul(v1,a)
return v(v1.x*a,v1.y*a)
end
function vec.__mul(v1,a)
return v(v1.x*a,v1.y*a)
end
function vec.__div(v1,a)
return v(v1.x/a,v1.y/a)
end
-- we use the ^ operator to mean dot product
function vec.__pow(v1,v2)
return v1.x*v2.x+v1.y*v2.y
end
function vec.__unm(v1)
return v(-v1.x,-v1.y)
end
-- this is not really the length of the vector, but length squared - easier to calculate, and can be used for most of the same stuff
function vec.__len(v1)
local x,y=v1:split()
return x*x+y*y
end
-- normalized vector
function vec:norm()
return self/sqrt(#self)
end
-- rotated 90-deg clockwise
function vec:rotcw()
return v(-self.y,self.x)
end
-- force coordinates to integers
function vec:ints()
return v(flr(self.x),flr(self.y))
end
-- used for destructuring, i.e.: x,y=v:split()
function vec:split()
return self.x,self.y
end
-- has to be there so our metatable works for both operators and methods
vec.__index = vec
-- creates a new vector
function v(x,y)
local vector={x=x,y=y}
setmetatable(vector,vec)
return vector
end
-- vector for each cardinal direction, ordered the same way pico-8 d-pad is
dirs={
v(-1,0),v(1,0),
v(0,-1),v(0,1)
}
-- a box is just a rectangle with some helper methods
box=kind()
function box:translate(v)
return make_box(
self.xl+v.x,self.yt+v.y,
self.xr+v.x,self.yb+v.y
)
end
function box:overlaps(b)
return
self.xr>=b.xl and
b.xr>=self.xl and
self.yb>=b.yt and
b.yb>=self.yt
end
function box:contains(pt)
return pt.x>=self.xl and
pt.y>=self.yt and
pt.x<=self.xr and
pt.y<=self.yb
end
function box:sepv(b)
local candidates={
v(b.xl-self.xr-1,0),
v(b.xr-self.xl+1,0),
v(0,b.yt-self.yb-1),
v(0,b.yb-self.yt+1)
}
return min_of(candidates,vec.__len)
end
function make_box(xl,yt,xr,yb)
if (xl>xr) xl,xr=xr,xl
if (yt>yb) yt,yb=yb,yt
return box:new({
xl=xl,yt=yt,xr=xr,yb=yb
})
end
function vec_box(v1,v2)
return make_box(
v1.x,v1.y,
v2.x,v2.y
)
end
-- entity root type
entity=kind({
t=0,
state="s_default",
solid=true
})
-- a big bag of all entities
entities={}
-- entities with some special props are tracked separately
entities_with={}
tracked_props={
"render","cbox",
"walls","shadow"
}
-- used to add/remove objects in the entities_with list
function update_with_table(e,fn)
for prop in all(tracked_props) do
if e[prop] then
local lst=entities_with[prop] or {}
fn(lst,e)
entities_with[prop]=lst
end
end
end
-- all entities do common stuff when created - mostly register in lists
e_id=1
function entity:create()
if not self.name then
self.name=e_id..""
e_id+=1
end
local name=self.name
entities[name]=self
update_with_table(self,add)
end
-- this is the core of our _update() method - update each entity in turn
function update_entities()
for n,e in pairs(entities) do
local update_fn=e[e.state]
local result = update_fn and update_fn(e,e.t) or nil
if result then
if result==true then
-- remove entity
entities[n]=nil
update_with_table(e,del)
else
-- set state
set(e,{
state=result,t=0
})
end
else
-- bump timer in this state
e.t+=1
end
end
end
-- renders entities, sorted by y to get proper occlusion
function render_entities()
ysorted={}
for d in all(entities_with.render) do
local y=d.pos and flr(d.pos.y) or 139
ysorted[y]=ysorted[y] or {}
add(ysorted[y],d)
end
for y=clipbox.yt,clipbox.yb do
for d in all(ysorted[y]) do
reset_palette()
d:render(d.t)
end
reset_palette()
end
end
function render_blob_shadows()
local sh_fill=fl_blend(5)
for e in all(entities_with.shadow) do
local sh=e.shadow
local p=e.pos+e.shadow
if clipbox:contains(p) then
cellipse(p.x,p.y,
sh.rx,sh.ry,sh_fill)
end
end
end
function c_box(e)
local b,p=e.cbox,e.pos
return p
and b:translate(p:ints())
or b
end
cqueue={}
function collide(ent,prop,cb)
add(cqueue,{e=ent,p=prop,cb=cb})
end
function do_collisions()
for c in all(cqueue) do
local e=c.e
local eb=c_box(e)
for o in all(entities_with[c.p]) do
if o!=e then
local ob=c_box(o)
if eb:overlaps(ob) then
local separate=c.cb(e,o)
if separate and e.solid and o.solid then
local sepv=eb:sepv(ob)
e.pos+=sepv
eb=eb:translate(sepv)
end
end
end
end
end
cqueue={} -- wipe the collision queue
end
function debug_collisions()
for e in all(entities_with.cbox) do
local eb=c_box(e)
rect(eb.xl,eb.yt,eb.xr,eb.yb,14)
end
end
-- all shapes accept a fill function which is responsible for actual drawing the functions just do calculations and clipping
-- draws a polygon from an array of points, using ln() to fill it points must be clockwise
function ngon(pts,ln)
local xls,xrs=ngon_setup(pts)
for y,xl in pairs(xls) do
local xr=xrs[y]
ln(xl,xr,y)
end
end
-- like ngon, but with a rectangular hole (used to mask shadows)
dummy_hole={tl={y=16000},br={}}
function holed_ngon(pts,hole,ln)
local xls,xrs=ngon_setup(pts)
hole=hole or dummy_hole
local htop,hbot,hl,hr=
hole.tl.y,hole.br.y,
hole.tl.x,hole.br.x
for y,xl in pairs(xls) do
local xr=xrs[y]
if y<htop or y>hbot then
ln(xl,xr,y)
else
local cl,cr=
min(hl,xr),max(hr,xl)
if xl<=cl then
ln(xl,cl,y)
end
if cr<=xr then
ln(cr,xr,y)
end
end
end
end
-- sets up min/max x of each polygon line
function ngon_setup(pts)
local xls,xrs={},{}
local npts=#pts
for i=0,npts-1 do
ngon_edge(
pts[i+1],pts[(i+1)%npts+1],
xls,xrs
)
end
return xls,xrs
end
function ngon_edge(a,b,xls,xrs)
local ax,ay=a.x,round(a.y)
local bx,by=b.x,round(b.y)
if (ay==by) return
local x,dx=
ax,(bx-ax)/abs(by-ay)
if ay<by then
for y=ay,by do
xrs[y]=x
x+=dx
end
else
for y=ay,by,-1 do
xls[y]=x
x+=dx
end
end
end
-- draws a filled rectangle with a custom fill fn
function crect(x1,y1,x2,y2,ln)
x1,x2=max(x1,0),mid(x2,127)
y1,y2=max(y1,0),min(y2,127)
if (x2<x1 or y2<y1) return
for y=y1,y2 do
ln(x1,x2,y)
end
end
-- draws a filled ellipse with a custom fill fn
function cellipse(cx,cy,rx,ry,ln)
cy,ry=round(cy),round(ry)
local w=0
local ryx,rxy=ry/rx,rx/ry
local dy=(-2*ry+1)*rxy
local dx=ryx
local ddx=2*ryx
local ddy=2*rxy
local lim=rx*ry
local v=ry*ry*rxy
local my=cy+ry-1
for y=cy-ry,cy-1 do
-- creep w up until we hit lim
while true do
if v+dx<=lim then
v+=dx
dx+=ddx
w+=1
else
break
end
end
-- draw line
if w>0 then
local l,r=
mid(cx-w,0,127),
mid(cx+w-1,0,127)
if (y>=0 and y<128) ln(l,r,y)
if (my>=0 and my<128) ln(l,r,my)
end
-- go down
v+=dy
dy+=ddy
my-=1
end
end
-------------------------------
-- basic fills
-------------------------------
-- a fill function is just a function(x1,x2,y) that draws a horizontal line
-- returns a fill function that draws a solid color
function fl_color(c)
return function(x1,x2,y)
rectfill(x1,y,x2,y,c)
end
end
-- used as fill function for ignored areas
function fl_none()
end
-------------------------------
-- fast blend fill
-------------------------------
-- sets up everything blend_line will need
function init_blending(nlevels)
-- tabulate sqrt() for speed
_sqrt={}
for i=0,4096 do
_sqrt[i]=sqrt(i)
end
-- populate look-up tables for blending based on palettes in sprite mem
for lv=1,nlevels do
-- light luts are stored in memory directly, table indexing is costly
local addr=0x4300+lv*0x100
local sx=lv-1
for c1=0,15 do
local nc=sget(sx,c1)
local topl=shl(nc,4)
for c2=0,15 do
poke(addr,
topl+sget(sx,c2))
addr+=1
end
end
end
end
function fl_blend(l)
local lutaddr=0x4300+shl(l,8)
return function(x1,x2,y)
local laddr=lutaddr
local yaddr=0x6000+shl(y,6)
local saddr,eaddr=
yaddr+band(shr(x1+1,1),0xffff),
yaddr+band(shr(x2-1,1),0xffff)
-- odd pixel on left?
if band(x1,1.99995)>=1 then
local a=saddr-1
local v=peek(a)
poke(a,
band(v,0xf) +
band(peek(bor(laddr,v)),0xf0)
)
end
-- full bytes
for addr=saddr,eaddr do
poke(addr,
peek(
bor(laddr,peek(addr))
)
)
end
-- odd pixel on right?
if band(x2,1.99995)<1 then
local a=eaddr+1
local v=peek(a)
poke(a,
band(peek(bor(laddr,v)),0xf) +
band(v,0xf0)
)
end
end
end
-- determines how far each level of light reaches this is distance *squared* due to the ordering here, light level 1 is the brightest, and 6 is the darkest (pitch black)
light_rng={
10*42,18*42,
26*42,34*42,
42*42,
}
-- special "guard" value to ensure nothing can be light level 0 without ifs
light_rng[0]=-1000
-- this is our "light" fill
-- function.
-- it operates by dimming
-- what's already there.
-- each horizontal line
-- is drawn by multiple
-- calls to another fill
-- function handling
-- the correct light level
-- for each segment.
light_fills={
fl_none,fl_blend(2),fl_blend(3),
fl_blend(4),fl_blend(5),fl_color(0)
}
brkpts={}
function fl_light(lx,ly,brightness,ln)
local brightnessf,fills=
brightness*brightness,
light_fills
return function(x1,x2,y)
-- transform coordinates
-- into light-space
local ox,oy,oe=x1-lx,y-ly,x2-lx
-- brightness range multiplier
-- + per line flicker effect
local mul=
brightnessf*
(rnd(0.16)+0.92)
-- calculate light levels
-- at left end, right end,
local ysq=oy*oy
local srng,erng,slv,elv=
ysq+ox*ox,
ysq+oe*oe
for lv=5,0,-1 do
local r=band(light_rng[lv]*mul,0xffff)
if not slv and srng>=r then
slv=lv+1
if (elv) break
end
if not elv and erng>=r then
elv=lv+1
if (slv) break
end
end
-- these will hold the lowest/highest light level within our line
local llv,hlv=1,max(slv,elv)
-- calculate breakpoints (x coordinates at which light level changes, in light-space) and lowest(brightest) light level within line
local mind=max(x1-lx,lx-x2)
for lv=hlv-1,1,-1 do
local brng=band(light_rng[lv]*mul,0xffff)
local brp=_sqrt[brng-ysq]
brkpts[lv]=brp
if not brp or brp<mind then
llv=lv+1
break
end
end
-- everything calculated, draw all segments now!
local xs,xe=lx+ox
-- from left bound to start of most-lit segment decreasing light lv (brightness increasing)
for l=slv,llv+1,-1 do
xe=lx-brkpts[l-1]
fills[l](xs,xe-1,y)
xs=xe
end
-- from most-lit zone to last break point increasing light lv (brightness decreasing)
for l=llv,elv-1 do
xe=lx+brkpts[l]
fills[l](xs,xe-1,y)
xs=xe
end
-- last segment from last breakpoint to the right bound
fills[elv](xs,x2,y)
end
end
-------------------------------
-- palette effects
-------------------------------
function init_palettes(n)
pals={}
for p=1,n do
pals[p]={}
for c=0,15 do
pals[p][c]=sget(p,c)
end
end
end
function reset_palette()
pal()
palt(14,true)
palt(0,false)
end
function set_palette(no)
for c,nc in pairs(pals[no]) do
pal(c,nc)
end
end
function dim_object(o)
local lcoeff=(o.pos-lght.pos).y/25
if lcoeff>0 then
local pt=mid(flr(lcoeff/0.1),0,6)
set_palette(8+pt)
end
end
-------------------------------
-- shadowcasting by walls
-------------------------------
function render_wall_shadows()
local render_one=
render_shadow_fn()
for e in all(entities_with.walls) do
foreach(e.walls,render_one)
end
end
function render_shadow_fn()
-- remember lighting info
local p,rng=lght.pos:ints(),lght:range()
local rngsq=rng*rng
local black=fl_color(0)
-- return function using it
return function(wall)
local s,e=wall.s,wall.e
local dist=wall.d^(p-s)
if (dist<=0) return
local ds,de=s-p,e-p
if (#ds>rngsq and #de>rngsq) return
local horiz=wall.d.x!=0
local cs,ce=
rng/max(abs(ds.x),abs(ds.y)),
rng/max(abs(de.x),abs(de.y))
local ps,pe=
ds*cs,de*ce
local pm=
v(abs(ps.x/pe.x)>1 and ps.x or pe.x,
abs(ps.y/pe.y)>1 and ps.y or pe.y)
local pts={s,e,p+pe,p+pm,p+ps}
if wall.h then
holed_ngon(pts,wall.h,black)
else
ngon(pts,black)
end
end
end
-------------------------------
-- solids (light obscuring)
-------------------------------
gobs={}
solid=kind({
extends=entity
})
function solid:create()
local def,pos=
self.def,self.pos
self.cbox=self.def.cbox
local hole=self.def.hole
if hole then
hole={
tl=pos+v(hole[1],hole[2]),
br=pos+v(hole[3],hole[4])
}
end
self.walls={}
for wd in all(self.def.walls) do
add(self.walls,{
s=pos+v(wd[1],wd[2]),
e=pos+v(wd[3],wd[4]),
d=dirs[wd[5]],
h=hole
})
end
entity.create(self)
end
solid.walked_into=true
function solid:render()
local s=self.def.sprite
local spos=
self.pos+v(-s.w*4,-s.h*8)
-- dynamic lighting
dim_object(self)
spr(s.n,spos.x,spos.y,s.w,s.h,self.flipped)
end
-------------------------------
-- building a room
-------------------------------
wall=kind({
walked_into=true,
extends=entity
})
function build_room(mx,my)
local obtab={}
for k,gob in pairs(gobs) do
obtab[gob.tile]=gob
end
for ty=0,15 do
for tx=0,15 do
local tile=mget(mx+tx,my+ty)
local spawn=obtab[tile]
if spawn then
solid:new({
pos=v(tx,ty)*8+spawn.off,
def=spawn
})
mset(tx,ty,128)
else
mset(tx,ty,tile)
end
end
end
end
function flags(pos,mask)
local x,y=
mid(pos.x,0,16),
mid(pos.y,0,15)
return band(fget(mget(x,y)),mask)
end
function process_walls()
process_walls_with(v(0,1),v(1,0),4,3)
process_walls_with(v(0,1),v(1,0),8,4)
process_walls_with(v(1,0),v(0,1),1,1)
process_walls_with(v(1,0),v(0,1),2,2)
find_wall_fronts()
end
function process_walls_with(dout,din,mask,wdir)
for row=0,15 do
local l,c,bv,prv=
dout*row-din*2,-2,0
repeat
repeat
prv=bv
l+=din
c+=1
bv=flags(l,mask)
until c==16 or bv!=prv
if prv!=0 then
add_wall(sl,l,wdir)
end
sl=l
until c==16
end
end
wparams={
{f=v(0,5),t=v(7,4),
we=v(0,4)},
{f=v(7,5),t=v(0,4),
we=v(7,4)},
{f=v(0,5),t=v(-1,13),
we=v(-1,5),h=v(-1,1),wi=true},
{f=v(0,12),t=v(-1,0),
we=v(-1,12),h=v(-1,14)},
}
function add_wall(from,to,dr)
local d,ps=dirs[dr],wparams[dr]
local cs,ce,co=
from*8+ps.f,
to*8+ps.we,
to*8+ps.t
local wbox=make_box(cs.x,cs.y,co.x,co.y)
local hole
if ps.h then
local ch=to*8+ps.h
local hbox=make_box(cs.x,cs.y,ch.x,ch.y)
hole={
tl=v(hbox.xl,hbox.yt),
br=v(hbox.xr,hbox.yb)
}
end
wall:new({
cbox=wbox,
walls={
{
s=ps.wi and ce or cs,
e=ps.wi and cs or ce,
d=-d,
h=hole
}
}
})
end
-------------------------------
-- front-facing walls
-------------------------------
-- front parts of walls are drawn as entities to let us darken them when they should be in shadow
wallfront=kind({
extends=entity
})
function wallfront:render()
dim_object(self)
map(self.mx,self.my,
self.pos.x,self.pos.y-16,
self.mw,2)
end
function find_wall_fronts()
for y=0,14 do
local pc,c,sx=0
for x=0,16 do
c=flags(v(x,y),16)+
flags(v(x,y+1),16)
if c!=pc or c==16 then
if pc==32 then
w=wallfront:new({
mx=sx,my=y,mw=x-sx,
pos=v(sx,y+2)*8
})
end
sx=x
end
pc=c
end
end
end
function tbox(speaker, message) -- add a new text box
local linebreak=1 -- keeps track of the last linebreak position
if #tbox_messages>0 and #tbox_messages%2==1 then -- add an empty line for a "new" dialogue box if a previous message exists in the queue
tbox_line(speaker, "")
end
for i=0,flr(#message/26) do -- search through the message and break it into words
local line=sub(message, linebreak, linebreak+26) -- lines are 26 characters but grab 26 to do a lookahead check
if #line==27 and #message>linebreak+26 then -- if we're not near the end of the message
for j=#line,0,-1 do -- look backward for the first whitespace character to determine the linebreak
if sub(line,j,j)==" " then
local lookahead=0
if j==#line then -- if the line ends perfectly at the end of a word...
lookahead=1
end
tbox_line(speaker, sub(line, 0, j+lookahead)) -- add the word to the array
linebreak+=j
break
end
end
else
tbox_line(speaker, line) -- add the rest of the message to the text boxes array
break -- only add the message once
end
end
end
-- a utility function for easily adding a line to the messages array
function tbox_line(speaker, line)
local line={speaker=speaker, line=line, animation=0}
add(tbox_messages, line)
end
-- check for button presses so we can clear text box messages
function tbox_interact()
if btnp(5) then
if #tbox_messages>1 then
if tbox_messages[2].animation==#tbox_messages[2].line then
tbox_dismiss()
end
elseif #tbox_messages>0 then
if tbox_messages[1].animation==#tbox_messages[1].line then
tbox_dismiss()
end
end
end
end
function tbox_dismiss()
sfx(10) -- play a sound effect
if #tbox_messages>1 then -- delete two lines if there are two
del(tbox_messages, tbox_messages[1])
end
del(tbox_messages, tbox_messages[1])
end
-- check if a text box is currently visible (useful if the dialogue clear button is used for other actions as well)
function tbox_active()
if #tbox_messages>0 then
return true
end
return false
end
-- draw the text boxes (if any)
function tbox_draw()
if #tbox_messages>0 then -- only draw if there are messages
rectfill(3, 103, 124, 123, 13) -- draw border rectangle
rectfill(5, 106, 122, 121, 2) -- draw fill rectangle
line(5, 105, 122, 105, 1) -- draw top border shadow
line(3, 124, 124, 124, 1) -- draw bottom border shadow
-- draw the speaker portrait
if #tbox_messages[1].speaker>0 then
local speaker_width=#tbox_messages[1].speaker*4
-- limit the width of the speaker box
if speaker_width>115 then
speaker_width=115
end
rectfill(3, 96, speaker_width+9, 102, 7) -- draw border rectangle
rectfill(5, 99, speaker_width+7, 105, 1) -- draw fill rectangle
line(5, 98, speaker_width+7, 98, 6) -- draw top border shadow
print(sub(tbox_messages[1].speaker, 0, 28), 7, 101, 7)
end
-- print the message
if tbox_messages[1].animation<#tbox_messages[1].line then
sfx(15)
tbox_messages[1].animation+=1
elseif #tbox_messages>1 and tbox_messages[2].animation<#tbox_messages[2].line then
sfx(15)
tbox_messages[2].animation+=1
end