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

353 lines
8.4 KiB
Lua
Raw Normal View History

local Parent = require("scenes.battlesystem.actors.parent")
local Battler = Parent:extend()
local MOVEMENT_NONE = "none"
local MOVEMENT_TWEENER = "tweener"
local MOVEMENT_MOTION = "motion"
local ZGRAVITY = 0.2
2020-08-04 22:40:58 +02:00
local MIDDLE_ARENA = 6
2021-05-09 15:07:38 +02:00
local outputColor = {
good = {0, 1, 0},
bad = {1, 0, 0},
pp = {0.3, 0.8, 1}
}
function Battler:new(world, x, y, z, owner)
Battler.super.new(self, world, x, y, z)
2020-08-04 22:40:58 +02:00
self.direction = utils.math.sign(MIDDLE_ARENA - x)
self.start = {}
self.start.x = x
self.start.y = y
2020-08-04 22:40:58 +02:00
self.start.direction = self.direction
self:initMovementSystem()
self.isBattler = true
self.speed = 3
self.isActive = false
self.debugActiveTimer = 0
2020-07-19 21:41:14 +02:00
2021-05-09 15:07:38 +02:00
self.output = {}
self.output.string = "0"
self.output.isBad = true
self.output.type = ""
self.showOutput = false
self.outputY = 0
2020-08-22 23:53:13 +02:00
2020-07-19 21:41:14 +02:00
self.isSelected = false
self.owner = owner
2021-03-13 15:49:21 +01:00
self.isDefending = false
self.tags = {}
self.choregraphy = nil
end
function Battler:destroy()
Battler.super.destroy(self)
end
2021-05-09 15:07:38 +02:00
function Battler:getOutputY()
return self.sprHeight or 32
end
function Battler:avoidedAttack()
self:newOutput("", "MISS")
2020-08-22 23:53:13 +02:00
end
function Battler:setDamageNumber(number, isPP)
2021-05-09 15:07:38 +02:00
local type = "good"
if (isPP == true) then type = "pp" end
if (number < 0) then type = "bad" end
local string = math.abs(math.floor(number))
self:newOutput(type, string)
end
function Battler:newOutput(type, string)
self.output.type = type or ""
self.output.string = string or "error"
self.outputY = self:getOutputY() - 8
self.showOutput = true
self.tweens:newTween(0, 0.4, {outputY = self:getOutputY()}, "outQuad")
self.tweens:newTimer(0.5, "removeOutput")
end
function Battler:getOuputColor(type)
return outputColor[type] or {1,1,1}
2020-08-22 23:53:13 +02:00
end
function Battler:setActive()
core.debug:print("cbs/actor","actor " .. self.id .. " is active")
self.isActive = true
self.debugActiveTimer = 0
end
function Battler:update(dt)
2019-08-16 23:04:30 +02:00
Battler.super.update(self, dt)
if (self.isActive) then
self.debugActiveTimer = self.debugActiveTimer + dt
if self.debugActiveTimer >= 0.5 then
core.debug:print("cbs/battler", "counter ended, switching active battler")
self.isActive = false
self.world:switchActiveBattler()
end
end
self:updateMovement(dt)
end
-- MOVE FUNCTIONS
-- All functions handling the moving
local MOVEMENT_DURATION = 0.20
function Battler:initMovementSystem()
self.xprevious, self.yprevious, self.zprevious = self.x, self.y, self.z
self.xspeed, self.yspeed, self.zspeed = 0,0,0
self.direction = self.start.direction
self.directionPrevious = self.start.direction
self.directionLocked = false
self.movementType = MOVEMENT_NONE
self:initJump()
end
function Battler:updateMovement(dt)
if (self.movementType == MOVEMENT_TWEENER) then
self:updateTweenerSpeed(dt)
elseif (self.movementType == MOVEMENT_MOTION) then
self:updateMotion(dt)
end
self.gspeed = math.sqrt(self.xspeed^2 + self.yspeed^2)
self:updateDirection(dt)
self:updateJump(dt)
self:updatePreviousPosition(dt)
end
function Battler:updatePreviousPosition()
self.xprevious = self.x
self.yprevious = self.y
self.zprevious = self.z
end
-- Tweener movement functions
function Battler:goTo(dx, dy, duration, easing)
local easing = easing or 'inOutQuad'
if duration > 0 then
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
end
self.tweens:newTimer(duration + 0.02, "goTo")
self.tweens:newTimer(duration + 0.02, "resetMovement")
self.movementType = MOVEMENT_TWEENER
end
function Battler:jumpTo(dx, dy, sizeFactor, duration, spinjump, easing)
local easing = easing or 'inOutQuad'
local dist = utils.math.pointDistance(self.x, self.y, dx, dy)
local jumpHeight = dist * 8 * sizeFactor
self.tweens:newTween(0, duration, {x = dx, y = dy}, easing)
self.tweens:newTimer(duration + 0.02, "jumpTo")
self:setJump(jumpHeight, spinjump, duration)
end
function Battler:updateTweenerSpeed(dt)
self.xspeed = (self.x - self.xprevious) / dt
self.yspeed = (self.y - self.yprevious) / dt
end
-- MOTION HANDLING
function Battler:setMotion(xspeed, yspeed)
self.xspeed = xspeed
self.yspeed = yspeed
self.movementType = MOVEMENT_MOTION
end
function Battler:updateMotion(dt)
self.x = self.x + (self.xspeed) * dt
self.y = self.y + (self.yspeed) * dt
end
function Battler:endMotion()
self.movementType = MOVEMENT_NONE
self.xspeed = 0
self.yspeed = 0
end
-- Direction handling
function Battler:updateDirection()
-- Handle direction
if math.abs(self.xspeed) >= 0.01 then
if (self.directionLocked == false) then
self.direction = utils.math.sign(self.xspeed)
end
end
end
-- Jump system
function Battler:initJump()
self.jump = {}
self.jump.useDefaultAnimation = true
self.jump.isJumping = false
self.jump.bounceNumber = 0
self.jump.isJumpingBack = false
end
function Battler:setJump(power, bounceNumber, useDefaultAnimation)
self.zspeed = power
self.jump.spin = (useDefaultAnimation == false)
self.jump.bounceNumber = bounceNumber
self.jump.isJumping = true
end
function Battler:jumpBack()
self:setJump(4, 0, true)
local dir = utils.math.pointDirection(self.x, self.y, self.start.x, self.start.y)
local hspeed, vspeed = utils.math.lengthdir(8, dir)
self:setMotion(hspeed, vspeed)
self.jump.isJumpingBack = true
end
function Battler:updateJump(dt)
if (self.jump.isJumping) then
self.zspeed = self.zspeed - ZGRAVITY
self.z = self.z + self.zspeed
if (self.z <= 0) then
if (self.jump.bounceNumber > 0) then
self.zspeed = self.zspeed * -0.5
self.jump.bounceNumber = self.jump.bounceNumber - 1
else
self.z = 0
self.jump.isJumping = false
self.jump.spin = false
self:timerResponse("jump")
if (self.jump.isJumpingBack) then
self:endMotion()
end
self:changeAnimation("idle")
end
end
end
end
-- CHOREGRAPHY FUNCTIONS
-- All functions related to the choregraphy system
function Battler:blockChoregraphy(isBlocking, currentlyBlocking, blockedBy)
if (isBlocking) then
self.currentlyBlocking = currentlyBlocking
self.blockedBy = blockedBy
end
end
function Battler:addTaggedAction(tag, choregraphy, taggedBy)
if (not utils.string.isEmpty(tag)) then
self.tags[tag] = taggedBy
self.choregraphy = choregraphy
end
end
function Battler:unlockTag(taggedBy)
for tag, actionTag in pairs(self.tags) do
if (self.choregraphy ~= nil) and (actionTag == taggedBy) then
self.choregraphy:finishTagAction(tag)
self.tags[tag] = nil
end
end
end
function Battler:unblockChoregraphy()
self.currentlyBlocking:finish()
self.currentlyBlocking = nil
end
function Battler:timerResponse(signal)
if ((self.currentlyBlocking ~= nil) and (signal == self.blockedBy)) then
self:unblockChoregraphy()
end
self:unlockTag(signal)
if (signal == "resetMovement") then
self.movementType = MOVEMENT_NONE
2021-05-09 15:07:38 +02:00
elseif (signal == "removeOutput") then
self.showOutput = false
end
end
function Battler:choregraphyEnded()
self.direction = self.start.direction
self.x = self.start.x
self.y = self.start.y
self.xspeed = 0
self.yspeed = 0
self.movementType = MOVEMENT_NONE
end
2020-08-05 11:40:29 +02:00
function Battler:getHurt()
end
function Battler:die()
self:destroy()
end
-- DRAW FUNCTIONS
2021-05-09 15:07:38 +02:00
function Battler:drawOutput()
if (self.showOutput) then
2020-08-22 23:53:13 +02:00
local x, y = self.world.map:gridToPixel(self.x, self.y, true)
2021-05-09 15:07:38 +02:00
local color = self:getOuputColor(self.output.type)
love.graphics.setColor(color[1], color[2], color[3], 1)
2020-08-22 23:53:13 +02:00
2021-05-09 15:07:38 +02:00
self.assets.fonts["hudnbrs_small"]:print(self.output.string, x, y - self.outputY - self.z, "center")
2020-08-22 23:53:13 +02:00
utils.graphics.resetColor()
end
end
function Battler:draw()
local x, y = self.world.map:gridToPixel(self.x, self.y, true)
love.graphics.setColor(1, 0, 0, 1)
love.graphics.rectangle("fill", x - 8, y - 32, 16, 32)
love.graphics.setColor(1, 1, 1, 1)
end
2020-08-04 22:19:11 +02:00
function Battler:initSprite()
if (self.assets.sprites[self.owner.name] == nil) then
self.assets:addSprite(self.owner.name, self:getSpritePath())
end
self.assets.sprites[self.owner.name]:setCustomSpeed(16)
self:setSprite(self.owner.name, 32, 48, true)
self:cloneSprite()
self:changeAnimation("idle")
end
function Battler:animationEnded(animation)
if (self.currentlyBlocking ~= nil and self.blockedBy=="animation") then
self:unblockChoregraphy()
end
self:unlockTag("animation")
2020-08-05 11:40:29 +02:00
self:getNewAnimation(animation)
end
function Battler:getNewAnimation(animation)
end
function Battler:validateAction()
end
return Battler