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

413 lines
12 KiB
Lua

local Battler = require("scenes.battlesystem.actors.battler")
local Hero = Battler:extend()
local gui = require "game.modules.gui"
-- INIT FUNCTIONS
-- Initialize the hero
function Hero:new(world, x, y, charid, charnumber)
Hero.super.new(self, world, x, y, 0)
self.isHero = true
self.turnAction = nil
self.startx, self.starty = self.x, self.y
self.dx, self.dy = self.x, self.y
self.direction = 1
self.directionPrevious = 1
if charid == nil then
core.debug:error("FATAL ERROR: charid not set")
end
self.charid = charid
self.charnumber = charnumber or 1
self.actionPerTurn = game.characters.list[self.charid].turns
self:initSprite()
self:initChoregraphySystem()
self:initVoices()
end
-- ASSETS FUNCTIONS
-- Load and play assets needed by the character
function Hero:initVoices()
self:addVoiceEffect("move")
self:addVoiceEffect("turnstart")
end
function Hero:addVoiceEffect(name)
local completename = self.charid .. "_" .. name
local path = "datas/gamedata/characters/" .. self.charid .. "/voices/" .. name .. ".wav"
self.assets:newSFX(completename, path)
end
function Hero:talk(name)
local completename = self.charid .. "_" .. name
self.assets.sfx[completename]:play()
end
-- INFO FUNCTIONS
-- All functions to get information from the character
function Hero:getStats()
return game.characters.list[self.charid].stats
end
-- ACTIVITY FUNCTION
-- Function to set or unset activity to the character
function Hero:setActive()
core.debug:print("cbs/hero", "hero " .. self.charid .. " is now active")
local gridsize = game.characters.list[self.charid].move
if (gridsize == nil) then
gridsize = 3
core.debug:warning("cbs/character", "move value is nil")
end
self.world.cursor:setGrid(self.x, self.y, "circle", gridsize, 1, self)
self.startx, self.starty = self.x, self.y
self.world.cursor:set(self.startx, self.starty)
self.currentAction = "selectDirection"
self:talk("turnstart")
self.directionPrevious = self.direction
end
-- UPDATE FUNCTION
-- Update the hero
function Hero:update(dt)
Hero.super.update(self, dt)
self.keys = self.scene:getKeys(1)
if (self.isChoregraphyActive) then
self:updateChoregraphy()
end
if (self.currentAction == "moving") then
self:updateMoving(dt)
elseif (self.currentAction == "selectAttack") then
self:updateActionSelection(dt)
elseif (self.currentAction == "selectDirection") then
self:updateDirectionSelection(dt)
end
end
function Hero:updateMoving(dt)
-- Store previous position, in order to be able to go back here more easily
-- when needed
self.xprevious = self.x
self.yprevious = self.y
-- Move the player toward its destination
self.x = (self.x) + ((self.dx) - (self.x)) * dt*10
self.y = (self.y) + ((self.dy) - (self.y)) * dt*10
-- Calculate speed to calculate animation speed
local xspeed, yspeed = math.abs(self.x - self.xprevious),
math.abs(self.y - self.yprevious)
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self:setCustomSpeed(speed * 10)
-- Handle direction
local direction = utils.math.sign(self.x - self.xprevious)
if direction ~= 0 then
self.direction = direction
end
-- If the hero is near enough its final destination, switch to
-- action selection
if (math.abs(self.x - self.dx) < 0.01) and (math.abs(self.y - self.dy) < 0.01) then
self.x = self.dx
self.y = self.dy
self:changeAnimation("idle")
self.currentAction = "selectAttack"
self.scene.menu:set( self )
end
end
function Hero:updateActionSelection(dt)
-- Change direction by pressing left or right when selecting the next action
if (self.keys["left"].isPressed) then
self.direction = -1
elseif (self.keys["right"].isPressed) then
self.direction = 1
end
end
function Hero:updateDirectionSelection(dt)
self.xprevious = self.x
self.yprevious = self.y
self.x = (self.x) + ((self.startx) - (self.x)) * dt*10
self.y = (self.y) + ((self.starty) - (self.y)) * dt*10
local xspeed, yspeed = math.abs(self.x - self.xprevious),
math.abs(self.y - self.yprevious)
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self:setCustomSpeed(speed * 10)
local direction = utils.math.sign(self.x - self.xprevious)
if direction ~= 0 then
self.direction = direction
end
if (math.abs(self.x - self.startx) < 0.01) and (math.abs(self.y - self.starty) < 0.01) then
self.x = self.startx
self.y = self.starty
self:changeAnimation("idle")
self.direction = self.directionPrevious
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:startChoregraphy("attack")
elseif (action_type == "skill") then
self:startChoregraphy(id)
else
self:switchActiveBattler( )
end
end
function Hero:receiveBackSignal()
self.currentAction = "selectDirection"
self.world.cursor:set(self.x, self.y)
self:changeAnimation("walk")
end
function Hero:timerResponse(timer)
if timer == "switchActiveBattler" then
self.world:switchActiveBattler()
elseif timer == "wait" then
self.choregraphy.changeAction = true
end
end
-- ACTION FUNCTIONS
-- All functions related to actions
function Hero:validateAction()
if (self.currentAction == "selectDirection") then
self:changeAnimation("walk", true)
self.currentAction = "moving"
self.dx, self.dy = self.world.cursor.x, self.world.cursor.y
self:talk("move")
self.world.cursor:unset( )
end
end
function Hero:switchActiveBattler()
self.tweens:newTimer(0.15, "switchActiveBattler")
end
-- CHOREGRAPHY FUNCTIONS
-- All functions related to the choregraphy system
function Hero:initChoregraphySystem()
self.choregraphy = {}
self.choregraphy.current = 0
self.choregraphy.isFinished = false
self.choregraphy.changeAction = true
self.choregraphy.data = {}
self.choregraphy.effectArea = nil
self.choregraphy.dx = self.x
self.choregraphy.dy = self.y
self.choregraphy.blockedBy = ""
self.isChoregraphyActive = false
end
function Hero:startChoregraphy(skill, dx, dy)
local skill = require("datas.gamedata.skills." .. skill)
self.choregraphy.current = 0
self.choregraphy.isFinished = false
self.choregraphy.changeAction = true
self.choregraphy.data = skill.choregraphy
self.choregraphy.effectArea = skill.effectArea
self.choregraphy.dx = dx or self.x
self.choregraphy.dy = dy or self.y
self.choregraphy.haveSentDamage = false
self.isChoregraphyActive = true
end
function Hero:updateChoregraphy(dt)
if (self.choregraphy.changeAction) then
self:switchAction()
end
end
function Hero:switchAction()
self.choregraphy.current = self.choregraphy.current + 1
local nextAction = self.choregraphy.data[self.choregraphy.current]
if nextAction == nil then
self.isChoregraphyActive = false
self:switchActiveBattler()
else
local condition = nextAction[2]
if (condition == "sentDamage") and (not self.choregraphy.haveSentDamage) then
core.debug:print("cbs/hero", "you didn't do damage, skipping " .. nextAction[1])
self:switchAction()
else
self:doChoregraphyAction(nextAction)
end
end
end
function Hero:doChoregraphyAction(choregraphyAction)
local type = choregraphyAction[1] or "unknown"
self.choregraphy.changeAction = true
if type == "wait" then
local duration = choregraphyAction[3] or 1
self:wait(duration)
elseif type == "setAnimation" then
local animation = choregraphyAction[3]
local blockProcess = choregraphyAction[4]
self:changeAnimation(animation)
if (blockProcess) then
self.choregraphy.blockedBy = animation
self.choregraphy.changeAction = false
end
elseif type == "sendDamage" then
local xx = self.x + self.direction
local yy = self.y
local power = choregraphyAction[3]
local accuracy = choregraphyAction[4]
local isSpecial = choregraphyAction[5]
local isAerial = choregraphyAction[6]
self.choregraphy.haveSentDamage = self:sendDamage(xx, yy, power, accuracy, isSpecial, isAerial)
elseif type == "addGFX" then
local sprite = choregraphyAction[3]
local dx = choregraphyAction[4]
local dy = choregraphyAction[5]
local affectedByDirection = choregraphyAction[6]
if (affectedByDirection) then
dx = dx * self.direction
end
local blockProcess = choregraphyAction[7]
local x = self.x
local y = self.y
local z = 0
self.world.obj.GFX(self.world, x + dx, y + dy, z, sprite, self, blockProcess)
elseif type == "playSFX" then
local sfx = choregraphyAction[3]
self.assets.sfx[sfx]:play()
else
core.debug:warning("cbs/hero", "unknown action type " .. type)
end
end
function Hero:wait(time)
self.tweens:newTimer(time, "wait")
self.choregraphy.changeAction = false
end
-- SPRITE FUNCTIONS
-- Handle the hero sprite
function Hero:initSprite()
self.assets:addSprite(self.charid, "datas/gamedata/characters/" .. self.charid .. "/sprites")
self.assets.sprites[self.charid]:setCustomSpeed(16)
self:setSprite(self.charid, 32, 48, true)
self:cloneSprite()
self:changeAnimation("idle")
end
function Hero:animationEnded(animation)
if (animation == self.choregraphy.blockedBy) then
self.choregraphy.blockedBy = ""
self.choregraphy.changeAction = true
end
end
-- STATS FUNCTIONS
-- Handle character stats and stuff
function Hero:setHP(value, relative)
if (relative) then
local hp = game.characters.list[self.charid].stats.hp
value = hp + value
end
game.characters.list[self.charid].stats.hp = value
end
-- DRAW FUNCTIONS
-- Draw everything related to the hero
function Hero:draw()
x, y = self.maputils.gridToPixel(self.x, self.y, true)
--love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
self:drawSprite()
end
function Hero:drawIcon(x, y)
local iconID = 1
self.assets.tileset["charicons"]:drawTile(iconID, x, y)
end
function Hero:drawHUD()
local HUDBASE = 8
local HUDSEP = 152
local x = HUDBASE + (self.charnumber-1)*HUDSEP
local y = self.world:getPlayerHUDPosition()
self.assets.images["e_speedster"]:draw(x, y)
core.screen:setScissor(x, y-16, 32, 40)
self.assets.sprites[self.charid]:drawAnimation(x+14, y+14)
core.screen:resetScissor( )
self.assets.images["m_speedster"]:draw(x, y)
self.assets.images["statusbar"]:draw(x+12, y-6)
local hp = game.characters.list[self.charid].stats.hp
local pp = game.characters.list[self.charid].stats.pp
local hpmax = game.characters.list[self.charid].stats.hpmax
local ppmax = game.characters.list[self.charid].stats.ppmax
local bar1 = math.floor((hp/hpmax)*107)
local bar2 = math.floor((pp/ppmax)*108)
love.graphics.setColor(248/255, 160/255, 0, 1)
gui.drawBar(x+29, y+5, bar1, 7)
love.graphics.setColor(0, 248/255, 248/255, 1)
gui.drawBar(x+17, y+17, bar2, 7)
utils.graphics.resetColor()
self.assets.fonts["hudnbrs_small"]:set()
love.graphics.print(hp .. "/" .. hpmax, x+34, y+5)
love.graphics.print(pp .. "/" .. ppmax, x+28, y+17)
local lvl = game.characters.list[self.charid].stats.level
if lvl < 100 then
lvl = "0" .. lvl
end
love.graphics.print(lvl, x+122, y-5)
end
return Hero