chore(cbs): hero's refactor
This commit is contained in:
parent
eec6214305
commit
d1070b36cc
2 changed files with 151 additions and 107 deletions
|
@ -1,6 +1,9 @@
|
||||||
local Battler = require("scenes.battlesystem.actors.battler")
|
local Battler = require("scenes.battlesystem.actors.battler")
|
||||||
local Hero = Battler:extend()
|
local Hero = Battler:extend()
|
||||||
|
|
||||||
|
-- INIT FUNCTIONS
|
||||||
|
-- Initialize the hero
|
||||||
|
|
||||||
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
|
||||||
|
@ -23,23 +26,15 @@ function Hero:new(world, x, y, charid, charnumber)
|
||||||
self.charnumber = charnumber or 1
|
self.charnumber = charnumber or 1
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hero:setAnimation(animation)
|
-- INFO FUNCTIONS
|
||||||
if (self.animation ~= animation) then
|
-- All functions to get information from the character
|
||||||
self.animation = animation
|
|
||||||
self.assets.sprites[self.charid]:changeAnimation(animation, true)
|
function Hero:getStats()
|
||||||
end
|
return game.characters.list[self.charid].stats
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hero:draw()
|
-- ACTIVITY FUNCTION
|
||||||
x, y = self.maputils.gridToPixel(self.x, self.y, true)
|
-- Function to set or unset activity to the character
|
||||||
--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:setActive()
|
function Hero:setActive()
|
||||||
local gridsize = game.characters.list[self.charid].base_stats.move
|
local gridsize = game.characters.list[self.charid].base_stats.move
|
||||||
|
@ -55,23 +50,45 @@ function Hero:setActive()
|
||||||
self.directionPrevious = self.direction
|
self.directionPrevious = self.direction
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- UPDATE FUNCTION
|
||||||
|
-- Update the hero
|
||||||
|
|
||||||
function Hero:update(dt)
|
function Hero:update(dt)
|
||||||
self.keys = self.scene:getKeys(1)
|
self.keys = self.scene:getKeys(1)
|
||||||
if (self.currentAction == "moving") then
|
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.xprevious = self.x
|
||||||
self.yprevious = self.y
|
self.yprevious = self.y
|
||||||
|
|
||||||
|
-- Move the player toward its destination
|
||||||
self.x = (self.x) + ((self.dx) - (self.x)) * dt*15
|
self.x = (self.x) + ((self.dx) - (self.x)) * dt*15
|
||||||
self.y = (self.y) + ((self.dy) - (self.y)) * dt*15
|
self.y = (self.y) + ((self.dy) - (self.y)) * dt*15
|
||||||
|
|
||||||
|
-- Calculate speed to calculate animation speed
|
||||||
local xspeed, yspeed = math.abs(self.x - self.xprevious),
|
local xspeed, yspeed = math.abs(self.x - self.xprevious),
|
||||||
math.abs(self.y - self.yprevious)
|
math.abs(self.y - self.yprevious)
|
||||||
|
|
||||||
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
|
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
|
||||||
self.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
|
self.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
|
||||||
|
|
||||||
|
-- Handle direction
|
||||||
local direction = utils.math.sign(self.x - self.xprevious)
|
local direction = utils.math.sign(self.x - self.xprevious)
|
||||||
if direction ~= 0 then
|
if direction ~= 0 then
|
||||||
self.direction = direction
|
self.direction = direction
|
||||||
end
|
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
|
if (math.abs(self.x - self.dx) < 0.01) and (math.abs(self.y - self.dy) < 0.01) then
|
||||||
self.x = self.dx
|
self.x = self.dx
|
||||||
self.y = self.dy
|
self.y = self.dy
|
||||||
|
@ -79,13 +96,18 @@ function Hero:update(dt)
|
||||||
self.currentAction = "selectAttack"
|
self.currentAction = "selectAttack"
|
||||||
self.scene.menu:set( self )
|
self.scene.menu:set( self )
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
elseif (self.currentAction == "selectAttack") then
|
function Hero:updateActionSelection(dt)
|
||||||
if (self.keys["B"].isPressed) then
|
-- Change direction by pressing left or right when selecting the next action
|
||||||
--self.currentAction = "selectDirection"
|
if (self.keys["left"].isPressed) then
|
||||||
--self.world.cursor:set(self.x, self.y)
|
self.direction = -1
|
||||||
|
elseif (self.keys["right"].isPressed) then
|
||||||
|
self.direction = 1
|
||||||
end
|
end
|
||||||
elseif (self.currentAction == "selectDirection") then
|
end
|
||||||
|
|
||||||
|
function Hero:updateDirectionSelection(dt)
|
||||||
self.xprevious = self.x
|
self.xprevious = self.x
|
||||||
self.yprevious = self.y
|
self.yprevious = self.y
|
||||||
|
|
||||||
|
@ -108,9 +130,34 @@ function Hero:update(dt)
|
||||||
self:setAnimation("idle")
|
self:setAnimation("idle")
|
||||||
self.direction = self.directionPrevious
|
self.direction = self.directionPrevious
|
||||||
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.world:switchActiveBattler( )
|
||||||
|
else
|
||||||
|
self.world:switchActiveBattler( )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Hero:receiveBackSignal()
|
||||||
|
self.currentAction = "selectDirection"
|
||||||
|
self.world.cursor:set(self.x, self.y)
|
||||||
|
self:setAnimation("walk")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ACTION FUNCTIONS
|
||||||
|
-- All functions related to actions
|
||||||
|
|
||||||
function Hero:validateAction()
|
function Hero:validateAction()
|
||||||
if (self.currentAction == "selectDirection") then
|
if (self.currentAction == "selectDirection") then
|
||||||
self:setAnimation("walk")
|
self:setAnimation("walk")
|
||||||
|
@ -121,28 +168,25 @@ function Hero:validateAction()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hero:getStats()
|
-- DRAW FUNCTIONS
|
||||||
return game.characters.list[self.charid].stats
|
-- Draw everything related to the hero
|
||||||
end
|
|
||||||
|
|
||||||
function Hero:getSignal(action_type, id)
|
function Hero:setAnimation(animation)
|
||||||
--print(action_type .. " " .. id)
|
if (self.animation ~= animation) then
|
||||||
if (action_type == "back") then
|
self.animation = animation
|
||||||
self.currentAction = "selectDirection"
|
self.assets.sprites[self.charid]:changeAnimation(animation, true)
|
||||||
self.world.cursor:set(self.x, self.y)
|
|
||||||
self:setAnimation("walk")
|
|
||||||
elseif (action_type == "defend") then
|
|
||||||
self.turnAction = "defend"
|
|
||||||
self.world:switchActiveBattler( )
|
|
||||||
else
|
|
||||||
self.world:switchActiveBattler( )
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hero:getBackSignal()
|
function Hero:draw()
|
||||||
self.currentAction = "selectDirection"
|
x, y = self.maputils.gridToPixel(self.x, self.y, true)
|
||||||
self.world.cursor:set(self.x, self.y)
|
--love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
|
||||||
self:setAnimation("walk")
|
self:drawSprite()
|
||||||
|
end
|
||||||
|
|
||||||
|
function Hero:drawIcon(x, y)
|
||||||
|
local iconID = 1
|
||||||
|
self.assets.tileset["charicons"]:drawTile(iconID, x, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Hero:drawHUD()
|
function Hero:drawHUD()
|
||||||
|
|
|
@ -151,7 +151,7 @@ function CharMenuWidget:drawCanvas()
|
||||||
end
|
end
|
||||||
|
|
||||||
function CharMenuWidget:action()
|
function CharMenuWidget:action()
|
||||||
self.character:getSignal(self.menuname, self.label)
|
self.character:receiveSignal(self.actionType)
|
||||||
self.scene:flushKeys()
|
self.scene:flushKeys()
|
||||||
self.scene.menusystem:reset()
|
self.scene.menusystem:reset()
|
||||||
end
|
end
|
||||||
|
@ -174,7 +174,7 @@ function BackMenuWidget:new(scene, menu_name, label, character)
|
||||||
end
|
end
|
||||||
|
|
||||||
function BackMenuWidget:action()
|
function BackMenuWidget:action()
|
||||||
self.character:getBackSignal()
|
self.character:receiveBackSignal()
|
||||||
self.scene:flushKeys()
|
self.scene:flushKeys()
|
||||||
self.scene.menusystem:reset()
|
self.scene.menusystem:reset()
|
||||||
end
|
end
|
||||||
|
@ -207,7 +207,7 @@ function SkillWidget:drawCanvas()
|
||||||
end
|
end
|
||||||
|
|
||||||
function SkillWidget:action()
|
function SkillWidget:action()
|
||||||
self.character:getSignal(self.menuname, self.label)
|
self.character:receiveSignal("skill", self.actionType)
|
||||||
self.scene:flushKeys()
|
self.scene:flushKeys()
|
||||||
self.scene.menusystem:reset()
|
self.scene.menusystem:reset()
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue