DX: buffer d'edges per a moviment (arregla input lag sense doble pas)

This commit is contained in:
2026-05-15 21:40:49 +02:00
parent 246b9cc687
commit 380cab4176
+37 -5
View File
@@ -113,6 +113,30 @@ fade = nil -- nil = sense transicio; o { phase="out"|"in", t=0, on_mid=... }
-- (musica.title, musica.entername) i music_on viuen a config.lua. -- (musica.title, musica.entername) i music_on viuen a config.lua.
music_actual = nil music_actual = nil
-- Buffer d'edges d'input direccional (esquerra/dreta/dalt/baix).
--
-- La logica del joc avanca cada TICS=6 frames pero el bucle d'update() corre
-- a 60 fps. Bufferitzem nomes les *transicions* (btnp = edge de pulsacio):
-- aixi un tap rapid (1-9 frames) genera UN sol moviment, encara que caiga
-- entre dos tics. El moviment "mantingut" funciona per btn() directe al tic
-- (vore tic_pepe): si la tecla segueix premuda quan arriba el tic, mou.
--
-- Per que no bufferitzar el btn()? Perque un tap de 7-9 frames (un tap humà
-- "normal" de ~130 ms) podria abarcar 2 tics consecutius i Pepe avançaria
-- 2 caselles d'un sol toc. Bufferitzar nomes l'edge garanteix 1 = 1.
input_buf = { up=false, down=false, left=false, right=false }
function sample_input()
if btnp(keys.up) then input_buf.up = true end
if btnp(keys.down) then input_buf.down = true end
if btnp(keys.left) then input_buf.left = true end
if btnp(keys.right) then input_buf.right = true end
end
function reset_input()
input_buf.up, input_buf.down, input_buf.left, input_buf.right = false, false, false, false
end
-- Estat de l'animacio de l'escala lateral (apareix quan no queden diners). -- Estat de l'animacio de l'escala lateral (apareix quan no queden diners).
-- "idle" → encara no s'ha disparat per a este mapa -- "idle" → encara no s'ha disparat per a este mapa
-- "growing" → en curs: cada escala_step_frames apareix una cel·la nova -- "growing" → en curs: cada escala_step_frames apareix una cel·la nova
@@ -428,11 +452,12 @@ function tic_pepe()
-- Moviment vertical (com en RUNNER.PAS, son if/else). -- Moviment vertical (com en RUNNER.PAS, son if/else).
-- Bloquegem entrar dins de qualsevol cel·la "paret" (inclou malos atrapats). -- Bloquegem entrar dins de qualsevol cel·la "paret" (inclou malos atrapats).
if btn(keys.up) then -- Edge bufferitzat (input_buf, per a taps curts) OR tecla mantinguda (btn).
if input_buf.up or btn(keys.up) then
if actual == ESCALA and not es_paret(pepe.x, pepe.y-1) then if actual == ESCALA and not es_paret(pepe.x, pepe.y-1) then
pepe.y = pepe.y - 1 pepe.y = pepe.y - 1
end end
elseif btn(keys.down) then elseif input_buf.down or btn(keys.down) then
if not es_paret(pepe.x, pepe.y+1) if not es_paret(pepe.x, pepe.y+1)
and (sotto == ESCALA or sotto == BUIT or sotto == DINERS) then and (sotto == ESCALA or sotto == BUIT or sotto == DINERS) then
pepe.y = pepe.y + 1 pepe.y = pepe.y + 1
@@ -440,11 +465,11 @@ function tic_pepe()
end end
-- Moviment horitzontal (no es pot moure si esta caent) -- Moviment horitzontal (no es pot moure si esta caent)
if btn(keys.left) then if input_buf.left or btn(keys.left) then
if not es_paret(pepe.x-1, pepe.y) and pepe.estat ~= CAENT then if not es_paret(pepe.x-1, pepe.y) and pepe.estat ~= CAENT then
pepe.x = pepe.x - 1 pepe.x = pepe.x - 1
end end
elseif btn(keys.right) then elseif input_buf.right or btn(keys.right) then
if not es_paret(pepe.x+1, pepe.y) and pepe.estat ~= CAENT then if not es_paret(pepe.x+1, pepe.y) and pepe.estat ~= CAENT then
pepe.x = pepe.x + 1 pepe.x = pepe.x + 1
end end
@@ -733,6 +758,9 @@ function update_playing()
-- Nota: la transicio a game over es directa (sense fade), perque el -- Nota: la transicio a game over es directa (sense fade), perque el
-- frame d'arribada es el mateix que el de sortida + overlay. -- frame d'arribada es el mateix que el de sortida + overlay.
if not fade_actiu() then if not fade_actiu() then
-- Sample d'input cada frame (acumulat fins al proxim tic_pepe)
sample_input()
-- Animacio independent de l'escala lateral (per frame, no per tic) -- Animacio independent de l'escala lateral (per frame, no per tic)
tic_escala_creixent() tic_escala_creixent()
@@ -773,9 +801,13 @@ function update_playing()
game_tic = game_tic + 1 game_tic = game_tic + 1
if not muriguent then if not muriguent then
tic_pepe() tic_pepe()
if fade_actiu() then return end -- canvi de fase iniciat if fade_actiu() then reset_input(); return end
check_mort_per_malos() check_mort_per_malos()
end end
-- Reset incondicional del buffer al tic: si Pepe estava muriguent,
-- descartem l'input acumulat durant l'animacio (no volem que el
-- primer moviment despres del respawn siga una tecla pulsada fa 2 s).
reset_input()
if (game_tic % MALO_RATIO) == 0 then if (game_tic % MALO_RATIO) == 0 then
tic_malos() tic_malos()
if not muriguent then check_mort_per_malos() end if not muriguent then check_mort_per_malos() end