diff --git a/config.lua b/config.lua index 315775f..bad77c6 100644 --- a/config.lua +++ b/config.lua @@ -188,6 +188,8 @@ add_input("right", { 192, 20, 21 }) add_input("left", { 192, 20, 21 }) add_input("focus", { 192, 20, 21 }) add_input("swap", { 20, 21 }) +add_input("exit", { 192, 20, 21, 3 }) +add_input("restart", { 192, 20, 21, 3 }) add_input("ui_up") add_input("ui_down") add_input("ui_right") diff --git a/game_handler/init.lua b/game_handler/init.lua index 1390c4d..2f2baf4 100644 --- a/game_handler/init.lua +++ b/game_handler/init.lua @@ -257,6 +257,35 @@ function game_handler.process_event(name, ...) msaa = 4, }) end + if name == "customkeydown" and game_handler.is_running() and not input.is_replaying() then + local key = ... + if not current_game.preview_mode then + if key == "exit" then + local death_overlay = require("ui.overlay.death") + if death_overlay.is_open then + death_overlay.layout.elements[2]:click() + else + if current_game.death_callback then + current_game.death_callback() + end + game_handler.stop() + require("ui.screens.levelselect.score").refresh() + game_handler.onupdate() + end + elseif key == "restart" then + local death_overlay = require("ui.overlay.death") + if death_overlay.is_open then + death_overlay.layout.elements[1]:click() + else + if current_game.death_callback then + current_game.death_callback() + end + game_handler.stop() + game_handler.retry() + end + end + end + end if current_game then -- allow game modules to have their own event handlers if current_game.running and current_game[name] ~= nil then diff --git a/input.lua b/input.lua index 4f73608..7d08018 100644 --- a/input.lua +++ b/input.lua @@ -88,7 +88,7 @@ local mapping = { ---records changes if recording ---gets input state from replay if replaying ---@param input_name string ----@param add_ui_button boolean +---@param add_ui_button boolean? ---@return boolean function input.get(input_name, add_ui_button) input_name = mapping[input_name] or input_name diff --git a/input_schemes/keyboard.lua b/input_schemes/keyboard.lua index fc6854d..3b35e6d 100644 --- a/input_schemes/keyboard.lua +++ b/input_schemes/keyboard.lua @@ -4,6 +4,8 @@ local keyboard = { right = { "right", "d" }, left = { "left", "a" }, swap = { "space" }, + exit = { "escape" }, + restart = { "up" }, ui_backspace = { "backspace" }, ui_delete = { "delete" }, ui_up = { "up" }, diff --git a/ui/key_repeat.lua b/ui/key_repeat.lua index 420c8ca..e2c9bea 100644 --- a/ui/key_repeat.lua +++ b/ui/key_repeat.lua @@ -1,6 +1,7 @@ local input = require("input") local key_repeat = {} -local keys = { "ui_up", "ui_down", "ui_left", "ui_right", "ui_click", "ui_backspace", "ui_delete" } +local keys = { "ui_up", "ui_down", "ui_left", "ui_right", "ui_click", "ui_backspace", "ui_delete", "exit", "restart" } +local modules = { "ui", "game_handler" } local states = {} local press_timers = {} local press_timer_repeat = {} @@ -20,10 +21,13 @@ function key_repeat.update(dt) press_timer_repeat[i] = 0.4 end if state ~= states[i] then - if state then - require("ui").process_event("customkeydown", keys[i]) - else - require("ui").process_event("customkeyup", keys[i]) + for j = 1, #modules do + local module = require(modules[j]) + if state then + module.process_event("customkeydown", keys[i]) + else + module.process_event("customkeyup", keys[i]) + end end end states[i] = state