systems - ecbambrick/SimpleEntityComponentSystem GitHub Wiki


Systems are where all the game logic is kept. While entities and components are used to store data, systems are what read and write that data. There are two types of systems: update systems and render systems. Update systems are called during the update phase of your game and render systems are called during the draw phase. Querying based on entity types should be your primary method of accessing entities.

Each system is called sequentially based on each system's priority. Order is very important (you wouldn't want to call collision before physics) so be wary of each system's priority.

Functions

nil secs.updateSystem(name, priority, update)
Creates a new update system.

nil secs.renderSystem(name, priority, update)
Creates a new render system.

nil secs.update(deltaTime)
calls the update function for every update system in order of priority.

nil secs.draw()
calls the update function for every render system in order of priority.

Example

note: the following examples use LÖVE.

secs.updatesystem("input", 100, function(dt)
    -- player input
    for e in ipairs(secs.query("moveable")) do
        e.dir.x = 0
        e.dir.y = 0
        if love.keyboard.isDown("down")  then e.dir.y =  1 end
        if love.keyboard.isDown("left")  then e.dir.x = -1 end
        if love.keyboard.isDown("right") then e.dir.x =  1 end
        if love.keyboard.isDown("up")    then e.dir.y = -1 end
    end
    -- system input
    if love.keyboard.isDown("lalt") and love.keyboard.isDown("f4") then
        love.event.quit()
    end
end)
secs.rendersystem("draw", 100, function()
    love.graphics.push()
    love.graphics.scale(WINDOW_SCALE)
    -- draw rectangles
    for e in ipairs(secs.query("drawableRectangles")) do
        love.graphics.rectangle("fill", e.pos.x, e.pos.y, e.rect.width, e.rect.height)
    end
    -- draw sprites
    for e in ipairs(secs.query("drawableSprites")) do
        love.graphics.draw(e.sprite.image, e.pos.x, e.pos.y)
    end
end)
⚠️ **GitHub.com Fallback** ⚠️