Merge branch 'simpler-gravity' of game-projects/gamecore into master

This commit is contained in:
Kazhnuz 2019-06-27 21:06:05 +02:00 committed by Gitea
commit 81c7dc7cd4
4 changed files with 36 additions and 49 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 - **world2D:** Make the hitbox an object, owned by the actor
- **actor:** Rename all function related to YGravity to just \*Gravity
### Fixed ### Fixed
- **world:** Remove a forgotten camera debug function - **world:** Remove a forgotten camera debug function
### Removed
- **actor:** Remove all function related to XGravity
## 0.5.1 - 2019-06-21 ## 0.5.1 - 2019-06-21
### Fixed ### 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) Player.super.new(self, world, "player", x, y, 16, 24, true)
self:setSprite("player", 8, 12) self:setSprite("player", 8, 12)
self:cloneSprite() self:cloneSprite()
self:setYGravity(480) self:setGravity(480)
self.isPunching = false self.isPunching = false
self.direction = 1 self.direction = 1

View file

@ -98,32 +98,6 @@ function Actor2D:changeSpeedToCollisionNormal(nx, ny)
self.xsp, self.ysp = xsp, ysp self.xsp, self.ysp = xsp, ysp
end end
function Actor2D:checkGroundX()
local dx, dy = self.x + utils.math.sign(self.xgrav), self.y
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
end
end
end
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
@ -142,37 +116,44 @@ function Actor2D:checkCollision(dx, dy)
end end
function Actor2D:initGravity() function Actor2D:initGravity()
local xgrav, ygrav
if (self.world.gravity.isDefault) then if (self.world.gravity.isDefault) then
self.xgrav = self.world.gravity.xgrav self.grav = self.world.gravity.grav
self.ygrav = self.world.gravity.ygrav
else else
self.xgrav = 0 self.grav = 0
self.ygrav = 0
end end
self.onGround = false self.onGround = false
end end
function Actor2D:setXGravity(grav) -- GRAVITY SYSTEM FUNCTIONS
self.xgrav = grav -- All functions related to gravity
end
function Actor2D:setYGravity(grav) function Actor2D:setGravity(grav)
self.ygrav = grav -- It's basically now a function with two roles at once :
-- - activate the gravity
-- - use the gravity value the dev want
self.grav = grav or self.world.gravity.grav
end end
function Actor2D:applyGravity(dt) function Actor2D:applyGravity(dt)
self.xsp = self.xsp + self.xgrav * dt self.ysp = self.ysp + self.grav * dt
self.ysp = self.ysp + self.ygrav * dt
if utils.math.sign(self.ysp) == utils.math.sign(self.ygrav) then if utils.math.sign(self.ysp) == utils.math.sign(self.grav) then
self:checkGroundY( ) self:checkGround( )
end end
end
if utils.math.sign(self.xsp) == utils.math.sign(self.xgrav) then function Actor2D:checkGround()
self:checkGroundX( ) 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.grav == 0) then
if col.normal.y ~= utils.math.sign(self.grav) then self.onGround = true end
end
end
end end
end end

View file

@ -67,13 +67,12 @@ function BaseWorld:initMap(mapfile)
self.mapfile = mapfile self.mapfile = mapfile
end end
function BaseWorld:setGravity(xgrav, ygrav, isDefault) function BaseWorld:setGravity(grav, isDefault)
local xgrav = xgrav or 0 local grav = grav or 0
local ygrav = ygrav or 0 local isDefault = isDefault or false
local isDefault = isDefault or 0
self.gravity = {} self.gravity = {}
self.gravity.xgrav, self.gravity.ygrav = xgrav, ygrav self.gravity.grav = grav
self.gravity.isDefault = isDefault self.gravity.isDefault = isDefault
end end