diff --git a/doc/index.html b/doc/index.html index f547acd..cd5610b 100644 --- a/doc/index.html +++ b/doc/index.html @@ -58,8 +58,7 @@ - +
tiny._VERSIONTiny-ecs Version, a period-separated three number string like "1.2.3" with - no leading zeros.Tiny-ecs Version, a period-separated three number string like "1.2.3"

Filter functions

@@ -86,6 +85,10 @@

World functions

+ + + + @@ -99,6 +102,18 @@ + + + + + + + + + + + + @@ -111,10 +126,6 @@ - - - - @@ -140,8 +151,7 @@ tiny._VERSION
- Tiny-ecs Version, a period-separated three number string like "1.2.3" with - no leading zeros. + Tiny-ecs Version, a period-separated three number string like "1.2.3" @@ -280,6 +290,31 @@ World:method can be used for any function in the library. For example, tiny.add(world, e1, e2, e3) is the same as world:add(e1, e2, e3).
+
+ + tiny.activate (world, ...) +
+
+ Activates Systems in the World. + Activated Systems will be update whenever tiny.update(world, dt) is called. + + +

Parameters:

+
    +
  • world + +
  • +
  • ... + Systems to activate. The Systems must already be added to the + World. +
  • +
+ + + + + +
tiny.add (world, ...) @@ -349,6 +384,73 @@ +
+
+ + tiny.deactivate (world, ...) +
+
+ Deactivates Systems in the World. + Deactivated Systems must be update manually, and will not update when the + rest of World updates. They will, however, process new Entities added while + the System is deactivated. + + +

Parameters:

+ + + + + + +
+
+ + tiny.getEntityCount (world) +
+
+ Gets count of Entities in World. + + +

Parameters:

+ + + + + + +
+
+ + tiny.getSystemCount (world) +
+
+ Gets count of Systems in World. + + +

Parameters:

+ + + + + +
@@ -415,34 +517,6 @@ - -
- - tiny.setSystemActive (world, system, active) -
-
- Sets if a System is active in a world. If the system is active, it will - update automatically when World:update(dt) is called. Otherwise, the user - must call World:updateSystem(system, dt) to update the unactivated system. - - -

Parameters:

- - - - - -
@@ -528,7 +602,7 @@
generated by LDoc 1.4.3 -Last updated 2015-03-29 20:36:32 +Last updated 2015-03-31 19:59:57
diff --git a/doc/source/tiny.lua.html b/doc/source/tiny.lua.html deleted file mode 100644 index c961c78..0000000 --- a/doc/source/tiny.lua.html +++ /dev/null @@ -1,452 +0,0 @@ - - - - - Reference - - - - -
- -
- -
-
-
- - -
- - - - - - -
- -

tiny.lua

