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

398 lines
11 KiB
Lua
Raw Permalink Normal View History

local Battler = require("scenes.battlesystem.actors.battler")
local Hero = Battler:extend()
2019-08-14 22:47:10 +02:00
local gui = require "game.modules.gui"
local StatusBar = require "scenes.battlesystem.gui.statusbar"
2019-08-14 22:47:10 +02:00
local ChoregraphySystem = require "scenes.battlesystem.actors.systems.choregraphy"
2019-08-14 17:32:33 +02:00
-- 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()
2019-08-24 16:13:22 +02:00
self:initCharacter(charid)
2019-08-14 13:56:25 +02:00
self:initSprite()
self:initChoregraphySystem()
self:initVoices()
self.statusbar = StatusBar(self)
self.side = "heroes"
2020-06-05 10:02:04 +02:00
self.doing = "nothing";
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
2019-08-14 17:32:33 +02:00
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
2019-08-14 17:32:33 +02:00
-- 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
2019-08-12 12:37:00 +02:00
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
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)
-- 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
2019-08-14 17:32:33 +02:00
self.xprevious = self.x
self.yprevious = self.y
2019-08-31 17:30:56 +02:00
self.zprevious = self.z
self.statusbar:update(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()
self.startx, self.starty = self.x, self.y
2019-08-31 17:30:56 +02:00
self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
self.direction = 1
self.directionPrevious = 1
2019-08-31 17:30:56 +02:00
self.directionLocked = false
self.unlockDirection = true
2019-08-24 16:13:22 +02:00
self:initJump()
end
2019-08-14 17:32:33 +02:00
2019-08-31 17:30:56 +02:00
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
2019-08-31 17:30:56 +02:00
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
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, timerName)
end
2019-08-31 17:30:56 +02:00
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)
2019-08-24 16:13:22 +02:00
end
function Hero:updateSpeed(dt)
self:applyMotion(dt)
self.xspeed = self.x - self.xprevious
self.yspeed = self.y - self.yprevious
2019-08-31 17:30:56 +02:00
self.zspeed = self.z - self.zprevious
self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)
2019-08-14 17:32:33 +02:00
-- Handle direction
if math.abs(self.xspeed) > 0 then
2019-08-31 17:30:56 +02:00
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
2019-08-14 17:32:33 +02:00
end
self:setCustomSpeed(self.gspeed * 320)
end
function Hero:changeDirection(dt)
2019-08-14 17:32:33 +02:00
-- 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
2019-08-24 16:13:22 +02:00
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')
2019-08-31 17:30:56 +02:00
self.jump.spin = spinjump
2019-08-24 16:13:22 +02:00
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
2019-08-14 17:32:33 +02:00
-- 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
2019-08-14 17:32:33 +02:00
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
2019-08-31 17:30:56 +02:00
elseif timer == 'action_jumpBack' then
self.unlockDirection = true
self:unblockChoregraphy()
elseif timer == self.choregraphy.blockedBy then
self:unblockChoregraphy()
end
end
2019-08-14 17:32:33 +02:00
-- 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
2019-08-14 17:32:33 +02:00
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")
2019-08-14 17:32:33 +02:00
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
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
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