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:
Pablo Ariel Mayobre 2023-02-14 18:14:24 -03:00
parent a4ae392341
commit 3d195c790f
3 changed files with 53 additions and 25 deletions

View file

@ -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