409 lines
11 KiB
Lua
409 lines
11 KiB
Lua
local Battler = require("scenes.battlesystem.actors.battler")
|
|
local Hero = Battler:extend()
|
|
|
|
local gui = require "game.modules.gui"
|
|
local StatusBar = require "scenes.battlesystem.gui.statusbar"
|
|
|
|
local ChoregraphySystem = require "scenes.battlesystem.actors.systems.choregraphy"
|
|
|
|
-- 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:initMovementSystem()
|
|
|
|
self:initCharacter(charid)
|
|
|
|
self:initSprite()
|
|
self:initChoregraphySystem()
|
|
|
|
self:initVoices()
|
|
|
|
self.statusbar = StatusBar(self)
|
|
|
|
self.side = "heroes"
|
|
end
|
|
|
|
-- CHARACTER FUNCTIONS
|
|
-- All functions related to character handling
|
|
|
|
function Hero:initCharacter(charid)
|
|
if charid == nil then
|
|
core.debug:error("FATAL ERROR: charid not set")
|
|
end
|
|
self.charid = charid
|
|
self.charnumber = charnumber or 1
|
|
|
|
self.hp = game.characters.list[self.charid].stats.hp
|
|
self.pp = game.characters.list[self.charid].stats.pp
|
|
|
|
self.actionPerTurn = game.characters.list[self.charid].turns
|
|
|
|
self.turnAction = nil
|
|
end
|
|
|
|
function Hero:getStats()
|
|
return game.characters.list[self.charid].stats
|
|
end
|
|
|
|
function Hero:setHP(value, relative)
|
|
if relative == true then
|
|
value = game.characters.list[self.charid].stats.hp + value
|
|
end
|
|
|
|
game.characters.list[self.charid].stats.hp = value
|
|
self.statusbar:updateHP()
|
|
end
|
|
|
|
function Hero:setPP(value, relative)
|
|
if relative == true then
|
|
value = game.characters.list[self.charid].stats.pp + value
|
|
end
|
|
|
|
game.characters.list[self.charid].stats.pp = value
|
|
self.statusbar:updatePP()
|
|
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.x, self.y = utils.math.round(self.x), utils.math.round(self.y)
|
|
self.startx, self.starty = self.x, self.y
|
|
|
|
self.world.cursor:set(self.startx, self.starty, "cursorMove")
|
|
|
|
--self:talk("turnstart")
|
|
|
|
self.directionPrevious = self.direction
|
|
end
|
|
|
|
-- UPDATE FUNCTION
|
|
-- Update the hero
|
|
|
|
function Hero:update(dt)
|
|
Hero.super.update(self, dt)
|
|
-- Get keys to have some keyboard functions
|
|
self.keys = self.scene:getKeys(1)
|
|
|
|
-- Calculate speed to calculate animation speed
|
|
self:updateSpeed(dt)
|
|
|
|
self:updateChoregraphy(dt)
|
|
|
|
if (self.scene:haveMenus()) then
|
|
self:changeDirection(dt)
|
|
end
|
|
|
|
self.xprevious = self.x
|
|
self.yprevious = self.y
|
|
self.zprevious = self.z
|
|
|
|
self.statusbar:update(dt)
|
|
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:changeDirection(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: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
|
|
|
|
-- INFO FUNCTIONS
|
|
-- Getting info about the actor
|
|
|
|
function Hero:getCharacterData()
|
|
return game.characters.list[self.charid]
|
|
end
|
|
|
|
-- ASSETS FUNCTIONS
|
|
-- Load and play assets needed by the character
|
|
|
|
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.blockingChoregraphy) then
|
|
self:unblockChoregraphy()
|
|
end
|
|
end
|
|
|
|
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
|
|
|
|
-- DRAW FUNCTIONS
|
|
-- Draw everything related to the hero
|
|
|
|
function Hero:draw()
|
|
self:drawSprite(0, -self.z)
|
|
end
|
|
|
|
function Hero:drawIcon(x, y)
|
|
local iconID = 1
|
|
self.assets.tileset["charicons"]:drawTile(iconID, x, y)
|
|
end
|
|
|
|
function Hero:drawHUD()
|
|
self.statusbar:draw()
|
|
end
|
|
|
|
return Hero
|