From bdfa62ab1df4df00b57913a3ce2ec1fc3f112c78 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 11 Sep 2017 21:01:43 +0200 Subject: Generalized methods for entities. Wrappers for frequently used ones --- not/World.lua | 71 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/not/World.lua b/not/World.lua index ca061c1..6833d87 100644 --- a/not/World.lua +++ b/not/World.lua @@ -137,48 +137,63 @@ function World:insertCloud (cloud) return cloud end -function World:getCloudsCount () +--- Verbose wrapper for inserting entities into entities table. +-- @param entity entity to insert +function World:insertEntity (entity) + if entity then + table.insert(self.entities, entity) + return entity + end +end + +--- Searches entities for those which return true with filtering function. +-- @param filter function with entity as parameter +-- @return table containing results of search +function World:getEntities (filter) + local result = {} + for _,entity in pairs(self.entities) do + if filter(entity) then + table.insert(result, entity) + end + end + return result +end + +--- Counts entities returning true with filtering function. +-- @param filter function with entity as parameter +-- @return entity count +function World:countEntities (filter) local count = 0 - for i,entity in ipairs(self.entities) do - if entity:is(Cloud) then + for _,entity in pairs(self.entities) do + if filter(entity) then count = count + 1 end end return count end +function World:getCloudsCount () + return self:countEntities(function (entity) + return entity:is(Cloud) + end) +end + function World:getNautsAll () - local nauts = {} - for i,entity in ipairs(self.entities) do - if entity:is(require("not.Hero")) and not entity.body:isDestroyed() then - table.insert(nauts, entity) - end - end - return nauts + return self:getEntities(function (entity) + return entity:is(require("not.Hero")) and not entity.body:isDestroyed() + end) end function World:getNautsPlayable () - local nauts = {} - for i,entity in ipairs(self.entities) do - if entity:is(require("not.Hero")) then - if entity.lives > -1 then - table.insert(nauts, entity) - end - end - end - return nauts + return self:getEntities(function (entity) + return entity:is(require("not.Hero")) and entity.lives > -1 + end) end function World:getNautsAlive () - local nauts = {} - for i,entity in ipairs(self.entities) do - if entity:is(require("not.Hero")) then - if entity.isAlive then - table.insert(nauts, entity) - end - end - end - return nauts + return self:getEntities(function (entity) + return entity:is(require("not.Hero")) and entity.isAlive + end) end -- get Map name -- cgit v1.1