improvement: add vertical motion to motion

This commit is contained in:
Kazhnuz 2021-08-07 10:26:29 +02:00
parent 0c4e960ec9
commit ee1995b91b

View file

@ -6,7 +6,7 @@ local MOVEMENT_TWEENER = "tweener"
local MOVEMENT_MOTION = "motion" local MOVEMENT_MOTION = "motion"
local MOVEMENT_TARGET = "target" local MOVEMENT_TARGET = "target"
local ZGRAVITY = 0.2 local ZGRAVITY = 12
local MIDDLE_ARENA = 6 local MIDDLE_ARENA = 6
@ -31,11 +31,13 @@ function Movable:initMovementSystem()
self.xspeed, self.yspeed, self.zspeed = 0,0,0 self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.speed = 0 self.speed = 0
self.angle = 0 self.angle = 0
self.vertAngle = 0
self.direction = self.start.direction self.direction = self.start.direction
self.directionPrevious = self.start.direction self.directionPrevious = self.start.direction
self.directionLocked = false self.directionLocked = false
self.movementType = MOVEMENT_NONE self.movementType = MOVEMENT_NONE
self.motion3D = false
self:initJump() self:initJump()
self:resetTarget() self:resetTarget()
@ -68,7 +70,6 @@ function Movable:updateMovement(dt)
end end
self:updateDirection() self:updateDirection()
self:updateJump(dt)
self:updatePreviousPosition() self:updatePreviousPosition()
end end
@ -122,9 +123,25 @@ end
function Movable:updateMotion(dt) function Movable:updateMotion(dt)
self:checkTarget(dt) self:checkTarget(dt)
self.xspeed, self.yspeed = utils.math.lengthdir(self.speed, self.angle) self.xspeed, self.yspeed, self.zspeed = self:getMotionCoord()
self.x = self.x + (self.xspeed) * dt self.x = self.x + (self.xspeed) * dt
self.y = self.y + (self.yspeed) * dt self.y = self.y + (self.yspeed) * dt
self.z = self.z + (self.zspeed) * dt
self:checkGround()
end
function Movable:getMotionCoord()
local gspeed, xspeed, yspeed, zspeed
if (self.motion3D) then
gspeed, zspeed = utils.math.lengthdir(self.speed, self.vertAngle)
xspeed, yspeed = utils.math.lengthdir(gspeed, self.angle)
else
xspeed, yspeed = utils.math.lengthdir(self.speed, self.angle)
zspeed = self:getJumpMotion()
end
return xspeed, yspeed, zspeed
end end
function Movable:endMotion() function Movable:endMotion()
@ -211,7 +228,7 @@ function Movable:stopJumping()
end end
function Movable:setJump(power, bounceNumber, useDefaultAnimation) function Movable:setJump(power, bounceNumber, useDefaultAnimation)
self.zspeed = power self.zspeed = (power * 60)
self.jump.useDefaultAnimation = useDefaultAnimation self.jump.useDefaultAnimation = useDefaultAnimation
self.jump.bounceNumber = bounceNumber self.jump.bounceNumber = bounceNumber
self.jump.isJumping = true self.jump.isJumping = true
@ -229,25 +246,31 @@ function Movable:jumpBack(height, speed)
self:jumpTo(self.start.x, self.start.y, height, speed, true) self:jumpTo(self.start.x, self.start.y, height, speed, true)
end end
function Movable:updateJump(dt) function Movable:getJumpMotion()
if (self.jump.isJumping) then if (self.jump.isJumping) then
self.zspeed = self.zspeed - ZGRAVITY return self.zspeed - ZGRAVITY
self.z = self.z + self.zspeed else
if (self.z <= 0) then return 0
end
end
function Movable:checkGround()
if (self.z <= 0 and self.jump.isJumping) then
if (self.jump.bounceNumber > 0) then if (self.jump.bounceNumber > 0) then
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.z = 0
self.zspeed = 0
self.jump.isJumping = false self.jump.isJumping = false
self.jump.spin = false self.jump.spin = false
self:timerResponse("jump") self:finishAction("jump")
if (self.jump.isMotionJump) then if (self.jump.isMotionJump) then
self:endMotion() self:endMotion()
end end
self:changeAnimation("idle") self:changeAnimation("idle")
end end
end end
end
end end
return Movable return Movable