From 0c4e960ec90d7a8e7e1f2ba10158270e85a6f99a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 7 Aug 2021 10:07:11 +0200 Subject: [PATCH] improvement: use target and motion for goTo --- .../scenes/battlesystem/actors/battler.lua | 12 +++-- .../scenes/battlesystem/actors/movable.lua | 49 ++++++++++--------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua index a6d6ee9..0b907b2 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua @@ -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 diff --git a/sonic-radiance.love/scenes/battlesystem/actors/movable.lua b/sonic-radiance.love/scenes/battlesystem/actors/movable.lua index a4775c9..65812ef 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/movable.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/movable.lua @@ -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()