chore: improve semantics

- Use "apply" when you apply collision, and not check
- Use "AtPoint" when you check at a point
This commit is contained in:
Kazhnuz 2020-11-26 20:41:02 +01:00
parent b2623cdb6a
commit 9eb0d321c8
5 changed files with 29 additions and 21 deletions

View file

@ -96,16 +96,16 @@ end
function Actor2D:move(dx, dy) function Actor2D:move(dx, dy)
local cols, colNumber = {}, 0 local cols, colNumber = {}, 0
if (self.isDestroyed == false) then if (self.isDestroyed == false) then
self.x, self.y, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, self.filter) self.x, self.y, cols, colNumber = self.mainHitbox:checkCollisionAtPoint(dx, dy, self.filter)
self.mainHitbox:updatePosition() self.mainHitbox:updatePosition()
end end
return self.x, self.y, cols, colNumber return self.x, self.y, cols, colNumber
end end
function Actor2D:checkCollision(dx, dy) function Actor2D:checkCollisionAtPoint(dx, dy)
local x, y, cols, colNumber = dx, dy, {}, 0 local x, y, cols, colNumber = dx, dy, {}, 0
if (self.isDestroyed == false) then if (self.isDestroyed == false) then
x, y, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, self.filter) x, y, cols, colNumber = self.mainHitbox:checkCollisionAtPoint(dx, dy, self.filter)
end end
return self.x, self.y, cols, colNumber return self.x, self.y, cols, colNumber
end end
@ -123,7 +123,7 @@ end
function Actor2D:checkGround() function Actor2D:checkGround()
local dx, dy = self.x, self.y + utils.math.sign(self.grav) local dx, dy = self.x, self.y + utils.math.sign(self.grav)
local newx, newy, cols, colNumber = self:checkCollision(dx, dy) local newx, newy, cols, colNumber = self:checkCollisionAtPoint(dx, dy)
for i, col in ipairs(cols) do for i, col in ipairs(cols) do
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
@ -145,15 +145,15 @@ end
-- HITBOXES FUNCTIONS -- HITBOXES FUNCTIONS
-- Functions related to actor hitboxes -- Functions related to actor hitboxes
function Actor2D:checkHitboxCollisions(name, filter) function Actor2D:applyHitboxCollisions(name, filter)
self:checkHitboxCollisionsAtPoint(name, self.x, self.y, filter) self:applyHitboxCollisionsAtPoint(name, self.x, self.y, filter)
end end
function Actor2D:checkHitboxCollisionsAtPoint(name, dx, dy, filter) function Actor2D:applyHitboxCollisionsAtPoint(name, dx, dy, filter)
local x, y, cols, colNumber = dx, dy, {}, 0 local x, y, cols, colNumber = dx, dy, {}, 0
local filter = filter or self.filter local filter = filter or self.filter
if (self.isDestroyed == false) and (self.hitboxes[name] ~= nil) then if (self.isDestroyed == false) and (self.hitboxes[name] ~= nil) then
x, y, cols, colNumber = self.hitboxes[name]:checkCollision(dx, dy, filter) x, y, cols, colNumber = self.hitboxes[name]:checkCollisionAtPoint(dx, dy, filter)
local type = self.hitboxes[name].type local type = self.hitboxes[name].type
for i, col in ipairs(cols) do for i, col in ipairs(cols) do
@ -161,7 +161,7 @@ function Actor2D:checkHitboxCollisionsAtPoint(name, dx, dy, filter)
end end
end end
return x, y, cols, colNumber return cols, colNumber
end end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS

View file

