게임 세부 설정
Game 클래스의 프로토타입 메서드에서 게임 세부 정보를 설정합니다.
// Prototype method of the Game class, controlling the specific game logic, make sure to reset the 'this' reference
Game.prototype = {
processClick: function (w, h) {
this.gb.processClick(w, h);
this.updateCounts();
if (this.gb.isGameWin()) {
this.gameEnd();
}
},
// Start the game
beginGame: function () {
this.setupLevel();
},
// Game end
gameEnd: function () {
this.level++;
this.resetGame();
},
// Reset the game, redirect 'this' using bind
resetGame: function () {
$("#levelDescriptor").html("Enter Level " + this.level);
setTimeout(
function () {
this.setupLevel(); // When 'this' is not reset, it refers to the window object
}.bind(this),
500
); // Use bind to redirect 'this' from window to the instance
},
// Set the difficulty level
setupLevel: function () {
this.gb = new GameBoard(this.level, this.level);
$(".board").html(""); // Clear game board
this.gb.populate(); // Reset all tiles to orange color
this.gb.renderBoard(); // Render game board and create tiles
this.sh.setGridSize(this.level); // Control tile size in game area
this.updateCounts(); // Update current level display
this.applyBindings(); // Flip the colors of tiles around the clicked tile
},
// Update current level display
updateCounts: function () {
$(".currLevel").html("Current Level: <b>" + this.level + "</b>");
},
applyBindings: function () {
var that = this; // Save 'this' as a variable before the DOM event callback for easy reference
$(".gamesquare").click(function () {
// Get the position of the clicked tile
var cname = $(this).attr("class").split(" ")[1];
var coord = cname.substring(5).split("q");
var height = parseInt(coord[1]);
var width = parseInt(coord[0]);
that.processClick(width, height);
});
},
onNewGameClick: function () {
this.level = 1;
this.setupLevel();
}
};
이 코드는 프로토타입 메서드를 추가하여 Game 생성자의 기능을 확장합니다. 이러한 메서드는 주요 게임 로직과 상호 작용을 정의합니다.