sonic-radiance/sonic-radiance.love/scenes/battlesystem/actors/hero.lua
2019-08-14 17:32:33 +02:00

201 lines
5.6 KiB
Lua

local Battler = require("scenes.battlesystem.actors.battler")
local Hero = Battler:extend()
-- 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.turnAction = nil
self.startx, self.starty = self.x, self.y
self.dx, self.dy = self.x, self.y
self.direction = 1
self.directionPrevious = 1
if charid == nil then
core.debug:error("FATAL ERROR: charid not set")
end
self.charid = charid
self.actionPerTurn = game.characters.list[self.charid].turns
self.assets:addSprite(charid, "datas/gamedata/characters/" .. charid .. "/sprites")
self.assets.sprites[self.charid]:setCustomSpeed(16)
self:setAnimation("idle")
self:setSprite(charid, 32, 48, true)
self.charnumber = charnumber or 1
end
-- INFO FUNCTIONS
-- All functions to get information from the character
function Hero:getStats()
return game.characters.list[self.charid].stats
end
-- ACTIVITY FUNCTION
-- Function to set or unset activity to the character
function Hero:setActive()
local gridsize = game.characters.list[self.charid].base_stats.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.startx, self.starty = self.x, self.y
self.world.cursor:set(self.startx, self.starty)
self.currentAction = "selectDirection"
self.directionPrevious = self.direction
end
-- UPDATE FUNCTION
-- Update the hero
function Hero:update(dt)
self.keys = self.scene:getKeys(1)
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.yprevious = self.y
-- Move the player toward its destination
self.x = (self.x) + ((self.dx) - (self.x)) * 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),
math.abs(self.y - self.yprevious)
local speed = math.sqrt(xspeed*xspeed + yspeed*yspeed) * 32
self.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
-- Handle direction
local direction = utils.math.sign(self.x - self.xprevious)
if direction ~= 0 then
self.direction = direction
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:setAnimation("idle")
self.currentAction = "selectAttack"
self.scene.menu:set( self )
end
end
function Hero:updateActionSelection(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:updateDirectionSelection(dt)
self.xprevious = self.x
self.yprevious = self.y
self.x = (self.x) + ((self.startx) - (self.x)) * dt*15
self.y = (self.y) + ((self.starty) - (self.y)) * dt*15
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.assets.sprites[self.charid]:setCustomSpeed(speed * 60)
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:setAnimation("idle")
self.direction = self.directionPrevious
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
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()
if (self.currentAction == "selectDirection") then
self:setAnimation("walk")
self.currentAction = "moving"
self.dx, self.dy = self.world.cursor.x, self.world.cursor.y
self.world.cursor:unset( )
end
end
-- DRAW FUNCTIONS
-- Draw everything related to the hero
function Hero:setAnimation(animation)
if (self.animation ~= animation) then
self.animation = animation
self.assets.sprites[self.charid]:changeAnimation(animation, true)
end
end
function Hero:draw()
x, y = self.maputils.gridToPixel(self.x, self.y, true)
--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:drawHUD()
local HUDBASE = -8
local HUDSEP = 152
local x = HUDBASE + (self.charnumber-1)*HUDSEP
local y = 32
self.assets.images["statusbar"]:draw(x, y)
end
return Hero