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

289 lines
8 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:initChoregraphySystem()
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:updateChoregraphy(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:getMovementDuration(dx, dy, factor)
local factor = factor or 1
local duration = MOVEMENT_DURATION / factor
local coef = 0.5
local dx, dy = dx, dy
local distance = utils.math.pointDistance(self.x, self.y, dx, dy) * coef
return duration * distance
end
function Hero:goTo(dx, dy, timerName, factor, easing)
local easing = easing or 'inOutQuad'
local factor = factor or 1
local duration = math.max(self:getMovementDuration(dx, dy, factor), 0.30)
if duration > 0 then
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
end
self.tweens:newTimer(duration + 0.02, timerName)
end
function Hero:jumpTo(dx, dy, size, timerName, spinjump, factor, easing)
local easing = easing or 'inOutQuad'
local factor = factor or 1
local duration = math.max(self:getMovementDuration(dx, dy, factor), 0.30)
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
self.tweens:newTimer(duration + 0.02, timerName)
self:setJump(size, 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
-- SIGNAL FUNCTIONS
-- All functions related to signal receiving
function Hero:receiveSignal(action_type, id)
if id == nil then
core.debug:print("battler/hero", "action selected : " .. action_type)
else
core.debug:print("battler/hero", "action selected : " .. action_type .. " (" .. id .. ")")
end
if (action_type == "defend") then
self.turnAction = "defend"
self:switchActiveBattler( )
elseif (action_type == "attack") then
--self:changeAnimation("hit1")
self:attack()
elseif (action_type == "skill") then
self.world.cursor:unset( )
self:useSkill(id, self.world.cursor.x, self.world.cursor.y)
elseif (action_type == "cursorMove") then
if (self.x ~= self.world.cursor.x) or (self.y ~= self.world.cursor.y) then
self:changeAnimation("walk", true)
self:goTo(self.world.cursor.x, self.world.cursor.y, 'cursorMove', 1)
self.assets.sfx["woosh"]:play()
else
self.world:resetActiveGrid()
self.scene.menu:set( self )
end
self.world.cursor:unset( )
else
self:switchActiveBattler( )
end
end
function Hero:positionSelected(x, y)
self:changeAnimation("walk", true)
self:goTo(self.world.cursor.x, self.world.cursor.y, 'cursorMove', 1)
self.assets.sfx["woosh"]:play()
end
function Hero:receiveBackSignal()
self.world.cursor:set(self.x, self.y, "cursorMove")
if (self.x ~= self.startx) or (self.y ~= self.starty) then
self.world.cursor:set(self.x, self.y, "cursorMove")
self.assets.sfx["woosh"]:play()
self:changeAnimation("walk")
end
end
function Hero:timerResponse(timer)
if timer == "switchActiveBattler" then
self.scene.turns:nextAction()
elseif timer == "wait" then
self.choregraphy.changeAction = true
elseif timer == "cursorMove" then
self:changeAnimation("idle")
self.world:resetActiveGrid()
self.scene.menu:set( self )
elseif timer == 'backMove' then
self:changeAnimation("idle")
self.direction = self.directionPrevious
elseif timer == 'action_jumpBack' then
self.unlockDirection = true
self:unblockChoregraphy()
elseif timer == self.choregraphy.blockedBy then
self:unblockChoregraphy()
end
end
-- ACTION FUNCTIONS
-- All functions related to actions
function Hero:switchActiveBattler()
print("Switching Active Battler")
self.tweens:newTimer(0.15, "switchActiveBattler")
end
-- CHOREGRAPHY FUNCTIONS
-- All functions related to the choregraphy system
function Hero:initChoregraphySystem()
self.choregraphy = ChoregraphySystem(self)
self.blockingChoregraphy = nil
end
function Hero:attack(id, dx, dy)
local skill = game.skills:getSkillData("attack")
self.choregraphy:start(skill, dx, dy)
end
function Hero:useSkill(id, dx, dy)
local skill = game.skills:getSkillData(id)
self:setPP(skill.cost * -1, true)
self.choregraphy:start(skill, dx, dy)
end
function Hero:updateChoregraphy(dt)
self.choregraphy:update(dt)
end
function Hero:blockChoregraphy(isBlocked, blockedBy)
if (isBlocked) then
self.blockingChoregraphy = blockedBy
end
end
function Hero:unblockChoregraphy()
self.choregraphy:endAction()
self.blockingChoregraphy = ""
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 (animation == self.blockingChoregraphy) then
self:unblockChoregraphy()
end
end
-- DRAW FUNCTIONS
-- Draw everything related to the hero
function Hero:draw()
self:drawSprite(0, -self.z)
end
return Hero