From 99ad6704c2f37a1370041b8a331406b12802ef32 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 27 Jan 2019 21:41:08 +0100 Subject: [PATCH] src/core: add the input system --- sonic-radiance.love/core/init.lua | 3 + sonic-radiance.love/core/input.lua | 108 +++++++++++++++++++++++++++ sonic-radiance.love/core/options.lua | 2 +- sonic-radiance.love/datas/inputs.lua | 16 ++++ 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 sonic-radiance.love/core/input.lua create mode 100644 sonic-radiance.love/datas/inputs.lua diff --git a/sonic-radiance.love/core/init.lua b/sonic-radiance.love/core/init.lua index e2ffc78..620c68c 100644 --- a/sonic-radiance.love/core/init.lua +++ b/sonic-radiance.love/core/init.lua @@ -28,14 +28,17 @@ local CoreSystem = Object:extend() local DebugSystem = require "core.debug" local Options = require "core.options" +local Input = require "core.input" function CoreSystem:new() self.debug = DebugSystem(self) self.options = Options(self) + self.input = Input(self) end function CoreSystem:update(dt) self.debug:update(dt) + self.input:update(dt) end function CoreSystem:exit() diff --git a/sonic-radiance.love/core/input.lua b/sonic-radiance.love/core/input.lua new file mode 100644 index 0000000..716dd46 --- /dev/null +++ b/sonic-radiance.love/core/input.lua @@ -0,0 +1,108 @@ +-- core/input.lua :: The input system. This object take care of transforming the +-- differents inputs source in a virtual controller for the game. + +--[[ + Copyright © 2019 Kazhnuz + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +]] + +local InputManager = Object:extend() + +function InputManager:new(controller) + self.controller = controller + self.data = self.controller.options.data.input[1] + + self.keys = {} + for k,v in pairs(self.data.keys) do + self.keys[k] = {} + self.keys[k].isDown = false + self.keys[k].isPressed = false + self.keys[k].isReleased = false + self.keys[k].test = "ok" + end + + self.fakekeys = {} + for k,v in pairs(self.data.keys) do + self.fakekeys[k] = {} + self.fakekeys[k].isDown = false + self.fakekeys[k].isPressed = false + self.fakekeys[k].isReleased = false + self.fakekeys[k].test = "ok" + end +end + +function InputManager:isDown(padkey) + local isdown = false + if self.data.type == "keyboard" then + local key = self.data.keys[padkey] + isdown = love.keyboard.isDown(key) + if isdown then + end + else + print("Warning: unsupported input device") + end + + return isdown +end + +function InputManager:translateAction(key) + --TODO:depreciated function + local padkey = "" + for k,v in pairs(self.data.keys) do + if v == key then padkey = k end + end + return padkey +end + +function InputManager:getKey(padkey) + local padkey = padkey + for k,v in pairs(self.data.keys) do + if (k == padkey) then key = v end + end + return key +end + +function InputManager:update(dt) + for k,v in pairs(self.keys) do + local isDown = self:isDown(k) + if (isDown) then + if not (self.keys[k].isDown) then + self.keys[k].isDown = true + self.keys[k].isPressed = true + self.keys[k].isReleased = false + else + if (self.keys[k].isPressed) then + self.keys[k].isPressed = false + end + end + else + if (self.keys[k].isDown) then + self.keys[k].isDown = false + self.keys[k].isPressed = false + self.keys[k].isReleased = true + else + if (self.keys[k].isReleased) then + self.keys[k].isReleased = false + end + end + end + end +end + +return InputManager diff --git a/sonic-radiance.love/core/options.lua b/sonic-radiance.love/core/options.lua index efd3075..0d3b82f 100644 --- a/sonic-radiance.love/core/options.lua +++ b/sonic-radiance.love/core/options.lua @@ -42,7 +42,7 @@ function OptionsManager:reset() self.data.video.fullscreen = false -- We load the default files - self.data.input = require "datas.input" + self.data.input = require "datas.inputs" -- TODO: have a way to auto-load a language according to the OS ? self.data.language = "en" diff --git a/sonic-radiance.love/datas/inputs.lua b/sonic-radiance.love/datas/inputs.lua new file mode 100644 index 0000000..053f97c --- /dev/null +++ b/sonic-radiance.love/datas/inputs.lua @@ -0,0 +1,16 @@ +return { + [1] = { + type = "keyboard", + keys = { + ["left"] = "left", + ["right"] = "right", + ["up"] = "up", + ["down"] = "down", + ["A"] = "a", + ["B"] = "z", + ["C"] = "e", + ["start"] = "return", + ["select"] = "space" + } + } + }