improvement: use a 360 motion system

This commit is contained in:
Kazhnuz 2021-08-06 23:47:20 +02:00
parent 4379f55f27
commit 8dd4e7ba62

View file

@ -28,6 +28,8 @@ end
function Movable:initMovementSystem() function Movable:initMovementSystem()
self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
self.xspeed, self.yspeed, self.zspeed = 0,0,0 self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.speed = 0
self.angle = 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
@ -43,6 +45,7 @@ end
function Movable:stopMoving() function Movable:stopMoving()
self.xspeed, self.yspeed, self.zspeed = 0,0,0 self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.speed = 0
if (self.movementType == MOVEMENT_TWEENER) then if (self.movementType == MOVEMENT_TWEENER) then
self:unlockTag("goTo") self:unlockTag("goTo")
self.tweens:removeNamedTween("goTo") self.tweens:removeNamedTween("goTo")
@ -108,13 +111,18 @@ end
-- MOTION HANDLING -- MOTION HANDLING
function Movable:setMotion(xspeed, yspeed) function Movable:setMotion(speed, angle)
self.xspeed = xspeed self.speed = speed
self.yspeed = yspeed self.angle = angle
self.movementType = MOVEMENT_MOTION self.movementType = MOVEMENT_MOTION
end end
function Movable:setMotionToPoint(speed, dx, dy)
self:setMotion(speed, utils.math.pointDirection(self.x, self.y, dx, dy))
end
function Movable:updateMotion(dt) function Movable:updateMotion(dt)
self.xspeed, self.yspeed = utils.math.lengthdir(self.speed, self.angle)
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
end end
@ -160,11 +168,9 @@ end
function Movable:jumpTo(dx, dy, height, speed, useDefaultAnimation) function Movable:jumpTo(dx, dy, height, speed, useDefaultAnimation)
height = height or 4 height = height or 4
speed = speed or 8 self:setMotionToPoint(speed, dx, dy)
self:setJump(height, 0, useDefaultAnimation) self:setJump(height, 0, useDefaultAnimation)
local dir = utils.math.pointDirection(self.x, self.y, dx, dy)
local hspeed, vspeed = utils.math.lengthdir(speed, dir)
self:setMotion(hspeed, vspeed)
self.jump.isMotionJump = true self.jump.isMotionJump = true
end end