sonic-radiance/sonic-radiance.love/scenes/battlesystem/entities/character.lua

154 lines
4.7 KiB
Lua
Raw Permalink Normal View History

local Actor = require("scenes.battlesystem.entities.actor")
local Character = Actor:extend()
function Character:new(controller, x, y, charid)
Character.super.new(self, controller, 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
err("FATAL ERROR: charid not set")
end
self.charid = charid
self.actionPerTurn = game.characters.list[self.charid].base_stats.turns
self.assets:addSprite(charid, "assets/sprites/characters/" .. charid)
self.assets.sprites[self.charid]:setCustomSpeed(16)
self:setAnimation("idle")
self:setSprite(charid, 32, 48, true)
end
function Character:setAnimation(animation)
if (self.animation ~= animation) then
self.animation = animation
self.assets.sprites[self.charid]:changeAnimation(animation, true)
end
end
function Character:draw()
x, y = self.controller.battlearena:gridToPixel(self.x, self.y, true)
--love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
self:drawSprite()
end
function Character:drawIcon(x, y)
local iconID = 1
self.assets.tileset["charicons"]:drawTile(iconID, x, y)
end
function Character:setActive()
local gridsize = game.characters.list[self.charid].base_stats.move
self.controller.cursor:setGrid("square", self.x, self.y, gridsize, self)
self.startx, self.starty = self.x, self.y
self.controller.cursor:set(self.startx, self.starty)
self.currentAction = "selectDirection"
self.directionPrevious = self.direction
end
function Character:update(dt)
if (self.currentAction == "moving") then
self.xprevious = self.x
self.yprevious = self.y
self.x = (self.x) + ((self.dx) - (self.x)) * dt*15
self.y = (self.y) + ((self.dy) - (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.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.controller.menu:set( self )
end
elseif (self.currentAction == "selectAttack") then
if (self.controller.keys["B"].isPressed) then
--self.currentAction = "selectDirection"
--self.controller.cursor:set(self.x, self.y)
end
elseif (self.currentAction == "selectDirection") then
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
end
function Character:validateAction()
if (self.currentAction == "selectDirection") then
self:setAnimation("walk")
self.currentAction = "moving"
self.dx, self.dy = self.controller.cursor.x, self.controller.cursor.y
self.controller.cursor:unset( )
end
end
function Character:getStats()
return game.characters.list[self.charid].stats
end
function Character:getSignal(action_type, id)
--print(action_type .. " " .. id)
if (action_type == "back") then
self.currentAction = "selectDirection"
self.controller.cursor:set(self.x, self.y)
self:setAnimation("walk")
elseif (action_type == "defend") then
self.turnAction = "defend"
self.controller.actormanager:switchActiveActor( )
else
self.controller.actormanager:switchActiveActor( )
end
end
function Character:getBackSignal()
self.currentAction = "selectDirection"
self.controller.cursor:set(self.x, self.y)
self:setAnimation("walk")
end
function Character:drawStatus(frame, x, y)
love.graphics.draw(frame, x, y)
local y1 = y + 8
local x1 = x + 16
local x2 = x1 + 120
love.graphics.setColor(.05, .8, .05, 1)
for i=0, 3 do
love.graphics.line(x1-i, y1+i, x2-i, y1+i)
end
utils.graphics.resetColor()
end
return Character