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

269 lines
7.6 KiB
Lua
Raw 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"
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.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
2019-08-13 22:28:05 +02:00
core.debug:error("FATAL ERROR: charid not set")
end
self.charid = charid
self.charnumber = charnumber or 1
2019-08-12 12:37:00 +02:00
self.actionPerTurn = game.characters.list[self.charid].turns
2019-08-14 13:56:25 +02:00
self:initSprite()
end
2019-08-14 17:32:33 +02:00
-- INFO FUNCTIONS
-- All functions to get information from the character
2019-08-14 17:32:33 +02:00
function Hero:getStats()
return game.characters.list[self.charid].stats
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.startx, self.starty = self.x, self.y
self.world.cursor:set(self.startx, self.starty)
self.currentAction = "selectDirection"
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)
self.keys = self.scene:getKeys(1)
if (self.currentAction == "moving") then
2019-08-14 17:32:33 +02:00
self:updateMoving(dt)
elseif (self.currentAction == "selectAttack") then
2019-08-14 17:32:33 +02:00
self:updateActionSelection(dt)
elseif (self.currentAction == "selectDirection") then
2019-08-14 17:32:33 +02:00
self:updateDirectionSelection(dt)
end
end
2019-08-14 17:32:33 +02:00
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
2019-08-14 17:32:33 +02:00
-- 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
2019-08-14 17:32:33 +02:00
-- 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:setCustomSpeed(speed * 10)
2019-08-14 17:32:33 +02:00
-- 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:changeAnimation("idle")
2019-08-14 17:32:33 +02:00
self.currentAction = "selectAttack"
self.scene.menu:set( self )
end
end
2019-08-14 17:32:33 +02:00
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
2019-08-14 17:32:33 +02:00
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
2019-08-14 17:32:33 +02:00
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)
2019-08-14 17:32:33 +02:00
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")
2019-08-14 17:32:33 +02:00
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( )
elseif (action_type == "attack") then
self:changeAnimation("hit1")
else
self.world:switchActiveBattler( )
end
end
2019-08-14 17:32:33 +02:00
function Hero:receiveBackSignal()
self.currentAction = "selectDirection"
self.world.cursor:set(self.x, self.y)
self:changeAnimation("walk")
end
2019-08-14 17:32:33 +02:00
-- ACTION FUNCTIONS
-- All functions related to actions
function Hero:validateAction()
if (self.currentAction == "selectDirection") then
self:changeAnimation("walk", true)
2019-08-14 17:32:33 +02:00
self.currentAction = "moving"
self.dx, self.dy = self.world.cursor.x, self.world.cursor.y
self.world.cursor:unset( )
end
end
-- SPRITE FUNCTIONS
-- Handle the hero sprite
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 == "hit1") then
self:sendDamage(self.x + self.direction, self.y, 33, 100, false, false)
self:changeAnimation("hit2")
elseif (animation == "hit2") then
self:sendDamage(self.x + self.direction, self.y, 33, 100, false, false)
self:changeAnimation("hit3")
elseif (animation == "hit3") then
self:sendDamage(self.x + self.direction, self.y, 33, 100, false, false)
core.debug:print("cbs/hero", "normal combo finished, switching actor")
self:changeAnimation("idle")
self.world:switchActiveBattler( )
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
end
game.characters.list[self.charid].stats.hp = value
end
-- DRAW FUNCTIONS
-- Draw everything related to the hero
2019-08-14 17:32:33 +02:00
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()
2019-08-14 22:47:10 +02:00
local HUDBASE = 8
2019-08-14 13:56:25 +02:00
local HUDSEP = 152
local x = HUDBASE + (self.charnumber-1)*HUDSEP
local y = self.world:getPlayerHUDPosition()
2019-08-14 22:47:10 +02:00
self.assets.images["e_speedster"]:draw(x, y)
2019-08-15 15:09:48 +02:00
core.screen:setScissor(x, y-16, 32, 40)
2019-08-14 22:47:10 +02:00
self.assets.sprites[self.charid]:drawAnimation(x+14, y+14)
2019-08-15 15:09:48 +02:00
core.screen:resetScissor( )
2019-08-14 22:47:10 +02:00
self.assets.images["m_speedster"]:draw(x, y)
self.assets.images["statusbar"]:draw(x+12, y-6)
local hp = game.characters.list[self.charid].stats.hp
local pp = game.characters.list[self.charid].stats.pp
local hpmax = game.characters.list[self.charid].stats.hpmax
local ppmax = game.characters.list[self.charid].stats.ppmax
local bar1 = math.floor((hp/hpmax)*107)
local bar2 = math.floor((pp/ppmax)*108)
love.graphics.setColor(248/255, 160/255, 0, 1)
gui.drawBar(x+29, y+5, bar1, 7)
love.graphics.setColor(0, 248/255, 248/255, 1)
gui.drawBar(x+17, y+17, bar2, 7)
utils.graphics.resetColor()
self.assets.fonts["hudnbrs_small"]:set()
love.graphics.print(hp .. "/" .. hpmax, x+34, y+5)
love.graphics.print(pp .. "/" .. ppmax, x+28, y+17)
local lvl = game.characters.list[self.charid].stats.level
if lvl < 100 then
lvl = "0" .. lvl
end
2019-08-14 13:56:25 +02:00
2019-08-14 22:47:10 +02:00
love.graphics.print(lvl, x+122, y-5)
end
return Hero