Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Last active August 23, 2019 19:51
Show Gist options
  • Save mlabbe/87ac3e5b3efa834cc6a3d61980af0263 to your computer and use it in GitHub Desktop.
Save mlabbe/87ac3e5b3efa834cc6a3d61980af0263 to your computer and use it in GitHub Desktop.
Pico-8 starting point
dev = true
ent_list = {}
types = {}
cam={0,0} -- world top-left
--
-- helpers
--
function get_player(n)
return ent_list[n]
end
function vec_dot(u,v)
return u[1]*v[1]+u[2]*v[2]
end
function vec_distsq(u,v)
local d={u[1]-v[1],
u[2]-v[2]}
return d[1]*d[1]+d[2]*d[2]
end
function vec_dist(u,v)
return sqrt(vec_distsq(u,v))
end
function vec_mag(v)
return sqrt(vec_dot(v,v))
end
function vec_norm(v)
local k = 1.0 / vec_mag(v)
local out = {v[1]*k, v[2]*k}
return out
end
function vec_sub(u,v)
return {u[1]-v[1],u[2]-v[2]}
end
function vec_add(u,v)
return {u[1]+v[1],u[2]+v[2]}
end
function vec_mul(v,k)
return {v[1]*k, v[2]*k}
end
function vec_str(v)
return v[1] .. ' x ' .. v[2]
end
--
-- entity handling
--
function ent_init(type, x, y)
local ent = {}
ent.type = type
ent.pos = {x, y}
ent.spr = 0
if ent.type.init ~= nil then
ent.type.init(ent)
end
add(ent_list, ent)
return ent
end
function ent_find_of_type(t)
for ent in all(ent_list) do
if ent.type == t then
return ent
end
end
return nil
end
-- pico8 -run new.p8
function _init()
-- if you don't see this, console logging is not enabled and you
-- are not ready to begin.
printh("init doom refresh daemon")
-- players must be in the first entity list positions for
-- get_player() to work.
ent_init(player, 50, 50)
end
function _update60()
for ent in all(ent_list) do
if ent.type.tick ~= nil then
ent.type.tick(ent)
end
end
end
function _draw()
for ent in all(ent_list) do
if ent.type.draw ~= nil then
ent.type.draw(ent)
else
spr(ent.spr,
flr(ent.pos[1]+cam[1]),
flr(ent.pos[2]+cam[2]))
end
end
-- debug vis
if dev then
print("mem: " .. stat(0), 80, 10, 15)
print("cpu: " .. stat(1), 80, 20, 15)
end
end
--
-- entity specialization
--
player = {
tick=function(base)
end,
draw=function(base)
pset(base.pos[1]+cam[1], base.pos[2]+cam[2], 15)
end,
}
add(types, player)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment