-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.p8
301 lines (291 loc) · 10.7 KB
/
example.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
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
function _init()
print = debug.print
world = physics.world{}
body = oop.class({}, {physics.body}, function(self)
world:addbody(self)
end)
mob = oop.class({
sprite="req",
}, {body})
function mob:draw()
spr(self.sprite, self.pos[1], self.pos[2])
end
rec = oop.class({
color="req",
static=true,
}, {body})
function rec:draw()
rectfill(
self.pos[1], self.pos[2],
self.pos[1] + self.size[1] - 1,
self.pos[2] + self.size[2] - 1,
self.color
)
end
p = mob{
pos={13*8, 11*8},
size={8, 8},
sprite=1,
}
bounds = {}
for x=0,15 do
for y=0,15 do
if fget(mget(x, y)) == 1 then
add(bounds, rec{
pos={x*8, y*8},
size={8, 8},
color={3},
})
end
end
end
end
-->8
function _update60()
if btnp(1) then
p:shove{3, 0}
elseif btnp(0) then
p:shove{-3, 0}
end
if btnp(2) then
p:shove{0, -6}
end
world:update()
end
-->8
function _draw()
cls(7)
foreach(bounds, rec.draw)
p:draw()
end
-->8
--pico-tools
oop = {}
local function make(vars, parents, new)
local required = {}
for k,v in pairs(vars) do
if v == "req" then
add(required, k)
vars[k] = nil
end
end
return function(self, input)
for v in all(required) do
assert(
input[v] ~= nil,
"missing constructor argument " .. v
)
end
for parent in all(parents) do
parent.new(self, input)
end
tools.deepassign(vars, self)
tools.assign(input, self)
if new then new(self) end
end
end
local function search(k, list)
for v in all(list) do
if v[k] then return v[k] end
end
end
local function parent_function(parents)
local parent
if #parents == 1 then
parent = parents[1]
else
parent = function(t, k)
return search(k, parents)
end
end
return parent
end
function oop.class(properties, parents, new, metatable)
local class = setmetatable({}, metatable or {})
if parents and #parents ~= 0 then
getmetatable(class).__index = parent_function(parents)
end
local instance_mt = {
__index = class
}
class.new = make(properties, parents, new)
function new(self, input)
local instance = setmetatable({}, instance_mt)
class.new(instance, input)
return instance
end
getmetatable(class).__call = new
return class
end
tools = {}
function tools.assign(t, initial)
initial = initial or {}
for k, v in pairs(t) do
initial[k] = v
end
return initial
end
function tools.deepassign(t, initial)
initial = initial or {}
for k, v in pairs(t) do
if type(v) == "table" then
initial[k] = tools.deepassign(v)
else
initial[k] = v
end
end
return initial
end
debug = {}
function debug.tstr(t, indent)
indent = indent or 0
local indentstr = ""
for i=0,indent do
indentstr = indentstr .. " "
end
local str = ""
for k, v in pairs(t) do
if type(v) == "table" then
str = str .. indentstr .. k .. "\n" .. debug.tstr(v, indent + 1) .. "\n"
else
str = str .. indentstr .. tostr(k) .. ": " .. tostr(v) .. "\n"
end
end
str = sub(str, 1, -2)
return str
end
function debug.print(...)
printh("\n")
for v in all{...} do
if type(v) == "table" then
printh(debug.tstr(v))
elseif type(v) == "nil" then
printh("nil")
else
printh(v)
end
end
end
physics = {}
function physics.collided(body1, body2)
local result = true
for i=1,2 do
result = result and
body1.pos[i] < body2.pos[i] + body2.size[i] and
body2.pos[i] < body1.pos[i] + body1.size[i]
end
return result
end
physics.world = oop.class{
bodies = {},
gravity = {0, .5},
}
function physics.world:update()
for body in all(self.bodies) do
if not body.static then
if body.mass[1] or body.mass[2] then
body:shove{
self.gravity[1] * body.weight[1], self.gravity[2] * body.weight[2]
}
if body.friction[1] ~= 0 or body.friction[2] ~= 0 then
body:slow(body.friction)
end
end
body.collisions = {}
for body2 in all(self.bodies) do
if band(body.layer, body2.layer) ~= 0 and body ~= body2 then
body:checkcollided(body2)
end
end
body:update()
end
end
end
function physics.world:addbody(body)
add(self.bodies, body)
return body
end
physics.body = oop.class{
pos="req",
size="req",
vel={0, 0},
mass={1, 1},
weight={1, 1},
friction={.1, 0},
collisions={},
static=false,
layer=0x1,
}
function physics.body:shove(vel)
for i=1,2 do
self.vel[i] += vel[i] * self.mass[i]
end
end
function physics.body:update()
for i=1,2 do
self.pos[i] += self.vel[i]
end
end
function physics.body:slow(vel)
for i=1,2 do
if self.vel[i] > 0 then
self.vel[i] -= vel[i] * self.mass[i]
if self.vel[i] < 0 then
self.vel[i] = 0
end
elseif self.vel[i] < 0 then
self.vel[i] += vel[i] * self.mass[i]
if self.vel[i] > 0 then
self.vel[i] = 0
end
end
end
end
function physics.body:checkcollided(body)
local oldpos = tools.assign(self.pos)
for i=1,2 do
local pos = tools.assign(oldpos)
pos[i] += self.vel[i]
if physics.collided({pos=pos, size=self.size}, body) then
add(self.collisions, body)
if self.vel[i] >= 0 then
self.pos[i] = body.pos[i] - self.size[i]
else
self.pos[i] = body.pos[i] + body.size[i]
end
self.vel[i] = 0
end
end
end
__gfx__
00000000888888882222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000800000082222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700808888082222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000808888082222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000808888082222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700808888082222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000800000082222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000888888882222222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__gff__
0000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0202020202020202020202020202020200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000002020202020202020200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020200000000020000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000020000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000020000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000202020000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000202020200000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0200000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0202020202020202020202020202020200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000