From c3ee812d1a0fb3ed7a832d83696175ce30e0b382 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 26 Nov 2020 21:08:29 +0100 Subject: [PATCH] chore: put automove in physics mixin Fixes #54 --- birb/modules/world/actors/actor2D.lua | 17 +++---------- birb/modules/world/actors/actor3D.lua | 17 +++---------- birb/modules/world/actors/mixins/physics.lua | 26 ++++++++++++++------ 3 files changed, 24 insertions(+), 36 deletions(-) diff --git a/birb/modules/world/actors/actor2D.lua b/birb/modules/world/actors/actor2D.lua index 752e893..446b912 100644 --- a/birb/modules/world/actors/actor2D.lua +++ b/birb/modules/world/actors/actor2D.lua @@ -61,21 +61,10 @@ end -- PHYSICS FUNCTIONS -- Handle movement and collisions. -function Actor2D:autoMove(dt) - self:updateHitboxes() - self.onGround = false - self:applyGravity(dt) - +function Actor2D:moveToFuturePosition(dt) local dx, dy = self:getFuturePosition(dt) - local newx, newy, cols, colNumber = self:move(dx, dy) - - -- apply after the movement the friction, until the player stop - -- note: the friction is applied according to the delta time, - -- thus the friction should be how much speed is substracted in 1 second - - self:solveAllCollisions(cols) - - self:applyFriction(dt) + local _, _, cols, colNumber = self:move(dx, dy) + return cols, colNumber end function Actor2D:changeSpeedToCollisionNormal(normal) diff --git a/birb/modules/world/actors/actor3D.lua b/birb/modules/world/actors/actor3D.lua index e49ff81..c9941dd 100644 --- a/birb/modules/world/actors/actor3D.lua +++ b/birb/modules/world/actors/actor3D.lua @@ -71,21 +71,10 @@ end -- PHYSICS FUNCTIONS -- Handle movement and collisions. -function Actor3D:autoMove(dt) - self:updateHitboxes() - self.onGround = false - self:applyGravity(dt) - +function Actor3D:moveToFuturePosition(dt) local dx, dy, dz = self:getFuturePosition(dt) - local newx, newy, newz, cols, colNumber = self:move(dx, dy, dz) - - -- apply after the movement the friction, until the player stop - -- note: the friction is applied according to the delta time, - -- thus the friction should be how much speed is substracted in 1 second - - self:solveAllCollisions(cols) - - self:applyFriction(dt) + local _, _, _, cols, colNumber = self:move(dx, dy, dz) + return cols, colNumber end function Actor3D:changeSpeedToCollisionNormal(normal) diff --git a/birb/modules/world/actors/mixins/physics.lua b/birb/modules/world/actors/mixins/physics.lua index be877f4..4bc2fad 100644 --- a/birb/modules/world/actors/mixins/physics.lua +++ b/birb/modules/world/actors/mixins/physics.lua @@ -127,9 +127,19 @@ function PhysicalActor:checkGround() end function PhysicalActor:autoMove(dt) - -- The base actor don't have coordinate - -- so the autoMove is only usefull to its - -- 2D and 3D childrens + self:updateHitboxes() + self.onGround = false + self:applyGravity(dt) + + local cols, colNumber = self:moveToFuturePosition(dt) + + -- apply after the movement the friction, until the player stop + -- note: the friction is applied according to the delta time, + -- thus the friction should be how much speed is substracted in 1 second + + self:solveAllCollisions(cols) + + self:applyFriction(dt) end -- HITBOX FUNCTIONS @@ -234,12 +244,12 @@ function PhysicalActor:applyHitboxCollisions(name, filter) local cols, colNumber = {}, 0 local filter = filter or self.filter if (self.isDestroyed == false) and (self.hitboxes[name] ~= nil) then - cols, colNumber = self.hitboxes[name]:checkCollision(filter) - local type = self.hitboxes[name].type + cols, colNumber = self.hitboxes[name]:checkCollision(filter) + local type = self.hitboxes[name].type - for i, col in ipairs(cols) do - self:hitboxResponse(name, type, col) - end + for i, col in ipairs(cols) do + self:hitboxResponse(name, type, col) + end end return cols, colNumber