From 03793e1fb8c03975455c22927ef89bacae9881a6 Mon Sep 17 00:00:00 2001 From: bizcochito Date: Wed, 19 Jan 2022 14:55:15 +0100 Subject: [PATCH 1/2] Made HasPressed function and implemented it --- data/scripts/entities/player.lua | 16 ++++----- data/scripts/keybind.lua | 56 +++++++++++++++++++------------- main.lua | 10 +++--- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/data/scripts/entities/player.lua b/data/scripts/entities/player.lua index 4798690..bbf670c 100644 --- a/data/scripts/entities/player.lua +++ b/data/scripts/entities/player.lua @@ -73,16 +73,16 @@ function Player:Smart() if math.abs(self.vel.x) < self.zeroSpeed then self.vel.x = 0 end - if Keybind:Check(Keybind.move.left) then + if Keybind:CheckDown(Keybind.move.left) then self.move_x = -self.moveSpeed - elseif Keybind:Check(Keybind.move.right) then + elseif Keybind:CheckDown(Keybind.move.right) then self.move_x = self.moveSpeed else self.move_x = 0 end self.vel.x = self.vel.x - if Keybind:Check(Keybind.move.jump) then + if Keybind:CheckDown(Keybind.move.jump) then if self.isOnGround then self.vel.y = -self.jumpImpulse end @@ -90,18 +90,18 @@ function Player:Smart() end self.dashCooldownTimer = math.max(0,self.dashCooldownTimer - current_dt) - if Keybind:Check(Keybind.move.dash) then + if Keybind:CheckDown(Keybind.move.dash) then if self.dashCooldownTimer == 0 and not self.isDashing and self.dashCount > 0 then self.dashCount = self.dashCount - 1 self.isDashing = true local vertical = 0 - if Keybind:Check(Keybind.move.down) then vertical = vertical + 1 end - if Keybind:Check(Keybind.move.up) then vertical = vertical - 1 end + if Keybind:CheckDown(Keybind.move.down) then vertical = vertical + 1 end + if Keybind:CheckDown(Keybind.move.up) then vertical = vertical - 1 end local horizontal = 0 - if Keybind:Check(Keybind.move.right) then horizontal = horizontal + 1 end - if Keybind:Check(Keybind.move.left) then horizontal = horizontal - 1 end + if Keybind:CheckDown(Keybind.move.right) then horizontal = horizontal + 1 end + if Keybind:CheckDown(Keybind.move.left) then horizontal = horizontal - 1 end if horizontal == 0 and vertical == 0 then horizontal = self.sprite_flip.x diff --git a/data/scripts/keybind.lua b/data/scripts/keybind.lua index c8a4fd8..a4c0917 100644 --- a/data/scripts/keybind.lua +++ b/data/scripts/keybind.lua @@ -3,8 +3,8 @@ Keybind.move = {} Keybind.menu = {} Keybind.debug = {} -function Keybind:Check(action) - for _, keyname in pairs(action) do +function Keybind:CheckDown(action) + for _, keyname in pairs(action.keys) do if type(keyname) == "string" then if love.keyboard.isDown(keyname) then return true end else @@ -14,43 +14,55 @@ function Keybind:Check(action) return false end -function Keybind:Colision(cat, key) +function Keybind:HasPressed(action) + if Keybind:CheckDown(action) then + if not action.pressed then + action.pressed = true + return true + end + else + action.pressed = false + end + return false +end + +function Keybind:CheckCollision(cat, key) for _, action in pairs(cat) do - for _, keyname in pairs(action) do + for _, keyname in pairs(action.keys) do if key == keyname then return true end end end return false end -function Keybind:Add(action, key) - table.insert(action, key) +function Keybind:AddKey(action, key) + table.insert(action.keys, key) end -function Keybind:Change(action, position, key) - action[position] = key +function Keybind:ChangeKey(action, position, key) + action.keys[position] = key end -function Keybind:Remove(action) - action = {} +function Keybind:RemoveKeys(action) + action.keys = {} end function Keybind:Default() --Menu - Keybind.menu.pause = {"escape"} + Keybind.menu.pause= { keys = {"escape"}} --Move - Keybind.move.left = {"left", "a"} - Keybind.move.right = {"right", "d"} - Keybind.move.up = {"up", "w"} - Keybind.move.down = {"down", "s"} - Keybind.move.jump = {"z", "space"} - Keybind.move.attack = {"x", 1} - Keybind.move.dash = {"c", 2} + Keybind.move.left = { keys = {"left", "a"}} + Keybind.move.right = { keys = {"right", "d"}} + Keybind.move.up = { keys = {"up", "w"}} + Keybind.move.down = { keys = {"down", "s"}} + Keybind.move.jump = { keys = {"z", "space"}} + Keybind.move.attack = { keys = {"x", 1}} + Keybind.move.dash = { keys = {"c", 2}} --Debug - Keybind.debug.debug = {"f1"} - Keybind.debug.reposition = {"f2"} - Keybind.debug.reload = {"f3"} - Keybind.debug.editor = {"f4"} + Keybind.debug.debug = { keys = {"f1"}} + Keybind.debug.reposition = { keys = {"f2"}} + Keybind.debug.reload = { keys = {"f3"}} + Keybind.debug.editor = { keys = {"f4"}} end -- Set default values at start diff --git a/main.lua b/main.lua index 1a0a51e..55d7814 100644 --- a/main.lua +++ b/main.lua @@ -66,7 +66,7 @@ function love.update(dt) --keypressed - if Keybind:Check(Keybind.menu.pause) then + if Keybind:HasPressed(Keybind.menu.pause) then if do_pause then do_pause = false else @@ -75,7 +75,7 @@ function love.update(dt) end end - if Keybind:Check(Keybind.debug.debug) then + if Keybind:HasPressed(Keybind.debug.debug) then if debug then debug = false debug_collision = true @@ -86,17 +86,17 @@ function love.update(dt) end end - if Keybind:Check(Keybind.debug.reposition) then + if Keybind:HasPressed(Keybind.debug.reposition) then if not editor_mode then main_Player.pos.x, main_Player.pos.y = 16,-10 end end - if Keybind:Check(Keybind.debug.reload) then + if Keybind:HasPressed(Keybind.debug.reload) then LoadLevel() end - if Keybind:Check(Keybind.debug.editor) then + if Keybind:HasPressed(Keybind.debug.editor) then if editor_mode then editor_mode = false else -- 2.39.2 From 2972cf1488a58f3af469a70076a5c45f5fe00901 Mon Sep 17 00:00:00 2001 From: bizcochito Date: Wed, 19 Jan 2022 14:58:49 +0100 Subject: [PATCH 2/2] Made HasPressed more memory efficient --- data/scripts/keybind.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/scripts/keybind.lua b/data/scripts/keybind.lua index a4c0917..35a5e05 100644 --- a/data/scripts/keybind.lua +++ b/data/scripts/keybind.lua @@ -21,7 +21,7 @@ function Keybind:HasPressed(action) return true end else - action.pressed = false + action.pressed = nil end return false end -- 2.39.2