diff options
| author | ne_mene <[email protected]> | 2026-03-08 22:21:42 +0100 |
|---|---|---|
| committer | ne_mene <[email protected]> | 2026-03-08 22:21:42 +0100 |
| commit | b744faa2e42ed37459fe3edb69c1149146233e5b (patch) | |
| tree | 25f238d3e467c7cf43e619b579e0df89762b1cca /src/input.lua | |
| parent | 95d50b15634bf4799a2005e381d82c110fbff39b (diff) | |
let there be light
Diffstat (limited to 'src/input.lua')
| -rw-r--r-- | src/input.lua | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/input.lua b/src/input.lua new file mode 100644 index 0000000..8029be2 --- /dev/null +++ b/src/input.lua @@ -0,0 +1,75 @@ +local lk = love.keyboard +local lm = love.mouse + +local inputs = {} +local keyEvents = {} +local mouseEvents = {} + +---@param triggers table pair like this {type, keycode/name} +function register_input(name, triggers) + inputs[name] = triggers +end + +function input_direction(left, right, up, down) + return bton(is_input_pressed(right)) - bton(is_input_pressed(left)), + bton(is_input_pressed(down)) - bton(is_input_pressed(up)) +end + +function is_input_just_pressed(name) + local inp = inputs[name] + local type + local code + + for _, trig in ipairs(inp) do + type = trig[1] + code = trig[2] + + if type == "mouse" then + if mouseEvents[code] ~= nil then return true end + end + if type == "key" then + if keyEvents[code] ~= nil then return true end + end + end +end + +function is_input_pressed(name) + local inp = inputs[name] + local type + local code + + for _, trig in ipairs(inp) do + type = trig[1] + code = trig[2] + + if type == "mouse" then + if lm.isDown(code) then return true end + end + if type == "key" then + if lk.isDown(code) then return true end + end + end +end + +function love.keypressed(key) + keyEvents[key] = true +end + +function love.mousepressed(x, y, btn) + mouseEvents[btn] = true +end + +function get_mouse_pos() + -- TODO: Fix mouse position relative to games canvas + local scrw, scrh = love.graphics.getDimensions() + return math.floor(lm.getX() / scrw * SCR_WIDTH), math.floor(lm.getY() / scrh * SCR_HEIGHT) +end + +function input_step() + for key in pairs(keyEvents) do + keyEvents[key] = nil + end + for key in pairs(mouseEvents) do + mouseEvents[key] = nil + end +end |
