diff --git a/game/wrapper/Concord/component.lua b/game/wrapper/Concord/component.lua new file mode 100644 index 0000000..842f728 --- /dev/null +++ b/game/wrapper/Concord/component.lua @@ -0,0 +1,37 @@ +local Concord = require("lib.Concord") + +-- Modules +local Entity = Concord.entity +local Component = Concord.component +local System = Concord.system +local World = Concord.world + +-- Containers +local Components = Concord.components + +local constructor = {} + +function constructor.default_component_lambda(c, data) + c.data = data +end + +--- build component ecs component +---@param name string +---@param lambda? function(c, data) +function constructor.component(name, lambda) + local ok, value = Components.try(name) + if not ok then + return Component(name, lambda or constructor.default_component_lambda) + end +end + +--- build component ecs state +---@param name string +function constructor.state(name) + local ok, value = Components.try(name) + if not ok then + return Component(name) + end +end + +return constructor diff --git a/game/wrapper/Concord/entity.lua b/game/wrapper/Concord/entity.lua new file mode 100644 index 0000000..3786627 --- /dev/null +++ b/game/wrapper/Concord/entity.lua @@ -0,0 +1,91 @@ +local format = string.format + +local reap = require("lib.reap") + +local BASE = reap.base_path(...) +local component_builder = require(format("%s.component", BASE)) + +local Concord = require("lib.Concord") + +-- Modules +local Entity = Concord.entity +local Component = Concord.component +local System = Concord.system +local World = Concord.world + +-- Containers +local Components = Concord.components + +local constructor = {} + +--- build entity ecs +function constructor.new(system) + if (type(system) == "table") then + local e = Entity() + for _, s in pairs(system) do + for c, f in pairs(s.components) do + if (type(c) == "string") then + component_builder.component(c, f) + else + component_builder.state(f) + end + end + end + return e + elseif (system == nil) then + return Entity() + else + return nil + end +end + +--- ensure component +---@param entity any +---@param name string +function constructor.ensure(entity, name, ...) + if (#{...} == 0) then + component_builder.state(name) + entity:ensure(name) + else + component_builder.component(name) + entity:ensure(name, ...) + end +end + +--- give component +---@param entity any +---@param name string +function constructor.give(entity, name, ...) + if (#{...} == 0) then + component_builder.state(name) + entity:give(name) + else + component_builder.component(name) + entity:give(name, ...) + end +end + +function constructor.remove(entity, name) + if Components.has(name) == false then + return false, nil + else + if entity:has(name) then + local data = entity[name] + entity:remove(name) + return true, data + else + return false, nil + end + end +end + +function constructor.destroy(entity) + local all_c = entity:getComponents() + for c_id, c in ipairs(all_c) do + entity:remove(c) + print("destroy", c_id, c) + end + entity:destroy() +end + +return constructor diff --git a/game/wrapper/Concord/system.lua b/game/wrapper/Concord/system.lua new file mode 100644 index 0000000..beed534 --- /dev/null +++ b/game/wrapper/Concord/system.lua @@ -0,0 +1,84 @@ +local insert = table.insert + +local Concord = require("lib.Concord") + +-- Modules +local Entity = Concord.entity +local Component = Concord.component +local System = Concord.system +local World = Concord.world + +-- Containers +local Components = Concord.components + + +local constructor = {} + +constructor.systems = {} + +local function is_component_complete(pool) + local is_complete = true + for _, v in pairs(pool) do + for _, c in ipairs(v) do + if (Components.has(c) == false) then + is_complete = false + break + end + end + if (not is_complete) then + break + end + end + return is_complete +end + +function constructor.new(name, pool) + if (constructor.systems[name]) then + return constructor.systems[name] + else + if (type(pool) == "table") then + if (is_component_complete(pool)) then + local self = System(pool) + self.name = name + constructor.systems[name] = self + return self + else + return nil + end + else + error("new system register error. pool and wrapper should be a table.") + return nil + end + end +end + +function constructor.add(world, path) + local namespace = {} + if (type(path) == "string") then + Concord.utils.loadNamespace(path, namespace) + else + for k,v in pairs(path) do + if type(v) == "string" then + local new_namespace = {} + Concord.utils.loadNamespace(v, new_namespace) + for l, w in pairs(new_namespace) do + namespace[l] = w + end + else + insert(namespace, v) + end + end + end + for k,v in pairs(namespace) do + local new_sys = v.new() + if (new_sys == nil) then + + else + if not world:hasSystem(new_sys) then + world:addSystems(new_sys) + end + end + end +end + +return constructor diff --git a/game/wrapper/Concord/world.lua b/game/wrapper/Concord/world.lua index e70231d..b3352c0 100644 --- a/game/wrapper/Concord/world.lua +++ b/game/wrapper/Concord/world.lua @@ -1,12 +1,12 @@ local format = string.format -local reap = require("libs.reap") +local reap = require("lib.reap") local BASE = reap.base_path(...) local component_builder = require(format("%s.component", BASE)) local entity_builder = require(format("%s.entity", BASE)) -local Concord = require("libs.Concord") +local Concord = require("lib.Concord") -- Modules local Entity = Concord.entity