mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 20:33:54 -04:00
Initial commit
This commit is contained in:
commit
58549d6b0a
14 changed files with 665 additions and 0 deletions
86
fluid/instance.lua
Normal file
86
fluid/instance.lua
Normal file
|
@ -0,0 +1,86 @@
|
|||
local PATH = (...):gsub('%.[^%.]+$', '')
|
||||
|
||||
local Pool = require(PATH..".pool")
|
||||
local EventManager = require(PATH..".eventManager")
|
||||
|
||||
local Instance = {}
|
||||
Instance.__index = Instance
|
||||
|
||||
function Instance.new()
|
||||
local instance = setmetatable({
|
||||
entities = Pool(),
|
||||
eventManager = EventManager(),
|
||||
|
||||
systems = {},
|
||||
namedSystems = {},
|
||||
}, Instance)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
function Instance:addEntity(e)
|
||||
e.instance = self
|
||||
self.entities:add(e)
|
||||
self:checkEntity(e)
|
||||
end
|
||||
|
||||
function Instance:checkEntity(e)
|
||||
for _, system in ipairs(self.systems) do
|
||||
if system:entityUpdated(e) then
|
||||
e.systems[#e.systems + 1] = system
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Instance:destroyEntity(e)
|
||||
self.entities:remove(e)
|
||||
|
||||
for _, system in ipairs(e.systems) do
|
||||
system:remove(e)
|
||||
end
|
||||
end
|
||||
|
||||
function Instance:addSystem(system, eventName)
|
||||
if not self.namedSystems[system] then
|
||||
self.systems[#self.systems + 1] = system
|
||||
self.namedSystems[system] = system
|
||||
end
|
||||
|
||||
self.eventManager:register(eventName, system)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function Instance:removeSystem(system)
|
||||
for index, other in ipairs(self.systems) do
|
||||
if system == other then
|
||||
table.remove(self.systems, index)
|
||||
end
|
||||
end
|
||||
|
||||
self.namedSystems[system] = nil
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function Instance:emit(...)
|
||||
self.eventManager:emit(...)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function Instance:update(dt)
|
||||
self:emit("update", dt)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function Instance:draw()
|
||||
self:emit("draw")
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
return setmetatable(Instance, {
|
||||
__call = function(_, ...) return Instance.new(...) end,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue