mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-01 20:03:55 -04:00
Allow named worlds
This commit is contained in:
parent
d4efca976c
commit
d0e227485e
6 changed files with 50 additions and 7 deletions
6
main.lua
6
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -14,6 +14,7 @@ System.mt = {
|
|||
__world = world,
|
||||
|
||||
__isSystem = true,
|
||||
__isBaseSystem = false, -- Overwrite value from baseSystem
|
||||
}, baseSystem)
|
||||
|
||||
for _, filter in pairs(baseSystem.__filter) do
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
29
src/worlds.lua
Normal file
29
src/worlds.lua
Normal 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
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue