improvement: use target and motion for goTo

This commit is contained in:
Kazhnuz 2021-08-07 10:07:11 +02:00
parent 58e68f0191
commit 0c4e960ec9
2 changed files with 33 additions and 28 deletions

View file

@ -130,10 +130,7 @@ function Battler:unblockChoregraphy()
end
function Battler:timerResponse(signal)
if ((self.currentlyBlocking ~= nil) and (signal == self.blockedBy)) then
self:unblockChoregraphy()
end
self:unlockTag(signal)
self:finishAction(signal)
if (signal == "resetMovement") then
self:resetMovementType()
@ -142,6 +139,13 @@ function Battler:timerResponse(signal)
end
end
function Battler:finishAction(signal)
if ((self.currentlyBlocking ~= nil) and (signal == self.blockedBy)) then
self:unblockChoregraphy()
end
self:unlockTag(signal)
end
function Battler:choregraphyEnded()
self:initMovementSystem()
end

View file

@ -49,13 +49,14 @@ function Movable:stopMoving()
self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.speed = 0
if (self.movementType == MOVEMENT_TWEENER) then
self:unlockTag("goTo")
self:finishAction("goTo")
self.tweens:removeNamedTween("goTo")
self.tweens:removeTimer("goTo")
self.tweens:removeTimer("resetMovement")
end
self.movementType = MOVEMENT_NONE
self:finishAction("goTo")
self:updatePreviousPosition()
self:resetMovementType()
end
function Movable:updateMovement(dt)
@ -81,15 +82,11 @@ end
-- Tweener movement functions
function Movable:goTo(dx, dy, duration, easing)
local easing = easing or 'inOutQuad'
self:stopMoving()
if duration > 0 then
self.tweens:setNamedTween("goTo", 0, duration, {x = dx, y = dy}, easing)
end
self.tweens:newTimer(duration + 0.02, "goTo")
self.tweens:newTimer(duration + 0.02, "resetMovement")
self.movementType = MOVEMENT_TWEENER
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)
@ -124,7 +121,7 @@ function Movable:setMotionToPoint(speed, dx, dy)
end
function Movable:updateMotion(dt)
self:checkTarget()
self:checkTarget(dt)
self.xspeed, self.yspeed = utils.math.lengthdir(self.speed, self.angle)
self.x = self.x + (self.xspeed) * dt
self.y = self.y + (self.yspeed) * dt
@ -159,30 +156,34 @@ function Movable:isNearTarget(distance)
end
if (self.target.z == nil) then
return (utils.math.pointDistance(self.x, self.y, self.target.x, self.target.z) <= distance)
return (utils.math.pointDistance(self.x, self.y, self.target.x, self.target.y) <= distance)
else
return (utils.math.pointDistance3D(self.x, self.y, self.z, self.target.x, self.target.y, self.target.z) <= distance)
end
end
function Movable:checkTarget()
function Movable:checkTarget(dt)
if (self:isTargetActive()) then
local isNearTarget = self:isNearTarget(self.speed)
local isNearTarget = self:isNearTarget(self.speed * dt)
if (isNearTarget) then
-- TODO: add a one-time signal to target handling
self:resetTarget()
if (self.movementType == MOVEMENT_TARGET) then
self.x = self.target.x
self.y = self.target.y
if (self.target.z ~= nil) then
self.z = self.target.z
end
self:stopMoving()
end
self:finishTarget()
end
end
end
function Movable:finishTarget()
-- TODO: add a one-time signal to target handling
if (self.movementType == MOVEMENT_TARGET) then
self.x = self.target.x
self.y = self.target.y
if (self.target.z ~= nil) then
self.z = self.target.z
end
self:stopMoving()
end
self:resetTarget()
end
-- Direction handling
function Movable:updateDirection()