-
---- @module tiny-ecs
--- @author Calvin Rose
-local tiny = {}
-
---- Tiny-ecs Version, a period-separated three number string like "1.2.3" with
--- no leading zeros.
-tiny._VERSION = "0.3.0"
-
-local tinsert = table.insert
-local tremove = table.remove
-local tconcat = table.concat
-local pairs = pairs
-local ipairs = ipairs
-local setmetatable = setmetatable
-local getmetatable = getmetatable
-
--- Local versions of the library functions
-local tiny_system
-local tiny_manageEntities
-local tiny_manageSystems
-local tiny_updateSystem
-local tiny_add
-local tiny_remove
-
---- Filter functions.
--- A Filter is a function that selects which Entities apply to a System.
--- @section Filter
-
---- Makes a Filter that filters Entities with specified Components.
--- An Entity must have all Components to match the filter.
--- @param ... List of Components
-function tiny.requireAll(...)
-    local components = {...}
-    local len = #components
-    return function(e)
-        local c
-        for i = 1, len do
-            c = components[i]
-            if e[c] == nil then
-                return false
-            end
-        end
-        return true
-    end
-end
-
---- Makes a Filter that filters Entities with specified Components.
--- An Entity must have at least one specified Component to match the filter.
--- @param ... List of Components
-function tiny.requireOne(...)
-    local components = {...}
-    local len = #components
-    return function(e)
-        local c
-        for i = 1, len do
-            c = components[i]
-            if e[c] ~= nil then
-                return true
-            end
-        end
-        return false
-    end
-end
-
---- System functions.
--- A System a wrapper around function callbacks for manipulating Entities.
--- @section System
-
-local systemMetaTable = {}
-
---- Creates a System.
--- @param callback Function of one argument, delta time, that is called once
--- per world update
--- @param filter Function of one argument, an Entity, that returns a boolean
--- @param entityCallback Function of two arguments, an Entity and delta time
--- @param onAdd Optional callback for when Enities are added to the System that
--- takes one argument, an Entity
--- @param onRemove Similar to onAdd, but is instead called when an Entity is
--- removed from the System
-function tiny.system(callback, filter, entityCallback, onAdd, onRemove)
-    local ret = {
-        callback = callback,
-        filter = filter,
-        entityCallback = entityCallback,
-        onAdd = onAdd,
-        onRemove = onRemove
-    }
-    setmetatable(ret, systemMetaTable)
-    return ret
-end
-tiny_system = tiny.system
-
---- Creates a System that processes Entities every update. Also provides
--- optional callbacks for when Entities are added or removed from the System.
--- @param filter Function of one argument, an Entity, that returns a boolean
--- @param entityCallback Function of two arguments, an Entity and delta time
--- @param onAdd Optional callback for when Entities are added to the System that
--- takes one argument, an Entity
--- @param onRemove Similar to onAdd, but is instead called when an Entity is
--- removed from the System
-function tiny.processingSystem(filter, entityCallback, onAdd, onRemove)
-    return tiny_system(nil, filter, entityCallback, onAdd, onRemove)
-end
-
-local worldMetaTable = { __index = tiny }
-
---- World functions.
--- A World is a container that manages Entities and Systems. The tiny-ecs module
--- is set to be the __index of all World tables, so the often clearer syntax of
--- World:method can be used for any function in the library. For example,
--- tiny.add(world, e1, e2, e3) is the same as world:add(e1, e2, e3).
--- @section World
-
---- Creates a new World.
--- Can optionally add default Systems and Entities.
--- @param ... Systems and Entities to add to the World
--- @return A new World
-function tiny.world(...)
-
-    local ret = {
-
-        -- Table of Entities to status
-        status = {},
-
-        -- Set of Entities
-        entities = {},
-
-        -- Number of Entities in World.
-        entityCount = 0,
-
-        -- Number of Systems in World.
-        systemCount = 0,
-
-        -- List of Systems
-        systems = {},
-
-        -- Table of Systems to whether or not they are active.
-        activeSystems = {},
-
-        -- Table of Systems to System Indices
-        systemIndices = {},
-
-        -- Table of Systems to Sets of matching Entities
-        systemEntities = {},
-
-        -- List of Systems to add next update
-        systemsToAdd = {},
-
-        -- List of Systems to remove next update
-        systemsToRemove = {}
-
-    }
-
-    tiny.add(ret, ...)
-    tiny.manageSystems(ret)
-    tiny.manageEntities(ret)
-
-    setmetatable(ret, worldMetaTable)
-    return ret
-
-end
-
---- Adds Entities and Systems to the World.
--- New objects will enter the World the next time World:update(dt) is called.
--- Also call this method when an Entity has had its Components changed, such
--- that it matches different Filters.
--- @param world
--- @param ... Systems and Entities
-function tiny.add(world, ...)
-    local args = {...}
-    local status = world.status
-    local entities = world.entities
-    local systemsToAdd = world.systemsToAdd
-    for _, obj in ipairs(args) do
-        if getmetatable(obj) == systemMetaTable then
-            tinsert(systemsToAdd, obj)
-        else -- Assume obj is an Entity
-            entities[obj] = true
-            status[obj] = "add"
-        end
-    end
-end
-tiny_add = tiny.add
-
---- Removes Entities and Systems from the World. Objects will exit the World the
--- next time World:update(dt) is called.
--- @param world
--- @param ... Systems and Entities
-function tiny.remove(world, ...)
-    local args = {...}
-    local status = world.status
-    local entities = world.entities
-    local systemsToRemove = world.systemsToRemove
-    for _, obj in ipairs(args) do
-        if getmetatable(obj) == systemMetaTable then
-            tinsert(systemsToRemove, obj)
-        elseif entities[obj] then -- Assume obj is an Entity
-            status[obj] = "remove"
-        end
-    end
-end
-tiny_remove = tiny.remove
-
---- Updates a System.
--- @param world
--- @param system A System in the World to update
--- @param dt Delta time
-function tiny.updateSystem(world, system, dt)
-    local callback = system.callback
-    local entityCallback = system.entityCallback
-
-    if callback then
-        callback(dt)
-    end
-
-    if entityCallback then
-        local entities = world.entities
-        local es = world.systemEntities[system]
-        if es then
-            for e in pairs(es) do
-                entityCallback(e, dt)
-            end
-        end
-    end
-end
-tiny_updateSystem = tiny.updateSystem
-
---- Adds and removes Systems that have been marked from the World.
--- The user of this library should seldom if ever call this.
--- @param world
-function tiny.manageSystems(world)
-
-        local systemEntities = world.systemEntities
-        local systemIndices = world.systemIndices
-        local entities = world.entities
-        local systems = world.systems
-        local systemsToAdd = world.systemsToAdd
-        local systemsToRemove = world.systemsToRemove
-        local activeSystems = world.activeSystems
-
-        -- Keep track of the number of Systems in the world
-        local deltaSystemCount = 0
-
-        -- Remove all Systems queued for removal
-        for i = 1, #systemsToRemove do
-            -- Pop system off the remove queue
-            local sys = systemsToRemove[i]
-            systemsToRemove[i] = nil
-
-            local sysID = systemIndices[sys]
-            if sysID then
-                tremove(systems, sysID)
-
-                local onRemove = sys.onRemove
-                if onRemove then
-                    for e in pairs(systemEntities[sys]) do
-                        onRemove(e)
-                    end
-                end
-
-                systemEntities[sys] = nil
-                activeSystems[sys] = nil
-                deltaSystemCount = deltaSystemCount - 1
-            end
-        end
-
-        -- Add Systems queued for addition
-        for i = 1, #systemsToAdd do
-            -- Pop system off the add queue
-            local sys = systemsToAdd[i]
-            systemsToAdd[i] = nil
-
-            -- Add system to world
-            local es = {}
-            systemEntities[sys] = es
-            tinsert(systems, sys)
-            systemIndices[sys] = #systems
-            activeSystems[sys] = true
-
-            local filter = sys.filter
-            if filter then
-                for e in pairs(entities) do
-                    es[e] = filter(e) and true or nil
-                end
-            end
-
-            deltaSystemCount = deltaSystemCount + 1
-        end
-
-        -- Update the number of Systems in the World
-        world.systemCount = world.systemCount + deltaSystemCount
-end
-tiny_manageSystems = tiny.manageSystems
-
---- Adds and removes Entities that have been marked.
--- The user of this library should seldom if ever call this.
--- @param world
-function tiny.manageEntities(world)
-
-    local statuses = world.status
-    local systemEntities = world.systemEntities
-    local entities = world.entities
-    local systems = world.systems
-
-    -- Keep track of the number of Entities in the World
-    local deltaEntityCount = 0
-
-    -- Add, remove, or change Entities
-    for e, s in pairs(statuses) do
-        if s == "add" then
-            deltaEntityCount = deltaEntityCount + 1
-            for sys, es in pairs(systemEntities) do
-                local filter = sys.filter
-                if filter then
-                    local matches = filter(e) and true or nil
-                    local onAdd = sys.onAdd
-                    if onAdd and matches and not es[e] then
-                        onAdd(e)
-                    end
-                    es[e] = matches
-                end
-            end
-        elseif s == "remove" then
-            deltaEntityCount = deltaEntityCount - 1
-            entities[e] = nil
-            for sys, es in pairs(systemEntities) do
-                local onRemove = sys.onRemove
-                if es[e] and onRemove then
-                    onRemove(e)
-                end
-                es[e] = nil
-            end
-        end
-        statuses[e] = nil
-    end
-
-    -- Update Entity count
-    world.entityCount = world.entityCount + deltaEntityCount
-
-end
-tiny_manageEntities = tiny.manageEntities
-
---- Updates the World.
--- Frees Entities that have been marked for freeing, adds
--- entities that have been marked for adding, etc.
--- @param world
--- @param dt Delta time
-function tiny.update(world, dt)
-
-    tiny_manageSystems(world)
-    tiny_manageEntities(world)
-
-    --  Iterate through Systems IN ORDER
-    for _, s in ipairs(world.systems) do
-        if world.activeSystems[s] then
-            tiny_updateSystem(world, s, dt)
-        end
-    end
-end
-
---- Removes all Entities from the World.
--- When World:update(dt) is next called,
--- all Entities will be removed.
--- @param world
-function tiny.clearEntities(world)
-    local status = world.status
-    for e in pairs(world.entities) do
-        status[e] = "remove"
-    end
-end
-
---- Removes all Systems from the World.
--- When World:update(dt) is next called,
--- all Systems will be removed.
--- @param world
-function tiny.clearSystems(world)
-    local newSystemsToRemove = {}
-    local systems = world.systems
-    for i = 1, #systems do
-        newSystemsToRemove[i] = systems[i]
-    end
-    world.systemsToRemove = newSystemsToRemove
-end
-
---- Sets if a System is active in a world. If the system is active, it will
--- update automatically when World:update(dt) is called. Otherwise, the user
--- must call World:updateSystem(system, dt) to update the unactivated system.
--- @param world
--- @param system A System in the World activate/deactivate
--- @param active Boolean new state of the System
-function tiny.setSystemActive(world, system, active)
-    world.activeSystem[system] = active and true or nil
-end
-
-return tiny
- - -
-
-
-generated by LDoc 1.4.3 -Last updated 2015-03-29 20:35:35 -
-
- - diff --git a/doc/topics/README.md.html b/doc/topics/README.md.html deleted file mode 100644 index 4a324af..0000000 --- a/doc/topics/README.md.html +++ /dev/null @@ -1,122 +0,0 @@ - - - - - tiny-ecs API - - - - -
- -
- -
-
-
- - -
- - - - - - -
- - # tiny-ecs # -Tiny-ecs is an Entity Component System for lua that's simple, flexible, and useful. -Because of lua's tabular nature, Entity Component Systems are a natural choice -for simulating large and complex systems. For more explanation on Entity -Component Systems, here is some -[basic info](http://en.wikipedia.org/wiki/Entity_component_system "Wikipedia"). -

