sonic-radiance/sonic-radiance.love/scenes/battlesystem/actors/battler.lua
2021-07-18 14:04:24 +02:00

382 lines
9.1 KiB
Lua

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
local MIDDLE_ARENA = 6
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)
self.direction = utils.math.sign(MIDDLE_ARENA - x)
self.start = {}
self.start.x = x
self.start.y = y
self.start.z = z
self.start.direction = self.direction
self:initMovementSystem()
self.isBattler = true
self.speed = 3
self.isActive = false
self.debugActiveTimer = 0
self.output = {}
self.output.string = "0"
self.output.isBad = true
self.output.type = ""
self.showOutput = false
self.outputY = 0
self.isSelected = false
self.owner = owner
self.isDefending = false
self.tags = {}
self.choregraphy = nil
end
function Battler:destroy()
Battler.super.destroy(self)
end
function Battler:getOutputY()
return self.sprHeight or 32
end
function Battler:avoidedAttack()
self:newOutput("", "MISS")
end
function Battler:setDamageNumber(number, isPP)
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}
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)
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:stopMoving()
self.xspeed, self.yspeed, self.zspeed = 0,0,0
if (self.movementType == MOVEMENT_TWEENER) then
self:unlockTag("goTo")
self.tweens:removeNamedTween("goTo")
self.tweens:removeTimer("goTo")
self.tweens:removeTimer("resetMovement")
end
self.movementType = MOVEMENT_NONE
self:updatePreviousPosition()
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'
self:stopMoving()
if duration > 0 then
self.tweens:setNamedTween("goTo", 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:goTo3D(dx, dy, dz, duration, easing)
local easing = easing or 'inOutQuad'
self:stopMoving()
self:stopJumping()
self.jump.useDefaultAnimation = false
if duration > 0 then
self.tweens:setNamedTween("goTo", 0, duration, {x = dx, y = dy, z = dz}, easing)
end
self.tweens:newTimer(duration + 0.02, "goTo")
self.tweens:newTimer(duration + 0.02, "resetMovement")
self.movementType = MOVEMENT_TWEENER
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.isMotionJump = false
end
function Battler:stopJumping()
self:initJump()
self.zspeed = 0
end
function Battler:setJump(power, bounceNumber, useDefaultAnimation)
self.zspeed = power
self.jump.useDefaultAnimation = useDefaultAnimation
self.jump.bounceNumber = bounceNumber
self.jump.isJumping = true
end
function Battler:jumpTo(dx, dy, height, speed, useDefaultAnimation)
height = height or 4
speed = speed or 8
self:setJump(height, 0, useDefaultAnimation)
local dir = utils.math.pointDirection(self.x, self.y, dx, dy)
local hspeed, vspeed = utils.math.lengthdir(speed, dir)
self:setMotion(hspeed, vspeed)
self.jump.isMotionJump = true
end
function Battler:jumpBack(height, speed)
self:jumpTo(self.start.x, self.start.y, height, speed, 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.isMotionJump) 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
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
function Battler:getHurt()
end
function Battler:die()
self:destroy()
end
-- DRAW FUNCTIONS
function Battler:drawOutput()
if (self.showOutput) then
local x, y = self.world.map:gridToPixel(self.x, self.y, true)
local color = self:getOuputColor(self.output.type)
love.graphics.setColor(color[1], color[2], color[3], 1)
self.assets.fonts["hudnbrs_small"]:print(self.output.string, x, y - self.outputY - self.z, "center")
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
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")
self:getNewAnimation(animation)
end
function Battler:getNewAnimation(animation)
end
function Battler:validateAction()
end
return Battler