Allow named worlds

This commit is contained in:
Tjakka5 2019-12-19 20:14:21 +01:00
parent d4efca976c
commit d0e227485e
6 changed files with 50 additions and 7 deletions

View file

@ -46,7 +46,11 @@ function test_system:init()
end end
function test_system:update(dt) -- luacheck: ignore function test_system:update(dt) -- luacheck: ignore
--print(#self.pool) --[=[
for _, v in ipairs(self.pool) do
print(v)
end
]=]
end end
function test_system:update2(dt) -- luacheck: ignore function test_system:update2(dt) -- luacheck: ignore

View file

@ -38,7 +38,7 @@ 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.__mt) local bag = setmetatable({}, self)
self.__populate(bag, ...) self.__populate(bag, ...)
return bag return bag

View file

@ -14,6 +14,7 @@ System.mt = {
__world = world, __world = world,
__isSystem = true, __isSystem = true,
__isBaseSystem = false, -- Overwrite value from baseSystem
}, baseSystem) }, baseSystem)
for _, filter in pairs(baseSystem.__filter) do for _, filter in pairs(baseSystem.__filter) do

View file

@ -18,6 +18,10 @@ function Type.isSystem(t)
return type(t) == "table" and t.__isSystem or false return type(t) == "table" and t.__isSystem or false
end end
function Type.isWorld(t)
return type(t) == "table" and t.__isWorld or false
end
function Type.isAssemblage(t) function Type.isAssemblage(t)
return type(t) == "table" and t.__isAssemblage or false return type(t) == "table" and t.__isAssemblage or false
end end

View file

@ -2,6 +2,7 @@
local PATH = (...):gsub('%.[^%.]+$', '') local PATH = (...):gsub('%.[^%.]+$', '')
local Worlds = require(PATH..".world")
local Type = require(PATH..".type") local Type = require(PATH..".type")
local List = require(PATH..".list") local List = require(PATH..".list")
@ -10,7 +11,11 @@ World.__index = World
--- Creates a new World. --- Creates a new World.
-- @return The new World -- @return The new World
function World.new() function World.new(name)
if (type(name) ~= "string") then
error("bad argument #1 to 'Component.new' (string expected, got "..type(name)..")", 2)
end
local world = setmetatable({ local world = setmetatable({
entities = List(), entities = List(),
systems = List(), systems = List(),
@ -22,9 +27,12 @@ function World.new()
__systemLookup = {}, __systemLookup = {},
__name = name,
__isWorld = true, __isWorld = true,
}, World) }, World)
Worlds.register(world)
return world return world
end end
@ -115,9 +123,6 @@ function World:addSystem(baseSystem, callbackName, callback, enabled)
local system = self.__systemLookup[baseSystem] local system = self.__systemLookup[baseSystem]
if (not system) then if (not system) then
-- System was not created for this world yet, so we create it ourselves -- System was not created for this world yet, so we create it ourselves
print("Created system")
system = baseSystem(self) system = baseSystem(self)
self.__systemLookup[baseSystem] = system self.__systemLookup[baseSystem] = system
@ -240,7 +245,7 @@ end
-- @return self -- @return self
function World:clear() function World:clear()
for i = 1, self.entities.size do for i = 1, self.entities.size do
self.removeEntity(self.entities:get(i)) self.removeEntity(self.entities[i])
end end
return self return self

29
src/worlds.lua Normal file
View file

@ -0,0 +1,29 @@
-- Worlds
local PATH = (...):gsub('%.[^%.]+$', '')
local Type = require(PATH..".type")
local Worlds = {}
function Worlds.register(name, world)
if (type(name) ~= "string") then
error("bad argument #1 to 'Worlds.register' (string expected, got "..type(name)..")", 3)
end
if (not Type.isWorld(world)) then
error("bad argument #2 to 'Worlds.register' (world expected, got "..type(world)..")", 3)
end
if (rawget(Worlds, name)) then
error("bad argument #2 to 'Worlds.register' (World with name '"..name.."' is already registerd)", 3)
end
Worlds[name] = component
end
return setmetatable(Worlds, {
__index = function(_, name)
error("Attempt to index world '"..tostring(name).."' that does not exist / was not registered", 2)
end
})