Last active
March 30, 2021 16:02
-
-
Save scambier/dce4433f07d723a490a687d890beb8c8 to your computer and use it in GitHub Desktop.
PICO-8 boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- gobals and tools | |
left,right,up,down,fire1,fire2=0,1,2,3,4,5 | |
black,dark_blue,dark_purple,dark_green,brown,dark_gray,light_gray,white,red,orange,yellow,green,blue,indigo,pink,peach=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 | |
-- | |
-- simple ecs | |
-- https://github.com/namuol/pico8-ecs | |
-- | |
local ecs = {} | |
function containsall(e, keys) | |
for _,name in pairs(keys) do | |
if e[name] == nil then | |
return false | |
end | |
end | |
return true | |
end | |
function ecs.entitieswith(entities, componentnames) | |
local results = {} | |
for id,entity in pairs(entities) do | |
if containsall(entity, componentnames) then | |
results[#results+1] = entity | |
end | |
end | |
return results | |
end | |
function ecs.world() | |
local world = {} | |
local componentsbyname = {} | |
function world.component(name) | |
if not componentsbyname[name] then | |
componentsbyname[name] = componentsbyname.size | |
end | |
return componentsbyname[name] | |
end | |
local entities = {} | |
function world.addentity(components) | |
local id = #entities+1 | |
entities[id] = components | |
return id | |
end | |
function world.removeentity(id) | |
entities[id] = nil | |
end | |
function world.getcomponents(id) | |
return entities[id] | |
end | |
function world.invoke(funcs) | |
for n,func in pairs(funcs) do | |
entities = func(entities) | |
end | |
end | |
return world | |
end | |
function filtered_entities(entities, filters) | |
return pairs(ecs.entitieswith(entities, filters)) | |
end | |
-- | |
-- coroutines manager | |
-- | |
local coroutines={} | |
function addcoroutine(key, fn) | |
coroutines[key] = cocreate(fn) | |
end | |
function getcoroutine(key) | |
return coroutines[key] | |
end | |
function startcoroutine(key) | |
coresume(coroutines[key]) | |
end | |
function stopcoroutine(key) | |
coroutines[key] = nil | |
end | |
function _coresolve() | |
for k,co in pairs(coroutines) do | |
if costatus(co)!='dead' then | |
coresume(co) | |
else | |
stopcoroutine(k) | |
end | |
end | |
end | |
function wait(seconds) | |
wait_frames(seconds*60) | |
end | |
function wait_frames(frames) | |
for i=1,frames do | |
yield() | |
end | |
end | |
-->8 | |
-- systems | |
-->8 | |
-- game states | |
gs = nil -- game/global state | |
function set_state(state) | |
gs = state | |
end | |
function state_game() | |
local world = ecs.world() | |
local state = {} | |
function state._init() | |
end | |
function state._update() | |
end | |
function state._draw() | |
end | |
return state | |
end | |
-->8 | |
-- main | |
states = { | |
game = state_game() | |
} | |
function _init() | |
set_state(states.game) | |
gs._init() | |
end | |
function _update60() | |
_coresolve() | |
gs._update() | |
end | |
function _draw() | |
gs._draw() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment