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

View file

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