improvement(cbs): make cleaner jumps

This commit is contained in:
Kazhnuz 2019-08-31 17:30:56 +02:00
parent ae47502dbf
commit daac217c6c

View file

@ -103,6 +103,7 @@ function Hero:update(dt)
self.xprevious = self.x self.xprevious = self.x
self.yprevious = self.y self.yprevious = self.y
self.zprevious = self.z
end end
-- MOVE FUNCTIONS -- MOVE FUNCTIONS
@ -112,32 +113,39 @@ local MOVEMENT_DURATION = 0.20
function Hero:initMovementSystem() function Hero:initMovementSystem()
self.startx, self.starty = self.x, self.y self.startx, self.starty = self.x, self.y
self.xprevious, self.yprevious = self.x, self.y self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
self.direction = 1 self.direction = 1
self.directionPrevious = 1 self.directionPrevious = 1
self.directionLocked = false
self.unlockDirection = true
self:initJump() self:initJump()
end end
function Hero:getMovementDuration(dx, dy, duration) function Hero:getMovementDuration(dx, dy, factor)
local duration = duration or MOVEMENT_DURATION local factor = factor or 1
local coef = 0.8 local duration = MOVEMENT_DURATION / factor
local coef = 0.5
local dx, dy = dx, dy local dx, dy = dx, dy
local distance = utils.math.pointDistance(self.x, self.y, dx, dy) * coef local distance = utils.math.pointDistance(self.x, self.y, dx, dy) * coef
return duration * distance return duration * distance
end end
function Hero:goTo(dx, dy, timerName, duration) function Hero:goTo(dx, dy, timerName, factor, easing)
local duration = math.max(self:getMovementDuration(dx, dy, duration), 0.30) local easing = easing or 'inOutQuad'
local factor = factor or 1
local duration = math.max(self:getMovementDuration(dx, dy, factor), 0.30)
if duration > 0 then if duration > 0 then
self.tweens:newTween(0, duration, {x = dx, y = dy}, 'inOutQuad') self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
end end
self.tweens:newTimer(duration + 0.02, timerName) self.tweens:newTimer(duration + 0.02, timerName)
end end
function Hero:jumpTo(dx, dy, size, timerName, spinjump, duration) function Hero:jumpTo(dx, dy, size, timerName, spinjump, factor, easing)
local duration = math.max(self:getMovementDuration(dx, dy, duration), 0.30) local easing = easing or 'inOutQuad'
self.tweens:newTween(0, duration, {x = dx, y = dy}, 'inOutQuad') local factor = factor or 1
local duration = math.max(self:getMovementDuration(dx, dy, factor), 0.30)
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
self.tweens:newTimer(duration + 0.02, timerName) self.tweens:newTimer(duration + 0.02, timerName)
self:setJump(size, spinjump, duration) self:setJump(size, spinjump, duration)
end end
@ -146,13 +154,29 @@ function Hero:updateSpeed(dt)
self:applyMotion(dt) self:applyMotion(dt)
self.xspeed = self.x - self.xprevious self.xspeed = self.x - self.xprevious
self.yspeed = self.y - self.yprevious self.yspeed = self.y - self.yprevious
self.zspeed = self.z - self.zprevious
self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2) self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)
-- Handle direction -- Handle direction
if math.abs(self.xspeed) > 0 then if math.abs(self.xspeed) > 0 then
if (self.directionLocked == false) then
self.direction = utils.math.sign(self.xspeed) self.direction = utils.math.sign(self.xspeed)
end end
else
if self.unlockDirection then
self.unlockDirection = false
self.directionLocked = false
end
end
if self.z > 0 and self.jump.spin == false then
if self.zspeed > 0 then
self:changeAnimation("jump")
else
self:changeAnimation("fall")
end
end
self:setCustomSpeed(self.gspeed * 320) self:setCustomSpeed(self.gspeed * 320)
end end
@ -175,6 +199,7 @@ function Hero:setJump(size, spinjump, duration)
local tweenDuration = duration / 2 local tweenDuration = duration / 2
self.tweens:newTween(0, tweenDuration, {z = size}, 'outQuad') self.tweens:newTween(0, tweenDuration, {z = size}, 'outQuad')
self.tweens:newTween(tweenDuration, tweenDuration, {z = 0}, 'inQuad') self.tweens:newTween(tweenDuration, tweenDuration, {z = 0}, 'inQuad')
self.jump.spin = spinjump
end end
function Hero:setMotionX(direction, speed) function Hero:setMotionX(direction, speed)
@ -228,7 +253,7 @@ function Hero:receiveSignal(action_type, id)
self:useSkill(id, self.world.cursor.x, self.world.cursor.y) self:useSkill(id, self.world.cursor.x, self.world.cursor.y)
elseif (action_type == "cursorMove") then elseif (action_type == "cursorMove") then
self:changeAnimation("walk", true) self:changeAnimation("walk", true)
self:goTo(self.world.cursor.x, self.world.cursor.y, 'cursorMove') self:goTo(self.world.cursor.x, self.world.cursor.y, 'cursorMove', 1)
self.assets.sfx["woosh"]:play() self.assets.sfx["woosh"]:play()
self.world.cursor:unset( ) self.world.cursor:unset( )
else else
@ -238,7 +263,7 @@ end
function Hero:receiveBackSignal() function Hero:receiveBackSignal()
self.world.cursor:set(self.x, self.y, "cursorMove") self.world.cursor:set(self.x, self.y, "cursorMove")
self:goTo(self.startx, self.starty, 'backMove') self:goTo(self.startx, self.starty, 'backMove', 1)
self.assets.sfx["woosh"]:play() self.assets.sfx["woosh"]:play()
self:changeAnimation("walk") self:changeAnimation("walk")
@ -256,6 +281,9 @@ function Hero:timerResponse(timer)
elseif timer == 'backMove' then elseif timer == 'backMove' then
self:changeAnimation("idle") self:changeAnimation("idle")
self.direction = self.directionPrevious self.direction = self.directionPrevious
elseif timer == 'action_jumpBack' then
self.unlockDirection = true
self.choregraphy.changeAction = true
elseif timer == self.choregraphy.blockedBy and self.choregraphy.changeAction == false then elseif timer == self.choregraphy.blockedBy and self.choregraphy.changeAction == false then
self.choregraphy.changeAction = true self.choregraphy.changeAction = true
end end
@ -412,13 +440,23 @@ end
function Hero:chorJump(args) function Hero:chorJump(args)
local xx, yy local xx, yy
local spinjump = true
local factor = 1
local easing = 'inOutQuad'
if args.name == "jumpBack" then if args.name == "jumpBack" then
xx, yy = self.choregraphy.startx, self.choregraphy.starty xx, yy = self.choregraphy.startx, self.choregraphy.starty
self.directionLocked = true
spinjump = false
factor = 2
easing = 'outQuad'
elseif args.name == "jumpToCursor" then elseif args.name == "jumpToCursor" then
xx, yy = self.choregraphy.dx, self.choregraphy.dy xx, yy = self.choregraphy.dx, self.choregraphy.dy
end end
self:jumpTo(xx, yy, 48, "action_jumpBack", false) local dist = utils.math.pointDistance(self.x, self.y, xx, yy)
local jumpHeight = dist * 16 / factor
self:jumpTo(xx, yy, jumpHeight, "action_jumpBack", spinjump, 1, easing)
self:blockChoregraphy(args.blockProcess, "action_jumpBack") self:blockChoregraphy(args.blockProcess, "action_jumpBack")
end end