## Use It ## -Copy paste tiny.lua into your source folder. -

## Overview ## -Tiny-ecs has four important types: Worlds, Filters, Systems, and Entities. -Entities, however, can be any lua table, and Filters are just functions that -take an Entity as a parameter. -

### Entities ### -Entities are simply lua tables of data that gets processed by Systems. Entities -should contain primarily data rather that code, as it is the Systems's job to -do logic on data. Henceforth, a key-value pair in an Entity will -be referred to as a Component. -

### Worlds ### -Worlds are the outermost containers in tiny-ecs that contain both Systems -and Entities. In typical use, only one World is used at a time. -

### Systems ### -Systems in tiny-ecs describe how to update Entities. Systems select certain Entities -using a Filter, and then only update those select Entities. Some Systems don't -update Entities, and instead just act as function callbacks every update. Tiny-ecs -provides functions for creating Systems easily. -

### Filters ### -Filters are used to select Entities. Filters can be any lua function, but -tiny-ecs provides some functions for generating common ones, like selecting -only Entities that have all required components. -

## Example ## -```lua -local tiny = require("tiny") -

local talkingSystem = tiny.processingSystem( - tiny.requireAll("name", "mass", "phrase"), - function (p, delta) - p.mass = p.mass + delta * 3 - print(p.name .. ", who weighs " .. p.mass .. " pounds, says, \"" .. p.phrase .. "\"") - end -) -

local joe = { - name = "Joe", - phrase = "I'm a plumber.", - mass = 150, - hairColor = "brown" -} -

local world = tiny.newWorld(talkingSystem, joe) -

for i = 1, 20 do - world:update(1) -end -``` -

## Testing ## -Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and run -`busted` from the command line to test. -

## TODO ## -

* Dynamic reordering of Systems -* More testing -* Performance testing / optimization -* API outside of source code -* Add more complete examples - - -

-
-
-generated by LDoc 1.4.3 -Last updated 2015-03-29 17:53:00 -
-
- -
tiny.activate (world, ...)Activates Systems in the World.
tiny.add (world, ...) Adds Entities and Systems to the World.Removes all Systems from the World.
tiny.deactivate (world, ...)Deactivates Systems in the World.
tiny.getEntityCount (world)Gets count of Entities in World.
tiny.getSystemCount (world)Gets count of Systems in World.
tiny.manageEntities (world) Adds and removes Entities that have been marked.
Removes Entities and Systems from the World.
tiny.setSystemActive (world, system, active)Sets if a System is active in a world.
tiny.update (world, dt) Updates the World.