From f021446439d387e14b4896b529bcced5ffc71f15 Mon Sep 17 00:00:00 2001 From: bizcochito Date: Tue, 18 Jan 2022 18:46:41 +0100 Subject: [PATCH] Made keybind editing "backend" --- data/scripts/entities/player.lua | 16 +++++------ data/scripts/keybind.lua | 47 +++++++++++++++++++++++++------- main.lua | 2 +- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/data/scripts/entities/player.lua b/data/scripts/entities/player.lua index 4ea1e5f..4798690 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.moveLeft) then + if Keybind:Check(Keybind.move.left) then self.move_x = -self.moveSpeed - elseif keybind:Check(keybind.moveRight) then + elseif Keybind:Check(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.moveJump) then + if Keybind:Check(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.moveDash) then + if Keybind:Check(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.moveDown) then vertical = vertical + 1 end - if keybind:Check(keybind.moveUp) then vertical = vertical - 1 end + if Keybind:Check(Keybind.move.down) then vertical = vertical + 1 end + if Keybind:Check(Keybind.move.up) then vertical = vertical - 1 end local horizontal = 0 - if keybind:Check(keybind.moveRight) then horizontal = horizontal + 1 end - if keybind:Check(keybind.moveLeft) then horizontal = horizontal - 1 end + if Keybind:Check(Keybind.move.right) then horizontal = horizontal + 1 end + if Keybind:Check(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 6d42b06..4f2a349 100644 --- a/data/scripts/keybind.lua +++ b/data/scripts/keybind.lua @@ -1,7 +1,9 @@ -keybind = {} +Keybind = {} +Keybind.move = {} +Keybind.menu = {} -function keybind:Check(key) - for _, keyname in pairs(key) do +function Keybind:Check(action) + for _, keyname in pairs(action) do if type(keyname) == "string" then if love.keyboard.isDown(keyname) then return true end else @@ -11,11 +13,36 @@ function keybind:Check(key) return false end +function Keybind:Colision(cat, key) + for _, action in pairs(cat) do + for _, keyname in pairs(action) do + if key == keyname then return true end + end + end + return false +end -keybind.moveLeft = {"left", "a"} -keybind.moveRight = {"right", "d"} -keybind.moveUp = {"up", "w"} -keybind.moveDown = {"down", "s"} -keybind.moveJump = {"z", "space"} -keybind.moveAttack = {"x", 1} -keybind.moveDash = {"c", 2} +function Keybind:Add(action, key) + table.insert(action, key) +end + +function Keybind:Change(action, position, key) + action[position] = key +end + +function Keybind:Remove(action) + action = {} +end + +function Keybind:Default() + 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} +end + +-- Set default values at start +Keybind:Default() diff --git a/main.lua b/main.lua index 15b32fb..428ee8e 100644 --- a/main.lua +++ b/main.lua @@ -48,7 +48,7 @@ function love.load() table.insert(LoadedEntities,Decoration:New(200,89,animation.decoration.candelabra,80)) table.insert(LoadedEntities,Fairy:New(200,88)) - gravity = 0.05 + gravity = 0.2 end function love.update(dt)