chore: refactor Movable

This commit is contained in:
Kazhnuz 2021-08-07 11:09:49 +02:00
parent 062070faae
commit a418ecfb21
2 changed files with 40 additions and 45 deletions

View file

@ -147,7 +147,7 @@ function Battler:finishAction(signal)
end
function Battler:choregraphyEnded()
self:initMovementSystem()
self:resetMovement()
end
function Battler:getHurt()

View file

@ -19,26 +19,16 @@ function Movable:new(world, x, y, z)
self.start.z = z
self.start.direction = self.direction
self:initMovementSystem()
self:resetMovement()
end
-- MOVE FUNCTIONS
-- All functions handling the moving
function Movable:initMovementSystem()
self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.speed = 0
self.angle = 0
self.vertAngle = 0
self.direction = self.start.direction
self.directionPrevious = self.start.direction
self.directionLocked = false
self.movementType = MOVEMENT_NONE
self.motion3D = false
self:initJump()
function Movable:resetMovement()
self:resetMotion()
self:resetJump()
self:resetDirection()
self:resetTarget()
end
@ -47,11 +37,8 @@ function Movable:resetMovementType()
end
function Movable:stopMoving()
self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.speed = 0
self:resetMotion()
self:finishAction("goTo")
self:updatePreviousPosition()
self:resetMovementType()
end
function Movable:updateMovement(dt)
@ -68,17 +55,18 @@ end
-- GoTo movement functions
function Movable:goTo(dx, dy, duration, easing)
self:stopMoving()
function Movable:goTo(dx, dy, duration)
self:resetMotion()
self:setTarget(dx, dy)
local speed = utils.math.pointDistance(self.x, self.y, dx, dy) / duration
self:setMotionToPoint(speed, dx, dy)
self.movementType = MOVEMENT_TARGET
end
function Movable:goTo3D(dx, dy, dz, duration, easing)
self:stopMoving()
self:stopJumping()
function Movable:goTo3D(dx, dy, dz, duration)
self:resetMotion()
self:resetJump()
self:setTarget(dx, dy, dz)
local speed = utils.math.pointDistance3D(self.x, self.y, self.z, dx, dy, dz) / duration
self:setMotionToPoint(speed, dx, dy, dz)
@ -88,6 +76,13 @@ end
-- MOTION HANDLING
function Movable:resetMotion()
self.speed, self.angle = 0, 0
self:updatePreviousPosition()
self:resetMovementType()
end
function Movable:setMotion(speed, angle, vertAngle)
self.speed = speed
self.angle = angle
@ -131,12 +126,6 @@ function Movable:getMotionCoord()
return xspeed, yspeed, zspeed
end
function Movable:endMotion()
self.movementType = MOVEMENT_NONE
self.xspeed = 0
self.yspeed = 0
end
-- Target handling
function Movable:setTarget(x, y, z)
@ -190,6 +179,12 @@ end
-- Direction handling
function Movable:resetDirection()
self.direction = self.start.direction
self.directionPrevious = self.start.direction
self.directionLocked = false
end
function Movable:updateDirection()
-- Handle direction
if math.abs(self.xspeed) >= 0.01 then
@ -201,17 +196,25 @@ end
-- Jump system
function Movable:initJump()
function Movable:resetJump()
self.jump = {}
self.jump.useDefaultAnimation = true
self.jump.isJumping = false
self.jump.bounceNumber = 0
self.jump.isMotionJump = false
self.jump.stopWhenLanding = false
self.zspeed = 0
self.motion3D = false
self.vertAngle = 0
end
function Movable:stopJumping()
self:initJump()
self.zspeed = 0
self:resetJump()
self:finishAction("jump")
if (self.jump.stopWhenLanding) then
self:resetMotion()
end
self:changeAnimation("idle")
end
function Movable:setJump(power, bounceNumber, useDefaultAnimation)
@ -226,7 +229,7 @@ function Movable:jumpTo(dx, dy, height, speed, useDefaultAnimation)
self:setMotionToPoint(speed, dx, dy)
self:setJump(height, 0, useDefaultAnimation)
self.jump.isMotionJump = true
self.jump.stopWhenLanding = true
end
function Movable:jumpBack(height, speed)
@ -247,15 +250,7 @@ function Movable:checkGround()
self.zspeed = self.zspeed * -0.5
self.jump.bounceNumber = self.jump.bounceNumber - 1
else
self.z = 0
self.zspeed = 0
self.jump.isJumping = false
self.jump.spin = false
self:finishAction("jump")
if (self.jump.isMotionJump) then
self:endMotion()
end
self:changeAnimation("idle")
self:stopJumping()
end
end
end