-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_functions.p8
296 lines (255 loc) · 4.68 KB
/
common_functions.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
pico-8 cartridge // http://www.pico-8.com
version 18
__lua__
-- debugging and environment
function draw_thrice(sprite,x,y,w,h,left)
spr(sprite,x,y,w,h,left)
spr(sprite,x+map_w,y,w,h,left)
spr(sprite,x-map_w,y,w,h,left)
end
-- function toggle_hitboxes()
-- hitboxes=not hitboxes
-- end
-- function toggle_memorydebug()
-- memorydebug = not memorydebug
-- end
-- function save_video()
-- extcmd('video')
-- end
function stop_music()
music(-1)
menuitem(1,"start music",toggle_music)
playmusic=false
end
function start_music()
music(0,0,1)
menuitem(1,"stop music",toggle_music)
playmusic=true
end
function toggle_music()
if playmusic then
stop_music()
else
start_music()
end
end
function debug_hitbox(_o)
if hitboxes then
local offset=_o.topoffset or 0
rect(_o.x,_o.y+offset,_o.x+_o.w,_o.y+_o.h,8)
end
end
-- common game routines
function rndminmax(min,max)
return rnd(max-min) + min
end
--sprite all 1 color
function setspritecolor(c)
for i=0,15 do
pal(i,c)
end
end
function jumpback(d)
if ugene.left then
ugene.dx+=d
else
ugene.dx-=d
end
end
function updatetimeouts()
for i,v in pairs(timeouts) do
if timeouts[i]>0 then
timeouts[i]-=1
end
end
end
function onscreen(o)
if (o.x+o.w > cam_x and o.x < cam_x+127) return true
return false
end
function box_hit(obj1,obj2)
hit=false
local xd=abs((obj1.x+(obj1.w/2))-(obj2.x+(obj2.w/2)))
local xs=obj1.w*0.5+obj2.w*0.5
local yd=abs((obj1.y+(obj1.h/2))-(obj2.y+(obj2.h/2)))
local ys=obj1.h/2+obj2.h/2
if xd<xs and yd<ys then
hit=true
end
return hit
end
-- cycles
function addcycle(_name,_duration,_data)
local _c={}
_c.name=_name
_c.duration=_duration
_c.age=0
_c.data=_data --table
_c.index=1
add(cycles,_c)
end
function updatecycle(cycle)
cycle.age+=1
if cycle.duration-cycle.age < 0 then
cycle.index+=1
cycle.age=0
end
if cycle.index > #cycle.data then
cycle.index=1
end
end
function printcycle(_name)
for cycle in all(cycles) do
if cycle.name==_name then
return cycle.data[cycle.index]
end
end
end
--messages
function updatemessage(m)
m.age-=1
m.y-=.5
if m.age<0 then
del(messages,m)
end
end
function drawmessage(m)
shadowfont(m.text,m.x,m.y,7,0)
end
--particles
function addpart(x,y,dx,dy,r,col,l,anim,anim_sp)
local p={}
p.x=x
p.y=y
p.dx=dx
p.dy=dy
p.r=r --radius
p.col=col
p.lifespan=l
p.anim=anim
p.anim_ix=1
p.anim_sp=anim_sp
p.speed_ct=0
add(parts,p)
end
function updatepart(p)
if p.lifespan > 0 then
p.lifespan-=1
--acceleration
p.x+=p.dx
p.y+=p.dy
--gravity
p.y+=0.2
p.speed_ct+=1
if (p.anim) and (p.anim_sp-p.speed_ct==0) then
p.speed_ct=0
p.anim_ix+=1
if (p.anim_ix>#p.anim) p.anim_ix=1
end
else
del(parts,p)
end
end
function drawpart(p)
--pset(part.x,part.y,part.col)
if (p.r) then
circfill(p.x,p.y,p.r,p.col)
else
spr(p.anim[p.anim_ix],p.x,p.y,1,1)
end
end
function shadowsprite(s,x,y,w,h,c)
--turn all pixels the chosen shadow color
for i=0,15 do
pal(i,c)
end
spr(s,x+1,y,w,h)
spr(s,x-1,y,w,h)
spr(s,x,y+1,w,h)
spr(s,x,y-1,w,h)
pal() --reset
spr(s,x,y,w,h)
end
function shadowfont(t,x,y,c1,c2)
print(t,x+1,y,c2)
print(t,x-1,y,c2)
print(t,x,y+1,c2)
print(t,x,y-1,c2)
print(t,x,y,c1)
end
function say(t,x,y,d,tail)
if timeouts["dialog"]==0 then
timeouts["dialog"]=d
dialog=""
return
end
local lines={}
local l=1
local tmplen=0
local maxlen=0
for i=1, #t do
if sub(t,i,i) == "/" then
if tmplen > maxlen then
maxlen=tmplen
end
tmplen=0
l+=1
else
if not(lines[l]) then
lines[l]=""
end
lines[l]=lines[l]..sub(t,i,i)
tmplen+=1
end
end
if tmplen > maxlen then
maxlen=tmplen
end
--for some reason, must be even number!
if maxlen % 2 ~= 0 then
maxlen+=1
end
local w=(maxlen*4)+8
local h
if #lines==1 then
local letterheights, gapheights
h=18
else
letterheights=#lines*5
gapheights=(#lines+1)*3
h=letterheights+gapheights
end
local offsetx, offsety=4,3
local balx=x-(w/2)+offsetx
local baly=y-h-offsety
-- top corners
spr(203,balx,baly,1,1)
spr(204,balx+w-8,baly,1,1)
--bottom corners
spr(219,balx,baly+h-8,1,1)
spr(220,balx+w-8,baly+h-8,1,1)
--fill left/right
for i=baly+8,baly+h-8 do
pset(balx-1,i,5)
pset(balx,i,6)
line(balx+1,i,balx+w-1,i,7)
pset(balx+w,i,5)
end
--fill top/bottom
for i=balx+8,balx+w-8 do
pset(i,baly-1,5)
line(i,baly,i,baly+h-1,7)
pset(i,baly+h-1,6)
pset(i,baly+h,5)
end
--fill middle
rectfill(balx+8,baly+8,balx+w-8,baly+h-8,7)
--tail
spr(205,balx+(w/2)+3,baly+h-1,1,1,tail)
--finally, the text!
local indenty = ((h-(#lines*7))/2)+1
for i=1, #lines do
local indentx = (w-(#lines[i]*4))/2
print(lines[i],balx+indentx,baly+(i*7-7)+indenty,0)
end
end