76 lines
3.3 KiB
JavaScript
76 lines
3.3 KiB
JavaScript
window.addEventListener("load", async function (){
|
|
const GAME_CONFIG ={
|
|
mainGrid:{width:10,height:20},
|
|
previewGrid:{width:6,height:6},
|
|
cellSize:30,
|
|
colors:["#CCCCCC", "red", "blue", "green", "yellow", "orange", "purple", "cyan", "#FAEBD7"],
|
|
}
|
|
const canvas = document.querySelector("#game-board");
|
|
const nextCanvas = document.querySelector("#next-piece-board");
|
|
const uiLvl = document.querySelector("#level");
|
|
const uiLines = document.querySelector("#lines");
|
|
const uiScore = document.querySelector("#score");
|
|
const startButton = document.querySelector("#start-button");
|
|
const overlay = document.querySelector("#game-overlay");
|
|
const eventBus = new EventBus();
|
|
const render = new Renderer(canvas, nextCanvas, eventBus, GAME_CONFIG.cellSize, GAME_CONFIG.colors);
|
|
new UIManager(eventBus, overlay, startButton, uiLvl, uiLines, uiScore);
|
|
new AnimationManager(eventBus);
|
|
new InputHandler(eventBus);
|
|
|
|
////////////////////////////////////////////
|
|
const joystickUp = document.querySelector("#joystick-up");
|
|
const joystickDown = document.querySelector("#joystick-down");
|
|
const joystickLeft = document.querySelector("#joystick-left");
|
|
const joystickRight = document.querySelector("#joystick-right");
|
|
|
|
joystickUp.addEventListener("click", () => eventBus.emit("rotate", {}));
|
|
joystickDown.addEventListener("click", () => eventBus.emit("move", { dx: 0, dy: 1 }));
|
|
joystickLeft.addEventListener("click", () => eventBus.emit("move", { dx: -1, dy: 0 }));
|
|
joystickRight.addEventListener("click", () => eventBus.emit("move", { dx: 1, dy: 0 }));
|
|
// ------------------------------------
|
|
|
|
////////////////////////////////////////////
|
|
let game = null;
|
|
let isAnimating = false;
|
|
function gameInit(){
|
|
const grid = new Grid(GAME_CONFIG.mainGrid.width, GAME_CONFIG.mainGrid.height);
|
|
const previewGrid = new Grid(GAME_CONFIG.previewGrid.width, GAME_CONFIG.previewGrid.height);
|
|
const pieceFactory = new PieceFactory();
|
|
const game = new Game(eventBus, grid, previewGrid, pieceFactory);
|
|
eventBus.emit("render", game.getRenderState());
|
|
return game;
|
|
}
|
|
function gameStart(){
|
|
if(isAnimating) return;
|
|
//Я подпишусь на эту функцию в EventBus. Событие будет вызываться при клавише пробел
|
|
if (game) {
|
|
window.cancelAnimationFrame(game.loopRequestID);
|
|
game.unSetupInputListeners();
|
|
game.unsubscribleStartGameplay();
|
|
game.unsubscribleStopGameplay();
|
|
//game.unSetupInputListeners() // <-- ГЛАВНОЕ: УДАЛЕНИЕ СЛУШАТЕЛЕЙ СТАРОЙ ИГРЫ
|
|
|
|
}
|
|
game = gameInit()
|
|
game.start();
|
|
|
|
}
|
|
function gameEnd(){
|
|
if (game) {
|
|
game.unSetupInputListeners();
|
|
game.unsubscribleStartGameplay();
|
|
game.unsubscribleStopGameplay();
|
|
window.cancelAnimationFrame(game.loopRequestID);
|
|
eventBus.emit("showGameOver", game.uiState())
|
|
}
|
|
}
|
|
eventBus.on("set-is-animating", ({state})=>{
|
|
isAnimating = state;
|
|
});
|
|
eventBus.on("gameEnd", gameEnd);
|
|
eventBus.on("gameStart", gameStart);
|
|
game = gameInit();
|
|
})
|
|
|