1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 17:19:31 +00:00

Define uqt.ecs callback order

This commit is contained in:
Étienne Fildadut 2019-12-29 20:33:29 +01:00
parent a1801679b6
commit 6dc939bd16
2 changed files with 60 additions and 25 deletions

View file

@ -15,6 +15,7 @@ let recDestroySystems = (system)
for i=#system.systems, 1, -1 do for i=#system.systems, 1, -1 do
let s = system.systems[i] let s = system.systems[i]
recDestroySystems(s) recDestroySystems(s)
s:onDestroy()
system.systems[i] = nil system.systems[i] = nil
if s.name then if s.name then
system.world.s[s.name] = nil system.world.s[s.name] = nil
@ -69,17 +70,21 @@ let system_mt = {
onAdd = :(e) end, onAdd = :(e) end,
--- Called when removing an entity from the system. --- Called when removing an entity from the system.
onRemove = :(e) end, onRemove = :(e) end,
--- Called when the system is instancied, before any call to :onnAddToWorld (including other systems in the world).
onInstance = :() end,
--- Called when the system is added to a world. --- Called when the system is added to a world.
onAddToWorld = :(world) end, onAddToWorld = :(world) end,
--- Called when the system is removed from a world (i.e., the world is destroyed). --- Called when the system is removed from a world (i.e., the world is destroyed).
onRemoveFromWorld = :(world) end, onRemoveFromWorld = :(world) end,
--- Called when the world is destroyed, after every call to :onRemoveFromWorld (including other systems in the world).
onDestroy = :() end,
--- Called when updating the system. --- Called when updating the system.
onUpdate = :(dt) end, onUpdate = :(dt) end,
--- Called when drawing the system. --- Called when drawing the system.
onDraw = :() end, onDraw = :() end,
--- Called when updating the system, for every entity the system contains. --- Called when updating the system, for every entity the system contains. Called after :onUpdate was called on the system.
process = :(e, dt) end, process = :(e, dt) end,
--- Called when drawing the system, for every entity the system contains. --- Called when drawing the system, for every entity the system contains. Called after :onDraw was called on the system.
render = :(e) end, render = :(e) end,
--- If set, the system will only update every interval seconds. --- If set, the system will only update every interval seconds.
@ -108,6 +113,7 @@ let system_mt = {
--- Methods --- --- Methods ---
--- Add entities to the system and its subsystems. --- Add entities to the system and its subsystems.
-- Entities are added to subsystems after they were succesfully added to their parent system.
-- If this is called on a subsystem instead of the world, be warned that this will bypass all the parent's systems filters. -- If this is called on a subsystem instead of the world, be warned that this will bypass all the parent's systems filters.
-- Since :remove will not search for entities in systems where they should have been filtered out, the added entities will not be removed -- Since :remove will not search for entities in systems where they should have been filtered out, the added entities will not be removed
-- when calling :remove on a parent system or the world. The entity can only be removed by calling :remove on the system :add was called on. -- when calling :remove on a parent system or the world. The entity can only be removed by calling :remove on the system :add was called on.
@ -130,11 +136,11 @@ let system_mt = {
entity[2] = { e, nil } entity[2] = { e, nil }
end end
end end
@entityCount += 1
@onAdd(e)
for _, s in ipairs(@systems) do for _, s in ipairs(@systems) do
s:add(e) s:add(e)
end end
@entityCount += 1
@onAdd(e)
end end
if ... then if ... then
return e, @add(...) return e, @add(...)
@ -143,6 +149,7 @@ let system_mt = {
end end
end, end,
--- Remove entities to the system and its subsystems. --- Remove entities to the system and its subsystems.
-- Entities are removed from subsystems after they were succesfully removed from their parent system.
-- If you intend to call this on a subsystem instead of the world, please read the warning in :add. -- If you intend to call this on a subsystem instead of the world, please read the warning in :add.
remove = :(e, ...) remove = :(e, ...)
if e ~= nil and @filter(e) then if e ~= nil and @filter(e) then
@ -191,6 +198,7 @@ let system_mt = {
end end
end, end,
--- Try to update the system and its subsystems. Should be called on every game update. --- Try to update the system and its subsystems. Should be called on every game update.
-- Subsystems are updated after their parent system.
update = :(dt) update = :(dt)
if @active then if @active then
if @interval then if @interval then
@ -199,32 +207,33 @@ let system_mt = {
return return
end end
end end
for _, s in ipairs(@systems) do @onUpdate(dt)
s:update(dt)
end
if @process ~= system_mt.process then if @process ~= system_mt.process then
for e in @iter() do for e in @iter() do
@process(e, dt) @process(e, dt)
end end
end end
@onUpdate(dt) for _, s in ipairs(@systems) do
s:update(dt)
end
if @interval then if @interval then
@_waited = 0 @_waited = 0
end end
end end
end, end,
--- Try to draw the system and its subsystems. Should be called on every game draw. --- Try to draw the system and its subsystems. Should be called on every game draw.
-- Subsystems are drawn after their parent system.
draw = :() draw = :()
if @visible then if @visible then
for _, s in ipairs(@systems) do @onDraw()
s:draw()
end
if @render ~= system_mt.render then if @render ~= system_mt.render then
for e in @iter() do for e in @iter() do
@render(e) @render(e)
end end
end end
@onDraw() for _, s in ipairs(@systems) do
s:draw()
end
end end
end, end,
--- Remove all the entities and subsystems in this system. --- Remove all the entities and subsystems in this system.
@ -257,6 +266,7 @@ let recInstanciateSystems = (world, systems)
if s.name then if s.name then
world.s[s.name] = system world.s[s.name] = system
end end
system:onInstance()
end end
return t return t
end end
@ -268,6 +278,10 @@ let recCallOnAddToWorld = (world, systems)
end end
end end
--- Self descriptive
let alwaysTrue = () return true end
let alwaysFalse = () return true end
--- ECS module. --- ECS module.
let ecs = { let ecs = {
--- Create and returns a world system based on a list of systems. --- Create and returns a world system based on a list of systems.
@ -275,7 +289,7 @@ let ecs = {
-- @impl ubiquitousse -- @impl ubiquitousse
world = (...) world = (...)
let world = setmetatable({ let world = setmetatable({
filter = (e) return true end, filter = ecs.all(),
s = {} s = {}
}, { __index = system_mt }) }, { __index = system_mt })
world.world = world world.world = world
@ -287,28 +301,36 @@ let ecs = {
--- Returns a filter that returns true if, for every argument, a field with the same name exists in the entity. --- Returns a filter that returns true if, for every argument, a field with the same name exists in the entity.
-- @impl ubiquitousse -- @impl ubiquitousse
all = (...) all = (...)
let l = {...} if ... then
return function(s, e) let l = {...}
for _, k in ipairs(l) do return function(s, e)
if e[k] == nil then for _, k in ipairs(l) do
return false if e[k] == nil then
return false
end
end end
return true
end end
return true else
return alwaysTrue
end end
end, end,
--- Returns a filter that returns true if one of the arguments if the name of a field in the entity. --- Returns a filter that returns true if one of the arguments if the name of a field in the entity.
-- @impl ubiquitousse -- @impl ubiquitousse
any = (...) any = (...)
let l = {...} if ... then
return function(s, e) let l = {...}
for _, k in ipairs(l) do return function(s, e)
if e[k] ~= nil then for _, k in ipairs(l) do
return true if e[k] ~= nil then
return true
end
end end
return false
end end
return false else
return alwaysFalse
end end
end, end,

View file

@ -157,6 +157,19 @@ util = {
return setmetatable(r, getmetatable(t)) return setmetatable(r, getmetatable(t))
end, end,
--- Returns a table which, when indexed, will require() the module with the index as a name (and a optional prefix).
-- @tparam string[opt=""] string that will prefix modules names when calling require()
-- @treturn table the requirer table
requirer = function(prefix)
prefix = prefix and tostring(prefix) or ""
return setmetatable({}, {
__index = function(self, key)
self[key] = require(prefix..tostring(key))
return self[key]
end
})
end,
----------------------- -----------------------
--- Random and UUID --- --- Random and UUID ---
----------------------- -----------------------