In this setup entities are tables, and components are properties of those tables. I have a system.lua that looks something like this:
Code: Select all
--- Run `process` if all varargs are not nil.
local function invoke (process, ...)
for i = 1, select('#', ...) do
if select(i, ...) == nil then return end
end
process(...)
end
--- Create a System.
--- @tparam function (entity, ...) extract
--- Extracts components from an entity, and returns all components
--- and any other arguments passed the `process` function.
--- @tparam function (...) process
--- Process components and other data returned by `extract`.
return function (extract, process)
return function (entities, ...)
for _, entity in ipairs(entities) do
invoke(process, extract(entity, ...))
end
end
end
Code: Select all
-- system/update/move.lua
return require 'system' (
function (entity, dt)
return entity.position, entity.velocity, dt
end,
function (p, v, dt)
p.x = p.x + v.x * dt
p.y = p.y + v.y * dt
end
)
Code: Select all
local game = {}
local updateSystems = {
require 'system.update.attack',
require 'system.update.fall',
require 'system.update.move',
require 'system.update.sleep',
require 'system.update.think',
DEBUG and require 'system.update.log',
-- more stuff here
}
local drawSystems = {
require 'system.draw.sprite',
DEBUG and require 'system.draw.hitbox',
}
function game:update (dt)
for _, update in ipairs(updateSystems) do
update(entities, dt)
end
end
function game:draw ()
for _, draw in ipairs(drawSystems) do
draw(entities)
end
end
return game