From 58e68f01916f16a5f179b3551463e233fb0e2957 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 7 Aug 2021 00:05:07 +0200 Subject: [PATCH] feat: add target-base movement --- .../scenes/battlesystem/actors/movable.lua | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/sonic-radiance.love/scenes/battlesystem/actors/movable.lua b/sonic-radiance.love/scenes/battlesystem/actors/movable.lua index e5cf41b..a4775c9 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/movable.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/movable.lua @@ -4,6 +4,7 @@ local Movable = ParentObject:extend() local MOVEMENT_NONE = "none" local MOVEMENT_TWEENER = "tweener" local MOVEMENT_MOTION = "motion" +local MOVEMENT_TARGET = "target" local ZGRAVITY = 0.2 @@ -37,6 +38,7 @@ function Movable:initMovementSystem() self.movementType = MOVEMENT_NONE self:initJump() + self:resetTarget() end function Movable:resetMovementType() @@ -59,10 +61,10 @@ end function Movable:updateMovement(dt) if (self.movementType == MOVEMENT_TWEENER) then self:updateTweenerSpeed(dt) - elseif (self.movementType == MOVEMENT_MOTION) then + self.speed = math.sqrt(self.xspeed^2 + self.yspeed^2) + elseif (self.movementType == MOVEMENT_MOTION) or (self.movementType == MOVEMENT_TARGET) then self:updateMotion(dt) end - self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2) self:updateDirection() self:updateJump(dt) @@ -122,6 +124,7 @@ function Movable:setMotionToPoint(speed, dx, dy) end function Movable:updateMotion(dt) + self:checkTarget() 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 @@ -133,6 +136,53 @@ function Movable:endMotion() self.yspeed = 0 end +-- Target handling + +function Movable:setTarget(x, y, z) + self.target = {} + self.target.x = x + self.target.y = y + self.target.z = z +end + +function Movable:isTargetActive() + return (self.target ~= nil) +end + +function Movable:resetTarget() + self.target = nil +end + +function Movable:isNearTarget(distance) + if (not self:isTargetActive()) then + return false + end + + if (self.target.z == nil) then + return (utils.math.pointDistance(self.x, self.y, self.target.x, self.target.z) <= 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() + if (self:isTargetActive()) then + local isNearTarget = self:isNearTarget(self.speed) + 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 + end + end +end + -- Direction handling function Movable:updateDirection()