From 0474c8161a85660633631df5b0b080753144233c Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 17 Jun 2019 11:29:40 +0200 Subject: [PATCH] refactor(levels): use gamecore world physics --- .../scenes/levels/entities/bullet.lua | 24 ++++------- .../scenes/levels/entities/debris.lua | 12 +----- .../scenes/levels/entities/parent.lua | 30 ++++---------- .../scenes/levels/entities/player.lua | 35 ++++++++-------- .../scenes/levels/entities/weapon.lua | 41 +++++++++---------- 5 files changed, 54 insertions(+), 88 deletions(-) diff --git a/imperium-porcorum.love/scenes/levels/entities/bullet.lua b/imperium-porcorum.love/scenes/levels/entities/bullet.lua index c25c5b8..f8d01f1 100644 --- a/imperium-porcorum.love/scenes/levels/entities/bullet.lua +++ b/imperium-porcorum.love/scenes/levels/entities/bullet.lua @@ -18,27 +18,21 @@ function Bullet:setFilter() end end -function Bullet:update(dt) - self:setFilter() - - local cols, cols_len - self.x, self.y, cols, cols_len = self:move(dt) - - for j=1,cols_len do - local other = cols[j].other - if other.type=="wall" and (self.life == 1) then - self:destroy() - end - end - - self.life = 1 - +function Bullet:updateEnd(dt) local isInsideView = self:isInsideView() if (isInsideView == false) or (self.xsp == 0) then self:destroy() end end +function Bullet:collisionResponse(collision) + local other = collision.other + + if other.type=="wall" then + self:destroy() + end +end + function Bullet:draw(dt) currentLevel:drawHitbox(self, 0,128,0) end diff --git a/imperium-porcorum.love/scenes/levels/entities/debris.lua b/imperium-porcorum.love/scenes/levels/entities/debris.lua index 7f5a5d5..1bdfc48 100644 --- a/imperium-porcorum.love/scenes/levels/entities/debris.lua +++ b/imperium-porcorum.love/scenes/levels/entities/debris.lua @@ -8,7 +8,7 @@ function Debris:new(world, x, y, speed, dir, timelimit) self.timer = 0 self.timelimit = timelimit or 2 self:setMotionDirection(dir, speed) - self.grav = 1 + self:setGravityFlag(true) self:setBounceFactor(0.5) self.xfrc = 0.046875*60*1.5*60*4 self.rotation = love.math.random(0, 360) @@ -24,14 +24,7 @@ function Debris:setFilter() end end -function Debris:update(dt) - self:setFilter() - self:gravity(dt) - self:applyFriction(dt) - - --self.x, self.y, cols, cols_len = currentLevel.world:move(self, self.x + self.xsp*dt, self.y + self.ysp*dt, bulletFilter) - cols, cols_len = self:move(dt) - +function Debris:updateEnd(dt) self.life = 1 local isInsideView = self:isInsideView() @@ -43,7 +36,6 @@ function Debris:update(dt) if (self.timer >= self.timelimit) then self:destroy() end - end function Debris:draw(dt) diff --git a/imperium-porcorum.love/scenes/levels/entities/parent.lua b/imperium-porcorum.love/scenes/levels/entities/parent.lua index ce11b1b..20e6590 100644 --- a/imperium-porcorum.love/scenes/levels/entities/parent.lua +++ b/imperium-porcorum.love/scenes/levels/entities/parent.lua @@ -8,13 +8,16 @@ function Entity:new(world, type, x, y, w, h, isSolid) end function Entity:initGravity() - self.grav = 0 self.gacc = 550 - self.onGround = false + Entity.super.initGravity(self) end -function Entity:update(dt) - +function Entity:setGravityFlag(flag) + if (flag) then + self:setYGravity(self.gacc) + else + self:setYGravity() + end end function Entity:setMotion(xsp, ysp) @@ -35,7 +38,7 @@ function Entity:setMotionDirection(dir,spd) end function Entity:gravity(dt) - self.ysp = self.ysp + self.gacc * self.grav * dt + self.ysp = self.ysp + self.ygrav * dt end function Entity:applyFriction(dt) @@ -53,23 +56,6 @@ function Entity:checkGround(ny) end end -function Entity:move(dt) - self.onGround = false - local xsp, ysp = self.xsp * dt, self.ysp * dt - local cols, cols_len - self.x, self.y, cols, cols_len = self.world:moveActor(self, self.x + xsp, self.y + ysp, self.filter) - - for i=1, cols_len do - local col = cols[i] - 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.y) - end - end - - return cols, cols_len -end - function Entity:getDirection() if not (utils.math.sign(self.xsp) == 0) then self.direction = utils.math.sign(self.xsp) diff --git a/imperium-porcorum.love/scenes/levels/entities/player.lua b/imperium-porcorum.love/scenes/levels/entities/player.lua index a4262cf..ff595cc 100644 --- a/imperium-porcorum.love/scenes/levels/entities/player.lua +++ b/imperium-porcorum.love/scenes/levels/entities/player.lua @@ -50,9 +50,10 @@ function Player:initStats() self.acc = 0.046875*60*1.5 self.dec = 0.5*60 self.xfrc = self.acc*60*4 - self.grav = 1 self.jmp = -5*60 + self:setGravityFlag(true) + self.isJumping = false end @@ -64,30 +65,28 @@ end -- UPDATE and COLLISION functions -- Physics and function called every game update -function Player:update(dt) +function Player:updateStart(dt) self.keys = self.scene.sources[1].keys - self:setCustomSpeed(math.abs(self.xsp / 60)) - self:actionMove(dt) + -- Gameplay movement functions + self:actionMove(dt) self:shoot(dt) - self:gravity(dt) self:limitMovement() - self:setFilter() - local cols, cols_len = self:move(dt) - - self:resolveCollisions(cols, cols_len) +end +function Player:updateEnd(dt) self.center.x, self.center.y = self:getCenter() - self:getDirection() - local _, height = self.world:getDimensions() - if self.y >= (height + 64) then self:die() end + -- Cosmetic changes are grouped at the end of the step + -- to be sure they reflect as good as possible the actual situation + self:getDirection() + self:setCustomSpeed(math.abs(self.xsp / 60)) self:setAnimation() end @@ -104,13 +103,11 @@ function Player:setFilter() end end -function Player:resolveCollisions(cols, cols_len) - local cols, cols_len = cols, cols_len - for j=1,cols_len do - local other = cols[j].other - if (other.type == "loot") then - other:takeLoot() - end +function Player:collisionResponse(collision) + local other = collision.other + + if (other.type == "loot") then + other:takeLoot() end end diff --git a/imperium-porcorum.love/scenes/levels/entities/weapon.lua b/imperium-porcorum.love/scenes/levels/entities/weapon.lua index e1bd2c8..a3a71e8 100644 --- a/imperium-porcorum.love/scenes/levels/entities/weapon.lua +++ b/imperium-porcorum.love/scenes/levels/entities/weapon.lua @@ -24,30 +24,11 @@ function Weapon:setFilter() end end -function Weapon:update(dt) - self:setFilter() - +function Weapon:updateStart(dt) self.rotation = self.rotation + (dt * 90)*8 +end - local cols, cols_len = self:move(dt) - - for j=1,cols_len do - local other = cols[j].other - if other.type=="wall" and (self.life == 1) then - self:destroy() - end - if other.type=="block" and (self.life == 1) then - self:destroy() - other:breakBlock() - end - if other.type=="ennemy" and (self.life == 1) then - self:destroy() - other:getDamage(1) - end - end - - self.life = 1 - +function Weapon:updateEnd(dt) local isInsideView = self:isInsideView() if (isInsideView == false) then self:destroy() @@ -56,6 +37,22 @@ function Weapon:update(dt) self:getDirection() end +function Weapon:collisionResponse(collision) + local other = collision.other + + if other.type=="wall" then + self:destroy() + end + if other.type=="block" then + self:destroy() + other:breakBlock() + end + if other.type=="ennemy" then + self:destroy() + other:getDamage(1) + end +end + function Weapon:draw(dt) local rotation = math.floor(self.rotation/45)*45 drawx, drawy = self:getCenter()