From 7cce6ea99f8c83c7adcca9e520596ee5b39b920c Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 27 Jun 2019 21:20:54 +0200 Subject: [PATCH] improvement(world): separate queryRect into 2 functions --- CHANGELOG.md | 1 + gamecore/modules/world/baseworld.lua | 38 +++++++++++++--------------- gamecore/modules/world/world2D.lua | 15 ++++++++++- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2119b49..d49bd5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **actor:** Rename all function related to YGravity to just \*Gravity +- **world** Separate "queryRect()" into two functions ### Fixed diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index 045ba19..e5c7652 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -128,6 +128,21 @@ function BaseWorld:getActors() return self.actors end +function BaseWorld:getActorsInRect(x, y, w, h) + local query = {} + local x2, y2 = x + w, y + h + for i,v in ipairs(self.actors) do + if (v.x >= x) and (v.x + v.w >= x1) and + (v.y >= y) and (v.y + v.h >= y1) then + + table.insert(query, v) + end + end + + return v +end + + function BaseWorld:getVisibleActors(id) local camx, camy, camw, camh = self.cameras:getViewCoordinate(id) local paddingw = camw * PADDING_VALUE @@ -137,14 +152,7 @@ function BaseWorld:getVisibleActors(id) local w = camw + paddingw * 2 local h = camh + paddingh * 2 - local query = self:queryRect(x, y, w, h) - local returnquery = {} - - for i,v in ipairs(query) do - table.insert(returnquery, v.owner) - end - - return returnquery + return self:getActorsInRect(x, y, w, h) end -- BODIES MANAGEMENT FUNCTIONS @@ -175,18 +183,8 @@ function BaseWorld:checkCollision(actor, x, y, filter) return x, y, {}, 0 end -function BaseWorld:queryRect(x, y, w, h) - local query = {} - local x2, y2 = x + w, y + h - for i,v in ipairs(self.actors) do - if (v.x >= x) and (v.x + v.w >= x1) and - (v.y >= y) and (v.y + v.h >= y1) then - - table.insert(query, v) - end - end - - return v +function BaseWorld:getBodiesInRect(x, y, w, h) + return {} end -- INFO FUNCTIONS diff --git a/gamecore/modules/world/world2D.lua b/gamecore/modules/world/world2D.lua index 98ce64e..4c5864e 100644 --- a/gamecore/modules/world/world2D.lua +++ b/gamecore/modules/world/world2D.lua @@ -51,6 +51,19 @@ function World2D:registerBody(body) return self.bodies:add(body, body.x, body.y, body.w, body.h) end +function World2D:getActorsInRect(x, y, w, h) + local bodies = self.bodies:queryRect(x, y, w, h) + local returnquery = {} + + for i,body in ipairs(bodies) do + if (body.isMainHitBox) then + table.insert(returnquery, body.owner) + end + end + + return returnquery +end + -- ACTORS FUNCTIONS -- Wrappers around Bump2D functions @@ -70,7 +83,7 @@ function World2D:checkCollision(actor, x, y, filter) return self.bodies:check(actor, x, y, filter) end -function World2D:queryRect(x, y, w, h) +function World2D:getBodiesInRect(x, y, w, h) return self.bodies:queryRect(x, y, w, h) end