From d0e227485e181d6ab9948af643828dcd663c5c7a Mon Sep 17 00:00:00 2001 From: Tjakka5 Date: Thu, 19 Dec 2019 20:14:21 +0100 Subject: [PATCH] Allow named worlds --- main.lua | 6 +++++- src/component.lua | 2 +- src/system.lua | 1 + src/type.lua | 4 ++++ src/world.lua | 15 ++++++++++----- src/worlds.lua | 29 +++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/worlds.lua diff --git a/main.lua b/main.lua index 26ef1f8..6040302 100644 --- a/main.lua +++ b/main.lua @@ -46,7 +46,11 @@ function test_system:init() end function test_system:update(dt) -- luacheck: ignore - --print(#self.pool) + --[=[ + for _, v in ipairs(self.pool) do + print(v) + end + ]=] end function test_system:update2(dt) -- luacheck: ignore diff --git a/src/component.lua b/src/component.lua index 43d4ecc..e7b3d90 100644 --- a/src/component.lua +++ b/src/component.lua @@ -38,7 +38,7 @@ end -- @return A new initialized Bag function Component:__initialize(...) if self.__populate then - local bag = setmetatable({}, self.__mt) + local bag = setmetatable({}, self) self.__populate(bag, ...) return bag diff --git a/src/system.lua b/src/system.lua index ecafd6b..f9b696c 100644 --- a/src/system.lua +++ b/src/system.lua @@ -14,6 +14,7 @@ System.mt = { __world = world, __isSystem = true, + __isBaseSystem = false, -- Overwrite value from baseSystem }, baseSystem) for _, filter in pairs(baseSystem.__filter) do diff --git a/src/type.lua b/src/type.lua index ae79628..3da59d9 100644 --- a/src/type.lua +++ b/src/type.lua @@ -18,6 +18,10 @@ function Type.isSystem(t) return type(t) == "table" and t.__isSystem or false end +function Type.isWorld(t) + return type(t) == "table" and t.__isWorld or false +end + function Type.isAssemblage(t) return type(t) == "table" and t.__isAssemblage or false end diff --git a/src/world.lua b/src/world.lua index 2bb13e5..86e76c3 100644 --- a/src/world.lua +++ b/src/world.lua @@ -2,6 +2,7 @@ local PATH = (...):gsub('%.[^%.]+$', '') +local Worlds = require(PATH..".world") local Type = require(PATH..".type") local List = require(PATH..".list") @@ -10,7 +11,11 @@ World.__index = World --- Creates a 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({ entities = List(), systems = List(), @@ -22,9 +27,12 @@ function World.new() __systemLookup = {}, + __name = name, __isWorld = true, }, World) + Worlds.register(world) + return world end @@ -115,9 +123,6 @@ function World:addSystem(baseSystem, callbackName, callback, enabled) local system = self.__systemLookup[baseSystem] if (not system) then -- System was not created for this world yet, so we create it ourselves - - print("Created system") - system = baseSystem(self) self.__systemLookup[baseSystem] = system @@ -240,7 +245,7 @@ end -- @return self function World:clear() for i = 1, self.entities.size do - self.removeEntity(self.entities:get(i)) + self.removeEntity(self.entities[i]) end return self diff --git a/src/worlds.lua b/src/worlds.lua new file mode 100644 index 0000000..0f8ad9c --- /dev/null +++ b/src/worlds.lua @@ -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 +}) \ No newline at end of file