From 4379f55f27280b236c0c708c8248d42fda3889dc Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 6 Aug 2021 20:18:48 +0200 Subject: [PATCH] chore: extract movable from Battler --- .../scenes/battlesystem/actors/battler.lua | 198 +----------------- .../scenes/battlesystem/actors/movable.lua | 196 +++++++++++++++++ 2 files changed, 199 insertions(+), 195 deletions(-) create mode 100644 sonic-radiance.love/scenes/battlesystem/actors/movable.lua diff --git a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua index accd010..6f8b446 100644 --- a/sonic-radiance.love/scenes/battlesystem/actors/battler.lua +++ b/sonic-radiance.love/scenes/battlesystem/actors/battler.lua @@ -1,14 +1,6 @@ -local Parent = require("scenes.battlesystem.actors.parent") +local Parent = require("scenes.battlesystem.actors.movable") local Battler = Parent:extend() -local MOVEMENT_NONE = "none" -local MOVEMENT_TWEENER = "tweener" -local MOVEMENT_MOTION = "motion" - -local ZGRAVITY = 0.2 - -local MIDDLE_ARENA = 6 - local outputColor = { good = {0, 1, 0}, bad = {1, 0, 0}, @@ -18,16 +10,6 @@ local outputColor = { function Battler:new(world, x, y, z, owner) Battler.super.new(self, world, x, y, z) - self.direction = utils.math.sign(MIDDLE_ARENA - x) - - self.start = {} - self.start.x = x - self.start.y = y - self.start.z = z - self.start.direction = self.direction - - self:initMovementSystem() - self.isBattler = true self.speed = 3 self.isActive = false @@ -117,175 +99,6 @@ function Battler:haveFrameSignal(signal) return utils.table.contain(self.frameSignals, signal) end --- MOVE FUNCTIONS --- All functions handling the moving - -local MOVEMENT_DURATION = 0.20 - -function Battler:initMovementSystem() - self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z - self.xspeed, self.yspeed, self.zspeed = 0,0,0 - self.direction = self.start.direction - self.directionPrevious = self.start.direction - self.directionLocked = false - - self.movementType = MOVEMENT_NONE - - self:initJump() -end - -function Battler:stopMoving() - self.xspeed, self.yspeed, self.zspeed = 0,0,0 - if (self.movementType == MOVEMENT_TWEENER) then - self:unlockTag("goTo") - self.tweens:removeNamedTween("goTo") - self.tweens:removeTimer("goTo") - self.tweens:removeTimer("resetMovement") - end - self.movementType = MOVEMENT_NONE - self:updatePreviousPosition() -end - -function Battler:updateMovement(dt) - if (self.movementType == MOVEMENT_TWEENER) then - self:updateTweenerSpeed(dt) - elseif (self.movementType == MOVEMENT_MOTION) then - self:updateMotion(dt) - end - self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2) - - self:updateDirection(dt) - self:updateJump(dt) - self:updatePreviousPosition(dt) -end - -function Battler:updatePreviousPosition() - self.xprevious = self.x - self.yprevious = self.y - self.zprevious = self.z -end - --- Tweener movement functions - -function Battler: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 -end - -function Battler:goTo3D(dx, dy, dz, duration, easing) - local easing = easing or 'inOutQuad' - self:stopMoving() - self:stopJumping() - self.jump.useDefaultAnimation = false - if duration > 0 then - self.tweens:setNamedTween("goTo", 0, duration, {x = dx, y = dy, z = dz}, easing) - end - self.tweens:newTimer(duration + 0.02, "goTo") - self.tweens:newTimer(duration + 0.02, "resetMovement") - - self.movementType = MOVEMENT_TWEENER -end - -function Battler:updateTweenerSpeed(dt) - self.xspeed = (self.x - self.xprevious) / dt - self.yspeed = (self.y - self.yprevious) / dt -end - --- MOTION HANDLING - -function Battler:setMotion(xspeed, yspeed) - self.xspeed = xspeed - self.yspeed = yspeed - self.movementType = MOVEMENT_MOTION -end - -function Battler:updateMotion(dt) - self.x = self.x + (self.xspeed) * dt - self.y = self.y + (self.yspeed) * dt -end - -function Battler:endMotion() - self.movementType = MOVEMENT_NONE - self.xspeed = 0 - self.yspeed = 0 -end - --- Direction handling - -function Battler:updateDirection() - -- Handle direction - if math.abs(self.xspeed) >= 0.01 then - if (self.directionLocked == false) then - self.direction = utils.math.sign(self.xspeed) - end - end -end - --- Jump system - -function Battler:initJump() - self.jump = {} - self.jump.useDefaultAnimation = true - self.jump.isJumping = false - self.jump.bounceNumber = 0 - self.jump.isMotionJump = false -end - -function Battler:stopJumping() - self:initJump() - self.zspeed = 0 -end - -function Battler:setJump(power, bounceNumber, useDefaultAnimation) - self.zspeed = power - self.jump.useDefaultAnimation = useDefaultAnimation - self.jump.bounceNumber = bounceNumber - self.jump.isJumping = true -end - -function Battler:jumpTo(dx, dy, height, speed, useDefaultAnimation) - height = height or 4 - speed = speed or 8 - 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 -end - -function Battler:jumpBack(height, speed) - self:jumpTo(self.start.x, self.start.y, height, speed, true) -end - -function Battler:updateJump(dt) - if (self.jump.isJumping) then - self.zspeed = self.zspeed - ZGRAVITY - self.z = self.z + self.zspeed - if (self.z <= 0) then - if (self.jump.bounceNumber > 0) then - self.zspeed = self.zspeed * -0.5 - self.jump.bounceNumber = self.jump.bounceNumber - 1 - else - self.z = 0 - self.jump.isJumping = false - self.jump.spin = false - self:timerResponse("jump") - if (self.jump.isMotionJump) then - self:endMotion() - end - self:changeAnimation("idle") - end - end - end -end - -- CHOREGRAPHY FUNCTIONS -- All functions related to the choregraphy system @@ -324,19 +137,14 @@ function Battler:timerResponse(signal) self:unlockTag(signal) if (signal == "resetMovement") then - self.movementType = MOVEMENT_NONE + self:resetMovementType() elseif (signal == "removeOutput") then self.showOutput = false end end function Battler:choregraphyEnded() - self.direction = self.start.direction - self.x = self.start.x - self.y = self.start.y - self.xspeed = 0 - self.yspeed = 0 - self.movementType = MOVEMENT_NONE + self:initMovementSystem() end function Battler:getHurt() diff --git a/sonic-radiance.love/scenes/battlesystem/actors/movable.lua b/sonic-radiance.love/scenes/battlesystem/actors/movable.lua new file mode 100644 index 0000000..dc1ab7e --- /dev/null +++ b/sonic-radiance.love/scenes/battlesystem/actors/movable.lua @@ -0,0 +1,196 @@ +local ParentObject = require "scenes.battlesystem.actors.parent" +local Movable = ParentObject:extend() + +local MOVEMENT_NONE = "none" +local MOVEMENT_TWEENER = "tweener" +local MOVEMENT_MOTION = "motion" + +local ZGRAVITY = 0.2 + +local MIDDLE_ARENA = 6 + +function Movable:new(world, x, y, z) + Movable.super.new(self, world, x, y, z) + self.direction = utils.math.sign(MIDDLE_ARENA - x) + + self.start = {} + self.start.x = x + self.start.y = y + self.start.z = z + self.start.direction = self.direction + + self:initMovementSystem() +end + +-- MOVE FUNCTIONS +-- All functions handling the moving + +function Movable:initMovementSystem() + self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z + self.xspeed, self.yspeed, self.zspeed = 0,0,0 + self.direction = self.start.direction + self.directionPrevious = self.start.direction + self.directionLocked = false + + self.movementType = MOVEMENT_NONE + + self:initJump() +end + +function Movable:resetMovementType() + self.movementType = MOVEMENT_NONE +end + +function Movable:stopMoving() + self.xspeed, self.yspeed, self.zspeed = 0,0,0 + if (self.movementType == MOVEMENT_TWEENER) then + self:unlockTag("goTo") + self.tweens:removeNamedTween("goTo") + self.tweens:removeTimer("goTo") + self.tweens:removeTimer("resetMovement") + end + self.movementType = MOVEMENT_NONE + self:updatePreviousPosition() +end + +function Movable:updateMovement(dt) + if (self.movementType == MOVEMENT_TWEENER) then + self:updateTweenerSpeed(dt) + elseif (self.movementType == MOVEMENT_MOTION) then + self:updateMotion(dt) + end + self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2) + + self:updateDirection() + self:updateJump(dt) + + self:updatePreviousPosition() +end + +function Movable:updatePreviousPosition() + self.xprevious = self.x + self.yprevious = self.y + self.zprevious = self.z +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 +end + +function Movable:goTo3D(dx, dy, dz, duration, easing) + local easing = easing or 'inOutQuad' + self:stopMoving() + self:stopJumping() + self.jump.useDefaultAnimation = false + if duration > 0 then + self.tweens:setNamedTween("goTo", 0, duration, {x = dx, y = dy, z = dz}, easing) + end + self.tweens:newTimer(duration + 0.02, "goTo") + self.tweens:newTimer(duration + 0.02, "resetMovement") + + self.movementType = MOVEMENT_TWEENER +end + +function Movable:updateTweenerSpeed(dt) + self.xspeed = (self.x - self.xprevious) / dt + self.yspeed = (self.y - self.yprevious) / dt +end + +-- MOTION HANDLING + +function Movable:setMotion(xspeed, yspeed) + self.xspeed = xspeed + self.yspeed = yspeed + self.movementType = MOVEMENT_MOTION +end + +function Movable:updateMotion(dt) + self.x = self.x + (self.xspeed) * dt + self.y = self.y + (self.yspeed) * dt +end + +function Movable:endMotion() + self.movementType = MOVEMENT_NONE + self.xspeed = 0 + self.yspeed = 0 +end + +-- Direction handling + +function Movable:updateDirection() + -- Handle direction + if math.abs(self.xspeed) >= 0.01 then + if (self.directionLocked == false) then + self.direction = utils.math.sign(self.xspeed) + end + end +end + +-- Jump system + +function Movable:initJump() + self.jump = {} + self.jump.useDefaultAnimation = true + self.jump.isJumping = false + self.jump.bounceNumber = 0 + self.jump.isMotionJump = false +end + +function Movable:stopJumping() + self:initJump() + self.zspeed = 0 +end + +function Movable:setJump(power, bounceNumber, useDefaultAnimation) + self.zspeed = power + self.jump.useDefaultAnimation = useDefaultAnimation + self.jump.bounceNumber = bounceNumber + self.jump.isJumping = true +end + +function Movable:jumpTo(dx, dy, height, speed, useDefaultAnimation) + height = height or 4 + speed = speed or 8 + 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 +end + +function Movable:jumpBack(height, speed) + self:jumpTo(self.start.x, self.start.y, height, speed, true) +end + +function Movable:updateJump(dt) + if (self.jump.isJumping) then + self.zspeed = self.zspeed - ZGRAVITY + self.z = self.z + self.zspeed + if (self.z <= 0) then + if (self.jump.bounceNumber > 0) then + self.zspeed = self.zspeed * -0.5 + self.jump.bounceNumber = self.jump.bounceNumber - 1 + else + self.z = 0 + self.jump.isJumping = false + self.jump.spin = false + self:timerResponse("jump") + if (self.jump.isMotionJump) then + self:endMotion() + end + self:changeAnimation("idle") + end + end + end +end +return Movable \ No newline at end of file