mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 04:13:58 -04:00
Allow for named components
This commit is contained in:
parent
f7e1be8e1d
commit
bc47eaa651
2 changed files with 53 additions and 1 deletions
37
main.lua
37
main.lua
|
@ -1,4 +1,41 @@
|
||||||
|
--[=[
|
||||||
local file = "examples.simpleDrawing"
|
local file = "examples.simpleDrawing"
|
||||||
-- local file = "examples.baseLayout.main"
|
-- local file = "examples.baseLayout.main"
|
||||||
|
|
||||||
require(file)
|
require(file)
|
||||||
|
]=]--
|
||||||
|
|
||||||
|
local Concord = require("src")
|
||||||
|
local Component = require("src.component")
|
||||||
|
|
||||||
|
local test_comp_1 = Concord.component("test_comp_1", function(e, x, y)
|
||||||
|
e.x = x
|
||||||
|
e.y = y
|
||||||
|
end)
|
||||||
|
|
||||||
|
local test_comp_2 = Concord.component("test_comp_2", function(e, a)
|
||||||
|
e.a = a
|
||||||
|
end)
|
||||||
|
|
||||||
|
local test_comp_3 = Concord.component("test_comp_3", function(e, b)
|
||||||
|
e.b = b
|
||||||
|
end)
|
||||||
|
|
||||||
|
local test_system = Concord.system({Component.test_comp_1})
|
||||||
|
function test_system:update(dt)
|
||||||
|
print(#self.pool)
|
||||||
|
end
|
||||||
|
|
||||||
|
local world = Concord.world()
|
||||||
|
|
||||||
|
local entity = Concord.entity()
|
||||||
|
entity:give(Component.test_comp_2, 100, 100)
|
||||||
|
entity:apply()
|
||||||
|
|
||||||
|
world:addEntity(entity)
|
||||||
|
|
||||||
|
world:addSystem(test_system(), "update")
|
||||||
|
|
||||||
|
function love.update(dt)
|
||||||
|
world:emit("update", dt)
|
||||||
|
end
|
|
@ -6,8 +6,21 @@ Component.__index = Component
|
||||||
--- Creates a new Component.
|
--- Creates a new Component.
|
||||||
-- @param populate A function that populates the Bag with values
|
-- @param populate A function that populates the Bag with values
|
||||||
-- @return A Component object
|
-- @return A Component object
|
||||||
function Component.new(populate)
|
function Component.new(name, populate)
|
||||||
|
if (type(name) ~= "string") then
|
||||||
|
error("bad argument #1 to 'Component.new' (string expected, got "..type(name)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not (populate == nil or type(populate) == "function") then
|
||||||
|
error("bad argument #2 to 'Component.new' (function/nil expected, got "..type(populate)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (Component[name] ~= nil) then
|
||||||
|
error("bad argument #2 to 'Component.new' (Component with name '"..name.."' already exists", 2)
|
||||||
|
end
|
||||||
|
|
||||||
local component = setmetatable({
|
local component = setmetatable({
|
||||||
|
__name = name,
|
||||||
__populate = populate,
|
__populate = populate,
|
||||||
|
|
||||||
__isComponent = true,
|
__isComponent = true,
|
||||||
|
@ -15,6 +28,8 @@ function Component.new(populate)
|
||||||
|
|
||||||
component.__mt = {__index = component}
|
component.__mt = {__index = component}
|
||||||
|
|
||||||
|
Component[name] = component
|
||||||
|
|
||||||
return component
|
return component
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue