sonic-radiance/sonic-radiance.love/scenes/battlesystem/actors/movable.lua
2021-08-07 00:05:07 +02:00

252 lines
No EOL
6.3 KiB
Lua

local ParentObject = require "scenes.battlesystem.actors.parent"
local Movable = ParentObject:extend()
local MOVEMENT_NONE = "none"
local MOVEMENT_TWEENER = "tweener"
local MOVEMENT_MOTION = "motion"
local MOVEMENT_TARGET = "target"
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.speed = 0
self.angle = 0
self.direction = self.start.direction
self.directionPrevious = self.start.direction
self.directionLocked = false
self.movementType = MOVEMENT_NONE
self:initJump()
self:resetTarget()
end
function Movable:resetMovementType()
self.movementType = MOVEMENT_NONE
end
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.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)
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: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(speed, angle)
self.speed = speed
self.angle = angle
self.movementType = MOVEMENT_MOTION
end
function Movable:setMotionToPoint(speed, dx, dy)
self:setMotion(speed, utils.math.pointDirection(self.x, self.y, 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
end
function Movable:endMotion()
self.movementType = MOVEMENT_NONE
self.xspeed = 0
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()
-- 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
self:setMotionToPoint(speed, dx, dy)
self:setJump(height, 0, useDefaultAnimation)
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