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

191 lines
5 KiB
Lua

local Battler = require("scenes.battlesystem.actors.battler")
local Hero = Battler:extend()
local ChoregraphySystem = require "scenes.battlesystem.actors.systems.choregraphy"
-- 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
-- UPDATE FUNCTION
-- Update the hero
function Hero:update(dt)
Hero.super.update(self, dt)
-- Calculate speed to calculate animation speed
self:updateSpeed(dt)
self.xprevious = self.x
self.yprevious = self.y
self.zprevious = self.z
end
-- MOVE FUNCTIONS
-- All functions handling the moving
local MOVEMENT_DURATION = 0.20
function Hero:initMovementSystem()
self.startx, self.starty = self.x, self.y
self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
self.direction = 1
self.directionPrevious = 1
self.directionLocked = false
self.unlockDirection = true
self:initJump()
end
function Hero:goTo(dx, dy, duration, easing)
local easing = easing or 'inOutQuad'
if duration > 0 then
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
end
self.tweens:newTimer(duration + 0.02, "goTo")
end
function Hero:jumpTo(dx, dy, sizeFactor, duration, spinjump, easing)
local easing = easing or 'inOutQuad'
local dist = utils.math.pointDistance(self.x, self.y, dx, dy)
local jumpHeight = dist * 8 * sizeFactor
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
self.tweens:newTimer(duration + 0.02, "jumpTo")
self:setJump(jumpHeight, spinjump, duration)
end
function Hero:updateSpeed(dt)
self:applyMotion(dt)
self.xspeed = self.x - self.xprevious
self.yspeed = self.y - self.yprevious
self.zspeed = self.z - self.zprevious
self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)
-- Handle direction
if math.abs(self.xspeed) > 0 then
if (self.directionLocked == false) then
self.direction = utils.math.sign(self.xspeed)
end
else
if self.unlockDirection then
self.unlockDirection = false
self.directionLocked = false
end
end
if self.z > 0 and self.jump.spin == false then
if self.zspeed > 0 then
self:changeAnimation("jump")
else
self:changeAnimation("fall")
end
end
self:setCustomSpeed(self.gspeed * 320)
end
function Hero:initJump()
self.jump = {}
self.jump.spin = false
end
function Hero:setJump(size, spinjump, duration)
local tweenDuration = duration / 2
self.tweens:newTween(0, tweenDuration, {z = size}, 'outQuad')
self.tweens:newTween(tweenDuration, tweenDuration, {z = 0}, 'inQuad')
self.jump.spin = spinjump
end
function Hero:setMotionX(direction, speed)
self.motion = speed
self.motionDirection = direction
end
function Hero:applyMotion(dt)
if self.motion ~= 0 and self.motion ~= nil then
local dx = self.x + self.motion * self.motionDirection * dt
-- {1, 0, "line", 5, true}
local ox = self.choregraphy.startx + (self.choregraphy.effectArea[1] * self.choregraphy.direction)
local oy = self.choregraphy.starty + self.choregraphy.effectArea[2]
local shape = self.choregraphy.effectArea[3]
local size = self.choregraphy.effectArea[4]
local direction = self.choregraphy.direction
local new_case_x = utils.math.round(dx)
local new_case_y = utils.math.round(self.y)
print(new_case_x, new_case_y, self.world:caseIsEmpty(new_case_x, new_case_y, self))
if self.maputils.isInMask(dx, self.y, ox, oy, shape, size, direction) and self.world:caseIsEmpty(new_case_x, new_case_y, self) then
self.x = dx
else
self.x = dx
self.motion = 0
if (self.blockingChoregraphy == 'action_dashForward') then
self:unblockChoregraphy()
self.direction = self.choregraphy.direction
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
end
function Hero:choregraphyEnded()
self.direction = 1
end
-- ASSETS FUNCTIONS
-- Load and play assets needed by the character
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")
end
function Hero:animationEnded(animation)
if (self.currentlyBlocking ~= nil and self.blockedBy=="animation") then
self:unblockChoregraphy()
end
end
-- DRAW FUNCTIONS
-- Draw everything related to the hero
function Hero:draw()
self:drawSprite(0, -self.z)
end
return Hero