mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 12:24:11 -04:00
Cache added and removed components
This commit is contained in:
parent
bb508ee947
commit
ecb3c2db7e
4 changed files with 67 additions and 18 deletions
13
main.lua
13
main.lua
|
@ -35,11 +35,12 @@ end)
|
||||||
|
|
||||||
local test_system = System("test_system", {Components.test_comp_1})
|
local test_system = System("test_system", {Components.test_comp_1})
|
||||||
|
|
||||||
local function onEntityAdded(e) -- luacheck: ignore
|
local function onEntityAdded(pool, e) -- luacheck: ignore
|
||||||
print("Added")
|
local test_comp = e:get(Components.test_comp_1)
|
||||||
|
print(test_comp.x)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onEntityRemoved(e) -- luacheck: ignore
|
local function onEntityRemoved(pool, e) -- luacheck: ignore
|
||||||
print("Removed")
|
print("Removed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,7 +65,11 @@ end
|
||||||
local world = World("testWorld")
|
local world = World("testWorld")
|
||||||
|
|
||||||
local entity = Entity()
|
local entity = Entity()
|
||||||
entity:give(Components.test_comp_1, 100, 100)
|
entity
|
||||||
|
:give(Components.test_comp_1, 100, 100)
|
||||||
|
:remove(Components.test_comp_1)
|
||||||
|
:give(Components.test_comp_1, 200, 100)
|
||||||
|
|
||||||
|
|
||||||
Worlds.testWorld:addEntity(entity)
|
Worlds.testWorld:addEntity(entity)
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,10 @@ end
|
||||||
-- @return A new initialized Bag
|
-- @return A new initialized Bag
|
||||||
function Component:__initialize(...)
|
function Component:__initialize(...)
|
||||||
if self.__populate then
|
if self.__populate then
|
||||||
local bag = setmetatable({}, self)
|
local bag = setmetatable({
|
||||||
|
__baseComponent = self,
|
||||||
|
}, self)
|
||||||
|
|
||||||
self.__populate(bag, ...)
|
self.__populate(bag, ...)
|
||||||
|
|
||||||
return bag
|
return bag
|
||||||
|
|
|
@ -13,9 +13,13 @@ function Entity.new()
|
||||||
local e = setmetatable({
|
local e = setmetatable({
|
||||||
world = nil,
|
world = nil,
|
||||||
|
|
||||||
|
__addedComponents = {},
|
||||||
|
__removedComponents = {},
|
||||||
|
__operations = {},
|
||||||
|
|
||||||
__components = {},
|
__components = {},
|
||||||
|
|
||||||
__isDirty = true,
|
__isDirty = false,
|
||||||
__wasAdded = false,
|
__wasAdded = false,
|
||||||
__wasRemoved = false,
|
__wasRemoved = false,
|
||||||
|
|
||||||
|
@ -25,18 +29,30 @@ function Entity.new()
|
||||||
return e
|
return e
|
||||||
end
|
end
|
||||||
|
|
||||||
local function give(e, baseComponent, ...)
|
local function giveOperation(e, component)
|
||||||
local component = baseComponent:__initialize(...)
|
local baseComponent = component.__baseComponent
|
||||||
|
|
||||||
e[baseComponent] = component
|
e[baseComponent] = component
|
||||||
e.__components[baseComponent] = component
|
e.__components[baseComponent] = component
|
||||||
|
end
|
||||||
|
|
||||||
|
local function removeOperation(e, baseComponent)
|
||||||
|
e[baseComponent] = nil
|
||||||
|
e.__components[baseComponent] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local function give(e, baseComponent, ...)
|
||||||
|
local component = baseComponent:__initialize(...)
|
||||||
|
|
||||||
|
e.__addedComponents[#e.__addedComponents + 1] = component
|
||||||
|
e.__operations[#e.__operations + 1] = giveOperation
|
||||||
|
|
||||||
e.__isDirty = true
|
e.__isDirty = true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function remove(e, baseComponent)
|
local function remove(e, baseComponent)
|
||||||
e[baseComponent] = nil
|
e.__removedComponents[#e.__removedComponents + 1] = baseComponent
|
||||||
e.__components[baseComponent] = nil
|
e.__operations[#e.__operations + 1] = removeOperation
|
||||||
|
|
||||||
e.__isDirty = true
|
e.__isDirty = true
|
||||||
end
|
end
|
||||||
|
@ -92,6 +108,26 @@ function Entity:assemble(assemblage, ...)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Entity:flush()
|
||||||
|
local addi, removei = 1, 1
|
||||||
|
|
||||||
|
for i = 1, #self.__operations do
|
||||||
|
local operation = self.__operations[i]
|
||||||
|
|
||||||
|
if (operation == giveOperation) then
|
||||||
|
operation(self, self.__addedComponents[addi])
|
||||||
|
self.__addedComponents[addi] = nil
|
||||||
|
addi = addi + 1
|
||||||
|
elseif (operation == removeOperation) then
|
||||||
|
operation(self, self.__removedComponents[removei])
|
||||||
|
self.__removedComponents[removei] = nil
|
||||||
|
removei = removei + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
self.__operations[i] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Destroys the Entity.
|
--- Destroys the Entity.
|
||||||
-- @return self
|
-- @return self
|
||||||
function Entity:destroy()
|
function Entity:destroy()
|
||||||
|
|
|
@ -75,9 +75,20 @@ function World:flush()
|
||||||
for i = 1, self.entities.size do
|
for i = 1, self.entities.size do
|
||||||
e = self.entities:get(i)
|
e = self.entities:get(i)
|
||||||
|
|
||||||
|
if e.__isDirty then
|
||||||
|
e:flush()
|
||||||
|
|
||||||
|
if (not e.__wasAdded) then -- The __wasAdded check below will handle this instead
|
||||||
|
for j = 1, self.systems.size do
|
||||||
|
self.systems:get(j):__evaluate(e)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
e.__isDirty = false
|
||||||
|
end
|
||||||
|
|
||||||
if e.__wasAdded then
|
if e.__wasAdded then
|
||||||
e.__wasAdded = false
|
e.__wasAdded = false
|
||||||
e.__isDirty = false
|
|
||||||
|
|
||||||
for j = 1, self.systems.size do
|
for j = 1, self.systems.size do
|
||||||
self.systems:get(j):__evaluate(e)
|
self.systems:get(j):__evaluate(e)
|
||||||
|
@ -95,14 +106,8 @@ function World:flush()
|
||||||
end
|
end
|
||||||
|
|
||||||
e.__wasRemoved = false
|
e.__wasRemoved = false
|
||||||
end
|
|
||||||
|
|
||||||
if e.__isDirty then
|
self:onEntityRemoved(e)
|
||||||
for j = 1, self.systems.size do
|
|
||||||
self.systems:get(j):__evaluate(e)
|
|
||||||
end
|
|
||||||
|
|
||||||
e.__isDirty = false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue