From 27bd6da3737d0e402c857f8541c841676bde17bf Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 19 May 2022 12:34:16 +0200 Subject: [PATCH] improvement(player): separate inputs --- .../subgames/world/actors/player/actions.lua | 17 +--- .../subgames/world/actors/player/controls.lua | 79 +++++++++++++++++++ .../subgames/world/actors/player/init.lua | 8 +- .../world/actors/player/movements.lua | 50 ++++++------ 4 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 sonic-radiance.love/game/modules/subgames/world/actors/player/controls.lua diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/player/actions.lua b/sonic-radiance.love/game/modules/subgames/world/actors/player/actions.lua index 80c82af..1f45708 100644 --- a/sonic-radiance.love/game/modules/subgames/world/actors/player/actions.lua +++ b/sonic-radiance.love/game/modules/subgames/world/actors/player/actions.lua @@ -1,22 +1,7 @@ local PlayerActions = Object:extend() function PlayerActions:initActions() - self.action = "normal" -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 + self.action = "idle" end return PlayerActions \ No newline at end of file diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/player/controls.lua b/sonic-radiance.love/game/modules/subgames/world/actors/player/controls.lua new file mode 100644 index 0000000..23afb89 --- /dev/null +++ b/sonic-radiance.love/game/modules/subgames/world/actors/player/controls.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/player/init.lua b/sonic-radiance.love/game/modules/subgames/world/actors/player/init.lua index 9a067c0..9552408 100644 --- a/sonic-radiance.love/game/modules/subgames/world/actors/player/init.lua +++ b/sonic-radiance.love/game/modules/subgames/world/actors/player/init.lua @@ -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 Score = require "game.modules.subgames.world.actors.player.score" 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" Player:implement(Actions) @@ -15,6 +17,7 @@ Player:implement(Movements) Player:implement(Score) Player:implement(Sprites) Player:implement(Team) +Player:implement(Controls) function Player:new(world, x, y, z, id) 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 function Player:updateStart(dt) - self:basicMovements() - self:doActions() - self:actionSwitch() + self:applyInputs() end function Player:update(dt) + self:updateMovements(dt) Player.super.update(self, dt) self.tweens:update(dt) end diff --git a/sonic-radiance.love/game/modules/subgames/world/actors/player/movements.lua b/sonic-radiance.love/game/modules/subgames/world/actors/player/movements.lua index 132021d..c6d1599 100644 --- a/sonic-radiance.love/game/modules/subgames/world/actors/player/movements.lua +++ b/sonic-radiance.love/game/modules/subgames/world/actors/player/movements.lua @@ -1,40 +1,44 @@ local PlayerMovement = Object:extend() +local SPEED = 160 + function PlayerMovement:initMovements() self:setGravity(480 * 2) end -function PlayerMovement:basicMovements() +function PlayerMovement:updateMovements() 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 +-- 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() self.xfrc, self.yfrc = 480 * 3, 480 * 3 end +-- JUMP FUNCTIONS + function PlayerMovement:jump() - if self.keys["A"].isPressed and (self.onGround) then - self.zsp = 280 * 1.33 - end + self.zsp = 280 * 1.33 +end + +function PlayerMovement:isNotJumping() + return self.onGround end return PlayerMovement