[NEW] Controller.lua - Gestiona tot lo relatiu al teclat i gamepad

[NEW] opcions_input.lua - Pantalla de definició de tecles per a teclat i pad
- Modificat el abad per a que use controller
This commit is contained in:
2026-04-03 19:33:51 +02:00
parent ca0393046e
commit de395abb06
5 changed files with 275 additions and 54 deletions

View File

@@ -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") -- local arcade_config = require("arcade_config")
cw = arcade_config.character_width cw = arcade_config.character_width
ch = arcade_config.character_height ch = arcade_config.character_height
@@ -307,34 +305,23 @@ function abad_state_normal()
abad_make_safe() abad_make_safe()
if key.down(keyRight) or pad.down(btnRight) then if controller:check("right") then
abad.update=abad_state_walking abad.update=abad_state_walking
abad.flip=false abad.flip=false
elseif key.down(keyLeft) or pad.down(btnLeft) then elseif controller:check("left") then
abad.update=abad_state_walking abad.update=abad_state_walking
abad.flip=true abad.flip=true
elseif key.down(keyJump) or pad.down(btnJump) then elseif controller:check("jump") then
abad_do_jump() abad_do_jump()
elseif key.down(keyDown) or pad.down(btnDown) then elseif controller:check("down") then
abad.update=abad_state_crouch abad.update=abad_state_crouch
--elseif btn(KEY_Z) then --elseif btn(KEY_Z) then
-- abad.respawning=240 -- abad.respawning=240
elseif key_release(keyShoot) or pad.down(btnShoot) and cacau.hab==-1 then elseif controller:check("shoot") then
abad_shot_cacau() abad_shot_cacau()
end end
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() function abad_state_crouch()
abad.bb.y=8 abad.bb.y=8
abad.bb.h=24 abad.bb.h=24
@@ -343,7 +330,7 @@ function abad_state_crouch()
abad.step=0 abad.step=0
abad.jumpfwd=false abad.jumpfwd=false
abad.jump_height=0 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.update=abad_state_normal
abad.bb.y=0 abad.bb.y=0
abad.bb.h=32 abad.bb.h=32
@@ -393,19 +380,19 @@ function abad_state_walking()
abad_make_safe() abad_make_safe()
-- Gestionar accions mentre es camina -- 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 abad_do_jump(true); -- jumping forward
return return
elseif key.down(keyDown) or pad.down(btnDown) then elseif controller:check("down") then
abad.update=abad_state_crouch 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() abad_shot_cacau()
end end
-- "Flipar" a l'Abad -- "Flipar" a l'Abad
if key.down(keyRight) or pad.down(btnRight) then if controller:check("right") then
abad.flip=false abad.flip=false
elseif key.down(keyLeft) or pad.down(btnLeft) then elseif controller:check("left") then
abad.flip=true abad.flip=true
elseif abad.wait==0 then elseif abad.wait==0 then
abad.update=abad_state_normal 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 x2_check = abad.x+abad.bb.x+abad.bb.w-1
local y_check = abad.y+abad.bb.h-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.flip=false
abad_advance() abad_advance()
if abad.wait==6 then sound.play(audio_low) end 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.flip=true
abad_advance() abad_advance()
if abad.wait==6 then sound.play(audio_low) end 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 if arc_check_tile(x1_check,y_check)==tiletype.stair and
arc_check_tile(x2_check,y_check)==tiletype.stair then arc_check_tile(x2_check,y_check)==tiletype.stair then
abad.y=abad.y-1 abad.y=abad.y-1
if abad.wait==6 then sound.play(audio_low) end if abad.wait==6 then sound.play(audio_low) end
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 if arc_check_tile(x1_check,y_check)==tiletype.stair and
arc_check_tile(x2_check,y_check)==tiletype.stair then arc_check_tile(x2_check,y_check)==tiletype.stair then
abad.y=abad.y+1 abad.y=abad.y+1

81
data/controller.lua Normal file
View File

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

View File

@@ -1,6 +1,7 @@
arcade_config = require("arcade_config") arcade_config = require("arcade_config")
coords = require "coords" coords = require "coords"
require "helpers" require "helpers"
require "controller"
require "flow" require "flow"
require "map" require "map"
@@ -14,6 +15,7 @@ require "scenes"
require "title" require "title"
require "game" require "game"
require "opcions" require "opcions"
require "opcions_input"
--require "fps" --require "fps"
--require "title" --require "title"
@@ -30,26 +32,6 @@ coords.set_config({
rooms_per_floor = mapa_rooms_per_piso, 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() function images_init()
tiles=surf.load("tiles.gif") tiles=surf.load("tiles.gif")
surf.source(tiles) surf.source(tiles)
@@ -100,12 +82,12 @@ function mini.init()
mapa_init() mapa_init()
fade.init() fade.init()
audio_init() audio_init()
keys_init() controller:init()
logo_config(font_sf) logo_config(font_sf)
surf.target(0) surf.target(0)
surf.cls(16) surf.cls(16)
flow:executar("logo") flow:executar("title")
end end
function mini.update() function mini.update()

View File

@@ -25,11 +25,13 @@ function opcions_update()
elseif menu_sel==1 then elseif menu_sel==1 then
sound.enabled(not sound.enabled()) sound.enabled(not sound.enabled())
elseif menu_sel==2 then elseif menu_sel==2 then
menu_count=0 -- menu_count=0
menu_state=update_redefine_keys_menu -- menu_state=update_redefine_keys_menu
flow:executar("opcions_teclat", true)
elseif menu_sel==3 then elseif menu_sel==3 then
menu_count=0 -- menu_count=0
menu_state=update_redefine_pad_menu -- menu_state=update_redefine_pad_menu
flow:executar("opcions_gamepad", true)
else else
-- Tornar -- Tornar
flow:next() flow:next()

169
data/opcions_input.lua Normal file
View File

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