mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-21 23:56:50 -06:00
Update demo to use tiny-ecs 2.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
function love.conf(t)
|
function love.conf(t)
|
||||||
t.identity = nil
|
t.identity = nil
|
||||||
t.version = "0.9.2"
|
t.version = "0.10.1"
|
||||||
t.console = false
|
t.console = false
|
||||||
t.window.title = "Commando Kibbles"
|
t.window.title = "Commando Kibbles"
|
||||||
t.window.icon = nil
|
t.window.icon = nil
|
||||||
@@ -11,7 +11,6 @@ function love.conf(t)
|
|||||||
t.window.minwidth = 600
|
t.window.minwidth = 600
|
||||||
t.window.minheight = 400
|
t.window.minheight = 400
|
||||||
t.window.fullscreen = false
|
t.window.fullscreen = false
|
||||||
t.window.fullscreentype = "normal"
|
|
||||||
t.window.vsync = true
|
t.window.vsync = true
|
||||||
t.window.fsaa = 0
|
t.window.fsaa = 0
|
||||||
t.window.display = 1
|
t.window.display = 1
|
||||||
|
|||||||
+349
-196
@@ -1,5 +1,5 @@
|
|||||||
--[[
|
--[[
|
||||||
Copyright (c) 2015 Calvin Rose
|
Copyright (c) 2016 Calvin Rose
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
@@ -23,13 +23,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||||||
-- @author Calvin Rose
|
-- @author Calvin Rose
|
||||||
-- @license MIT
|
-- @license MIT
|
||||||
-- @copyright 2015
|
-- @copyright 2015
|
||||||
local tiny = { _VERSION = "1.1-6" }
|
local tiny = { _VERSION = "scm" }
|
||||||
|
|
||||||
-- Local versions of standard lua functions
|
-- Local versions of standard lua functions
|
||||||
local tinsert = table.insert
|
local tinsert = table.insert
|
||||||
local tremove = table.remove
|
local tremove = table.remove
|
||||||
local tsort = table.sort
|
local tsort = table.sort
|
||||||
local pairs = pairs
|
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local type = type
|
local type = type
|
||||||
local select = select
|
local select = select
|
||||||
@@ -85,88 +84,121 @@ local tiny_remove
|
|||||||
--
|
--
|
||||||
-- @section Filter
|
-- @section Filter
|
||||||
|
|
||||||
|
-- A helper function to compile filters.
|
||||||
|
local filterJoin
|
||||||
|
|
||||||
|
-- A helper function to filters from string
|
||||||
|
local filterBuildString
|
||||||
|
|
||||||
|
do
|
||||||
|
|
||||||
|
local loadstring = loadstring or load
|
||||||
|
local function getchr(c)
|
||||||
|
return "\\" .. c:byte()
|
||||||
|
end
|
||||||
|
local function make_safe(text)
|
||||||
|
return ("%q"):format(text):gsub('\n', 'n'):gsub("[\128-\255]", getchr)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function filterJoinRaw(prefix, seperator, ...)
|
||||||
|
local accum = {}
|
||||||
|
local build = {}
|
||||||
|
for i = 1, select('#', ...) do
|
||||||
|
local item = select(i, ...)
|
||||||
|
if type(item) == 'string' then
|
||||||
|
accum[#accum + 1] = ("(e[%s] ~= nil)"):format(make_safe(item))
|
||||||
|
elseif type(item) == 'function' then
|
||||||
|
build[#build + 1] = ('local subfilter_%d_ = select(%d, ...)'):format(i, i)
|
||||||
|
accum[#accum + 1] = ('(subfilter_%d_(system, e))'):format(i)
|
||||||
|
else
|
||||||
|
error 'Filter token must be a string or a filter function.'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local source = ('do %s\n return function(system, e) return %s(%s) end end'):format(
|
||||||
|
table.concat(build, '\n'),
|
||||||
|
prefix,
|
||||||
|
table.concat(accum, seperator)
|
||||||
|
)
|
||||||
|
local loader, err = loadstring(source)
|
||||||
|
if err then error(err) end
|
||||||
|
return loader(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function filterJoin(...)
|
||||||
|
local state, value = pcall(filterJoinRaw, ...)
|
||||||
|
if state then return value else return nil, value end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function buildPart(str)
|
||||||
|
local accum = {}
|
||||||
|
local subParts = {}
|
||||||
|
str = str:gsub('%b()', function(p)
|
||||||
|
subParts[#subParts + 1] = buildPart(p:sub(2, -2))
|
||||||
|
return ('\255%d'):format(#subParts)
|
||||||
|
end)
|
||||||
|
for invert, part, sep in str:gmatch('(%!?)([^%|%&%!]+)([%|%&%!]?)') do
|
||||||
|
if part:match('^\255%d+$') then
|
||||||
|
local partIndex = tonumber(part:match(part:sub(2)))
|
||||||
|
accum[#accum + 1] = ('%s(%s)'):format(invert == '' and '' or 'not', subParts[partIndex])
|
||||||
|
else
|
||||||
|
accum[#accum + 1] = ("(e[%s] %s nil)"):format(make_safe(part), invert == '' and '~=' or '==')
|
||||||
|
end
|
||||||
|
if sep ~= '' then
|
||||||
|
accum[#accum + 1] = (sep == '|' and ' or ' or ' and ')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return table.concat(accum)
|
||||||
|
end
|
||||||
|
|
||||||
|
function filterBuildString(str)
|
||||||
|
local source = ("return function(_, e) return %s end"):format(buildPart(str))
|
||||||
|
local loader, err = loadstring(source)
|
||||||
|
if err then
|
||||||
|
error(err)
|
||||||
|
end
|
||||||
|
return loader()
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
--- Makes a Filter that selects Entities with all specified Components and
|
--- Makes a Filter that selects Entities with all specified Components and
|
||||||
-- Filters.
|
-- Filters.
|
||||||
function tiny.requireAll(...)
|
function tiny.requireAll(...)
|
||||||
local components = {...}
|
return filterJoin('', ' and ', ...)
|
||||||
local len = #components
|
|
||||||
return function(system, e)
|
|
||||||
local c
|
|
||||||
for i = 1, len do
|
|
||||||
c = components[i]
|
|
||||||
if type(c) == 'function' then
|
|
||||||
if not c(system, e) then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
elseif e[c] == nil then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Makes a Filter that selects Entities with at least one of the specified
|
--- Makes a Filter that selects Entities with at least one of the specified
|
||||||
-- Components and Filters.
|
-- Components and Filters.
|
||||||
function tiny.requireAny(...)
|
function tiny.requireAny(...)
|
||||||
local components = {...}
|
return filterJoin('', ' or ', ...)
|
||||||
local len = #components
|
|
||||||
return function(system, e)
|
|
||||||
local c
|
|
||||||
for i = 1, len do
|
|
||||||
c = components[i]
|
|
||||||
if type(c) == 'function' then
|
|
||||||
if c(system, e) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
elseif e[c] ~= nil then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Makes a Filter that rejects Entities with all specified Components and
|
--- Makes a Filter that rejects Entities with all specified Components and
|
||||||
-- Filters, and selects all other Entities.
|
-- Filters, and selects all other Entities.
|
||||||
function tiny.rejectAll(...)
|
function tiny.rejectAll(...)
|
||||||
local components = {...}
|
return filterJoin('not', ' and ', ...)
|
||||||
local len = #components
|
|
||||||
return function(system, e)
|
|
||||||
local c
|
|
||||||
for i = 1, len do
|
|
||||||
c = components[i]
|
|
||||||
if type(c) == 'function' then
|
|
||||||
if not c(system, e) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
elseif e[c] == nil then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Makes a Filter that rejects Entities with at least one of the specified
|
--- Makes a Filter that rejects Entities with at least one of the specified
|
||||||
-- Components and Filters, and selects all other Entities.
|
-- Components and Filters, and selects all other Entities.
|
||||||
function tiny.rejectAny(...)
|
function tiny.rejectAny(...)
|
||||||
local components = {...}
|
return filterJoin('not', ' or ', ...)
|
||||||
local len = #components
|
end
|
||||||
return function(system, e)
|
|
||||||
local c
|
--- Makes a Filter from a string. Syntax of `pattern` is as follows.
|
||||||
for i = 1, len do
|
--
|
||||||
c = components[i]
|
-- * Tokens are alphanumeric strings including underscores.
|
||||||
if type(c) == 'function' then
|
-- * Tokens can be separated by |, &, or surrounded by parentheses.
|
||||||
if c(system, e) then
|
-- * Tokens can be prefixed with !, and are then operated on with a boolean 'not'.
|
||||||
return false
|
--
|
||||||
end
|
-- Examples are best:
|
||||||
elseif e[c] ~= nil then
|
-- 'a|b|c' - Matches entities with an 'a' component OR a 'b' component or a 'c' component.
|
||||||
return false
|
-- 'a&!b&c' - Matches entities with an 'a' component AND NOT a 'b' component AND a 'c' component.
|
||||||
end
|
-- 'a|(b&c&d)|e - Matches 'a' OR ('b' AND 'c' AND 'd') OR 'e'
|
||||||
end
|
-- @param pattern
|
||||||
return true
|
function tiny.filter(pattern)
|
||||||
end
|
local state, value = pcall(filterBuildString, pattern)
|
||||||
|
if state then return value else return nil, value end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- System functions.
|
--- System functions.
|
||||||
@@ -187,6 +219,10 @@ end
|
|||||||
-- from the System.
|
-- from the System.
|
||||||
-- * `function system:onModify(dt)` - Called when the System is modified by
|
-- * `function system:onModify(dt)` - Called when the System is modified by
|
||||||
-- adding or removing Entities from the System.
|
-- adding or removing Entities from the System.
|
||||||
|
-- * `function system:onAddToWorld(world)` - Called when the System is added
|
||||||
|
-- to the World, before any entities are added to the system.
|
||||||
|
-- * `function system:onRemoveFromWorld(world)` - Called when the System is
|
||||||
|
-- removed from the world, after all Entities are removed from the System.
|
||||||
--
|
--
|
||||||
-- For Filters, it is convenient to use `tiny.requireAll` or `tiny.requireAny`,
|
-- For Filters, it is convenient to use `tiny.requireAll` or `tiny.requireAny`,
|
||||||
-- but one can write their own filters as well. Set the Filter of a System like
|
-- but one can write their own filters as well. Set the Filter of a System like
|
||||||
@@ -222,6 +258,14 @@ end
|
|||||||
-- in the next update, if it has one. This is usually managed by tiny-ecs, so
|
-- in the next update, if it has one. This is usually managed by tiny-ecs, so
|
||||||
-- users should mostly ignore this, too.
|
-- users should mostly ignore this, too.
|
||||||
--
|
--
|
||||||
|
-- There is another option to (hopefully) increase performance in systems that have
|
||||||
|
-- items added to or removed from them often, and have lots of entities in them.
|
||||||
|
-- Setting the `nocache' field of the system might improve performance. It is still
|
||||||
|
-- experimental. There are some restriction to systems without caching, however.
|
||||||
|
-- * There is no `entities` table.
|
||||||
|
-- * Callbacks such onAdd, onRemove, and onModify will never be called
|
||||||
|
-- * Noncached systems cannot be sorted (There is no entities list to sort).
|
||||||
|
--
|
||||||
-- @section System
|
-- @section System
|
||||||
|
|
||||||
-- Use an empty table as a key for identifying Systems. Any table that contains
|
-- Use an empty table as a key for identifying Systems. Any table that contains
|
||||||
@@ -235,21 +279,31 @@ end
|
|||||||
|
|
||||||
-- Update function for all Processing Systems.
|
-- Update function for all Processing Systems.
|
||||||
local function processingSystemUpdate(system, dt)
|
local function processingSystemUpdate(system, dt)
|
||||||
local entities = system.entities
|
|
||||||
local preProcess = system.preProcess
|
local preProcess = system.preProcess
|
||||||
local process = system.process
|
local process = system.process
|
||||||
local postProcess = system.postProcess
|
local postProcess = system.postProcess
|
||||||
local entity
|
|
||||||
|
|
||||||
if preProcess then
|
if preProcess then
|
||||||
preProcess(system, dt)
|
preProcess(system, dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
if process then
|
if process then
|
||||||
local len = #entities
|
if system.nocache then
|
||||||
for i = 1, len do
|
local entities = system.world.entityList
|
||||||
entity = entities[i]
|
local filter = system.filter
|
||||||
process(system, entity, dt)
|
if filter then
|
||||||
|
for i = 1, #entities do
|
||||||
|
local entity = entities[i]
|
||||||
|
if filter(system, entity) then
|
||||||
|
process(system, entity, dt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local entities = system.entities
|
||||||
|
for i = 1, #entities do
|
||||||
|
process(system, entities[i], dt)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -258,7 +312,7 @@ local function processingSystemUpdate(system, dt)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sorts Systems by a function system.sort(entity1, entity2) on modify.
|
-- Sorts Systems by a function system.sortDelegate(entity1, entity2) on modify.
|
||||||
local function sortedSystemOnModify(system, dt)
|
local function sortedSystemOnModify(system, dt)
|
||||||
local entities = system.entities
|
local entities = system.entities
|
||||||
local indices = system.indices
|
local indices = system.indices
|
||||||
@@ -272,8 +326,7 @@ local function sortedSystemOnModify(system, dt)
|
|||||||
end
|
end
|
||||||
tsort(entities, sortDelegate)
|
tsort(entities, sortDelegate)
|
||||||
for i = 1, #entities do
|
for i = 1, #entities do
|
||||||
local entity = entities[i]
|
indices[entities[i]] = i
|
||||||
indices[entity] = i
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -290,9 +343,9 @@ end
|
|||||||
-- Processing Systems have three extra callbacks besides those inheritted from
|
-- Processing Systems have three extra callbacks besides those inheritted from
|
||||||
-- vanilla Systems.
|
-- vanilla Systems.
|
||||||
--
|
--
|
||||||
-- function system:preProcess(entities, dt) -- Called before iteration.
|
-- function system:preProcess(dt) -- Called before iteration.
|
||||||
-- function system:process(entities, dt) -- Process each entity.
|
-- function system:process(entity, dt) -- Process each entity.
|
||||||
-- function system:postProcess(entity, dt) -- Called after iteration.
|
-- function system:postProcess(dt) -- Called after iteration.
|
||||||
--
|
--
|
||||||
-- Processing Systems have their own `update` method, so don't implement a
|
-- Processing Systems have their own `update` method, so don't implement a
|
||||||
-- a custom `update` callback for Processing Systems.
|
-- a custom `update` callback for Processing Systems.
|
||||||
@@ -348,7 +401,7 @@ local worldMetaTable
|
|||||||
-- Can optionally add default Systems and Entities. Returns the new World along
|
-- Can optionally add default Systems and Entities. Returns the new World along
|
||||||
-- with default Entities and Systems.
|
-- with default Entities and Systems.
|
||||||
function tiny.world(...)
|
function tiny.world(...)
|
||||||
local ret = {
|
local ret = setmetatable({
|
||||||
|
|
||||||
-- List of Entities to add
|
-- List of Entities to add
|
||||||
entitiesToAdd = {},
|
entitiesToAdd = {},
|
||||||
@@ -356,6 +409,9 @@ function tiny.world(...)
|
|||||||
-- List of Entities to remove
|
-- List of Entities to remove
|
||||||
entitiesToRemove = {},
|
entitiesToRemove = {},
|
||||||
|
|
||||||
|
-- List of Entities to change
|
||||||
|
entitiesToChange = {},
|
||||||
|
|
||||||
-- List of Entities to add
|
-- List of Entities to add
|
||||||
systemsToAdd = {},
|
systemsToAdd = {},
|
||||||
|
|
||||||
@@ -365,28 +421,33 @@ function tiny.world(...)
|
|||||||
-- Set of Entities
|
-- Set of Entities
|
||||||
entities = {},
|
entities = {},
|
||||||
|
|
||||||
|
-- List of Entities
|
||||||
|
entityList = {},
|
||||||
|
|
||||||
-- Number of Entities in World
|
-- Number of Entities in World
|
||||||
entityCount = 0,
|
entityCount = 0,
|
||||||
|
|
||||||
-- List of Systems
|
-- List of Systems
|
||||||
systems = {}
|
systems = {}
|
||||||
}
|
}, worldMetaTable)
|
||||||
|
|
||||||
tiny_add(ret, ...)
|
tiny_add(ret, ...)
|
||||||
tiny_manageSystems(ret)
|
tiny_manageSystems(ret)
|
||||||
tiny_manageEntities(ret)
|
tiny_manageEntities(ret)
|
||||||
|
|
||||||
return setmetatable(ret, worldMetaTable), ...
|
return ret, ...
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds an Entity to the world.
|
--- Adds an Entity to the world.
|
||||||
-- Also call this on Entities that have changed Components such that they
|
-- Also call this on Entities that have changed Components such that they
|
||||||
-- match different Filters. Returns the Entity.
|
-- match different Filters. Returns the Entity.
|
||||||
function tiny.addEntity(world, entity)
|
function tiny.addEntity(world, entity)
|
||||||
local e2a = world.entitiesToAdd
|
|
||||||
e2a[#e2a + 1] = entity
|
|
||||||
if world.entities[entity] then
|
if world.entities[entity] then
|
||||||
tiny_removeEntity(world, entity)
|
local e2c = world.entitiesToChange
|
||||||
|
e2c[#e2c + 1] = entity
|
||||||
|
else
|
||||||
|
local e2a = world.entitiesToAdd
|
||||||
|
e2a[#e2a + 1] = entity
|
||||||
end
|
end
|
||||||
return entity
|
return entity
|
||||||
end
|
end
|
||||||
@@ -394,8 +455,10 @@ tiny_addEntity = tiny.addEntity
|
|||||||
|
|
||||||
--- Adds a System to the world. Returns the System.
|
--- Adds a System to the world. Returns the System.
|
||||||
function tiny.addSystem(world, system)
|
function tiny.addSystem(world, system)
|
||||||
|
assert(system.world == nil, "System already belongs to a World.")
|
||||||
local s2a = world.systemsToAdd
|
local s2a = world.systemsToAdd
|
||||||
s2a[#s2a + 1] = system
|
s2a[#s2a + 1] = system
|
||||||
|
system.world = world
|
||||||
return system
|
return system
|
||||||
end
|
end
|
||||||
tiny_addSystem = tiny.addSystem
|
tiny_addSystem = tiny.addSystem
|
||||||
@@ -403,9 +466,8 @@ tiny_addSystem = tiny.addSystem
|
|||||||
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
||||||
-- added Entities and Systems.
|
-- added Entities and Systems.
|
||||||
function tiny.add(world, ...)
|
function tiny.add(world, ...)
|
||||||
local obj
|
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
obj = select(i, ...)
|
local obj = select(i, ...)
|
||||||
if obj then
|
if obj then
|
||||||
if isSystem(obj) then
|
if isSystem(obj) then
|
||||||
tiny_addSystem(world, obj)
|
tiny_addSystem(world, obj)
|
||||||
@@ -428,6 +490,7 @@ tiny_removeEntity = tiny.removeEntity
|
|||||||
|
|
||||||
--- Removes a System from the world. Returns the System.
|
--- Removes a System from the world. Returns the System.
|
||||||
function tiny.removeSystem(world, system)
|
function tiny.removeSystem(world, system)
|
||||||
|
assert(system.world == world, "System does not belong to this World.")
|
||||||
local s2r = world.systemsToRemove
|
local s2r = world.systemsToRemove
|
||||||
s2r[#s2r + 1] = system
|
s2r[#s2r + 1] = system
|
||||||
return system
|
return system
|
||||||
@@ -435,16 +498,15 @@ end
|
|||||||
tiny_removeSystem = tiny.removeSystem
|
tiny_removeSystem = tiny.removeSystem
|
||||||
|
|
||||||
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
||||||
-- all rmeove Systems and Entities
|
-- all removed Systems and Entities
|
||||||
function tiny.remove(world, ...)
|
function tiny.remove(world, ...)
|
||||||
local obj
|
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
obj = select(i, ...)
|
local obj = select(i, ...)
|
||||||
if obj then
|
if obj then
|
||||||
if isSystem(obj) then
|
if isSystem(obj) then
|
||||||
tiny_removeSystem(world, obj)
|
tiny_removeSystem(world, obj)
|
||||||
else -- Assume obj is an Entity
|
else -- Assume obj is an Entity
|
||||||
tiny_removeEntity(world, obj)
|
tiny_removeEntity(world, obj)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -454,68 +516,79 @@ tiny_remove = tiny.remove
|
|||||||
|
|
||||||
-- Adds and removes Systems that have been marked from the World.
|
-- Adds and removes Systems that have been marked from the World.
|
||||||
function tiny_manageSystems(world)
|
function tiny_manageSystems(world)
|
||||||
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
||||||
|
|
||||||
-- Early exit
|
-- Early exit
|
||||||
if #s2a == 0 and #s2r == 0 then
|
if #s2a == 0 and #s2r == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local entities = world.entities
|
world.systemsToAdd = {}
|
||||||
local systems = world.systems
|
world.systemsToRemove = {}
|
||||||
local system, index, filter
|
|
||||||
local entityList, entityIndices, entityIndex, onRemove, onAdd
|
|
||||||
|
|
||||||
-- Remove Systems
|
local worldEntityList = world.entityList
|
||||||
for i = 1, #s2r do
|
local systems = world.systems
|
||||||
system = s2r[i]
|
|
||||||
index = system.index
|
-- Remove Systems
|
||||||
if system.world == world then
|
for i = 1, #s2r do
|
||||||
onRemove = system.onRemove
|
local system = s2r[i]
|
||||||
if onRemove then
|
local index = system.index
|
||||||
entityList = system.entities
|
local onRemove = system.onRemove
|
||||||
for j = 1, #entityList do
|
if onRemove and not system.nocache then
|
||||||
onRemove(system, entityList[j])
|
local entityList = system.entities
|
||||||
end
|
for j = 1, #entityList do
|
||||||
end
|
onRemove(system, entityList[j])
|
||||||
tremove(systems, index)
|
|
||||||
for j = index, #systems do
|
|
||||||
systems[j].index = j
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
s2r[i] = nil
|
|
||||||
|
|
||||||
-- Clean up System
|
|
||||||
system.world = nil
|
|
||||||
system.entities = nil
|
|
||||||
system.indices = nil
|
|
||||||
system.index = nil
|
|
||||||
end
|
end
|
||||||
|
tremove(systems, index)
|
||||||
|
for j = index, #systems do
|
||||||
|
systems[j].index = j
|
||||||
|
end
|
||||||
|
local onRemoveFromWorld = system.onRemoveFromWorld
|
||||||
|
if onRemoveFromWorld then
|
||||||
|
onRemoveFromWorld(system, world)
|
||||||
|
end
|
||||||
|
s2r[i] = nil
|
||||||
|
|
||||||
-- Add Systems
|
-- Clean up System
|
||||||
for i = 1, #s2a do
|
system.world = nil
|
||||||
system = s2a[i]
|
system.entities = nil
|
||||||
if systems[system.index] ~= system then
|
system.indices = nil
|
||||||
entityList = {}
|
system.index = nil
|
||||||
entityIndices = {}
|
end
|
||||||
system.entities = entityList
|
|
||||||
system.indices = entityIndices
|
|
||||||
if system.active == nil then
|
|
||||||
system.active = true
|
|
||||||
end
|
|
||||||
system.modified = true
|
|
||||||
system.world = world
|
|
||||||
index = #systems + 1
|
|
||||||
system.index = index
|
|
||||||
systems[index] = system
|
|
||||||
|
|
||||||
-- Try to add Entities
|
-- Add Systems
|
||||||
onAdd = system.onAdd
|
for i = 1, #s2a do
|
||||||
filter = system.filter
|
local system = s2a[i]
|
||||||
|
if systems[system.index or 0] ~= system then
|
||||||
|
if not system.nocache then
|
||||||
|
system.entities = {}
|
||||||
|
system.indices = {}
|
||||||
|
end
|
||||||
|
if system.active == nil then
|
||||||
|
system.active = true
|
||||||
|
end
|
||||||
|
system.modified = true
|
||||||
|
system.world = world
|
||||||
|
local index = #systems + 1
|
||||||
|
system.index = index
|
||||||
|
systems[index] = system
|
||||||
|
local onAddToWorld = system.onAddToWorld
|
||||||
|
if onAddToWorld then
|
||||||
|
onAddToWorld(system, world)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Try to add Entities
|
||||||
|
if not system.nocache then
|
||||||
|
local entityList = system.entities
|
||||||
|
local entityIndices = system.indices
|
||||||
|
local onAdd = system.onAdd
|
||||||
|
local filter = system.filter
|
||||||
if filter then
|
if filter then
|
||||||
for entity in pairs(entities) do
|
for j = 1, #worldEntityList do
|
||||||
|
local entity = worldEntityList[j]
|
||||||
if filter(system, entity) then
|
if filter(system, entity) then
|
||||||
entityIndex = #entityList + 1
|
local entityIndex = #entityList + 1
|
||||||
entityList[entityIndex] = entity
|
entityList[entityIndex] = entity
|
||||||
entityIndices[entity] = entityIndex
|
entityIndices[entity] = entityIndex
|
||||||
if onAdd then
|
if onAdd then
|
||||||
@@ -525,82 +598,135 @@ function tiny_manageSystems(world)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
s2a[i] = nil
|
|
||||||
end
|
end
|
||||||
|
s2a[i] = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Adds and removes Entities that have been marked.
|
-- Adds, removes, and changes Entities that have been marked.
|
||||||
function tiny_manageEntities(world)
|
function tiny_manageEntities(world)
|
||||||
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
|
|
||||||
|
local e2a = world.entitiesToAdd
|
||||||
|
local e2r = world.entitiesToRemove
|
||||||
|
local e2c = world.entitiesToChange
|
||||||
|
|
||||||
-- Early exit
|
-- Early exit
|
||||||
if #e2a == 0 and #e2r == 0 then
|
if #e2a == 0 and #e2r == 0 and #e2c == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
world.entitiesToChange = {}
|
||||||
|
world.entitiesToAdd = {}
|
||||||
|
world.entitiesToRemove = {}
|
||||||
|
|
||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
|
local entityList = world.entityList
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local entityCount = world.entityCount
|
local entityCount = world.entityCount
|
||||||
local entity, system, index
|
|
||||||
local onRemove, onAdd, ses, seis, filter, tmpEntity
|
-- Change Entities
|
||||||
|
for i = 1, #e2c do
|
||||||
|
local entity = e2c[i]
|
||||||
|
if entities[entity] then
|
||||||
|
for j = 1, #systems do
|
||||||
|
local system = systems[j]
|
||||||
|
if not system.nocache then
|
||||||
|
local ses = system.entities
|
||||||
|
local seis = system.indices
|
||||||
|
local index = seis[entity]
|
||||||
|
local filter = system.filter
|
||||||
|
if filter and filter(system, entity) then
|
||||||
|
if not index then
|
||||||
|
system.modified = true
|
||||||
|
index = #ses + 1
|
||||||
|
ses[index] = entity
|
||||||
|
seis[entity] = index
|
||||||
|
local onAdd = system.onAdd
|
||||||
|
if onAdd then
|
||||||
|
onAdd(system, entity)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif index then
|
||||||
|
system.modified = true
|
||||||
|
local tmpEntity = ses[#ses]
|
||||||
|
ses[index] = tmpEntity
|
||||||
|
seis[tmpEntity] = index
|
||||||
|
seis[entity] = nil
|
||||||
|
ses[#ses] = nil
|
||||||
|
local onRemove = system.onRemove
|
||||||
|
if onRemove then
|
||||||
|
onRemove(system, entity)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
e2c[i] = nil
|
||||||
|
end
|
||||||
|
|
||||||
-- Remove Entities
|
-- Remove Entities
|
||||||
for i = 1, #e2r do
|
for i = 1, #e2r do
|
||||||
entity = e2r[i]
|
local entity = e2r[i]
|
||||||
if entities[entity] then
|
e2r[i] = nil
|
||||||
|
local listIndex = entities[entity]
|
||||||
|
if listIndex then
|
||||||
|
-- Remove Entity from world state
|
||||||
|
local lastEntity = entityList[#entityList]
|
||||||
|
entities[lastEntity] = listIndex
|
||||||
entities[entity] = nil
|
entities[entity] = nil
|
||||||
|
entityList[listIndex] = lastEntity
|
||||||
|
entityList[#entityList] = nil
|
||||||
entityCount = entityCount - 1
|
entityCount = entityCount - 1
|
||||||
|
-- Remove from cached systems
|
||||||
for j = 1, #systems do
|
for j = 1, #systems do
|
||||||
system = systems[j]
|
local system = systems[j]
|
||||||
ses = system.entities
|
if not system.nocache then
|
||||||
seis = system.indices
|
local ses = system.entities
|
||||||
index = seis[entity]
|
local seis = system.indices
|
||||||
|
local index = seis[entity]
|
||||||
if index then
|
if index then
|
||||||
system.modified = true
|
system.modified = true
|
||||||
tmpEntity = ses[#ses]
|
local tmpEntity = ses[#ses]
|
||||||
ses[index] = tmpEntity
|
ses[index] = tmpEntity
|
||||||
seis[tmpEntity] = index
|
seis[tmpEntity] = index
|
||||||
seis[entity] = nil
|
seis[entity] = nil
|
||||||
ses[#ses] = nil
|
ses[#ses] = nil
|
||||||
onRemove = system.onRemove
|
local onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
onRemove(system, entity)
|
onRemove(system, entity)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
e2r[i] = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add Entities
|
-- Add Entities
|
||||||
for i = 1, #e2a do
|
for i = 1, #e2a do
|
||||||
entity = e2a[i]
|
local entity = e2a[i]
|
||||||
if not entities[entity] then
|
if not entities[entity] then
|
||||||
entities[entity] = true
|
local listIndex = #entityList + 1
|
||||||
|
entities[entity] = listIndex
|
||||||
|
entityList[listIndex] = entity
|
||||||
entityCount = entityCount + 1
|
entityCount = entityCount + 1
|
||||||
|
|
||||||
for j = 1, #systems do
|
for j = 1, #systems do
|
||||||
system = systems[j]
|
local system = systems[j]
|
||||||
ses = system.entities
|
if not system.nocache then
|
||||||
seis = system.indices
|
local ses = system.entities
|
||||||
filter = system.filter
|
local seis = system.indices
|
||||||
if filter and filter(system, entity) then
|
local filter = system.filter
|
||||||
system.modified = true
|
if filter and filter(system, entity) then
|
||||||
index = #ses + 1
|
system.modified = true
|
||||||
ses[index] = entity
|
local index = #ses + 1
|
||||||
seis[entity] = index
|
ses[index] = entity
|
||||||
onAdd = system.onAdd
|
seis[entity] = index
|
||||||
if onAdd then
|
local onAdd = system.onAdd
|
||||||
onAdd(system, entity)
|
if onAdd then
|
||||||
|
onAdd(system, entity)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
e2a[i] = nil
|
e2a[i] = nil
|
||||||
end
|
end
|
||||||
@@ -609,6 +735,25 @@ function tiny_manageEntities(world)
|
|||||||
world.entityCount = entityCount
|
world.entityCount = entityCount
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Manages Entities and Systems marked for deletion or addition. Call this
|
||||||
|
-- before modifying Systems and Entities outside of a call to `tiny.update`.
|
||||||
|
-- Do not call this within a call to `tiny.update`.
|
||||||
|
function tiny.refresh(world)
|
||||||
|
tiny_manageSystems(world)
|
||||||
|
tiny_manageEntities(world)
|
||||||
|
local systems = world.systems
|
||||||
|
for i = 1, #systems do
|
||||||
|
local system = systems[i]
|
||||||
|
if system.active then
|
||||||
|
local onModify = system.onModify
|
||||||
|
if onModify and system.modified then
|
||||||
|
onModify(system, 0)
|
||||||
|
end
|
||||||
|
system.modified = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
|
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
|
||||||
-- which is a Filter that selects Systems from the World, and updates only those
|
-- which is a Filter that selects Systems from the World, and updates only those
|
||||||
-- Systems. If `filter` is not supplied, all Systems are updated. Put this
|
-- Systems. If `filter` is not supplied, all Systems are updated. Put this
|
||||||
@@ -619,23 +764,22 @@ function tiny.update(world, dt, filter)
|
|||||||
tiny_manageEntities(world)
|
tiny_manageEntities(world)
|
||||||
|
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system, update, interval, onModify
|
|
||||||
|
|
||||||
-- Iterate through Systems IN ORDER
|
-- Iterate through Systems IN ORDER
|
||||||
for i = 1, #systems do
|
for i = 1, #systems do
|
||||||
system = systems[i]
|
local system = systems[i]
|
||||||
if system.active and ((not filter) or filter(world, system)) then
|
if system.active and ((not filter) or filter(world, system)) then
|
||||||
|
|
||||||
-- Call the modify callback on Systems that have been modified.
|
-- Call the modify callback on Systems that have been modified.
|
||||||
onModify = system.onModify
|
local onModify = system.onModify
|
||||||
if onModify and system.modified then
|
if onModify and system.modified then
|
||||||
onModify(system, dt)
|
onModify(system, dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Update Systems that have an update method (most Systems)
|
-- Update Systems that have an update method (most Systems)
|
||||||
update = system.update
|
local update = system.update
|
||||||
if update then
|
if update then
|
||||||
interval = system.interval
|
local interval = system.interval
|
||||||
if interval then
|
if interval then
|
||||||
local bufferedTime = (system.bufferedTime or 0) + dt
|
local bufferedTime = (system.bufferedTime or 0) + dt
|
||||||
while bufferedTime >= interval do
|
while bufferedTime >= interval do
|
||||||
@@ -657,8 +801,9 @@ end
|
|||||||
|
|
||||||
--- Removes all Entities from the World.
|
--- Removes all Entities from the World.
|
||||||
function tiny.clearEntities(world)
|
function tiny.clearEntities(world)
|
||||||
for e in pairs(world.entities) do
|
local el = world.entityList
|
||||||
tiny_removeEntity(world, e)
|
for i = 1, #el do
|
||||||
|
tiny_removeEntity(world, el[i])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -693,6 +838,10 @@ function tiny.setSystemIndex(world, system, index)
|
|||||||
local oldIndex = system.index
|
local oldIndex = system.index
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
|
|
||||||
|
if index < 0 then
|
||||||
|
index = tiny.getSystemCount(world) + 1 + index
|
||||||
|
end
|
||||||
|
|
||||||
tremove(systems, oldIndex)
|
tremove(systems, oldIndex)
|
||||||
tinsert(systems, index, system)
|
tinsert(systems, index, system)
|
||||||
|
|
||||||
@@ -712,6 +861,7 @@ worldMetaTable = {
|
|||||||
remove = tiny.remove,
|
remove = tiny.remove,
|
||||||
removeEntity = tiny.removeEntity,
|
removeEntity = tiny.removeEntity,
|
||||||
removeSystem = tiny.removeSystem,
|
removeSystem = tiny.removeSystem,
|
||||||
|
refresh = tiny.refresh,
|
||||||
update = tiny.update,
|
update = tiny.update,
|
||||||
clearEntities = tiny.clearEntities,
|
clearEntities = tiny.clearEntities,
|
||||||
clearSystems = tiny.clearSystems,
|
clearSystems = tiny.clearSystems,
|
||||||
@@ -719,7 +869,10 @@ worldMetaTable = {
|
|||||||
getSystemCount = tiny.getSystemCount,
|
getSystemCount = tiny.getSystemCount,
|
||||||
getSystemIndex = tiny.getSystemIndex,
|
getSystemIndex = tiny.getSystemIndex,
|
||||||
setSystemIndex = tiny.setSystemIndex
|
setSystemIndex = tiny.setSystemIndex
|
||||||
}
|
},
|
||||||
|
__tostring = function(self)
|
||||||
|
return "<tiny-ecs_World>"
|
||||||
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
return tiny
|
return tiny
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ end
|
|||||||
|
|
||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
local s = gamestate.current()
|
local s = gamestate.current()
|
||||||
if s and s.restartOnSpace and love.keyboard.isDown(" ") then
|
if s and s.restartOnSpace and love.keyboard.isDown("space") then
|
||||||
local TransitionScreen = require "src.entities.TransitionScreen"
|
local TransitionScreen = require "src.entities.TransitionScreen"
|
||||||
world:add(TransitionScreen(true, Intro()))
|
world:add(TransitionScreen(true, Intro()))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ end
|
|||||||
|
|
||||||
function Intro:update(dt)
|
function Intro:update(dt)
|
||||||
self.time = self.time + dt
|
self.time = self.time + dt
|
||||||
if love.keyboard.isDown(" ") and self.time > 0.55 then
|
if love.keyboard.isDown("space") and self.time > 0.55 then
|
||||||
world:add(TransitionScreen(true, Level("assets/lvl1")))
|
world:add(TransitionScreen(true, Level("assets/lvl1")))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user