modules/world: add initial way to check if an entity is on ground

This commit is contained in:
Kazhnuz 2019-05-02 20:10:31 +02:00
parent f04d0c6b4e
commit 0d5068fdaa

View file

@ -116,6 +116,7 @@ function Actor2D:setFilter()
end end
function Actor2D:autoMove(dt) function Actor2D:autoMove(dt)
self.onGround = false
self:applyGravity(dt) self:applyGravity(dt)
local newx, newy, cols, colNumber = self:move(self.x + self.xsp * dt, self.y + self.ysp * dt) local newx, newy, cols, colNumber = self:move(self.x + self.xsp * dt, self.y + self.ysp * dt)
@ -135,6 +136,7 @@ function Actor2D:solveAllCollisions(cols)
self:collisionResponse(v) self:collisionResponse(v)
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
self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y) self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y)
self:checkGround(col.normal.x, col.normal.y)
end end
end end
end end
@ -157,6 +159,16 @@ function Actor2D:changeSpeedToCollisionNormal(nx, ny)
self.xsp, self.ysp = xsp, ysp self.xsp, self.ysp = xsp, ysp
end end
function Actor2D:checkGround(nx, ny)
if not (self.xgrav == 0) then
if nx ~= utils.math.sign(self.xgrav) then self.onGround = true end
end
if not (self.ygrav == 0) then
if ny ~= utils.math.sign(self.ygrav) then self.onGround = true end
end
end
function Actor2D:move(dx, dy) function Actor2D:move(dx, dy)
local cols, colNumber local cols, colNumber
self.x, self.y, cols, colNumber = self.world:moveActor(self, dx, dy, self.filter) self.x, self.y, cols, colNumber = self.world:moveActor(self, dx, dy, self.filter)
@ -173,6 +185,8 @@ function Actor2D:initGravity()
self.xgrav = 0 self.xgrav = 0
self.ygrav = 0 self.ygrav = 0
end end
self.onGround = false
end end
function Actor2D:setXGravity(grav) function Actor2D:setXGravity(grav)