From e916c28630569bf933889d018a00dd16a66206db Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 27 Jun 2019 21:03:34 +0200 Subject: [PATCH] fix(actor2D): simplify gravity system by not handling x by default --- CHANGELOG.md | 7 +++ examples/gameplay/plateform/actors/player.lua | 2 +- gamecore/modules/world/actors/actor2D.lua | 50 ++++++------------- gamecore/modules/world/baseworld.lua | 9 ++-- 4 files changed, 26 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ed7e67..2119b49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,10 +29,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **world2D:** Make the hitbox an object, owned by the actor +- **actor:** Rename all function related to YGravity to just \*Gravity + + ### Fixed - **world:** Remove a forgotten camera debug function +### Removed + +- **actor:** Remove all function related to XGravity + ## 0.5.1 - 2019-06-21 ### Fixed diff --git a/examples/gameplay/plateform/actors/player.lua b/examples/gameplay/plateform/actors/player.lua index 1c95872..67d5781 100644 --- a/examples/gameplay/plateform/actors/player.lua +++ b/examples/gameplay/plateform/actors/player.lua @@ -5,7 +5,7 @@ function Player:new(world, x, y, id) Player.super.new(self, world, "player", x, y, 16, 24, true) self:setSprite("player", 8, 12) self:cloneSprite() - self:setYGravity(480) + self:setGravity(480) self.isPunching = false self.direction = 1 diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index e1970a6..1838669 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -98,27 +98,14 @@ function Actor2D:changeSpeedToCollisionNormal(nx, ny) self.xsp, self.ysp = xsp, ysp end -function Actor2D:checkGroundX() - local dx, dy = self.x + utils.math.sign(self.xgrav), self.y +function Actor2D:checkGround() + local dx, dy = self.x, self.y + utils.math.sign(self.grav) local newx, newy, cols, colNumber = self:checkCollision(dx, dy) for i, col in ipairs(cols) do if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then - if not (self.ygrav == 0) then - if col.normal.x ~= utils.math.sign(self.xgrav) then self.onGround = true end - end - end - end -end - -function Actor2D:checkGroundY() - local dx, dy = self.x, self.y + utils.math.sign(self.ygrav) - local newx, newy, cols, colNumber = self:checkCollision(dx, dy) - - for i, col in ipairs(cols) do - if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then - if not (self.ygrav == 0) then - if col.normal.y ~= utils.math.sign(self.ygrav) then self.onGround = true end + if not (self.grav == 0) then + if col.normal.y ~= utils.math.sign(self.grav) then self.onGround = true end end end end @@ -142,37 +129,28 @@ function Actor2D:checkCollision(dx, dy) end function Actor2D:initGravity() - local xgrav, ygrav - if (self.world.gravity.isDefault) then - self.xgrav = self.world.gravity.xgrav - self.ygrav = self.world.gravity.ygrav + self.grav = self.world.gravity.grav else - self.xgrav = 0 - self.ygrav = 0 + self.grav = 0 end self.onGround = false end -function Actor2D:setXGravity(grav) - self.xgrav = grav -end +function Actor2D:setGravity(grav) + -- It's basically now a function with two roles at once : + -- - activate the gravity + -- - use the gravity value the dev want -function Actor2D:setYGravity(grav) - self.ygrav = grav + self.grav = grav or self.world.gravity.grav end function Actor2D:applyGravity(dt) - self.xsp = self.xsp + self.xgrav * dt - self.ysp = self.ysp + self.ygrav * dt + self.ysp = self.ysp + self.grav * dt - if utils.math.sign(self.ysp) == utils.math.sign(self.ygrav) then - self:checkGroundY( ) - end - - if utils.math.sign(self.xsp) == utils.math.sign(self.xgrav) then - self:checkGroundX( ) + if utils.math.sign(self.ysp) == utils.math.sign(self.grav) then + self:checkGround( ) end end diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index 02007ad..045ba19 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -67,13 +67,12 @@ function BaseWorld:initMap(mapfile) self.mapfile = mapfile end -function BaseWorld:setGravity(xgrav, ygrav, isDefault) - local xgrav = xgrav or 0 - local ygrav = ygrav or 0 - local isDefault = isDefault or 0 +function BaseWorld:setGravity(grav, isDefault) + local grav = grav or 0 + local isDefault = isDefault or false self.gravity = {} - self.gravity.xgrav, self.gravity.ygrav = xgrav, ygrav + self.gravity.grav = grav self.gravity.isDefault = isDefault end