-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.js
More file actions
56 lines (56 loc) · 1.52 KB
/
Game.js
File metadata and controls
56 lines (56 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Game {
constructor(ctx) {
this.ctx = ctx;
this.board = new Board(ctx);
this.cell_size = 100;
this.preview_piece_size = 100;
}
initialize() {
this.setupPiecePreview();
this.drawPiecePreview();
this.board.draw();
var startingPiece = this.getStartingPiece();
this.board.placeStartPiece(startingPiece);
}
setupPiecePreview() {
this.piece_preview = [];
for (var i = 0; i < 5; i++) {
this.piece_preview.push(this.getRandomPiece());
}
}
getRandomPiece() {
var piece_def = piece_defs[getRandomInt(piece_defs.length)];
return new Piece(this.ctx, this.board, this.cell_size, piece_def);
}
drawPiecePreview() {
var x = 0;
var y = 80;
var preview_piece_size = this.preview_piece_size;
this.piece_preview.forEach(function(preview_piece) {
preview_piece.draw(x, y);
y += preview_piece_size;
});
}
getNextPiece() {
var piece = this.piece_preview.pop();
this.piece_preview.unshift(this.getRandomPiece());
this.drawPiecePreview();
return piece;
}
getStartingPiece() {
var starting_piece_def = start_piece_defs[getRandomInt(start_piece_defs.length)];
return new Piece(this.ctx, this.board, this.cell_size, starting_piece_def);
}
click(x, y) {
console.log('clicked ' + x + ', ' + y);
if (!this.board.isWithinBounds(x, y)) {
console.log('not on the board');
return;
}
var piece = this.getNextPiece();
this.board.placeGamePiece(piece, x, y);
}
start() {
this.board.startFilling();
}
}