From 0d5068fdaa6ff94d88d67718c9507732bc6be75f Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 2 May 2019 20:10:31 +0200 Subject: [PATCH] modules/world: add initial way to check if an entity is on ground --- gamecore/modules/world/actors/actor2D.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index c4d8836..6437182 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -116,6 +116,7 @@ function Actor2D:setFilter() end function Actor2D:autoMove(dt) + self.onGround = false self:applyGravity(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) if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y) + self:checkGround(col.normal.x, col.normal.y) end end end @@ -157,6 +159,16 @@ function Actor2D:changeSpeedToCollisionNormal(nx, ny) self.xsp, self.ysp = xsp, ysp 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) local cols, colNumber 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.ygrav = 0 end + + self.onGround = false end function Actor2D:setXGravity(grav)