From de395abb0613d250c778ef2a2546939d9287e12a Mon Sep 17 00:00:00 2001 From: JailGamer Date: Fri, 3 Apr 2026 19:33:51 +0200 Subject: [PATCH] =?UTF-8?q?[NEW]=20Controller.lua=20-=20Gestiona=20tot=20l?= =?UTF-8?q?o=20relatiu=20al=20teclat=20i=20gamepad=20[NEW]=20opcions=5Finp?= =?UTF-8?q?ut.lua=20-=20Pantalla=20de=20definici=C3=B3=20de=20tecles=20per?= =?UTF-8?q?=20a=20teclat=20i=20pad=20-=20Modificat=20el=20abad=20per=20a?= =?UTF-8?q?=20que=20use=20controller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/abad.lua | 43 ++++------- data/controller.lua | 81 ++++++++++++++++++++ data/main.lua | 26 +------ data/opcions.lua | 10 ++- data/opcions_input.lua | 169 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 275 insertions(+), 54 deletions(-) create mode 100644 data/controller.lua create mode 100644 data/opcions_input.lua diff --git a/data/abad.lua b/data/abad.lua index 29b4bb3..cfeb3aa 100644 --- a/data/abad.lua +++ b/data/abad.lua @@ -1,5 +1,3 @@ --- TO DO: Afegir un contador per a saber quan de temps está parat el abad per a traure al Imp -keysdown = {} -- local arcade_config = require("arcade_config") cw = arcade_config.character_width ch = arcade_config.character_height @@ -307,34 +305,23 @@ function abad_state_normal() abad_make_safe() - if key.down(keyRight) or pad.down(btnRight) then + if controller:check("right") then abad.update=abad_state_walking abad.flip=false - elseif key.down(keyLeft) or pad.down(btnLeft) then + elseif controller:check("left") then abad.update=abad_state_walking abad.flip=true - elseif key.down(keyJump) or pad.down(btnJump) then + elseif controller:check("jump") then abad_do_jump() - elseif key.down(keyDown) or pad.down(btnDown) then + elseif controller:check("down") then abad.update=abad_state_crouch --elseif btn(KEY_Z) then -- abad.respawning=240 - elseif key_release(keyShoot) or pad.down(btnShoot) and cacau.hab==-1 then + elseif controller:check("shoot") then abad_shot_cacau() end end -function key_release(keyid) - -- if keysdown[keyid] then print("Pressed "..keyid) end - if key.down(keyid) then - keysdown[keyid] = true - elseif keysdown[keyid] then - keysdown[keyid] = false - return true - end - return false -end - function abad_state_crouch() abad.bb.y=8 abad.bb.h=24 @@ -343,7 +330,7 @@ function abad_state_crouch() abad.step=0 abad.jumpfwd=false abad.jump_height=0 - if not (key.down(keyDown) or pad.down(btnDown)) then + if not controller:check("down") then abad.update=abad_state_normal abad.bb.y=0 abad.bb.h=32 @@ -393,19 +380,19 @@ function abad_state_walking() abad_make_safe() -- Gestionar accions mentre es camina - if key.down(keyJump) or pad.down(btnJump) then + if controller:check("jump") then abad_do_jump(true); -- jumping forward return - elseif key.down(keyDown) or pad.down(btnDown) then + elseif controller:check("down") then abad.update=abad_state_crouch - elseif key.down(keyShoot) or pad.down(btnShoot) and cacau.hab==-1 then + elseif controller:check("shoot") then abad_shot_cacau() end -- "Flipar" a l'Abad - if key.down(keyRight) or pad.down(btnRight) then + if controller:check("right") then abad.flip=false - elseif key.down(keyLeft) or pad.down(btnLeft) then + elseif controller:check("left") then abad.flip=true elseif abad.wait==0 then abad.update=abad_state_normal @@ -477,21 +464,21 @@ function abad_state_stairs() local x2_check = abad.x+abad.bb.x+abad.bb.w-1 local y_check = abad.y+abad.bb.h-1 - if key.down(keyRight) or pad.down(btnRight) then + if controller:check("right") then abad.flip=false abad_advance() if abad.wait==6 then sound.play(audio_low) end - elseif key.down(keyLeft) or pad.down(btnLeft) then + elseif controller:check("left") then abad.flip=true abad_advance() if abad.wait==6 then sound.play(audio_low) end - elseif key.down(keyUp) or pad.down(btnUp) then + elseif controller:check("up") then if arc_check_tile(x1_check,y_check)==tiletype.stair and arc_check_tile(x2_check,y_check)==tiletype.stair then abad.y=abad.y-1 if abad.wait==6 then sound.play(audio_low) end end - elseif key.down(keyDown) or pad.down(btnDown) then + elseif controller:check("down") then if arc_check_tile(x1_check,y_check)==tiletype.stair and arc_check_tile(x2_check,y_check)==tiletype.stair then abad.y=abad.y+1 diff --git a/data/controller.lua b/data/controller.lua new file mode 100644 index 0000000..7f6e222 --- /dev/null +++ b/data/controller.lua @@ -0,0 +1,81 @@ + +controller = { + input = { kb=0, pad=1 }, + keysdown = {}, + kb_keys= { {name="up", code="keyup"}, + {name="down", code="keydow"}, + {name="left", code="keyleft"}, + {name="right",code="keyright"}, + {name="jump", code="keyjump"}, + {name="shoot",code="keyshoot"}}, + pad_keys= { {name="up", code="btnup"}, + {name="down", code="btndown"}, + {name="left", code="keleft"}, + {name="right",code="btnright"}, + {name="jump", code="btnjump"}, + {name="shoot",code="btnshoot"}, + {name="next", code="btncycle1"}, + {name="prev", code="btncycle2"}, + {name="pause",code="btnpause"}} +} + +function controller:init() + -- Configuració dels input + keyUp = tonumber(config.key("keyup")) or key.UP + keyDown = tonumber(config.key("keydown")) or key.DOWN + keyLeft = tonumber(config.key("keyleft")) or key.LEFT + keyRight = tonumber(config.key("keyright")) or key.RIGHT + keyJump = tonumber(config.key("keyjump")) or key.UP + keyShoot = tonumber(config.key("keyshoot")) or key.SPACE + + btnUp = tonumber(config.key("btnup")) or pad.UP + btnDown = tonumber(config.key("btndown")) or pad.DOWN + btnLeft = tonumber(config.key("btnleft")) or pad.LEFT + btnRight = tonumber(config.key("btnright")) or pad.RIGHT + btnJump = tonumber(config.key("btnjump")) or pad.B + btnShoot = tonumber(config.key("btnshoot")) or pad.A + btnCycle1 = tonumber(config.key("btncycle1")) or pad.RIGHTSHOULDER + btnCycle2 = tonumber(config.key("btncycle2")) or pad.LEFTSHOULDER + btnPause = tonumber(config.key("btnpause")) or pad.START +end + +function controller:define(code, key_code) + config.key(code, key_code) + -- To do: comprovar que es un code valid + return true +end + +function controller:check(action) + local result = false + if action=="up" then + result = key.down(keyUp) or pad.down(btnUp) + elseif action=="down" then + result = key.down(keyDown) or pad.down(btnDown) + elseif action=="left" then + result = key.down(keyLeft) or pad.down(btnLeft) + elseif action=="right" then + result = key.down(keyRight) or pad.down(btnRight) + elseif action=="jump" then + result = key.down(keyJump) or pad.down(btnJump) + elseif action=="shoot" then + -- result = key.down(keyShoot) or pad.down(btnShoot) + result = self:key_release(keyShoot) or self:key_release(btnShoot) + end + return result +end + +function controller:key_release(keyid) + if keyid==nil then + print("INVALID KEY") + return + end + -- if keysdown[keyid] then print("Pressed "..keyid) end + if key.down(keyid) or pad.down(keyid) then + self.keysdown[keyid] = true + elseif self.keysdown[keyid] + then + self.keysdown[keyid] = false + return true + end + return false +end \ No newline at end of file diff --git a/data/main.lua b/data/main.lua index 15f27d2..e6c147e 100644 --- a/data/main.lua +++ b/data/main.lua @@ -1,6 +1,7 @@ arcade_config = require("arcade_config") coords = require "coords" require "helpers" +require "controller" require "flow" require "map" @@ -14,6 +15,7 @@ require "scenes" require "title" require "game" require "opcions" +require "opcions_input" --require "fps" --require "title" @@ -30,26 +32,6 @@ coords.set_config({ rooms_per_floor = mapa_rooms_per_piso, }) -function keys_init() - -- Configuració dels input - keyUp = tonumber(config.key("keyup")) or key.UP - keyDown = tonumber(config.key("keydown")) or key.DOWN - keyLeft = tonumber(config.key("keyleft")) or key.LEFT - keyRight = tonumber(config.key("keyright")) or key.RIGHT - keyJump = tonumber(config.key("keyjump")) or key.UP - keyShoot = tonumber(config.key("keyshoot")) or key.SPACE - - btnUp = tonumber(config.key("btnup")) or pad.UP - btnDown = tonumber(config.key("btndown")) or pad.DOWN - btnLeft = tonumber(config.key("btnleft")) or pad.LEFT - btnRight = tonumber(config.key("btnright")) or pad.RIGHT - btnJump = tonumber(config.key("btnjump")) or pad.B - btnShoot = tonumber(config.key("btnshoot")) or pad.A - btnCycle1 = tonumber(config.key("btncycle1")) or pad.RIGHTSHOULDER - btnCycle2 = tonumber(config.key("btncycle2")) or pad.LEFTSHOULDER - btnPause = tonumber(config.key("btnpause")) or pad.START -end - function images_init() tiles=surf.load("tiles.gif") surf.source(tiles) @@ -100,12 +82,12 @@ function mini.init() mapa_init() fade.init() audio_init() - keys_init() + controller:init() logo_config(font_sf) surf.target(0) surf.cls(16) - flow:executar("logo") + flow:executar("title") end function mini.update() diff --git a/data/opcions.lua b/data/opcions.lua index 5cb0578..b1a20af 100644 --- a/data/opcions.lua +++ b/data/opcions.lua @@ -25,11 +25,13 @@ function opcions_update() elseif menu_sel==1 then sound.enabled(not sound.enabled()) elseif menu_sel==2 then - menu_count=0 - menu_state=update_redefine_keys_menu + -- menu_count=0 + -- menu_state=update_redefine_keys_menu + flow:executar("opcions_teclat", true) elseif menu_sel==3 then - menu_count=0 - menu_state=update_redefine_pad_menu + -- menu_count=0 + -- menu_state=update_redefine_pad_menu + flow:executar("opcions_gamepad", true) else -- Tornar flow:next() diff --git a/data/opcions_input.lua b/data/opcions_input.lua new file mode 100644 index 0000000..3d125f4 --- /dev/null +++ b/data/opcions_input.lua @@ -0,0 +1,169 @@ +local input_type=controller.input.kb +local input_keys={} +local menu_opt = 0 + +function opcions_teclat_init() + input_type=controller.input.kb + -- definició de tecles a configurar en ordre + input_keys = controller.kb_keys + opcions_input_init() +end + +function opcions_gamepad_init() + input_type=controller.input.pad + -- definició de botons a configurar en ordre + input_keys = controller.pad_keys + opcions_input_init() +end + +function opcions_input_init() + surf.target(0) + surf.cls(16) + menu_opt = 1 + flow:next() +end + +function opcions_input_draw() + surf.target(0) + surf.cls(16) + local s="" + if input_type==controller.input.kb then + s="PULSA TECLA PER A " + if menu_opt==1 then + arc_textB(s.."AMUNT", 40, 48, 13) + elseif menu_opt==2 then + arc_textB(s.."AVALL", 40, 48, 13) + elseif menu_opt==3 then + arc_textB(s.."ESQUERRA", 28, 48, 13) + elseif menu_opt==4 then + arc_textB(s.."DRETA", 40, 48, 13) + elseif menu_opt==5 then + arc_textB(s.."BOTAR", 40, 48, 13) + elseif menu_opt==6 then + arc_textB(s.."DISPAR", 36, 48, 13) + end + elseif input_type==controller.input.pad then + s="PULSA BOTÓ PER A " + if menu_opt==1 then + arc_textB(s.."AMUNT", 44, 48, 13) + elseif menu_opt==2 then + arc_textB(s.."AVALL", 44, 48, 13) + elseif menu_opt==3 then + arc_textB(s.."ESQUERRA", 32, 48, 13) + elseif menu_opt==4 then + arc_textB(s.."DRETA", 44, 48, 13) + elseif menu_opt==5 then + arc_textB(s.."BOTAR", 44, 48, 13) + elseif menu_opt==6 then + arc_textB(s.."DISPAR", 40, 48, 13) + elseif menu_opt==7 then + arc_textB(s.."GPS ARRERE", 28, 48, 13) + elseif menu_opt==8 then + arc_textB(s.."GPS AVANT", 28, 48, 13) + elseif menu_opt==9 then + arc_textB(s.."PAUSA", 44, 48, 13) + end + end +end + +function opcions_input_update() + local curr_key = input_keys[menu_opt] + + if key.press(key.ESCAPE) then + flow:next() + return + end + + if input_type==controller.input.kb then + local k = key.press() + if k ~= 0 and k~=key.ESCAPE then + print(k..", "..curr_key.name..", "..curr_key.code) + config_key(curr_key, k) + menu_opt = menu_opt+1 + end + elseif input_type==controller.input.pad then + local btn = pad.press(); + if btn ~= -1 then + config_key(curr_key, btn) + menu_opt = menu_opt+1 + end + end + + if menu_opt >#input_keys then menu_opt=1 end +end + +function opcions_input_show() + opcions_input_draw() + opcions_input_update() +end + +function opcions_input_end() + menu_opt = 0 + -- menu_state = update_options_menu; + flow:finish() +end + +function config_key(def_key, k) + local end_setup=false + local valid_key = true + -- if input_type==controller.input.kb then + -- if def_key.name=="up" then + -- keyUp=k + -- elseif def_key.name=="down" then + -- keyDown=k + -- elseif def_key.name=="left" then + -- keyLeft=k + -- elseif def_key.name=="right" then + -- keyRight=k + -- elseif def_key.name=="jump" then + -- keyJump=k + -- elseif def_key.name=="shoot" then + -- keyShoot=k + -- end_setup = true + -- else + -- valid_key = false + -- end + -- elseif input_type==controller.input.pad then + -- if def_key.name=="up" then + -- btnUp=k + -- elseif def_key.name=="down" then + -- btnDown=k + -- elseif def_key.name=="left" then + -- btnLeft=k + -- elseif def_key.name=="right" then + -- btnRight=k + -- elseif def_key.name=="jump" then + -- btnJump=k + -- elseif def_key.name=="shoot" then + -- btnShoot=k + -- elseif def_key.name=="next" then + -- btnCycle1=k + -- elseif def_key.name=="prev" then + -- btnCycle2=k + -- elseif def_key.name=="pause" then + -- btnPause=k + -- end_setup = true + -- else + -- valid_key = false + -- end + -- end + + if (input_type==controller.input.kb and def_key.name=="shoot") + or (input_type==controller.input.pad and def_key.name=="pause") then + end_setup = true + end + + if not controller:define(def_key.code, k) then valid_key=false end + + if valid_key then + print("DEFINED > "..def_key.name..", "..def_key.code.." = "..k) + config.key(def_key.code, k) + else + print("ERROR AL DEFINIR") + end + + if end_setup then flow:next() end +end + +flow:registrar("opcions_teclat", { opcions_teclat_init, opcions_input_show, opcions_input_end } ) +flow:registrar("opcions_gamepad", { opcions_gamepad_init, opcions_input_show, opcions_input_end } ) \ No newline at end of file