improvement: improve the run/dash action

This commit is contained in:
Kazhnuz 2021-04-21 16:54:42 +02:00
parent 38c197a9fb
commit 9a9615b120
2 changed files with 40 additions and 8 deletions

View file

@ -15,6 +15,7 @@ function PlayerActions:initActions()
self.canAct = true self.canAct = true
self.speedFactor = 1 self.speedFactor = 1
self.forceAction = nil self.forceAction = nil
self.dashJustStarted = false
end end
function PlayerActions:canDoAction(action) function PlayerActions:canDoAction(action)
@ -59,24 +60,43 @@ function PlayerActions:actionMove()
end end
if (self.currentAction == "jumpdash" or self.currentAction == "run") then if (self.currentAction == "jumpdash" or self.currentAction == "run") then
self.xsp, self.ysp = utils.math.lengthdir(charSpeed * RUN_FACTOR, math.rad(self.charsetManager.angle[self.charDir])) local xsp, ysp = self:getDash()
self.xsp = -self.xsp
if (utils.table.contain({"up", "down"}, self.charDir)) then if (utils.table.contain({"up", "down"}, self.charDir)) then
if self.keys["left"].isDown then if self.keys["left"].isDown then
self.xsp = -charSpeed xsp = -charSpeed
end end
if self.keys["right"].isDown then if self.keys["right"].isDown then
self.xsp = charSpeed 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()
end end
else else
if self.keys["up"].isDown then if self.keys["up"].isDown then
self.ysp = -charSpeed ysp = -charSpeed
end end
if self.keys["down"].isDown then if self.keys["down"].isDown then
self.ysp = charSpeed 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()
end end
end end
self.xsp, self.ysp = xsp, ysp
else else
if self.keys["up"].isDown then if self.keys["up"].isDown then
self.ysp = -charSpeed self.ysp = -charSpeed
@ -97,6 +117,15 @@ function PlayerActions:actionMove()
end end
end end
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
function PlayerActions:actionJump() function PlayerActions:actionJump()
if self.keys["B"].isPressed then if self.keys["B"].isPressed then
if (self.onGround and self.canJump) then if (self.onGround and self.canJump) then
@ -137,12 +166,13 @@ end
function PlayerActions:actionRun() function PlayerActions:actionRun()
if self.keys["C"].isPressed then if self.keys["C"].isPressed then
if (self:canDoAction("run") and self.speedFactor > 0) then if (self:canDoAction("run") and self.speedFactor > 0) then
self.assets.sfx["dash"]:play() self.dashJustStarted = true
if (utils.table.contain({"run", "idle"}, self.currentAction)) then if (utils.table.contain({"run", "idle"}, self.currentAction)) then
self.currentAction = "run" self.currentAction = "run"
elseif (utils.table.contain({"jumpdash", "jump"}, self.currentAction)) then elseif (utils.table.contain({"jumpdash", "jump"}, self.currentAction)) then
self.currentAction = "jumpdash" self.currentAction = "jumpdash"
end end
self.xsp, self.ysp = self:getDash()
end end
elseif (not self.keys["C"].isDown) then elseif (not self.keys["C"].isDown) then
if (self.currentAction == "run") then if (self.currentAction == "run") then
@ -185,6 +215,8 @@ function PlayerActions:drawActionEffect()
local dx, dy = utils.math.lengthdir(20, math.rad(self.charsetManager.angle[self.charDir])) local dx, dy = utils.math.lengthdir(20, math.rad(self.charsetManager.angle[self.charDir]))
if (self.charDir == "down") then if (self.charDir == "down") then
dy = 8 dy = 8
elseif (self.charDir == "left" or self.charDir == "right") then
dy = -8
end end
local x, y = self.x + 8 - dx, self.y + 8 - self.z + dy 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])) self.assets.sprites["dash"]:drawAnimation(x, y, math.rad(self.charsetManager.angle[self.charDir]))