mirror of
https://github.com/Reuh/ubiquitousse.git
synced 2025-10-27 17:19:31 +00:00
ecs: add callbackFiltered and emitFiltered
This commit is contained in:
parent
bd28610ff4
commit
aa332a0adf
20 changed files with 697 additions and 568 deletions
40
ecs/commonsystems/children.can
Normal file
40
ecs/commonsystems/children.can
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
--- Children system.
|
||||
-- Allows to build a hierarchy between entities.
|
||||
--
|
||||
-- An entity's parent entity is stored in its `parent` component.
|
||||
-- An entity's children are stored in its `children` component (list of children entities).
|
||||
--
|
||||
-- You can set theses values before adding the entity to the world; when you add the entity it will add itself
|
||||
-- to its parent children list and add all its children to the world.
|
||||
--
|
||||
-- If you remove an entity from the world, it will also remove all its children from the world.
|
||||
|
||||
return {
|
||||
name = "children",
|
||||
filter = true,
|
||||
onAdd = :(e)
|
||||
if not e.children then e.children = {} end
|
||||
if e.parent then -- add to parent
|
||||
let parentchildren = e.parent.children
|
||||
table.insert(parentchildren, e)
|
||||
end
|
||||
for _, o in ipairs(e.children) do -- add predefined children
|
||||
o.parent = e
|
||||
@world:add(o)
|
||||
end
|
||||
end,
|
||||
onRemove = :(e)
|
||||
for i=#e.children, 1, -1 do -- remove children
|
||||
@world:remove(e.children[i])
|
||||
end
|
||||
if e.parent then -- remove from parent
|
||||
let parentchildren = e.parent.children
|
||||
for i=#parentchildren, 1, -1 do
|
||||
if parentchildren[i] == e then
|
||||
table.remove(parentchildren, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
33
ecs/commonsystems/children.lua
Normal file
33
ecs/commonsystems/children.lua
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
return { -- ./ecs/commonsystems/children.can:12
|
||||
["name"] = "children", -- ./ecs/commonsystems/children.can:13
|
||||
["filter"] = true, -- ./ecs/commonsystems/children.can:14
|
||||
["onAdd"] = function(self, e) -- ./ecs/commonsystems/children.can:15
|
||||
if not e["children"] then -- ./ecs/commonsystems/children.can:16
|
||||
e["children"] = {} -- ./ecs/commonsystems/children.can:16
|
||||
end -- ./ecs/commonsystems/children.can:16
|
||||
if e["parent"] then -- ./ecs/commonsystems/children.can:17
|
||||
local parentchildren -- ./ecs/commonsystems/children.can:18
|
||||
parentchildren = e["parent"]["children"] -- ./ecs/commonsystems/children.can:18
|
||||
table["insert"](parentchildren, e) -- ./ecs/commonsystems/children.can:19
|
||||
end -- ./ecs/commonsystems/children.can:19
|
||||
for _, o in ipairs(e["children"]) do -- ./ecs/commonsystems/children.can:21
|
||||
o["parent"] = e -- ./ecs/commonsystems/children.can:22
|
||||
self["world"]:add(o) -- ./ecs/commonsystems/children.can:23
|
||||
end -- ./ecs/commonsystems/children.can:23
|
||||
end, -- ./ecs/commonsystems/children.can:23
|
||||
["onRemove"] = function(self, e) -- ./ecs/commonsystems/children.can:26
|
||||
for i = # e["children"], 1, - 1 do -- ./ecs/commonsystems/children.can:27
|
||||
self["world"]:remove(e["children"][i]) -- ./ecs/commonsystems/children.can:28
|
||||
end -- ./ecs/commonsystems/children.can:28
|
||||
if e["parent"] then -- ./ecs/commonsystems/children.can:30
|
||||
local parentchildren -- ./ecs/commonsystems/children.can:31
|
||||
parentchildren = e["parent"]["children"] -- ./ecs/commonsystems/children.can:31
|
||||
for i = # parentchildren, 1, - 1 do -- ./ecs/commonsystems/children.can:32
|
||||
if parentchildren[i] == e then -- ./ecs/commonsystems/children.can:33
|
||||
table["remove"](parentchildren, i) -- ./ecs/commonsystems/children.can:34
|
||||
break -- ./ecs/commonsystems/children.can:35
|
||||
end -- ./ecs/commonsystems/children.can:35
|
||||
end -- ./ecs/commonsystems/children.can:35
|
||||
end -- ./ecs/commonsystems/children.can:35
|
||||
end -- ./ecs/commonsystems/children.can:35
|
||||
} -- ./ecs/commonsystems/children.can:35
|
||||
35
ecs/commonsystems/timer.can
Normal file
35
ecs/commonsystems/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 = :(e, 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
|
||||
}
|
||||
23
ecs/commonsystems/timer.lua
Normal file
23
ecs/commonsystems/timer.lua
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
local timer -- ./ecs/commonsystems/timer.can:4
|
||||
timer = require((...):match("^(.-)ecs%.timer") .. "scene") -- ./ecs/commonsystems/timer.can:4
|
||||
return { -- ./ecs/commonsystems/timer.can:6
|
||||
["name"] = "timer", -- ./ecs/commonsystems/timer.can:7
|
||||
["filter"] = "timer", -- ./ecs/commonsystems/timer.can:8
|
||||
["default"] = {}, -- ./ecs/commonsystems/timer.can:9
|
||||
["process"] = function(self, e, t, dt) -- ./ecs/commonsystems/timer.can:12
|
||||
t:update(dt) -- ./ecs/commonsystems/timer.can:13
|
||||
if t:dead() then -- ./ecs/commonsystems/timer.can:14
|
||||
self["world"]:remove(t["entity"]) -- ./ecs/commonsystems/timer.can:15
|
||||
end -- ./ecs/commonsystems/timer.can:15
|
||||
end, -- ./ecs/commonsystems/timer.can:15
|
||||
["run"] = function(self, func) -- ./ecs/commonsystems/timer.can:20
|
||||
local t = timer["run"](func) -- ./ecs/commonsystems/timer.can:21
|
||||
self["world"]:add({ ["timer"] = t }) -- ./ecs/commonsystems/timer.can:23
|
||||
return t -- ./ecs/commonsystems/timer.can:25
|
||||
end, -- ./ecs/commonsystems/timer.can:25
|
||||
["tween"] = function(self, duration, tbl, to, method) -- ./ecs/commonsystems/timer.can:28
|
||||
local t = timer["tween"](duration, tbl, to, method) -- ./ecs/commonsystems/timer.can:29
|
||||
self["world"]:add({ ["timer"] = t }) -- ./ecs/commonsystems/timer.can:31
|
||||
return t -- ./ecs/commonsystems/timer.can:33
|
||||
end -- ./ecs/commonsystems/timer.can:33
|
||||
} -- ./ecs/commonsystems/timer.can:33
|
||||
Loading…
Add table
Add a link
Reference in a new issue