mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 12:24:11 -04:00
Add World:query
This method allows you to query the World in order to find a set of Entities that matches a specific Filter.
This commit is contained in:
parent
a4ae392341
commit
3d195c790f
3 changed files with 53 additions and 25 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
local PATH = (...):gsub('%.[^%.]+$', '')
|
||||
|
||||
local Filter = require(PATH..".filter")
|
||||
local Entity = require(PATH..".entity")
|
||||
local Type = require(PATH..".type")
|
||||
local List = require(PATH..".list")
|
||||
|
@ -71,6 +72,27 @@ function World:newEntity()
|
|||
return Entity(self)
|
||||
end
|
||||
|
||||
function World:query(def, onMatch)
|
||||
local filter = Filter.parse(nil, def)
|
||||
|
||||
local list = nil
|
||||
if not Type.isCallable(onMatch) then
|
||||
list = type(onMatch) == "table" and onMatch or {}
|
||||
end
|
||||
|
||||
for _, e in ipairs(self.__entities) do
|
||||
if Filter.match(e, filter) then
|
||||
if list then
|
||||
table.insert(list, e)
|
||||
else
|
||||
onMatch(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return list
|
||||
end
|
||||
|
||||
--- Removes an Entity from the World.
|
||||
-- @tparam Entity e Entity to remove
|
||||
-- @treturn World self
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue