Files
decoda/app/src/atividades/programacao/exemplo2/game.js
2026-06-24 21:00:19 -03:00

118 lines
3.0 KiB
JavaScript

import Phaser from "phaser";
import { BaseGameScene } from "../../../shared/BaseGameScene.js";
import { setupExemplo2API } from "./hooks/setupExemplo2API.js";
import { validationSolution } from "./validation/validators.js";
import { gameConfig } from "./config/config.js";
import { Constantes } from "./ui/constants.js";
import { montarDisplay } from "./ui/layout.js";
export class Exemplo2Scene extends BaseGameScene {
constructor() {
super("Exemplo2Scene");
this.textoAtual = "";
this.textoDisplay = null;
}
preload() {
this.preloadGlobalAssets();
}
create() {
this.validatorFunc = (historico) =>
validationSolution(historico, this.configFase, gameConfig, this);
// Registra a API do interpreter e os handlers de run/reset via BaseGameScene
this.setupStandardController(
() => setupExemplo2API(this),
this.validatorFunc
);
this.montarFase();
}
onBeforeRun() {
this.isRunning = true;
this.historico = [];
this.textoAtual = "";
this.textoDisplay.setText("");
this.textoDisplay.setColor("#e0e0ff");
}
onReset() {
this.isRunning = false;
this.textoAtual = "";
this.textoDisplay.setText("");
this.textoDisplay.setColor("#e0e0ff");
}
async onSuccess() {
this.isRunning = false;
this.textoDisplay.setColor("#69f0ae");
return new Promise((resolve) => {
this.tweens.add({
targets: this.textoDisplay,
scaleX: 1.1,
scaleY: 1.1,
duration: 150,
yoyo: true,
repeat: 2,
onComplete: () => {
this.textoDisplay.setColor("#e0e0ff");
resolve();
},
});
});
}
async onFailure() {
this.isRunning = false;
this.textoDisplay.setColor("#ff4444");
return new Promise((resolve) => {
this.time.delayedCall(500, () => {
this.textoDisplay.setColor("#e0e0ff");
resolve();
});
});
}
montarFase() {
if (this.textoDisplay) this.textoDisplay.destroy();
this.textoAtual = "";
// montarDisplay cria os objetos visuais e retorna a referência ao texto de saída
this.textoDisplay = montarDisplay(this);
}
// --- API exposta ao js-interpreter via setupExemplo2API ---
imprimir(texto) {
this.textoAtual = texto;
this.textoDisplay.setText(texto);
this.historico.push({ tipo: "imprimir", texto });
}
}
/**
* Factory para criar a configuração Phaser do jogo Exemplo2.
* Injeta configFase e gameConfig no registry antes do boot da cena.
*/
export const createGame = (elementoPai, configFaseAtual) => {
const scene = new Exemplo2Scene();
const { LARGURA, ALTURA } = Constantes;
return {
type: Phaser.AUTO,
width: LARGURA,
height: ALTURA,
backgroundColor: "#0a0a1a",
parent: elementoPai,
scale: { mode: Phaser.Scale.FIT, autoCenter: Phaser.Scale.CENTER_BOTH },
scene,
callbacks: {
preBoot: (game) => {
game.registry.set("configFase", configFaseAtual);
game.registry.set("gameConfig", gameConfig);
},
},
};
};