sonic-radiance/sonic-radiance.love/scenes/overworld/actors/player/actions.lua
2021-04-11 11:08:59 +02:00

169 lines
5.3 KiB
Lua

local PlayerActions = Object:extend()
local BASE_SPEED = 120
local RUN_FACTOR = 2.5
local JMP_STRENGHT = 2.5
local ACTIONS = {}
ACTIONS["speedster"] = {"run"}
ACTIONS["technic"] = {"fly"}
ACTIONS["power"] = {"punch"}
function PlayerActions:initActions()
self.currentAction = "idle"
end
function PlayerActions:canDoAction(action)
local classCanDoAction = utils.table.contain(ACTIONS[self:getCurrentCharType()], action)
local playerCanDoAction = utils.table.contain(game.actions, action)
return (classCanDoAction and playerCanDoAction)
end
function PlayerActions:actionMove()
if (self.currentAction == "punch") then
return
end
if (self.currentAction == "jumpdash" or self.currentAction == "run") then
self.xsp, self.ysp = utils.math.lengthdir(BASE_SPEED * RUN_FACTOR, math.rad(self.charsetManager.angle[self.charDir]))
self.xsp = -self.xsp
if (utils.table.contain({"up", "down"}, self.charDir)) then
if self.keys["left"].isDown then
self.xsp = -BASE_SPEED
end
if self.keys["right"].isDown then
self.xsp = BASE_SPEED
end
else
if self.keys["up"].isDown then
self.ysp = -BASE_SPEED
end
if self.keys["down"].isDown then
self.ysp = BASE_SPEED
end
end
else
if self.keys["up"].isDown then
self.ysp = -BASE_SPEED
self.charDir = "up"
end
if self.keys["down"].isDown then
self.ysp = BASE_SPEED
self.charDir = "down"
end
if self.keys["left"].isDown then
self.xsp = -BASE_SPEED
self.charDir = "left"
end
if self.keys["right"].isDown then
self.xsp = BASE_SPEED
self.charDir = "right"
end
end
end
function PlayerActions:actionJump()
if self.keys["B"].isPressed then
if (self.onGround) then
self:goUpward(JMP_STRENGHT)
self.assets.sfx["jump"]:play()
if (self.currentAction == "run") then
self.currentAction = "jumpdash"
else
self.currentAction = "jump"
end
elseif (self.currentAction == "jump" and self:canDoAction("fly")) then
self.currentAction = "fly"
self.grav = 0
self.zsp = 0
self.tweens:newTimer(0.75, "endFly")
self.assets.sfx["fly"]:play()
end
end
if self.keys["B"].isReleased then
self:endFly()
end
end
function PlayerActions:endFly()
if (self.currentAction == "fly") then
self:goUpward(0)
self.currentAction = "idle"
end
end
function PlayerActions:actionSwitch()
if self.keys["select"].isPressed and (self.currentAction == "idle") then
self:switchActiveCharacter()
end
end
function PlayerActions:actionRun()
if self.keys["C"].isPressed then
if (self:canDoAction("run")) then
self.assets.sfx["dash"]:play()
if (utils.table.contain({"run", "idle"}, self.currentAction)) then
self.currentAction = "run"
elseif (utils.table.contain({"jumpdash", "jump"}, self.currentAction)) then
self.currentAction = "jumpdash"
end
end
elseif (not self.keys["C"].isDown) then
if (self.currentAction == "run") then
self.currentAction = "idle"
elseif (self.currentAction == "jumpdash") then
self.currentAction = "jump"
end
end
end
function PlayerActions:actionPunch()
if self.keys["C"].isPressed then
if (self:canDoAction("punch")) then
self.assets.sfx["hit"]:play()
self.xsp, self.ysp = utils.math.lengthdir(BASE_SPEED * RUN_FACTOR, math.rad(self.charsetManager.angle[self.charDir]))
self.xsp = -self.xsp
self.currentAction = "punch"
self.tweens:newTimer(0.15, "endPunch")
self.assets.sprites["punch"]:changeAnimation("default", true)
end
end
end
function PlayerActions:endPunch()
if (self.currentAction == "punch") then
self.currentAction = "idle"
end
end
function PlayerActions:endJump()
if (self.currentAction == "jump") then
self.currentAction = "idle"
elseif (self.currentAction == "jumpdash") then
self.currentAction = "run"
end
end
function PlayerActions:drawActionEffect()
if (((self.currentAction == "run") or (self.currentAction == "jumpdash")) and (self.xsp ~= 0 or self.ysp ~= 0)) then
local dx, dy = utils.math.lengthdir(20, math.rad(self.charsetManager.angle[self.charDir]))
if (self.charDir == "down") then
dy = 8
end
local x, y = self.x + 8 - dx, self.y + 8 - self.z + dy
self.assets.sprites["dash"]:drawAnimation(x, y, math.rad(self.charsetManager.angle[self.charDir]))
end
if (self.currentAction == "punch") then
local dx, dy = utils.math.lengthdir(20, math.rad(self.charsetManager.angle[self.charDir]))
if (self.charDir == "down") then
dy = 8
end
local x, y = self.x + 8 - dx, self.y + 8 - self.z + dy
print(x, y)
self.assets.sprites["punch"]:drawAnimation(x, y, math.rad(self.charsetManager.angle[self.charDir]))
end
end
return PlayerActions