mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-12-07 05:34:55 -05:00
Added type checking
This commit is contained in:
parent
fda6cd7237
commit
281bb53a5b
7 changed files with 101 additions and 8 deletions
|
|
@ -8,6 +8,8 @@ function Component.new(populate)
|
||||||
local component = setmetatable({
|
local component = setmetatable({
|
||||||
__populate = populate,
|
__populate = populate,
|
||||||
__inherit = inherit,
|
__inherit = inherit,
|
||||||
|
|
||||||
|
__isComponent = true,
|
||||||
}, Component)
|
}, Component)
|
||||||
|
|
||||||
component.__mt = {__index = component}
|
component.__mt = {__index = component}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
local PATH = (...):gsub('%.[^%.]+$', '')
|
local PATH = (...):gsub('%.[^%.]+$', '')
|
||||||
|
|
||||||
|
local Type = require(PATH..".type")
|
||||||
local List = require(PATH..".list")
|
local List = require(PATH..".list")
|
||||||
|
|
||||||
local Entity = {}
|
local Entity = {}
|
||||||
|
|
@ -12,6 +13,8 @@ function Entity.new()
|
||||||
components = {},
|
components = {},
|
||||||
removed = {},
|
removed = {},
|
||||||
instances = List(),
|
instances = List(),
|
||||||
|
|
||||||
|
__isEntity = true,
|
||||||
}, Entity)
|
}, Entity)
|
||||||
|
|
||||||
return e
|
return e
|
||||||
|
|
@ -22,6 +25,10 @@ end
|
||||||
-- @param ... The values passed to the Component
|
-- @param ... The values passed to the Component
|
||||||
-- @return self
|
-- @return self
|
||||||
function Entity:give(component, ...)
|
function Entity:give(component, ...)
|
||||||
|
if not Type.isComponent(component) then
|
||||||
|
error("bad argument #1 to 'Entity:give' (Component expected, got "..type(component)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
self.components[component] = component:__initialize(...)
|
self.components[component] = component:__initialize(...)
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
@ -31,6 +38,10 @@ end
|
||||||
-- @param component The Component to remove
|
-- @param component The Component to remove
|
||||||
-- @return self
|
-- @return self
|
||||||
function Entity:remove(component)
|
function Entity:remove(component)
|
||||||
|
if not Type.isComponent(component) then
|
||||||
|
error("bad argument #1 to 'Entity:remove' (Component expected, got "..type(component)..")")
|
||||||
|
end
|
||||||
|
|
||||||
self.removed[component] = true
|
self.removed[component] = true
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
@ -65,6 +76,10 @@ end
|
||||||
-- @param component The Component to get
|
-- @param component The Component to get
|
||||||
-- @return The Bag from the Component
|
-- @return The Bag from the Component
|
||||||
function Entity:get(component)
|
function Entity:get(component)
|
||||||
|
if not Type.isComponent(component) then
|
||||||
|
error("bad argument #1 to 'Entity:get' (Component expected, got "..type(component)..")")
|
||||||
|
end
|
||||||
|
|
||||||
return self.components[component]
|
return self.components[component]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -72,6 +87,10 @@ end
|
||||||
-- @params component The Component to check against
|
-- @params component The Component to check against
|
||||||
-- @return True if the entity has the Bag. False otherwise
|
-- @return True if the entity has the Bag. False otherwise
|
||||||
function Entity:has(component)
|
function Entity:has(component)
|
||||||
|
if not Type.isComponent(component) then
|
||||||
|
error("bad argument #1 to 'Entity:has' (Component expected, got "..type(component)..")")
|
||||||
|
end
|
||||||
|
|
||||||
return self.components[component] ~= nil
|
return self.components[component] ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
local PATH = (...):gsub('%.init$', '')
|
local PATH = (...):gsub('%.init$', '')
|
||||||
|
|
||||||
|
local Type = require(PATH..".type")
|
||||||
|
|
||||||
local Concord = {}
|
local Concord = {}
|
||||||
|
|
||||||
--- Initializes the library with some optional settings
|
--- Initializes the library with some optional settings
|
||||||
|
|
@ -17,10 +19,18 @@ function Concord.init(settings)
|
||||||
Concord.instances = {}
|
Concord.instances = {}
|
||||||
|
|
||||||
Concord.addInstance = function(instance)
|
Concord.addInstance = function(instance)
|
||||||
|
if not Type.isInstance(instance) then
|
||||||
|
error("bad argument #1 to 'Concord.addInstance' (Instance expected, got "..type(instance)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
table.insert(Concord.instances, instance)
|
table.insert(Concord.instances, instance)
|
||||||
end
|
end
|
||||||
|
|
||||||
Concord.removeInstance = function(instance)
|
Concord.removeInstance = function(instance)
|
||||||
|
if not Type.isInstance(instance) then
|
||||||
|
error("bad argument #1 to 'Concord.addInstance' (Instance expected, got "..type(instance)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
for i, instance in ipairs(Concord.instances) do
|
for i, instance in ipairs(Concord.instances) do
|
||||||
table.remove(Concord.instances, i)
|
table.remove(Concord.instances, i)
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
local PATH = (...):gsub('%.[^%.]+$', '')
|
local PATH = (...):gsub('%.[^%.]+$', '')
|
||||||
|
|
||||||
|
local Entity = require(PATH..".entity")
|
||||||
|
local System = require(PATH..".system")
|
||||||
|
local Type = require(PATH..".type")
|
||||||
local List = require(PATH..".list")
|
local List = require(PATH..".list")
|
||||||
|
|
||||||
local Instance = {}
|
local Instance = {}
|
||||||
|
|
@ -13,6 +16,8 @@ function Instance.new()
|
||||||
systems = List(),
|
systems = List(),
|
||||||
events = {},
|
events = {},
|
||||||
removed = {},
|
removed = {},
|
||||||
|
|
||||||
|
__isInstance = true,
|
||||||
}, Instance)
|
}, Instance)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
@ -22,6 +27,10 @@ end
|
||||||
-- @param e The Entity to add
|
-- @param e The Entity to add
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:addEntity(e)
|
function Instance:addEntity(e)
|
||||||
|
if not Type.isEntity(e) then
|
||||||
|
error("bad argument #1 to 'Instance:addEntity' (Entity expected, got "..type(e)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
e.instances:add(self)
|
e.instances:add(self)
|
||||||
self.entities:add(e)
|
self.entities:add(e)
|
||||||
self:checkEntity(e)
|
self:checkEntity(e)
|
||||||
|
|
@ -33,6 +42,10 @@ end
|
||||||
-- @param e The Entity to check
|
-- @param e The Entity to check
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:checkEntity(e)
|
function Instance:checkEntity(e)
|
||||||
|
if not Type.isEntity(e) then
|
||||||
|
error("bad argument #1 to 'Instance:checkEntity' (Entity expected, got "..type(e)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
for i = 1, self.systems.size do
|
for i = 1, self.systems.size do
|
||||||
self.systems:get(i):__check(e)
|
self.systems:get(i):__check(e)
|
||||||
end
|
end
|
||||||
|
|
@ -44,6 +57,10 @@ end
|
||||||
-- @param e The Entity to mark
|
-- @param e The Entity to mark
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:removeEntity(e)
|
function Instance:removeEntity(e)
|
||||||
|
if not Type.isEntity(e) then
|
||||||
|
error("bad argument #1 to 'Instance:removeEntity' (Entity expected, got "..type(e)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
self.removed[#self.removed + 1] = e
|
self.removed[#self.removed + 1] = e
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
@ -77,6 +94,10 @@ end
|
||||||
-- @param enabled If the system is enabled. Defaults to true
|
-- @param enabled If the system is enabled. Defaults to true
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:addSystem(system, eventName, callback, enabled)
|
function Instance:addSystem(system, eventName, callback, enabled)
|
||||||
|
if not Type.isSystem(system) then
|
||||||
|
error("bad argument #1 to 'Instance:addSystem' (System expected, got "..type(system)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
if system.__instance and system.__instance ~= self then
|
if system.__instance and system.__instance ~= self then
|
||||||
error("System already in instance '" ..tostring(system.__instance).."'")
|
error("System already in instance '" ..tostring(system.__instance).."'")
|
||||||
end
|
end
|
||||||
|
|
@ -106,6 +127,10 @@ end
|
||||||
-- @param callback The callback it was registered with. Defaults to eventName
|
-- @param callback The callback it was registered with. Defaults to eventName
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:enableSystem(system, eventName, callback)
|
function Instance:enableSystem(system, eventName, callback)
|
||||||
|
if not Type.isSystem(system) then
|
||||||
|
error("bad argument #1 to 'Instance:enableSystem' (System expected, got "..type(system)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
return self:setSystem(system, eventName, callback, true)
|
return self:setSystem(system, eventName, callback, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -115,6 +140,10 @@ end
|
||||||
-- @param callback The callback it was registered with. Defaults to eventName
|
-- @param callback The callback it was registered with. Defaults to eventName
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:disableSystem(system, eventName, callback)
|
function Instance:disableSystem(system, eventName, callback)
|
||||||
|
if not Type.isSystem(system) then
|
||||||
|
error("bad argument #1 to 'Instance:disableSystem' (System expected, got "..type(system)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
return self:setSystem(system, eventName, callback, false)
|
return self:setSystem(system, eventName, callback, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -125,8 +154,13 @@ end
|
||||||
-- @param enable The state to set it to
|
-- @param enable The state to set it to
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:setSystem(system, eventName, callback, enable)
|
function Instance:setSystem(system, eventName, callback, enable)
|
||||||
|
if not Type.isSystem(system) then
|
||||||
|
error("bad argument #1 to 'Instance:setSystem' (System expected, got "..type(system)..")", 2)
|
||||||
|
end
|
||||||
|
|
||||||
callback = callback or eventName
|
callback = callback or eventName
|
||||||
|
|
||||||
|
if callback then
|
||||||
local listeners = self.events[eventName]
|
local listeners = self.events[eventName]
|
||||||
|
|
||||||
if listeners then
|
if listeners then
|
||||||
|
|
@ -139,6 +173,7 @@ function Instance:setSystem(system, eventName, callback, enable)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
@ -148,6 +183,10 @@ end
|
||||||
-- @param ... Parameters passed to listeners
|
-- @param ... Parameters passed to listeners
|
||||||
-- @return self
|
-- @return self
|
||||||
function Instance:emit(eventName, ...)
|
function Instance:emit(eventName, ...)
|
||||||
|
if not eventName or type(eventName) ~= "string" then
|
||||||
|
error("bad argument #1 to 'Instance:emit' (String expected, got "..type(eventName)..")")
|
||||||
|
end
|
||||||
|
|
||||||
self:flush()
|
self:flush()
|
||||||
|
|
||||||
local listeners = self.events[eventName]
|
local listeners = self.events[eventName]
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@ function Pool.new(name, filter)
|
||||||
pool.name = name
|
pool.name = name
|
||||||
pool.filter = filter
|
pool.filter = filter
|
||||||
|
|
||||||
|
pool.__isPool = true
|
||||||
|
|
||||||
return pool
|
return pool
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ System.mt = {
|
||||||
__all = {},
|
__all = {},
|
||||||
__pools = {},
|
__pools = {},
|
||||||
__instance = nil,
|
__instance = nil,
|
||||||
|
|
||||||
|
__isSystem = true,
|
||||||
}, systemProto)
|
}, systemProto)
|
||||||
|
|
||||||
for _, filter in pairs(systemProto.__filter) do
|
for _, filter in pairs(systemProto.__filter) do
|
||||||
|
|
|
||||||
19
concord/type.lua
Normal file
19
concord/type.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
local Type = {}
|
||||||
|
|
||||||
|
function Type.isComponent(t)
|
||||||
|
return type(t) == "table" and t.__isComponent
|
||||||
|
end
|
||||||
|
|
||||||
|
function Type.isEntity(t)
|
||||||
|
return type(t) == "table" and t.__isEntity
|
||||||
|
end
|
||||||
|
|
||||||
|
function Type.isSystem(t)
|
||||||
|
return type(t) == "table" and t.__isSystem
|
||||||
|
end
|
||||||
|
|
||||||
|
function Type.isInstance(t)
|
||||||
|
return type(t) == "table" and t.__isInstance
|
||||||
|
end
|
||||||
|
|
||||||
|
return Type
|
||||||
Loading…
Add table
Add a link
Reference in a new issue