fix(actor2D): simplify gravity system by not handling x by default

This commit is contained in:
Kazhnuz 2019-06-27 21:03:34 +02:00
parent 9f66df8537
commit e916c28630
4 changed files with 26 additions and 42 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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