mirror of
https://github.com/Reuh/ubiquitousse.git
synced 2025-10-27 17:19:31 +00:00
Add callback, children, timer example systems
This commit is contained in:
parent
6ee98c097f
commit
9f4c03a136
3 changed files with 119 additions and 0 deletions
35
ecs/callback.can
Normal file
35
ecs/callback.can
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
--- Callback system
|
||||
-- Allow to call callbacks defined in other systems in entity methods.
|
||||
-- Example:
|
||||
-- entity.callback:onMove(...) -- call onAdd(system, entitySystemTable, ...) on every system the entity belong to (which has this callback)
|
||||
-- Can be also used for onAdd, etc.
|
||||
|
||||
--- Recursively get a list of systems with a certain method.
|
||||
let recGetSystemsWithMethod = (method, systems, l={})
|
||||
for _, s in ipairs(systems) do
|
||||
if s[method] then
|
||||
table.insert(l, s)
|
||||
end
|
||||
recGetSystemsWithMethod(method, s.systems, l)
|
||||
end
|
||||
return l
|
||||
end
|
||||
|
||||
return {
|
||||
name = "callback",
|
||||
filter = true,
|
||||
methods = {
|
||||
__index = :(c, k)
|
||||
let s = recGetSystemsWithMethod(k, {@world})
|
||||
@methods[k] = :(c, ...)
|
||||
let e = c.entity
|
||||
for _, sys in ipairs(s) do
|
||||
if sys._previous[e] then
|
||||
sys[k](sys, e[sys.name], ...)
|
||||
end
|
||||
end
|
||||
end
|
||||
return @_methods_mt[k]
|
||||
end
|
||||
}
|
||||
}
|
||||
49
ecs/children.can
Normal file
49
ecs/children.can
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
--- Children system
|
||||
-- Allow to build a hierarchy between entities.
|
||||
-- Children are stored directly in the .children entity table: they are added when their parent is added, and removed when it is removed from the world.
|
||||
|
||||
return {
|
||||
name = "children",
|
||||
filter = true,
|
||||
default = {
|
||||
parent = nil, -- reference to parent entity, if any
|
||||
-- ... list of children
|
||||
},
|
||||
methods = {
|
||||
--- Add a new entity to the world, using this entity as a parent.
|
||||
add = :(c, o)
|
||||
if not o.children then
|
||||
o.children = {}
|
||||
end
|
||||
o.children.parent = c.entity
|
||||
table.insert(c, o)
|
||||
@world:add(o)
|
||||
end,
|
||||
--- Remove an entity from the world and from this entity's children.
|
||||
remove = :(c, o)
|
||||
@world:remove(o)
|
||||
for i=#c, 1, -1 do
|
||||
if c[i] == o then
|
||||
table.remove(c, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
o.children.parent = nil
|
||||
end
|
||||
},
|
||||
onAdd = :(c)
|
||||
for _, o in ipairs(c) do
|
||||
o.children.parent = c.entity
|
||||
@world:add(o)
|
||||
end
|
||||
end,
|
||||
onRemove = :(c)
|
||||
for _, o in ipairs(c) do
|
||||
@world:remove(o)
|
||||
o.children.parent = nil
|
||||
end
|
||||
if c.parent then
|
||||
c.parent.children:remove(c.entity)
|
||||
end
|
||||
end
|
||||
}
|
||||
35
ecs/timer.can
Normal file
35
ecs/timer.can
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
--- Timer system
|
||||
-- Handles ubiquitousse timers.
|
||||
|
||||
let timer = require((...):match("^(.-)ecs%.timer").."scene")
|
||||
|
||||
return {
|
||||
name = "timer",
|
||||
filter = "timer",
|
||||
default = {
|
||||
-- timer object
|
||||
},
|
||||
process = :(t, dt)
|
||||
t:update(dt)
|
||||
if t:dead() then
|
||||
@world:remove(t.entity)
|
||||
end
|
||||
end,
|
||||
--- System methods ---
|
||||
--- Add a new timer
|
||||
run = :(func)
|
||||
local t = timer.run(func)
|
||||
@world:add {
|
||||
timer = t
|
||||
}
|
||||
return t
|
||||
end,
|
||||
--- Add a new tween
|
||||
tween = :(duration, tbl, to, method)
|
||||
local t = timer.tween(duration, tbl, to, method)
|
||||
@world:add {
|
||||
timer = t
|
||||
}
|
||||
return t
|
||||
end
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue