2019-08-14 16:26:23 +02:00
|
|
|
local Battler = require("scenes.battlesystem.actors.battler")
|
|
|
|
local Hero = Battler:extend()
|
2019-03-10 13:11:26 +01:00
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
local MOVEMENT_NONE = "none"
|
|
|
|
local MOVEMENT_TWEENER = "tweener"
|
|
|
|
local MOVEMENT_MOTION = "motion"
|
|
|
|
|
|
|
|
local ZGRAVITY = 0.2
|
|
|
|
|
2019-08-14 17:32:33 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialize the hero
|
|
|
|
|
2020-07-19 17:06:17 +02:00
|
|
|
function Hero:new(world, x, y, owner, charnumber)
|
2019-08-14 16:26:23 +02:00
|
|
|
Hero.super.new(self, world, x, y, 0)
|
2019-03-10 13:11:26 +01:00
|
|
|
self.isHero = true
|
2020-07-19 17:06:17 +02:00
|
|
|
self.owner = owner
|
2019-03-10 13:11:26 +01:00
|
|
|
|
2019-08-24 15:44:07 +02:00
|
|
|
self:initMovementSystem()
|
2019-08-22 22:04:14 +02:00
|
|
|
|
2019-08-14 20:24:32 +02:00
|
|
|
self:initSprite()
|
2019-08-19 15:34:52 +02:00
|
|
|
|
2020-05-01 17:04:17 +02:00
|
|
|
self.side = "heroes"
|
2019-08-19 15:34:52 +02:00
|
|
|
end
|
|
|
|
|
2019-08-14 17:32:33 +02:00
|
|
|
-- UPDATE FUNCTION
|
|
|
|
-- Update the hero
|
|
|
|
|
2019-08-14 16:26:23 +02:00
|
|
|
function Hero:update(dt)
|
2019-08-16 23:04:30 +02:00
|
|
|
Hero.super.update(self, dt)
|
2020-07-25 16:47:29 +02:00
|
|
|
self:updateMovement(dt)
|
2019-08-24 15:44:07 +02:00
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
self:updateAnimation(dt)
|
2019-08-24 15:44:07 +02:00
|
|
|
end
|
2019-03-10 13:11:26 +01:00
|
|
|
|
2019-08-24 15:44:07 +02:00
|
|
|
-- MOVE FUNCTIONS
|
|
|
|
-- All functions handling the moving
|
2019-08-14 17:32:33 +02:00
|
|
|
|
2019-08-24 21:06:05 +02:00
|
|
|
local MOVEMENT_DURATION = 0.20
|
|
|
|
|
2019-08-24 15:44:07 +02:00
|
|
|
function Hero:initMovementSystem()
|
2019-08-31 17:30:56 +02:00
|
|
|
self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
|
2020-07-25 16:47:29 +02:00
|
|
|
self.xspeed, self.yspeed, self.zspeed = 0,0,0
|
2019-08-24 15:44:07 +02:00
|
|
|
self.direction = 1
|
|
|
|
self.directionPrevious = 1
|
2019-08-31 17:30:56 +02:00
|
|
|
self.directionLocked = false
|
2020-07-25 16:47:29 +02:00
|
|
|
|
|
|
|
self.movementType = MOVEMENT_NONE
|
2019-08-24 16:13:22 +02:00
|
|
|
|
|
|
|
self:initJump()
|
2019-08-24 15:44:07 +02:00
|
|
|
end
|
2019-08-14 17:32:33 +02:00
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
function Hero: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 Hero:updatePreviousPosition()
|
|
|
|
self.xprevious = self.x
|
|
|
|
self.yprevious = self.y
|
|
|
|
self.zprevious = self.z
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Tweener movement functions
|
|
|
|
|
2020-07-25 10:24:07 +02:00
|
|
|
function Hero:goTo(dx, dy, duration, easing)
|
2019-08-31 17:30:56 +02:00
|
|
|
local easing = easing or 'inOutQuad'
|
2019-08-24 21:49:16 +02:00
|
|
|
if duration > 0 then
|
2019-08-31 17:30:56 +02:00
|
|
|
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
|
2019-08-24 21:49:16 +02:00
|
|
|
end
|
2020-07-25 10:24:07 +02:00
|
|
|
self.tweens:newTimer(duration + 0.02, "goTo")
|
2020-07-25 16:47:29 +02:00
|
|
|
self.tweens:newTimer(duration + 0.02, "resetMovement")
|
|
|
|
|
|
|
|
self.movementType = MOVEMENT_TWEENER
|
2019-08-24 15:44:07 +02:00
|
|
|
end
|
|
|
|
|
2020-07-25 10:24:07 +02:00
|
|
|
function Hero:jumpTo(dx, dy, sizeFactor, duration, spinjump, easing)
|
2019-08-31 17:30:56 +02:00
|
|
|
local easing = easing or 'inOutQuad'
|
2020-07-25 10:24:07 +02:00
|
|
|
local dist = utils.math.pointDistance(self.x, self.y, dx, dy)
|
|
|
|
local jumpHeight = dist * 8 * sizeFactor
|
2019-08-31 17:30:56 +02:00
|
|
|
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
|
2020-07-25 10:24:07 +02:00
|
|
|
self.tweens:newTimer(duration + 0.02, "jumpTo")
|
|
|
|
self:setJump(jumpHeight, spinjump, duration)
|
2019-08-24 16:13:22 +02:00
|
|
|
end
|
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
function Hero:updateTweenerSpeed(dt)
|
|
|
|
self.xspeed = (self.x - self.xprevious) / dt
|
|
|
|
self.yspeed = (self.y - self.yprevious) / dt
|
|
|
|
end
|
2019-08-24 15:44:07 +02:00
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
-- MOTION HANDLING
|
|
|
|
|
|
|
|
function Hero:setMotion(xspeed, yspeed)
|
|
|
|
self.xspeed = xspeed
|
|
|
|
self.yspeed = yspeed
|
|
|
|
self.movementType = MOVEMENT_MOTION
|
|
|
|
end
|
|
|
|
|
|
|
|
function Hero:updateMotion(dt)
|
|
|
|
self.x = self.x + (self.xspeed) * dt
|
|
|
|
self.y = self.y + (self.yspeed) * dt
|
|
|
|
end
|
2019-08-14 17:32:33 +02:00
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
function Hero:endMotion()
|
|
|
|
self.movementType = MOVEMENT_NONE
|
|
|
|
self.xspeed = 0
|
|
|
|
self.yspeed = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Direction handling
|
|
|
|
|
|
|
|
function Hero:updateDirection()
|
2019-08-14 17:32:33 +02:00
|
|
|
-- Handle direction
|
2020-07-25 17:07:53 +02:00
|
|
|
if math.abs(self.xspeed) >= 0.01 then
|
2019-08-31 17:30:56 +02:00
|
|
|
if (self.directionLocked == false) then
|
|
|
|
self.direction = utils.math.sign(self.xspeed)
|
|
|
|
end
|
|
|
|
end
|
2019-03-10 13:11:26 +01:00
|
|
|
end
|
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
-- Jump system
|
|
|
|
|
2019-08-24 16:13:22 +02:00
|
|
|
function Hero:initJump()
|
|
|
|
self.jump = {}
|
2020-07-25 16:47:29 +02:00
|
|
|
self.jump.useDefaultAnimation = true
|
|
|
|
self.jump.isJumping = false
|
|
|
|
self.jump.bounceNumber = 0
|
|
|
|
self.jump.isJumpingBack = false
|
2019-08-24 16:13:22 +02:00
|
|
|
end
|
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
function Hero:setJump(power, bounceNumber, useDefaultAnimation)
|
|
|
|
self.zspeed = power
|
2019-08-31 17:30:56 +02:00
|
|
|
self.jump.spin = spinjump
|
2020-07-25 16:47:29 +02:00
|
|
|
self.jump.bounceNumber = bounceNumber
|
|
|
|
self.jump.isJumping = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Hero:jumpBack()
|
|
|
|
self:setJump(3, 0, true)
|
|
|
|
self:setMotion(-10, 0)
|
|
|
|
self.jump.isJumpingBack = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Hero: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.isJumpingBack) then
|
|
|
|
self:endMotion()
|
|
|
|
end
|
|
|
|
self:changeAnimation("idle")
|
2019-08-24 21:46:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-18 19:02:14 +02:00
|
|
|
-- CHOREGRAPHY FUNCTIONS
|
|
|
|
-- All functions related to the choregraphy system
|
|
|
|
|
2020-07-25 08:43:15 +02:00
|
|
|
function Hero:blockChoregraphy(isBlocking, currentlyBlocking, blockedBy)
|
|
|
|
if (isBlocking) then
|
|
|
|
self.currentlyBlocking = currentlyBlocking
|
|
|
|
self.blockedBy = blockedBy
|
|
|
|
end
|
2019-08-18 19:02:14 +02:00
|
|
|
end
|
|
|
|
|
2020-07-25 08:43:15 +02:00
|
|
|
function Hero:unblockChoregraphy()
|
|
|
|
self.currentlyBlocking:finish()
|
|
|
|
self.currentlyBlocking = nil
|
2019-08-25 15:54:26 +02:00
|
|
|
end
|
|
|
|
|
2020-07-25 10:24:07 +02:00
|
|
|
function Hero:timerResponse(signal)
|
|
|
|
if ((self.currentlyBlocking ~= nil) and (signal == self.blockedBy)) then
|
2020-07-25 08:43:15 +02:00
|
|
|
self:unblockChoregraphy()
|
2019-08-24 22:21:07 +02:00
|
|
|
end
|
2020-07-25 16:47:29 +02:00
|
|
|
|
|
|
|
if (signal == "resetMovement") then
|
|
|
|
self.movementType = MOVEMENT_NONE
|
|
|
|
end
|
2019-08-24 22:21:07 +02:00
|
|
|
end
|
|
|
|
|
2020-07-25 10:24:07 +02:00
|
|
|
function Hero:choregraphyEnded()
|
|
|
|
self.direction = 1
|
2020-07-25 17:07:53 +02:00
|
|
|
self.x = self.start.x
|
|
|
|
self.y = self.start.y
|
|
|
|
self.xspeed = 0
|
|
|
|
self.yspeed = 0
|
|
|
|
self.movementType = MOVEMENT_NONE
|
2020-07-25 10:24:07 +02:00
|
|
|
end
|
|
|
|
|
2019-08-24 15:44:07 +02:00
|
|
|
-- ASSETS FUNCTIONS
|
|
|
|
-- Load and play assets needed by the character
|
2019-08-14 17:32:33 +02:00
|
|
|
|
2019-08-14 20:24:32 +02:00
|
|
|
function Hero:initSprite()
|
2020-07-19 17:06:17 +02:00
|
|
|
self.assets:addSprite(self.owner.name, "datas/gamedata/characters/" .. self.owner.name .. "/sprites")
|
|
|
|
self.assets.sprites[self.owner.name]:setCustomSpeed(16)
|
|
|
|
self:setSprite(self.owner.name, 32, 48, true)
|
2019-08-14 20:24:32 +02:00
|
|
|
self:cloneSprite()
|
|
|
|
self:changeAnimation("idle")
|
2019-08-14 17:32:33 +02:00
|
|
|
end
|
|
|
|
|
2019-08-14 20:44:20 +02:00
|
|
|
function Hero:animationEnded(animation)
|
2020-07-25 08:43:15 +02:00
|
|
|
if (self.currentlyBlocking ~= nil and self.blockedBy=="animation") then
|
2020-05-02 13:01:43 +02:00
|
|
|
self:unblockChoregraphy()
|
2019-08-14 20:44:20 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-25 16:47:29 +02:00
|
|
|
function Hero:updateAnimation(dt)
|
|
|
|
if (self.z > 0 and self.jump.useDefaultAnimation) then
|
|
|
|
if self.zspeed > 0 then
|
|
|
|
self:changeAnimation("jump")
|
|
|
|
else
|
|
|
|
self:changeAnimation("fall")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-25 17:07:53 +02:00
|
|
|
self:setCustomSpeed(self.gspeed * 160 * dt)
|
2020-07-25 16:47:29 +02:00
|
|
|
end
|
|
|
|
|
2019-08-14 20:24:32 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw everything related to the hero
|
|
|
|
|
2019-08-14 17:32:33 +02:00
|
|
|
function Hero:draw()
|
2019-08-24 16:13:22 +02:00
|
|
|
self:drawSprite(0, -self.z)
|
2019-08-14 17:32:33 +02:00
|
|
|
end
|
|
|
|
|
2019-08-14 16:26:23 +02:00
|
|
|
return Hero
|