Automate naming process

This commit is contained in:
Tjakka5 2019-12-22 23:07:42 +01:00
parent e0f88025ba
commit 144a42dc9e
7 changed files with 78 additions and 53 deletions

View file

@ -9,8 +9,8 @@ require(file)
local Concord = require("src")
local Component = require("src.component")
local Components = require("src.components")
local Component = Concord.component
local Components = Concord.components
local System = Concord.system
local Systems = Concord.systems
@ -20,20 +20,23 @@ local Entity = Concord.entity
local World = Concord.world
local Worlds = Concord.worlds
Component("test_comp_1", function(e, x, y)
e.x = x
e.y = y
end)
Concord.loadComponents("test/components")
Component("test_comp_2", function(e, a)
local test_comp_2 = Component(function(e, a)
e.a = a
end)
Components.register("test_comp_2", test_comp_2)
Component("test_comp_3", function(e, b)
local test_comp_3 = Component(function(e, b)
e.b = b
end)
Components.register("test_comp_3", test_comp_3)
local test_system = System("test_system", {Components.test_comp_1})
local test_system = System({Components.test_comp_1})
Systems.register("test_system", test_system)
local function onEntityAdded(pool, e) -- luacheck: ignore
print("Added")
@ -61,7 +64,8 @@ function test_system:update2(dt) -- luacheck: ignore
end
local world = World("testWorld")
local world = World()
Worlds.register("testWorld", world)
local entity = Entity()
entity

View file

@ -1,25 +1,14 @@
--- Assemblage
local PATH = (...):gsub('%.[^%.]+$', '')
local Assemblages = require(PATH..".world")
local Assemblage = {}
Assemblage.__index = Assemblage
function Assemblage.new(name, assemble)
if (type(name) ~= "string") then
error("bad argument #1 to 'Assemblage.new' (string expected, got "..type(name)..")", 2)
end
function Assemblage.new(assemble)
local assemblage = setmetatable({
__assemble = assemble,
__name = name,
__isAssemblage = true,
}, Assemblage)
Assemblages.register(name, assemblage)
return assemblage
end

View file

@ -1,26 +1,17 @@
--- Component
local PATH = (...):gsub('%.[^%.]+$', '')
local Components = require(PATH..".components")
local Component = {}
Component.__index = Component
--- Creates a new Component.
-- @param populate A function that populates the Bag with values
-- @return A Component object
function Component.new(name, populate)
if (type(name) ~= "string") then
error("bad argument #1 to 'Component.new' (string expected, got "..type(name)..")", 2)
end
function Component.new(populate)
if not (type(populate) == "function") then
error("bad argument #2 to 'Component.new' (function expected, got "..type(populate)..")", 2)
error("bad argument #1 to 'Component.new' (function expected, got "..type(populate)..")", 2)
end
local baseComponent = setmetatable({
__name = name,
__populate = populate,
__isBaseComponent = true,
@ -28,8 +19,6 @@ function Component.new(name, populate)
baseComponent.__mt = {__index = baseComponent}
Components.register(name, baseComponent)
return baseComponent
end

View file

@ -45,4 +45,57 @@ Concord.worlds = require(PATH..".worlds")
Concord.assemblage = require(PATH..".assemblage")
Concord.assemblages = require(PATH..".assemblages")
local function load(pathOrFiles, namespace)
if (type(pathOrFiles) ~= "string" and type(pathOrFiles) ~= "table") then
error("bad argument #1 to 'load' (string/table of strings expected, got "..type(pathOrFiles)..")", 3) -- luacheck: ignore
end
if (type(pathOrFiles) == "string") then
local info = love.filesystem.getInfo(pathOrFiles) -- luacheck: ignore
if (info == nil or info.type ~= "directory") then
error("bad argument #1 to 'load' (path '"..pathOrFiles.."' not found)", 3) -- luacheck: ignore
end
local files = love.filesystem.getDirectoryItems(pathOrFiles)
for _, file in ipairs(files) do
local name = file:sub(1, #file - 4)
local path = pathOrFiles.."."..name
namespace.register(name, require(path))
end
elseif (type(pathOrFiles == "table")) then
for _, path in ipairs(pathOrFiles) do
if (type(path) ~= "string") then
error("bad argument #2 to 'load' (string/table of strings expected, got table containing "..type(path)..")", 3) -- luacheck: ignore
end
local name = path
local dotIndex, slashIndex = path:match("^.*()%."), path:match("^.*()%/")
if (dotIndex or slashIndex) then
name = path:sub((dotIndex or slashIndex) + 1)
end
namespace.register(name, require(path))
end
end
end
function Concord.loadComponents(pathOrFiles)
load(pathOrFiles, Concord.components)
end
function Concord.loadSystems(pathOrFiles)
load(pathOrFiles, Concord.systems)
end
function Concord.loadWorlds(pathOrFiles)
load(pathOrFiles, Concord.worlds)
end
function Concord.loadAssemblages(pathOrFiles)
load(pathOrFiles, Concord.assemblages)
end
return Concord

View file

@ -2,7 +2,6 @@
local PATH = (...):gsub('%.[^%.]+$', '')
local Systems = require(PATH..".systems")
local Pool = require(PATH..".pool")
local System = {}
@ -36,20 +35,13 @@ System.mt = {
--- Creates a new System prototype.
-- @param ... Variable amounts of filters
-- @return A new System prototype
function System.new(name, ...)
if (type(name) ~= "string") then
error("bad argument #1 to 'System.new' (string expected, got "..type(name)..")", 2)
end
function System.new(...)
local baseSystem = setmetatable({
__name = name,
__isBaseSystem = true,
__filter = {...},
}, System.mt)
baseSystem.__index = baseSystem
Systems.register(name, baseSystem)
return baseSystem
end

View file

@ -2,7 +2,6 @@
local PATH = (...):gsub('%.[^%.]+$', '')
local Worlds = require(PATH..".worlds")
local Type = require(PATH..".type")
local List = require(PATH..".list")
@ -11,11 +10,7 @@ World.__index = World
--- Creates a new World.
-- @return The new World
function World.new(name)
if (type(name) ~= "string") then
error("bad argument #1 to 'World.new' (string expected, got "..type(name)..")", 2)
end
function World.new()
local world = setmetatable({
entities = List(),
systems = List(),
@ -27,12 +22,9 @@ function World.new(name)
__systemLookup = {},
__name = name,
__isWorld = true,
}, World)
Worlds.register(name, world)
return world
end

View file

@ -0,0 +1,6 @@
local Component = require("src").component
return Component(function(e, x, y)
e.x = x
e.y = y
end)