Add serialization and deserialization functions to component, entity, world

This commit is contained in:
Tjakka5 2020-01-04 13:26:26 +01:00
parent c217183cb9
commit 6cd66e6737
6 changed files with 214 additions and 33 deletions

View file

@ -5,7 +5,8 @@
local PATH = (...):gsub('%.[^%.]+$', '')
local Type = require(PATH..".type")
local Components = require(PATH..".components")
local Type = require(PATH..".type")
local Entity = {}
Entity.__mt = {
@ -173,6 +174,41 @@ function Entity:getWorld()
return self.__world
end
function Entity:serialize()
local data = {}
for _, component in pairs(self.__components) do
if component.__name then
local componentData = component:serialize()
componentData.__name = component.__name
data[#data + 1] = componentData
end
end
return data
end
function Entity:deserialize(data)
for i = 1, #data do
local componentData = data[i]
if (not Components[componentData.__name]) then
error("bad argument #1 to 'Entity:deserialize' (ComponentClass "..type(componentData.__name).." wasn't yet loaded)") -- luacheck: ignore
end
local componentClass = Components[componentData.__name]
local component = componentClass:__new()
component:deserialize(componentData)
self[componentClass] = component
self.__components[componentClass] = component
self:__dirty()
end
end
return setmetatable(Entity, {
__call = function(_, ...)
return Entity.new(...)