@ -111,7 +111,7 @@ function Actor3D:move(dx, dy, dz)
local cols, colNumber = {}, 0 local cols, colNumber = {}, 0
local oldx, oldy, oldz = self.x, self.y, self.z local oldx, oldy, oldz = self.x, self.y, self.z
if (self.isDestroyed == false) then if (self.isDestroyed == false) then
self.x, self.y, self.z, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, dz, self.filter) self.x, self.y, self.z, cols, colNumber = self.mainHitbox:checkCollisionAtPoint(dx, dy, dz, self.filter)
self.mainHitbox:updatePosition() self.mainHitbox:updatePosition()
self.world:updateShape(self) self.world:updateShape(self)
end end
@ -125,10 +125,10 @@ function Actor3D:move(dx, dy, dz)
return self.x, self.y, self.z, cols, colNumber return self.x, self.y, self.z, cols, colNumber
end end
function Actor3D:checkCollision(dx, dy, dz) function Actor3D:checkCollisionAtPoint(dx, dy, dz)
local x, y, z, cols, colNumber = dx, dy, dz, {}, 0 local x, y, z, cols, colNumber = dx, dy, dz, {}, 0
if (self.isDestroyed == false) then if (self.isDestroyed == false) then
x, y, z, cols, colNumber = self.mainHitbox:checkCollision(dx, dy, dz, self.filter) x, y, z, cols, colNumber = self.mainHitbox:checkCollisionAtPoint(dx, dy, dz, self.filter)
end end
return self.x, self.y, self.z, cols, colNumber return self.x, self.y, self.z, cols, colNumber
end end
@ -147,7 +147,7 @@ end
function Actor3D:checkGround() function Actor3D:checkGround()
local dx, dy, dz = self.x, self.y, self.z - utils.math.sign(self.grav) local dx, dy, dz = self.x, self.y, self.z - utils.math.sign(self.grav)
local newx, newy, newz, cols, colNumber = self:checkCollision(dx, dy, dz) local newx, newy, newz, cols, colNumber = self:checkCollisionAtPoint(dx, dy, dz)
for i, col in ipairs(cols) do for i, col in ipairs(cols) do
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
if not (self.grav == 0) then if not (self.grav == 0) then
@ -168,15 +168,15 @@ end
-- HITBOXES FUNCTIONS -- HITBOXES FUNCTIONS
-- Functions related to actor hitboxes -- Functions related to actor hitboxes
function Actor3D:checkHitboxCollisions(name, filter) function Actor3D:applyHitboxCollisions(name, filter)
self:checkHitboxCollisionsAtPoint(name, self.x, self.y, self.z, filter) return self:applyHitboxCollisionsAtPoint(name, self.x, self.y, self.z, filter)
end end
function Actor3D:checkHitboxCollisionsAtPoint(name, dx, dy, dz, filter) function Actor3D:applyHitboxCollisionsAtPoint(name, dx, dy, dz, filter)
local x, y, z, cols, colNumber = dx, dy, dz, {}, 0 local x, y, z, cols, colNumber = dx, dy, dz, {}, 0
local filter = filter or self.filter local filter = filter or self.filter
if (self.isDestroyed == false) and (self.hitboxes[name] ~= nil) then if (self.isDestroyed == false) and (self.hitboxes[name] ~= nil) then
x, y, z, cols, colNumber = self.hitboxes[name]:checkCollision(dx, dy, dz, filter) x, y, z, cols, colNumber = self.hitboxes[name]:checkCollisionAtPoint(dx, dy, dz, filter)
local type = self.hitboxes[name].type local type = self.hitboxes[name].type
for i, col in ipairs(cols) do for i, col in ipairs(cols) do
@ -184,7 +184,7 @@ function Actor3D:checkHitboxCollisionsAtPoint(name, dx, dy, dz, filter)
end end
end end
return x, y, z, cols, colNumber return cols, colNumber
end end
-- SHADOW FUNCTIONS -- SHADOW FUNCTIONS

View file

@ -226,7 +226,7 @@ end
function PhysicalActor:checkHitboxesCollisions(filter) function PhysicalActor:checkHitboxesCollisions(filter)
for k, v in pairs(self.hitboxes) do for k, v in pairs(self.hitboxes) do
self:checkHitboxCollisions(k, filter) self:applyHitboxCollisions(k, filter)
end end
end end

View file

@ -115,7 +115,11 @@ end
-- COLLISION FUNCTIONS -- COLLISION FUNCTIONS
-- Handle Hitbox position -- Handle Hitbox position
function Hitbox2D:checkCollision(dx, dy, filter) function Hitbox2D:checkCollision(filter)
return self:checkCollisionAtPoint(self.owner.x, self.owner.y, filter)
end
function Hitbox2D:checkCollisionAtPoint(dx, dy, filter)
self:updatePosition() self:updatePosition()
local dx, dy = self.ox + dx, self.oy + dy local dx, dy = self.ox + dx, self.oy + dy

View file

@ -118,7 +118,11 @@ end
-- COLLISION FUNCTIONS -- COLLISION FUNCTIONS
-- Handle Hitbox position -- Handle Hitbox position
function Hitbox3D:checkCollision(dx, dy, dz, filter) function Hitbox3D:checkCollision(filter)
return self:checkCollisionAtPoint(self.owner.x, self.owner.y, self.owner.z, filter)
end
function Hitbox3D:checkCollisionAtPoint(dx, dy, dz, filter)
self:updatePosition() self:updatePosition()
local dx, dy = self.ox + dx, self.oy + dy, self.oz + dz local dx, dy = self.ox + dx, self.oy + dy, self.oz + dz