improvement(world): separate queryRect into 2 functions

This commit is contained in:
Kazhnuz 2019-06-27 21:20:54 +02:00
parent f68e300019
commit 7cce6ea99f
3 changed files with 33 additions and 21 deletions

View file

@ -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 - **actor:** Rename all function related to YGravity to just \*Gravity
- **world** Separate "queryRect()" into two functions
### Fixed ### Fixed

View file

@ -128,6 +128,21 @@ function BaseWorld:getActors()
return self.actors return self.actors
end 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) function BaseWorld:getVisibleActors(id)
local camx, camy, camw, camh = self.cameras:getViewCoordinate(id) local camx, camy, camw, camh = self.cameras:getViewCoordinate(id)
local paddingw = camw * PADDING_VALUE local paddingw = camw * PADDING_VALUE
@ -137,14 +152,7 @@ function BaseWorld:getVisibleActors(id)
local w = camw + paddingw * 2 local w = camw + paddingw * 2
local h = camh + paddingh * 2 local h = camh + paddingh * 2
local query = self:queryRect(x, y, w, h) return self:getActorsInRect(x, y, w, h)
local returnquery = {}
for i,v in ipairs(query) do
table.insert(returnquery, v.owner)
end
return returnquery
end end
-- BODIES MANAGEMENT FUNCTIONS -- BODIES MANAGEMENT FUNCTIONS
@ -175,18 +183,8 @@ function BaseWorld:checkCollision(actor, x, y, filter)
return x, y, {}, 0 return x, y, {}, 0
end end
function BaseWorld:queryRect(x, y, w, h) function BaseWorld:getBodiesInRect(x, y, w, h)
local query = {} return {}
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 end
-- INFO FUNCTIONS -- INFO FUNCTIONS

View file

@ -51,6 +51,19 @@ function World2D:registerBody(body)
return self.bodies:add(body, body.x, body.y, body.w, body.h) return self.bodies:add(body, body.x, body.y, body.w, body.h)
end 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 -- ACTORS FUNCTIONS
-- Wrappers around Bump2D functions -- Wrappers around Bump2D functions
@ -70,7 +83,7 @@ function World2D:checkCollision(actor, x, y, filter)
return self.bodies:check(actor, x, y, filter) return self.bodies:check(actor, x, y, filter)
end end
function World2D:queryRect(x, y, w, h) function World2D:getBodiesInRect(x, y, w, h)
return self.bodies:queryRect(x, y, w, h) return self.bodies:queryRect(x, y, w, h)
end end