diff options
Diffstat (limited to 'honey/ecs.lua')
-rw-r--r-- | honey/ecs.lua | 68 |
1 files changed, 39 insertions, 29 deletions
diff --git a/honey/ecs.lua b/honey/ecs.lua index 3bdf850..4eee343 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -98,25 +98,26 @@ function Filter.checkNOR(self, entity) end ---===== worlds =====-- +--===== levels =====-- -World = {} -World.__index = World +Level = {} +Level.__index = Level -function World.new(_) +function Level.new(_) local self = {} self.systems = {} self.entities = {} - setmetatable(self, World) + self.nextId = 1 + setmetatable(self, Level) return self end -setmetatable(World, {__call=World.new}) +setmetatable(Level, {__call=Level.new}) local function systemLt(a, b) return (a.priority or 50) < (b.priority or 50) end -function World.addSystem(self, system) +function Level.addSystem(self, system) assert(system.update, "systems must have an 'update' key") assert(system.filter, "systems must have a 'filter' key") system.entities = {} @@ -125,67 +126,76 @@ function World.addSystem(self, system) end -local function addEntityToSystem(system, entity) +local function addEntityToSystem(system, id, entity) -- check if entity is already present - if system.entities[entity] then return end + if system.entities[id] then return end if system.onAddEntity then - system.onAddEntity(entity) + system.onAddEntity(id, entity) end - system.entities[entity] = true + system.entities[id] = true end -local function removeEntityFromSystem(system, entity) +local function removeEntityFromSystem(system, id, entity) -- check if entity is already absent - if not system.entities[entity] then return end + if not system.entities[id] then return end if system.onRemoveEntity then - system.onRemoveEntity(entity) + system.onRemoveEntity(id, entity) end - system.entities[entity] = nil + system.entities[id] = nil end -function World.addEntity(self, entity) - self.entities[entity] = true +function Level.addEntity(self, entity) + table.insert(self.entities, entity) + local id = self.nextId + self.nextId = id + 1 + for _, system in ipairs(self.systems) do if system.filter(entity) then - addEntityToSystem(system, entity) + addEntityToSystem(system, id, entity) end end + + return id end -function World.reconfigureEntity(self, entity) +function Level.reconfigureEntity(self, id) + local entity = self.entities[id] + for _, system in ipairs(self.systems) do if system.filter(entity) then - addEntityToSystem(system, entity) + addEntityToSystem(system, id, entity) else - removeEntityFromSystem(system, entity) + removeEntityFromSystem(system, id, entity) end end end -function World.removeEntity(self, entity) - self.entities[entity] = nil +function Level.removeEntity(self, id) + local entity = self.entities[id] for _, system in ipairs(self.systems) do - removeEntityFromSystem(system, entity) + removeEntityFromSystem(system, id, entity) end + self.entities[id] = nil end -function World.reconfigureAllEntities(self) - for entity in pairs(self.entities) do - self:reconfigureEntity(entity) +function Level.reconfigureAllEntities(self) + for id in ipairs(self.entities) do + self:reconfigureEntity(id) end end -function World.update(self, dt) +function Level.update(self, dt) for _, system in ipairs(self.systems) do - for entity in pairs(system.entities) do + for id in pairs(system.entities) do + local entity = self.entities[id] system.update(entity, dt) end end |