2021-08-06 20:18:48 +02:00
|
|
|
local ParentObject = require "scenes.battlesystem.actors.parent"
|
|
|
|
local Movable = ParentObject:extend()
|
|
|
|
|
|
|
|
local MOVEMENT_NONE = "none"
|
|
|
|
local MOVEMENT_MOTION = "motion"
|
2021-08-07 00:05:07 +02:00
|
|
|
local MOVEMENT_TARGET = "target"
|
2021-08-06 20:18:48 +02:00
|
|
|
|
2021-08-07 10:26:29 +02:00
|
|
|
local ZGRAVITY = 12
|
2021-08-06 20:18:48 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-08-07 11:09:49 +02:00
|
|
|
self:resetMovement()
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- MOVE FUNCTIONS
|
|
|
|
-- All functions handling the moving
|
|
|
|
|
2021-08-07 11:09:49 +02:00
|
|
|
function Movable:resetMovement()
|
|
|
|
self:resetMotion()
|
|
|
|
self:resetJump()
|
|
|
|
self:resetDirection()
|
2021-08-07 00:05:07 +02:00
|
|
|
self:resetTarget()
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:resetMovementType()
|
|
|
|
self.movementType = MOVEMENT_NONE
|
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:stopMoving()
|
2021-08-07 11:09:49 +02:00
|
|
|
self:resetMotion()
|
2021-08-07 10:07:11 +02:00
|
|
|
self:finishAction("goTo")
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
2021-08-07 11:14:08 +02:00
|
|
|
function Movable:update(dt)
|
|
|
|
Movable.super.update(self, dt)
|
2021-08-07 10:40:07 +02:00
|
|
|
self:updateMotion(dt)
|
2021-08-06 20:18:48 +02:00
|
|
|
self:updateDirection()
|
|
|
|
self:updatePreviousPosition()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:updatePreviousPosition()
|
|
|
|
self.xprevious = self.x
|
|
|
|
self.yprevious = self.y
|
|
|
|
self.zprevious = self.z
|
|
|
|
end
|
|
|
|
|
2021-08-07 10:40:07 +02:00
|
|
|
-- GoTo movement functions
|
2021-08-06 20:18:48 +02:00
|
|
|
|
2021-08-07 11:09:49 +02:00
|
|
|
function Movable:goTo(dx, dy, duration)
|
|
|
|
self:resetMotion()
|
2021-08-07 10:07:11 +02:00
|
|
|
self:setTarget(dx, dy)
|
|
|
|
local speed = utils.math.pointDistance(self.x, self.y, dx, dy) / duration
|
|
|
|
self:setMotionToPoint(speed, dx, dy)
|
2021-08-07 11:09:49 +02:00
|
|
|
|
2021-08-07 10:07:11 +02:00
|
|
|
self.movementType = MOVEMENT_TARGET
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
2021-08-07 11:09:49 +02:00
|
|
|
function Movable:goTo3D(dx, dy, dz, duration)
|
|
|
|
self:resetMotion()
|
|
|
|
self:resetJump()
|
2021-08-07 10:36:44 +02:00
|
|
|
self:setTarget(dx, dy, dz)
|
|
|
|
local speed = utils.math.pointDistance3D(self.x, self.y, self.z, dx, dy, dz) / duration
|
|
|
|
self:setMotionToPoint(speed, dx, dy, dz)
|
2021-08-06 20:18:48 +02:00
|
|
|
|
2021-08-07 10:36:44 +02:00
|
|
|
self.movementType = MOVEMENT_TARGET
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- MOTION HANDLING
|
|
|
|
|
2021-08-07 11:09:49 +02:00
|
|
|
function Movable:resetMotion()
|
|
|
|
self.speed, self.angle = 0, 0
|
2021-08-07 18:48:01 +02:00
|
|
|
self.xspeed, self.yspeed = 0,0
|
2021-08-07 11:09:49 +02:00
|
|
|
|
|
|
|
self:updatePreviousPosition()
|
|
|
|
self:resetMovementType()
|
|
|
|
end
|
|
|
|
|
2021-08-07 10:36:44 +02:00
|
|
|
function Movable:setMotion(speed, angle, vertAngle)
|
2021-08-06 23:47:20 +02:00
|
|
|
self.speed = speed
|
|
|
|
self.angle = angle
|
2021-08-07 10:36:44 +02:00
|
|
|
self.motion3D = (vertAngle ~= nil)
|
|
|
|
if (self.motion3D) then
|
|
|
|
self.vertAngle = vertAngle
|
|
|
|
end
|
2021-08-06 20:18:48 +02:00
|
|
|
self.movementType = MOVEMENT_MOTION
|
|
|
|
end
|
|
|
|
|
2021-08-07 10:36:44 +02:00
|
|
|
function Movable:setMotionToPoint(speed, dx, dy, dz)
|
|
|
|
local angle = utils.math.pointDirection(self.x, self.y, dx, dy)
|
|
|
|
local vertAngle = nil
|
|
|
|
if (dz ~= nil) then
|
|
|
|
local distance2D = utils.math.pointDistance(self.x, self.y, dx, dy)
|
|
|
|
vertAngle = utils.math.pointDirection(0, self.z, distance2D, dz)
|
|
|
|
end
|
|
|
|
self:setMotion(speed, angle, vertAngle)
|
2021-08-06 23:47:20 +02:00
|
|
|
end
|
|
|
|
|
2021-08-06 20:18:48 +02:00
|
|
|
function Movable:updateMotion(dt)
|
2021-08-07 10:07:11 +02:00
|
|
|
self:checkTarget(dt)
|
2021-08-07 10:26:29 +02:00
|
|
|
self.xspeed, self.yspeed, self.zspeed = self:getMotionCoord()
|
2021-08-06 20:18:48 +02:00
|
|
|
self.x = self.x + (self.xspeed) * dt
|
|
|
|
self.y = self.y + (self.yspeed) * dt
|
2021-08-07 10:26:29 +02:00
|
|
|
self.z = self.z + (self.zspeed) * dt
|
|
|
|
|
|
|
|
self:checkGround()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:getMotionCoord()
|
|
|
|
local gspeed, xspeed, yspeed, zspeed
|
|
|
|
if (self.motion3D) then
|
|
|
|
gspeed, zspeed = utils.math.lengthdir(self.speed, self.vertAngle)
|
|
|
|
xspeed, yspeed = utils.math.lengthdir(gspeed, self.angle)
|
|
|
|
else
|
|
|
|
xspeed, yspeed = utils.math.lengthdir(self.speed, self.angle)
|
|
|
|
zspeed = self:getJumpMotion()
|
|
|
|
end
|
|
|
|
|
|
|
|
return xspeed, yspeed, zspeed
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
2021-08-07 00:05:07 +02:00
|
|
|
-- 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
|
2021-08-07 10:07:11 +02:00
|
|
|
return (utils.math.pointDistance(self.x, self.y, self.target.x, self.target.y) <= distance)
|
2021-08-07 00:05:07 +02:00
|
|
|
else
|
|
|
|
return (utils.math.pointDistance3D(self.x, self.y, self.z, self.target.x, self.target.y, self.target.z) <= distance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-07 10:07:11 +02:00
|
|
|
function Movable:checkTarget(dt)
|
2021-08-07 00:05:07 +02:00
|
|
|
if (self:isTargetActive()) then
|
2021-08-07 10:07:11 +02:00
|
|
|
local isNearTarget = self:isNearTarget(self.speed * dt)
|
2021-08-07 00:05:07 +02:00
|
|
|
if (isNearTarget) then
|
2021-08-07 10:07:11 +02:00
|
|
|
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
|
2021-08-07 00:05:07 +02:00
|
|
|
end
|
2021-08-07 10:07:11 +02:00
|
|
|
self:stopMoving()
|
2021-08-07 00:05:07 +02:00
|
|
|
end
|
2021-08-07 10:07:11 +02:00
|
|
|
self:resetTarget()
|
2021-08-07 00:05:07 +02:00
|
|
|
end
|
|
|
|
|
2021-08-06 20:18:48 +02:00
|
|
|
-- Direction handling
|
|
|
|
|
2021-08-07 11:09:49 +02:00
|
|
|
function Movable:resetDirection()
|
|
|
|
self.direction = self.start.direction
|
|
|
|
self.directionPrevious = self.start.direction
|
|
|
|
self.directionLocked = false
|
|
|
|
end
|
|
|
|
|
2021-08-06 20:18:48 +02:00
|
|
|
function Movable:updateDirection()
|
|
|
|
-- Handle direction
|
|
|
|
if math.abs(self.xspeed) >= 0.01 then
|
2021-08-07 18:48:01 +02:00
|
|
|
if not (self.directionLocked or self.jump.lockDir) then
|
2021-08-06 20:18:48 +02:00
|
|
|
self.direction = utils.math.sign(self.xspeed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Jump system
|
|
|
|
|
2021-08-07 11:09:49 +02:00
|
|
|
function Movable:resetJump()
|
2021-08-06 20:18:48 +02:00
|
|
|
self.jump = {}
|
|
|
|
self.jump.useDefaultAnimation = true
|
|
|
|
self.jump.isJumping = false
|
|
|
|
self.jump.bounceNumber = 0
|
2021-08-07 11:09:49 +02:00
|
|
|
self.jump.stopWhenLanding = false
|
2021-08-07 18:48:01 +02:00
|
|
|
self.jump.lockDir = false
|
2021-08-07 11:09:49 +02:00
|
|
|
|
|
|
|
self.zspeed = 0
|
|
|
|
self.motion3D = false
|
|
|
|
self.vertAngle = 0
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:stopJumping()
|
2021-08-07 18:48:01 +02:00
|
|
|
self.z = 0
|
2021-08-07 11:39:34 +02:00
|
|
|
self.tweens:newTimer(0.01, "jump")
|
2021-08-07 11:09:49 +02:00
|
|
|
if (self.jump.stopWhenLanding) then
|
|
|
|
self:resetMotion()
|
|
|
|
end
|
2021-08-07 11:39:34 +02:00
|
|
|
self:resetJump()
|
|
|
|
self:land()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:land()
|
|
|
|
-- Empty function
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:setJump(power, bounceNumber, useDefaultAnimation)
|
2021-08-07 10:26:29 +02:00
|
|
|
self.zspeed = (power * 60)
|
2021-08-06 20:18:48 +02:00
|
|
|
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
|
2021-08-06 23:47:20 +02:00
|
|
|
self:setMotionToPoint(speed, dx, dy)
|
|
|
|
|
2021-08-06 20:18:48 +02:00
|
|
|
self:setJump(height, 0, useDefaultAnimation)
|
2021-08-07 11:09:49 +02:00
|
|
|
self.jump.stopWhenLanding = true
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:jumpBack(height, speed)
|
|
|
|
self:jumpTo(self.start.x, self.start.y, height, speed, true)
|
2021-08-07 18:48:01 +02:00
|
|
|
self.jump.lockDir = true
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
|
2021-08-07 10:26:29 +02:00
|
|
|
function Movable:getJumpMotion()
|
|
|
|
if (self.jump.isJumping) then
|
|
|
|
return self.zspeed - ZGRAVITY
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Movable:checkGround()
|
|
|
|
if (self.z <= 0 and self.jump.isJumping) then
|
|
|
|
if (self.jump.bounceNumber > 0) then
|
|
|
|
self.zspeed = self.zspeed * -0.5
|
|
|
|
self.jump.bounceNumber = self.jump.bounceNumber - 1
|
|
|
|
else
|
2021-08-07 11:09:49 +02:00
|
|
|
self:stopJumping()
|
2021-08-06 20:18:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-07 10:26:29 +02:00
|
|
|
|
2021-08-06 20:18:48 +02:00
|
|
|
return Movable
|