chore:refactor the hero system to remove currentAction function

This commit is contained in:
Kazhnuz 2019-08-24 15:44:07 +02:00
parent 3ab6a0e6e2
commit c78aa214ae
4 changed files with 94 additions and 130 deletions

View file

@ -9,12 +9,21 @@ local gui = require "game.modules.gui"
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
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
core.debug:error("FATAL ERROR: charid not set")
end
@ -24,44 +33,15 @@ function Hero:new(world, x, y, charid, charnumber)
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:initSprite()
self:initChoregraphySystem()
self:initVoices()
self.turnAction = nil
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
-- STATS FUNCTIONS
-- All functions related to stats
function Hero:setHP(value, relative)
if relative == true then
value = game.characters.list[self.charid].stats.hp + value
@ -93,8 +73,7 @@ function Hero:setActive()
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.world.cursor:set(self.startx, self.starty, "cursorMove")
self:talk("turnstart")
@ -106,58 +85,55 @@ end
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)
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
if (self.scene:haveMenus()) then
self:changeDirection(dt)
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
end
-- 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
-- MOVE FUNCTIONS
-- All functions handling the moving
-- Calculate speed to calculate animation speed
local xspeed, yspeed = math.abs(self.x - self.xprevious),
math.abs(self.y - self.yprevious)
function Hero:initMovementSystem()
self.startx, self.starty = self.x, self.y
self.xprevious, self.yprevious = self.x, self.y
self.direction = 1
self.directionPrevious = 1
end
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self:setCustomSpeed(speed * 10)
function Hero:goTo(dx, dy, timerName)
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
local direction = utils.math.sign(self.x - self.xprevious)
if direction ~= 0 then
self.direction = direction
if math.abs(self.xspeed) > 0 then
self.direction = utils.math.sign(self.xspeed)
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.world:resetActiveGrid()
self.scene.menu:set( self )
end
self:setCustomSpeed(self.gspeed * 320)
end
function Hero:updateActionSelection(dt)
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
@ -166,31 +142,6 @@ function Hero:updateActionSelection(dt)
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
@ -208,14 +159,18 @@ function Hero:receiveSignal(action_type, id)
self:attack()
elseif (action_type == "skill") then
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
self:switchActiveBattler( )
end
end
function Hero:receiveBackSignal()
self.currentAction = "selectDirection"
self.world.cursor:set(self.x, self.y)
self.world.cursor:set(self.x, self.y, "cursorMove")
self:goTo(self.startx, self.starty, 'backMove')
self:changeAnimation("walk")
end
@ -225,24 +180,19 @@ function Hero:timerResponse(timer)
self.world:switchActiveBattler()
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
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
@ -365,8 +315,8 @@ function Hero:wait(time)
self.choregraphy.changeAction = false
end
-- SPRITE FUNCTIONS
-- Handle the hero sprite
-- ASSETS FUNCTIONS
-- Load and play assets needed by the character
function Hero:initSprite()
self.assets:addSprite(self.charid, "datas/gamedata/characters/" .. self.charid .. "/sprites")
@ -383,16 +333,20 @@ function Hero:animationEnded(animation)
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
function Hero:initVoices()
self:addVoiceEffect("move")
self:addVoiceEffect("turnstart")
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
-- DRAW FUNCTIONS

View file

@ -16,16 +16,19 @@ function Cursor:new(world)
self.tx = 1
self.ty = 1
self.signal = ""
self.grid = maputils.newEmptyMap()
end
function Cursor:set(x, y)
function Cursor:set(x, y, signal)
self.x = math.max(math.min(x, 12), 1)
self.y = math.max(math.min(y, 07), 1)
self.tx = self.x
self.ty = self.y
self.isActive = true
self.signal = signal or ""
self.world:setActiveGridFromGrid(self.grid)
end
@ -118,7 +121,7 @@ function Cursor:update(dt)
end
if (keys["A"].isPressed) then
self.world:sendSignalToCurrentBattler()
self.world:sendSignalToCurrentBattler(self.signal)
end
self.tweens:update(dt)

View file

@ -40,6 +40,13 @@ function BattleSystem:finishBattle()
self.screen = VictoryScreen(self)
end
function BattleSystem:haveMenus()
for k,v in pairs(self.menusystem.menus) do
return true
end
return false
end
function BattleSystem:update(dt)
self.world:update(dt)
if (self.screen ~= nil) then

View file

@ -273,8 +273,8 @@ function World:finishBattle()
self.scene:finishBattle()
end
function World:sendSignalToCurrentBattler()
self.actionlist[self.turns.current].actor:validateAction()
function World:sendSignalToCurrentBattler(signal)
self.actionlist[self.turns.current].actor:receiveSignal(signal)
end
-- ACTIVEGRID FUNCTIONS