2021-04-10 17:14:41 +02:00
|
|
|
local PlayerActions = Object:extend()
|
|
|
|
|
|
|
|
local BASE_SPEED = 120
|
2021-04-10 19:32:41 +02:00
|
|
|
local RUN_FACTOR = 2.5
|
2021-04-10 17:14:41 +02:00
|
|
|
local JMP_STRENGHT = 2.5
|
|
|
|
|
2021-04-10 17:59:01 +02:00
|
|
|
local ACTIONS = {}
|
|
|
|
ACTIONS["speedster"] = {"run"}
|
|
|
|
ACTIONS["technic"] = {"fly"}
|
|
|
|
ACTIONS["power"] = {"punch"}
|
|
|
|
|
2021-04-10 17:14:41 +02:00
|
|
|
function PlayerActions:initActions()
|
2021-04-10 17:41:53 +02:00
|
|
|
self.currentAction = "idle"
|
2021-04-18 12:34:30 +02:00
|
|
|
self.canJump = true
|
|
|
|
self.canAct = true
|
|
|
|
self.speedFactor = 1
|
|
|
|
self.forceAction = nil
|
2021-04-21 16:54:42 +02:00
|
|
|
self.dashJustStarted = false
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
|
2021-04-10 17:59:01 +02:00
|
|
|
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
|
|
|
|
|
2021-04-18 12:34:30 +02:00
|
|
|
function PlayerActions:act()
|
|
|
|
if (self.forceAction ~= nil) then
|
|
|
|
self:forcedAction()
|
|
|
|
else
|
|
|
|
self:actionMove()
|
|
|
|
self:actionJump()
|
|
|
|
self:actionSwitch()
|
|
|
|
self:actionRun()
|
|
|
|
self:actionPunch()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerActions:forcedAction()
|
|
|
|
local charSpeed = BASE_SPEED * self.speedFactor
|
|
|
|
|
|
|
|
if (self.forceAction == "slide") then
|
|
|
|
if (self.xsp ~= 0 or self.ysp ~= 0) then
|
|
|
|
if (self.currentAction == "run") then
|
|
|
|
charSpeed = charSpeed * RUN_FACTOR
|
|
|
|
end
|
|
|
|
self.xsp, self.ysp = utils.math.lengthdir(charSpeed, math.rad(self.charsetManager.angle[self.charDir]))
|
|
|
|
self.xsp = -self.xsp
|
|
|
|
else
|
|
|
|
self:actionMove()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-10 17:14:41 +02:00
|
|
|
function PlayerActions:actionMove()
|
2021-04-18 12:34:30 +02:00
|
|
|
local charSpeed = BASE_SPEED * self.speedFactor
|
|
|
|
|
2021-04-10 20:47:34 +02:00
|
|
|
if (self.currentAction == "punch") then
|
|
|
|
return
|
|
|
|
end
|
2021-04-18 12:34:30 +02:00
|
|
|
|
2021-04-10 19:32:41 +02:00
|
|
|
if (self.currentAction == "jumpdash" or self.currentAction == "run") then
|
2021-04-21 16:54:42 +02:00
|
|
|
local xsp, ysp = self:getDash()
|
2021-04-10 19:32:41 +02:00
|
|
|
if (utils.table.contain({"up", "down"}, self.charDir)) then
|
|
|
|
if self.keys["left"].isDown then
|
2021-04-21 16:54:42 +02:00
|
|
|
xsp = -charSpeed
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
|
|
|
if self.keys["right"].isDown then
|
2021-04-21 16:54:42 +02:00
|
|
|
xsp = charSpeed
|
|
|
|
end
|
|
|
|
if (self.ysp == 0) then
|
|
|
|
if (self.currentAction == "jumpdash") then
|
|
|
|
self.currentAction = "jump"
|
|
|
|
else
|
|
|
|
self.currentAction = "idle"
|
|
|
|
end
|
|
|
|
elseif (self.dashJustStarted) then
|
|
|
|
self.dashJustStarted = false
|
|
|
|
self.assets.sfx["dash"]:play()
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if self.keys["up"].isDown then
|
2021-04-21 16:54:42 +02:00
|
|
|
ysp = -charSpeed
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
|
|
|
if self.keys["down"].isDown then
|
2021-04-21 16:54:42 +02:00
|
|
|
ysp = charSpeed
|
|
|
|
end
|
|
|
|
if (self.xsp == 0) then
|
|
|
|
if (self.currentAction == "jumpdash") then
|
|
|
|
self.currentAction = "jump"
|
|
|
|
else
|
|
|
|
self.currentAction = "idle"
|
|
|
|
end
|
|
|
|
elseif (self.dashJustStarted) then
|
|
|
|
self.dashJustStarted = false
|
|
|
|
self.assets.sfx["dash"]:play()
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
|
|
|
end
|
2021-04-21 16:54:42 +02:00
|
|
|
self.xsp, self.ysp = xsp, ysp
|
2021-04-10 19:32:41 +02:00
|
|
|
else
|
|
|
|
if self.keys["up"].isDown then
|
2021-04-18 12:34:30 +02:00
|
|
|
self.ysp = -charSpeed
|
2021-04-10 19:32:41 +02:00
|
|
|
self.charDir = "up"
|
|
|
|
end
|
|
|
|
if self.keys["down"].isDown then
|
2021-04-18 12:34:30 +02:00
|
|
|
self.ysp = charSpeed
|
2021-04-10 19:32:41 +02:00
|
|
|
self.charDir = "down"
|
|
|
|
end
|
|
|
|
if self.keys["left"].isDown then
|
2021-04-18 12:34:30 +02:00
|
|
|
self.xsp = -charSpeed
|
2021-04-10 19:32:41 +02:00
|
|
|
self.charDir = "left"
|
|
|
|
end
|
|
|
|
if self.keys["right"].isDown then
|
2021-04-18 12:34:30 +02:00
|
|
|
self.xsp = charSpeed
|
2021-04-10 19:32:41 +02:00
|
|
|
self.charDir = "right"
|
|
|
|
end
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-21 16:54:42 +02:00
|
|
|
function PlayerActions:getCharspeed()
|
|
|
|
return BASE_SPEED * self.speedFactor
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerActions:getDash()
|
|
|
|
local xsp, ysp = utils.math.lengthdir(self:getCharspeed() * RUN_FACTOR, math.rad(self.charsetManager.angle[self.charDir]))
|
|
|
|
return -xsp, ysp
|
|
|
|
end
|
|
|
|
|
2021-04-10 17:14:41 +02:00
|
|
|
function PlayerActions:actionJump()
|
|
|
|
if self.keys["B"].isPressed then
|
2021-04-18 12:34:30 +02:00
|
|
|
if (self.onGround and self.canJump) then
|
2021-04-10 17:14:41 +02:00
|
|
|
self:goUpward(JMP_STRENGHT)
|
|
|
|
self.assets.sfx["jump"]:play()
|
2021-04-10 19:32:41 +02:00
|
|
|
if (self.currentAction == "run") then
|
|
|
|
self.currentAction = "jumpdash"
|
|
|
|
else
|
|
|
|
self.currentAction = "jump"
|
|
|
|
end
|
2021-04-10 18:28:52 +02:00
|
|
|
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()
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
end
|
2021-04-10 18:28:52 +02:00
|
|
|
|
|
|
|
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
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerActions:actionSwitch()
|
2021-04-10 17:41:53 +02:00
|
|
|
if self.keys["select"].isPressed and (self.currentAction == "idle") then
|
2021-04-10 17:14:41 +02:00
|
|
|
self:switchActiveCharacter()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-10 19:32:41 +02:00
|
|
|
function PlayerActions:actionRun()
|
2021-04-11 11:08:59 +02:00
|
|
|
if self.keys["C"].isPressed then
|
2021-04-18 12:34:30 +02:00
|
|
|
if (self:canDoAction("run") and self.speedFactor > 0) then
|
2021-04-21 16:54:42 +02:00
|
|
|
self.dashJustStarted = true
|
2021-04-10 19:32:41 +02:00
|
|
|
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
|
2021-04-21 16:54:42 +02:00
|
|
|
self.xsp, self.ysp = self:getDash()
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
2021-04-11 11:08:59 +02:00
|
|
|
elseif (not self.keys["C"].isDown) then
|
|
|
|
if (self.currentAction == "run") then
|
|
|
|
self.currentAction = "idle"
|
|
|
|
elseif (self.currentAction == "jumpdash") then
|
|
|
|
self.currentAction = "jump"
|
|
|
|
end
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-10 20:47:34 +02:00
|
|
|
function PlayerActions:actionPunch()
|
|
|
|
if self.keys["C"].isPressed then
|
|
|
|
if (self:canDoAction("punch")) then
|
2021-04-11 11:08:59 +02:00
|
|
|
self.assets.sfx["hit"]:play()
|
2021-04-10 20:47:34 +02:00
|
|
|
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
|
|
|
|
|
2021-04-10 17:14:41 +02:00
|
|
|
function PlayerActions:endJump()
|
2021-04-10 19:32:41 +02:00
|
|
|
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
|
2021-04-21 16:54:42 +02:00
|
|
|
elseif (self.charDir == "left" or self.charDir == "right") then
|
|
|
|
dy = -8
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
|
|
|
local x, y = self.x + 8 - dx, self.y + 8 - self.z + dy
|
2021-08-21 17:05:49 +02:00
|
|
|
self.assets.sprites["dash"]:draw(x, y, math.rad(self.charsetManager.angle[self.charDir]))
|
2021-04-10 19:32:41 +02:00
|
|
|
end
|
2021-04-10 20:47:34 +02:00
|
|
|
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
|
2021-08-21 17:05:49 +02:00
|
|
|
self.assets.sprites["punch"]:draw(x, y, math.rad(self.charsetManager.angle[self.charDir]))
|
2021-04-10 20:47:34 +02:00
|
|
|
end
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return PlayerActions
|