improvement(player): separate inputs

This commit is contained in:
Kazhnuz 2022-05-19 12:34:16 +02:00
parent a35079f1c7
commit 27bd6da373
4 changed files with 112 additions and 42 deletions

View file

@ -1,22 +1,7 @@
local PlayerActions = Object:extend() local PlayerActions = Object:extend()
function PlayerActions:initActions() function PlayerActions:initActions()
self.action = "normal" self.action = "idle"
end
function PlayerActions:doActions()
if self.keys["B"].isPressed and (self.onGround) then
-- Nothing for the moment
end
end
function PlayerActions:actionSwitch()
if self.keys["L1"].isPressed and (self.action == "normal") and (self.onGround) then
self:switchActiveCharacter()
end
if self.keys["R1"].isPressed and (self.action == "normal") and (self.onGround) then
self:switchActiveCharacter(-1)
end
end end
return PlayerActions return PlayerActions

View file

@ -0,0 +1,79 @@
local PlayerControls = Object:extend()
function PlayerControls:applyInputs()
if (self.world.autorun == true) then
self:applyAutorunInput()
else
self:applyMoveInput()
end
self:applyJumpInput()
self:applyActionsInputs()
self:applySwitchInputs()
end
function PlayerControls:applyMoveInput()
local angle, strenght = self:dpadToAngle()
if (strenght ~= 0) then
self:goTowardDir(angle, strenght)
end
end
function PlayerControls:applyAutorunInput()
self:goX()
if self.keys["up"].isDown then
self:goY(-1)
end
if self.keys["down"].isDown then
self:goY(1)
end
end
function PlayerControls:dpadToAngle()
local xsp, ysp = 0, 0
local strenght = 0
local angle = 0
if self.keys["up"].isDown then
ysp = -1
end
if self.keys["down"].isDown then
ysp = 1
end
if self.keys["left"].isDown then
xsp = -1
end
if self.keys["right"].isDown then
xsp = 1
end
if (xsp ~= 0 or ysp ~= 0) then
angle = utils.math.pointDirection(0, 0, xsp, ysp)
strenght = 1
end
return angle, strenght
end
function PlayerControls:applyJumpInput()
if self.keys["A"].isPressed and (self:isNotJumping()) then
self:jump()
end
end
function PlayerControls:applyActionsInputs()
if self.keys["B"].isPressed and (self.onGround) then
-- Nothing for the moment
end
end
function PlayerControls:applySwitchInputs()
if self.keys["L1"].isPressed and (self.action == "normal") and (self.onGround) then
self:switchActiveCharacter()
end
if self.keys["R1"].isPressed and (self.action == "normal") and (self.onGround) then
self:switchActiveCharacter(-1)
end
end
return PlayerControls

View file

@ -8,6 +8,8 @@ local Actions = require "game.modules.subgames.world.actors.player.actions"
local Movements = require "game.modules.subgames.world.actors.player.movements" local Movements = require "game.modules.subgames.world.actors.player.movements"
local Score = require "game.modules.subgames.world.actors.player.score" local Score = require "game.modules.subgames.world.actors.player.score"
local Sprites = require "game.modules.subgames.world.actors.player.sprites" local Sprites = require "game.modules.subgames.world.actors.player.sprites"
local Controls = require "game.modules.subgames.world.actors.player.controls"
local Team = require "scenes.overworld.actors.player.team" local Team = require "scenes.overworld.actors.player.team"
Player:implement(Actions) Player:implement(Actions)
@ -15,6 +17,7 @@ Player:implement(Movements)
Player:implement(Score) Player:implement(Score)
Player:implement(Sprites) Player:implement(Sprites)
Player:implement(Team) Player:implement(Team)
Player:implement(Controls)
function Player:new(world, x, y, z, id) function Player:new(world, x, y, z, id)
Player.super.new(self, world, "player", x, y, 0, 16, 12, 24, true) Player.super.new(self, world, "player", x, y, 0, 16, 12, 24, true)
@ -28,12 +31,11 @@ function Player:new(world, x, y, z, id)
end end
function Player:updateStart(dt) function Player:updateStart(dt)
self:basicMovements() self:applyInputs()
self:doActions()
self:actionSwitch()
end end
function Player:update(dt) function Player:update(dt)
self:updateMovements(dt)
Player.super.update(self, dt) Player.super.update(self, dt)
self.tweens:update(dt) self.tweens:update(dt)
end end

View file

@ -1,40 +1,44 @@
local PlayerMovement = Object:extend() local PlayerMovement = Object:extend()
local SPEED = 160
function PlayerMovement:initMovements() function PlayerMovement:initMovements()
self:setGravity(480 * 2) self:setGravity(480 * 2)
end end
function PlayerMovement:basicMovements() function PlayerMovement:updateMovements()
self:setFrc() self:setFrc()
if self.keys["up"].isDown then
self.ysp = -160
end
if self.keys["down"].isDown then
self.ysp = 160
end
if (self.world.autorun == true) then
self.xsp = 160
else
if self.keys["left"].isDown then
self.xsp = -160
end
if self.keys["right"].isDown then
self.xsp = 160
end
end
self:jump()
end end
-- GOTO FUNCTIONS
-- Help the movable go toward something
function PlayerMovement:goTowardDir(angle, strenght)
self.xsp, self.ysp = utils.math.lengthdir(SPEED, angle)
end
function PlayerMovement:goX(dir)
self.xsp = SPEED * (dir or 1)
end
function PlayerMovement:goY(dir)
self.ysp = SPEED * (dir or 1)
end
-- FRICTION
function PlayerMovement:setFrc() function PlayerMovement:setFrc()
self.xfrc, self.yfrc = 480 * 3, 480 * 3 self.xfrc, self.yfrc = 480 * 3, 480 * 3
end end
-- JUMP FUNCTIONS
function PlayerMovement:jump() function PlayerMovement:jump()
if self.keys["A"].isPressed and (self.onGround) then
self.zsp = 280 * 1.33 self.zsp = 280 * 1.33
end end
function PlayerMovement:isNotJumping()
return self.onGround
end end
return PlayerMovement return PlayerMovement