src/core: add the input system

This commit is contained in:
Kazhnuz 2019-01-27 21:41:08 +01:00
parent 48cde0d947
commit 99ad6704c2
4 changed files with 128 additions and 1 deletions

View file

@ -28,14 +28,17 @@ local CoreSystem = Object:extend()
local DebugSystem = require "core.debug" local DebugSystem = require "core.debug"
local Options = require "core.options" local Options = require "core.options"
local Input = require "core.input"
function CoreSystem:new() function CoreSystem:new()
self.debug = DebugSystem(self) self.debug = DebugSystem(self)
self.options = Options(self) self.options = Options(self)
self.input = Input(self)
end end
function CoreSystem:update(dt) function CoreSystem:update(dt)
self.debug:update(dt) self.debug:update(dt)
self.input:update(dt)
end end
function CoreSystem:exit() function CoreSystem:exit()

View file

@ -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

View file

@ -42,7 +42,7 @@ function OptionsManager:reset()
self.data.video.fullscreen = false self.data.video.fullscreen = false
-- We load the default files -- 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 ? -- TODO: have a way to auto-load a language according to the OS ?
self.data.language = "en" self.data.language = "en"

View file

@ -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"
}
}
}