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 end
function Battler:choregraphyEnded() function Battler:choregraphyEnded()
self:initMovementSystem() self:resetMovement()
end end
function Battler:getHurt() function Battler:getHurt()

View file

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