diff --git a/guante_blanco.lua b/guante_blanco.lua index 12adb10..8f7faaf 100644 --- a/guante_blanco.lua +++ b/guante_blanco.lua @@ -48,6 +48,19 @@ MS_POR_JIFFY = 20 -- alterna 1,2 y solo se mueve cuando == 2 → 1 paso de perro cada 2 del jugador). PERRO_RATIO = 2 +-- Duraciones de transición (ms) +MS_ESCAPE = 2500 +MS_MORDIDO = 1500 +MS_PREGUNTA_MIN = 600 -- antes de aceptar S/N + +-- Estados de la máquina global +ESTADO_PASE = "pase" +ESTADO_JUEGO = "juego" +ESTADO_ESCAPE = "escape" +ESTADO_MORDIDO = "mordido" +ESTADO_PREGUNTA = "pregunta" +ESTADO_FIN = "fin" + -- ============================================================ -- PALETA (mapeo CPC firmware → CGA disponible) -- Original: INK 0,0 (negro) / 1,26 (pastel) / 2,15 (blanco) / @@ -234,6 +247,10 @@ robado = 0 -- joyas recogidas por el ladrón mensaje_msg = "" -- mensaje de la fila inferior (Choque, etc.) retardo = RETARDO_INI +-- Máquina de estados +estado = ESTADO_PASE +estado_t0_ms = 0 + -- Perro guardián perro = { activo = false, -- perro==1 del original @@ -702,6 +719,156 @@ function mover_jugador(xf, yf) tick_perro_post_jugador() end +-- ============================================================ +-- MÁQUINA DE ESTADOS +-- ============================================================ +function set_estado(s) + estado = s + estado_t0_ms = time() +end + +function tiempo_estado_ms() return time() - estado_t0_ms end + +-- Reset completo del estado de partida (equivale al RUN del original). +function reset_partida() + init_mapa() + generar_objetos() + retardo = RETARDO_INI + mensaje_msg = "" + rm = 1 + xp = 6 + yp = 4 + escapado = false + muerto = false + hombre_glifo = GL_LADRON + perro.activo = false + perro.pendiente = false + perro.paso = 0 +end + +-- ----- PANTALLA PASE (líneas 720-960 del original) ----- +function update_pase() + pintar_fondo() + color(COL_LADRON, COL_FONDO) + print("- P A S E -", 11, 1) + color(COL_TEXTO, COL_FONDO) + print("Puertas y ventanas para escapar", 1, 3) + + -- Leyenda de simbolos (líneas 840-930 del original) + color(COL_LADRON, COL_FONDO) + print(chr(GL_LADRON).." Usted, el ladron", 2, 6) + color(COL_PUERTA, COL_FONDO) + print(chr(GL_PUERTA_H)..chr(GL_PUERTA_V).." Puertas", 2, 8) + color(COL_CONMUT, COL_FONDO) + print(chr(GL_CONM_L_OFF)..chr(GL_CONM_R_OFF).." Luces apagadas", 2, 10) + print(chr(GL_CONM_L_ON) ..chr(GL_CONM_R_ON) .." Luces encendidas", 2, 11) + color(COL_VENTANA, COL_FONDO) + print(chr(GL_VENT_H)..chr(GL_VENT_V).." Ventanas", 2, 13) + color(COL_JOYA, COL_FONDO) + print(chr(GL_JOYA).." Piedras preciosas", 2, 15) + color(COL_OBSTACULO, COL_FONDO) + print(chr(GL_OBSTACULO).." Obstaculos", 2, 17) + color(COL_PERRO, COL_FONDO) + print(chr(GL_PERRO).." El perro", 2, 19) + + if (cnt() // 30) % 2 == 0 then + color(COL_LADRON, COL_FONDO) + print("Pulsa ESPACIO para empezar", 3, 22) + end + + if btnp(KEY_SPACE) then + reset_partida() + set_estado(ESTADO_JUEGO) + end +end + +-- ----- JUEGO ----- +function update_juego() + tick_perro_pendiente() + + if (cnt() - ultimo_tic) >= TICS_JUGADOR then + local xf, yf = 0, 0 + if btn(KEY_UP) then yf = -1 + elseif btn(KEY_DOWN) then yf = 1 end + if btn(KEY_LEFT) then xf = -1 + elseif btn(KEY_RIGHT) then xf = 1 end + if xf ~= 0 or yf ~= 0 then + mover_jugador(xf, yf) + ultimo_tic = cnt() + end + end + + pintar_fondo() + pintar_banner() + pintar_habitacion() + pintar_marcador() + pintar_msg() + + if escapado then set_estado(ESTADO_ESCAPE) return end + if muerto then set_estado(ESTADO_MORDIDO) return end +end + +-- ----- ESCAPE (líneas 1910-1970 del original) ----- +function update_escape() + pintar_fondo() + color(COL_LADRON, COL_FONDO) + print("Usted ha escapado", 2, 3) + color(COL_TEXTO, COL_FONDO) + print("con", 7, 5) + if robado == joyas_total then + print("todas las", 7, 7) + end + color(COL_JOYA, COL_FONDO) + print(string.format("%2d", robado), 7, 7 + ((robado == joyas_total) and 2 or 0)) + color(COL_LADRON, COL_FONDO) + print("joyas", 7, 9 + ((robado == joyas_total) and 2 or 0)) + + if tiempo_estado_ms() >= MS_ESCAPE then + set_estado(ESTADO_PREGUNTA) + end +end + +-- ----- MORDIDO (línea 2660 del original) ----- +function update_mordido() + -- Sigue mostrando el último frame del juego con "MORDIDO" encima + pintar_fondo() + pintar_banner() + pintar_habitacion() + pintar_marcador() + + color(COL_MSG, COL_FONDO) + print(" MORDIDO ", 12, FILA_MSG) + + if tiempo_estado_ms() >= MS_MORDIDO then + set_estado(ESTADO_PREGUNTA) + end +end + +-- ----- PREGUNTA (líneas 240-300 del original) ----- +function update_pregunta() + pintar_fondo() + color(COL_LADRON, COL_FONDO) + print("Quiere jugar", 4, 3) + print("otra vez?", 5, 5) + color(COL_JOYA, COL_FONDO) + print("S/N", 7, 7) + + if tiempo_estado_ms() < MS_PREGUNTA_MIN then return end + if btnp(KEY_S) then + reset_partida() + set_estado(ESTADO_JUEGO) + elseif btnp(KEY_N) then + set_estado(ESTADO_FIN) + end +end + +-- ----- FIN ----- +function update_fin() + pintar_fondo() + color(COL_LADRON, COL_FONDO) + print("F I N", 13, 11) +end + -- ============================================================ -- BUCLE PRINCIPAL -- ============================================================ @@ -723,36 +890,16 @@ function init() perro.activo = false perro.pendiente = false perro.paso = 0 + set_estado(ESTADO_PASE) cls() end function update() - -- Movimiento del jugador (cursores). Fase 5 manejará escapado/muerto. - if escapado or muerto then - pintar_fondo() - color(COL_TEXTO, COL_FONDO) - if escapado then print("ESCAPADO (Fase 5)", 1, 10) end - if muerto then print("MUERTO (Fase 5)", 1, 10) end - return + if estado == ESTADO_PASE then update_pase() + elseif estado == ESTADO_JUEGO then update_juego() + elseif estado == ESTADO_ESCAPE then update_escape() + elseif estado == ESTADO_MORDIDO then update_mordido() + elseif estado == ESTADO_PREGUNTA then update_pregunta() + elseif estado == ESTADO_FIN then update_fin() end - - tick_perro_pendiente() - - if (cnt() - ultimo_tic) >= TICS_JUGADOR then - local xf, yf = 0, 0 - if btn(KEY_UP) then yf = -1 - elseif btn(KEY_DOWN) then yf = 1 end - if btn(KEY_LEFT) then xf = -1 - elseif btn(KEY_RIGHT) then xf = 1 end - if xf ~= 0 or yf ~= 0 then - mover_jugador(xf, yf) - ultimo_tic = cnt() - end - end - - pintar_fondo() - pintar_banner() - pintar_habitacion() - pintar_marcador() - pintar_msg() end