diff options
| author | ne_mene <[email protected]> | 2026-03-09 17:33:25 +0100 |
|---|---|---|
| committer | ne_mene <[email protected]> | 2026-03-09 17:33:25 +0100 |
| commit | ac2a4291182769510fdc0c877a52acaa0fb0ce4a (patch) | |
| tree | 8a7e174514d23e4ac02e25e137fa72938dc5754d | |
| parent | b744faa2e42ed37459fe3edb69c1149146233e5b (diff) | |
deinit func for comps
| -rw-r--r-- | TODO.txt | 10 | ||||
| -rw-r--r-- | src/ecs.lua | 29 |
2 files changed, 18 insertions, 21 deletions
diff --git a/TODO.txt b/TODO.txt deleted file mode 100644 index a6dd4cb..0000000 --- a/TODO.txt +++ /dev/null @@ -1,10 +0,0 @@ -HIGH PRIORITY: -- Event system -- Player movement - -LOW PRIORITY: -- Fix mouse position - -DONE: -- ECS -- Input system diff --git a/src/ecs.lua b/src/ecs.lua index 1df5421..9f995b6 100644 --- a/src/ecs.lua +++ b/src/ecs.lua @@ -1,6 +1,7 @@ local current_scene = nil -local compfuncs = {} +local comp_inits = {} +local comp_deinits = {} function TAGCOMP(_) end @@ -25,7 +26,7 @@ function new_scene() comp_entq = {}, comp_removeq_size = 0, } - for comp, _ in pairs(compfuncs) do + for comp, _ in pairs(comp_inits) do newscene.compmask[comp] = { sparse = {}, dense = {}, @@ -43,15 +44,16 @@ function get_current_scene() return current_scene end -function register_comp(name, func) - assert(compfuncs[name] == nil, "Component '"..name.."' is already registered.") +function register_comp(name, init, deinit) + assert(comp_inits[name] == nil, "Component '"..name.."' is already registered.") - compfuncs[name] = func + comp_inits[name] = init + comp_deinits[name] = deinit end function add_comp(ent, comp, ...) assert(current_scene, "No scene up.") - assert(compfuncs[comp], "Unknown component '"..tostring(comp).."'") + assert(comp_inits[comp], "Unknown component '"..tostring(comp).."'") assert(current_scene.entities[ent.id], "Entity "..tostring(ent.id).." doesn't exist.") local mask = current_scene.compmask[comp] @@ -61,14 +63,19 @@ function add_comp(ent, comp, ...) mask.sparse[ent.id] = mask.size mask.dense[mask.size] = ent.id - compfuncs[comp](current_scene.entities[ent.id], ...) + comp_inits[comp](current_scene.entities[ent.id], ...) end local function remove_comp(ent, comp) assert(current_scene, "No scene up.") - assert(compfuncs[comp], "Unknown component '"..tostring(comp).."'") + assert(comp_inits[comp], "Unknown component '"..tostring(comp).."'") assert(current_scene.entities[ent.id], "Entity "..tostring(ent.id).." doesn't exist.") + local deinit = comp_deinits[comp] + if deinit then + deinit(ent) + end + local mask = current_scene.compmask[comp] assert(mask.sparse[ent.id], "Entity "..tostring(ent.id).." does not have component of type '"..comp.."'.") @@ -85,7 +92,7 @@ end function run_system(comp, func, ...) assert(current_scene, "No scene up.") - assert(compfuncs[comp], "Unknown component '"..tostring(comp).."'") + assert(comp_inits[comp], "Unknown component '"..tostring(comp).."'") for _, entid in ipairs(current_scene.compmask[comp].dense) do func(current_scene.entities[entid], ...) @@ -106,7 +113,7 @@ end function has_comp(ent, comp) assert(current_scene, "No scene up.") - assert(compfuncs[comp], "Unknown component '"..tostring(comp).."'") + assert(comp_inits[comp], "Unknown component '"..tostring(comp).."'") assert(current_scene.entities[ent.id], "Entity "..tostring(ent.id).." doesn't exist.") return current_scene.compmask[comp].sparse[ent.id] ~= nil @@ -114,7 +121,7 @@ end function queue_remove_comp(ent, comp) assert(current_scene, "No scene up.") - assert(compfuncs[comp], "Unknown component '"..tostring(comp).."'") + assert(comp_inits[comp], "Unknown component '"..tostring(comp).."'") if not has_comp(ent, comp) then return end |
