sonic-radiance/sonic-radiance.love/scenes/battlesystem/actors/hero.lua

244 lines
5.6 KiB
Lua
Raw Normal View History

local Battler = require("scenes.battlesystem.actors.battler")
local Hero = Battler:extend()
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
function Hero:new(world, x, y, owner, charnumber)
Hero.super.new(self, world, x, y, 0)
self.isHero = true
self.owner = owner
self:initMovementSystem()
self:initSprite()
self.side = "heroes"
end
2019-08-14 17:32:33 +02:00
-- UPDATE FUNCTION
-- Update the hero
function Hero:update(dt)
2019-08-16 23:04:30 +02:00
Hero.super.update(self, dt)
self:updateMovement(dt)
self:updateAnimation(dt)
end
-- MOVE FUNCTIONS
-- All functions handling the moving
2019-08-14 17:32:33 +02:00
local MOVEMENT_DURATION = 0.20
function Hero:initMovementSystem()
2019-08-31 17:30:56 +02:00
self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.direction = 1
self.directionPrevious = 1
2019-08-31 17:30:56 +02:00
self.directionLocked = false
self.movementType = MOVEMENT_NONE
2019-08-24 16:13:22 +02:00
self:initJump()
end
2019-08-14 17:32:33 +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
function Hero:goTo(dx, dy, duration, easing)
2019-08-31 17:30:56 +02:00
local easing = easing or 'inOutQuad'
if duration > 0 then
2019-08-31 17:30:56 +02:00
self.tweens:newTween(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 Hero:jumpTo(dx, dy, sizeFactor, duration, spinjump, easing)
2019-08-31 17:30:56 +02:00
local easing = easing or 'inOutQuad'
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)
self.tweens:newTimer(duration + 0.02, "jumpTo")
self:setJump(jumpHeight, spinjump, duration)
2019-08-24 16:13:22 +02:00
end
function Hero:updateTweenerSpeed(dt)
self.xspeed = (self.x - self.xprevious) / dt
self.yspeed = (self.y - self.yprevious) / dt
end
-- 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
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
end
-- Jump system
2019-08-24 16:13:22 +02:00
function Hero:initJump()
self.jump = {}
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
function Hero:setJump(power, bounceNumber, useDefaultAnimation)
self.zspeed = power
2019-08-31 17:30:56 +02:00
self.jump.spin = spinjump
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")
end
end
end
end
-- CHOREGRAPHY FUNCTIONS
-- All functions related to the choregraphy system
function Hero:blockChoregraphy(isBlocking, currentlyBlocking, blockedBy)
if (isBlocking) then
self.currentlyBlocking = currentlyBlocking
self.blockedBy = blockedBy
end
end
function Hero:unblockChoregraphy()
self.currentlyBlocking:finish()
self.currentlyBlocking = nil
end
function Hero:timerResponse(signal)
if ((self.currentlyBlocking ~= nil) and (signal == self.blockedBy)) then
self:unblockChoregraphy()
end
if (signal == "resetMovement") then
self.movementType = MOVEMENT_NONE
end
end
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
end
-- ASSETS FUNCTIONS
-- Load and play assets needed by the character
2019-08-14 17:32:33 +02:00
function Hero:initSprite()
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)
self:cloneSprite()
self:changeAnimation("idle")
2019-08-14 17:32:33 +02:00
end
function Hero:animationEnded(animation)
if (self.currentlyBlocking ~= nil and self.blockedBy=="animation") then
self:unblockChoregraphy()
end
end
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)
end
-- 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
return Hero