chore:refactor the hero system to remove currentAction function
This commit is contained in:
parent
3ab6a0e6e2
commit
c78aa214ae
4 changed files with 94 additions and 130 deletions
|
@ -9,12 +9,21 @@ local gui = require "game.modules.gui"
|
||||||
function Hero:new(world, x, y, charid, charnumber)
|
function Hero:new(world, x, y, charid, charnumber)
|
||||||
Hero.super.new(self, world, x, y, 0)
|
Hero.super.new(self, world, x, y, 0)
|
||||||
self.isHero = true
|
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
|
|
||||||
|
|
||||||
|
self:initMovementSystem()
|
||||||
|
|
||||||
|
self:initCharacter()
|
||||||
|
|
||||||
|
self:initSprite()
|
||||||
|
self:initChoregraphySystem()
|
||||||
|
|
||||||
|
self:initVoices()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- CHARACTER FUNCTIONS
|
||||||
|
-- All functions related to character handling
|
||||||
|
|
||||||
|
function Hero:initCharacter(charid)
|
||||||
if charid == nil then
|
if charid == nil then
|
||||||
core.debug:error("FATAL ERROR: charid not set")
|
core.debug:error("FATAL ERROR: charid not set")
|
||||||
end
|
end
|
||||||
|
@ -24,44 +33,15 @@ function Hero:new(world, x, y, charid, charnumber)
|
||||||
self.hp = game.characters.list[self.charid].stats.hp
|
self.hp = game.characters.list[self.charid].stats.hp
|
||||||
self.pp = game.characters.list[self.charid].stats.pp
|
self.pp = game.characters.list[self.charid].stats.pp
|
||||||
|
|
||||||
|
|
||||||
self.actionPerTurn = game.characters.list[self.charid].turns
|
self.actionPerTurn = game.characters.list[self.charid].turns
|
||||||
|
|
||||||
self:initSprite()
|
self.turnAction = nil
|
||||||
self:initChoregraphySystem()
|
|
||||||
|
|
||||||
self:initVoices()
|
|
||||||
end
|
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()
|
function Hero:getStats()
|
||||||
return game.characters.list[self.charid].stats
|
return game.characters.list[self.charid].stats
|
||||||
end
|
end
|
||||||
|
|
||||||
-- STATS FUNCTIONS
|
|
||||||
-- All functions related to stats
|
|
||||||
|
|
||||||
function Hero:setHP(value, relative)
|
function Hero:setHP(value, relative)
|
||||||
if relative == true then
|
if relative == true then
|
||||||
value = game.characters.list[self.charid].stats.hp + value
|
value = game.characters.list[self.charid].stats.hp + value
|
||||||
|
@ -93,8 +73,7 @@ function Hero:setActive()
|
||||||
end
|
end
|
||||||
self.world.cursor:setGrid(self.x, self.y, "circle", gridsize, 1, self)
|
self.world.cursor:setGrid(self.x, self.y, "circle", gridsize, 1, self)
|
||||||
self.startx, self.starty = self.x, self.y
|
self.startx, self.starty = self.x, self.y
|
||||||
self.world.cursor:set(self.startx, self.starty)
|
self.world.cursor:set(self.startx, self.starty, "cursorMove")
|
||||||
self.currentAction = "selectDirection"
|
|
||||||
|
|
||||||
self:talk("turnstart")
|
self:talk("turnstart")
|
||||||
|
|
||||||
|
@ -106,58 +85,55 @@ end
|
||||||
|
|
||||||
function Hero:update(dt)
|
function Hero:update(dt)
|
||||||
Hero.super.update(self, dt)
|
Hero.super.update(self, dt)
|
||||||
|
-- Get keys to have some keyboard functions
|
||||||
self.keys = self.scene:getKeys(1)
|
self.keys = self.scene:getKeys(1)
|
||||||
|
|
||||||
|
-- Calculate speed to calculate animation speed
|
||||||
|
self:updateSpeed(dt)
|
||||||
|
|
||||||
if (self.isChoregraphyActive) then
|
if (self.isChoregraphyActive) then
|
||||||
self:updateChoregraphy()
|
self:updateChoregraphy()
|
||||||
end
|
end
|
||||||
|
|
||||||
if (self.currentAction == "moving") then
|
if (self.scene:haveMenus()) then
|
||||||
self:updateMoving(dt)
|
self:changeDirection(dt)
|
||||||
elseif (self.currentAction == "selectAttack") then
|
|
||||||
self:updateActionSelection(dt)
|
|
||||||
elseif (self.currentAction == "selectDirection") then
|
|
||||||
self:updateDirectionSelection(dt)
|
|
||||||
end
|
|
||||||
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.xprevious = self.x
|
||||||
self.yprevious = self.y
|
self.yprevious = self.y
|
||||||
|
end
|
||||||
|
|
||||||
-- Move the player toward its destination
|
-- MOVE FUNCTIONS
|
||||||
self.x = (self.x) + ((self.dx) - (self.x)) * dt*10
|
-- All functions handling the moving
|
||||||
self.y = (self.y) + ((self.dy) - (self.y)) * dt*10
|
|
||||||
|
|
||||||
-- Calculate speed to calculate animation speed
|
function Hero:initMovementSystem()
|
||||||
local xspeed, yspeed = math.abs(self.x - self.xprevious),
|
self.startx, self.starty = self.x, self.y
|
||||||
math.abs(self.y - self.yprevious)
|
self.xprevious, self.yprevious = self.x, self.y
|
||||||
|
self.direction = 1
|
||||||
|
self.directionPrevious = 1
|
||||||
|
end
|
||||||
|
|
||||||
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
|
function Hero:goTo(dx, dy, timerName)
|
||||||
self:setCustomSpeed(speed * 10)
|
local DURATION = 0.66
|
||||||
|
self.tweens:newTween(0, DURATION, {x = dx, y = dy}, 'inQuad')
|
||||||
|
self.tweens:newTimer(DURATION + 0.02, timerName)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Hero:updateSpeed(dt)
|
||||||
|
self.xspeed = self.x - self.xprevious
|
||||||
|
self.yspeed = self.y - self.yprevious
|
||||||
|
|
||||||
|
self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)
|
||||||
|
|
||||||
-- Handle direction
|
-- Handle direction
|
||||||
local direction = utils.math.sign(self.x - self.xprevious)
|
if math.abs(self.xspeed) > 0 then
|
||||||
if direction ~= 0 then
|
self.direction = utils.math.sign(self.xspeed)
|
||||||
self.direction = direction
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If the hero is near enough its final destination, switch to
|
self:setCustomSpeed(self.gspeed * 320)
|
||||||
-- 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.world:resetActiveGrid()
|
|
||||||
self.scene.menu:set( self )
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hero:updateActionSelection(dt)
|
function Hero:changeDirection(dt)
|
||||||
-- Change direction by pressing left or right when selecting the next action
|
-- Change direction by pressing left or right when selecting the next action
|
||||||
if (self.keys["left"].isPressed) then
|
if (self.keys["left"].isPressed) then
|
||||||
self.direction = -1
|
self.direction = -1
|
||||||
|
@ -166,31 +142,6 @@ function Hero:updateActionSelection(dt)
|
||||||
end
|
end
|
||||||
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
|
-- SIGNAL FUNCTIONS
|
||||||
-- All functions related to signal receiving
|
-- All functions related to signal receiving
|
||||||
|
|
||||||
|
@ -208,14 +159,18 @@ function Hero:receiveSignal(action_type, id)
|
||||||
self:attack()
|
self:attack()
|
||||||
elseif (action_type == "skill") then
|
elseif (action_type == "skill") then
|
||||||
self:useSkill(id)
|
self:useSkill(id)
|
||||||
|
elseif (action_type == "cursorMove") then
|
||||||
|
self:changeAnimation("walk", true)
|
||||||
|
self:goTo(self.world.cursor.x, self.world.cursor.y, 'cursorMove')
|
||||||
|
self.world.cursor:unset( )
|
||||||
else
|
else
|
||||||
self:switchActiveBattler( )
|
self:switchActiveBattler( )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hero:receiveBackSignal()
|
function Hero:receiveBackSignal()
|
||||||
self.currentAction = "selectDirection"
|
self.world.cursor:set(self.x, self.y, "cursorMove")
|
||||||
self.world.cursor:set(self.x, self.y)
|
self:goTo(self.startx, self.starty, 'backMove')
|
||||||
|
|
||||||
self:changeAnimation("walk")
|
self:changeAnimation("walk")
|
||||||
end
|
end
|
||||||
|
@ -225,24 +180,19 @@ function Hero:timerResponse(timer)
|
||||||
self.world:switchActiveBattler()
|
self.world:switchActiveBattler()
|
||||||
elseif timer == "wait" then
|
elseif timer == "wait" then
|
||||||
self.choregraphy.changeAction = true
|
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ACTION FUNCTIONS
|
-- ACTION FUNCTIONS
|
||||||
-- All functions related to actions
|
-- 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()
|
function Hero:switchActiveBattler()
|
||||||
self.tweens:newTimer(0.15, "switchActiveBattler")
|
self.tweens:newTimer(0.15, "switchActiveBattler")
|
||||||
end
|
end
|
||||||
|
@ -365,8 +315,8 @@ function Hero:wait(time)
|
||||||
self.choregraphy.changeAction = false
|
self.choregraphy.changeAction = false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- SPRITE FUNCTIONS
|
-- ASSETS FUNCTIONS
|
||||||
-- Handle the hero sprite
|
-- Load and play assets needed by the character
|
||||||
|
|
||||||
function Hero:initSprite()
|
function Hero:initSprite()
|
||||||
self.assets:addSprite(self.charid, "datas/gamedata/characters/" .. self.charid .. "/sprites")
|
self.assets:addSprite(self.charid, "datas/gamedata/characters/" .. self.charid .. "/sprites")
|
||||||
|
@ -383,16 +333,20 @@ function Hero:animationEnded(animation)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- STATS FUNCTIONS
|
function Hero:initVoices()
|
||||||
-- Handle character stats and stuff
|
self:addVoiceEffect("move")
|
||||||
|
self:addVoiceEffect("turnstart")
|
||||||
function Hero:setHP(value, relative)
|
|
||||||
if (relative) then
|
|
||||||
local hp = game.characters.list[self.charid].stats.hp
|
|
||||||
value = hp + value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
game.characters.list[self.charid].stats.hp = value
|
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
|
end
|
||||||
|
|
||||||
-- DRAW FUNCTIONS
|
-- DRAW FUNCTIONS
|
||||||
|
|
|
@ -16,16 +16,19 @@ function Cursor:new(world)
|
||||||
self.tx = 1
|
self.tx = 1
|
||||||
self.ty = 1
|
self.ty = 1
|
||||||
|
|
||||||
|
self.signal = ""
|
||||||
|
|
||||||
self.grid = maputils.newEmptyMap()
|
self.grid = maputils.newEmptyMap()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Cursor:set(x, y)
|
function Cursor:set(x, y, signal)
|
||||||
self.x = math.max(math.min(x, 12), 1)
|
self.x = math.max(math.min(x, 12), 1)
|
||||||
self.y = math.max(math.min(y, 07), 1)
|
self.y = math.max(math.min(y, 07), 1)
|
||||||
self.tx = self.x
|
self.tx = self.x
|
||||||
self.ty = self.y
|
self.ty = self.y
|
||||||
|
|
||||||
self.isActive = true
|
self.isActive = true
|
||||||
|
self.signal = signal or ""
|
||||||
|
|
||||||
self.world:setActiveGridFromGrid(self.grid)
|
self.world:setActiveGridFromGrid(self.grid)
|
||||||
end
|
end
|
||||||
|
@ -118,7 +121,7 @@ function Cursor:update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
if (keys["A"].isPressed) then
|
if (keys["A"].isPressed) then
|
||||||
self.world:sendSignalToCurrentBattler()
|
self.world:sendSignalToCurrentBattler(self.signal)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.tweens:update(dt)
|
self.tweens:update(dt)
|
||||||
|
|
|
@ -40,6 +40,13 @@ function BattleSystem:finishBattle()
|
||||||
self.screen = VictoryScreen(self)
|
self.screen = VictoryScreen(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function BattleSystem:haveMenus()
|
||||||
|
for k,v in pairs(self.menusystem.menus) do
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
function BattleSystem:update(dt)
|
function BattleSystem:update(dt)
|
||||||
self.world:update(dt)
|
self.world:update(dt)
|
||||||
if (self.screen ~= nil) then
|
if (self.screen ~= nil) then
|
||||||
|
|
|
@ -273,8 +273,8 @@ function World:finishBattle()
|
||||||
self.scene:finishBattle()
|
self.scene:finishBattle()
|
||||||
end
|
end
|
||||||
|
|
||||||
function World:sendSignalToCurrentBattler()
|
function World:sendSignalToCurrentBattler(signal)
|
||||||
self.actionlist[self.turns.current].actor:validateAction()
|
self.actionlist[self.turns.current].actor:receiveSignal(signal)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ACTIVEGRID FUNCTIONS
|
-- ACTIVEGRID FUNCTIONS
|
||||||
|
|
Loading…
Reference in a new issue