-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
87 lines (65 loc) · 2.07 KB
/
renderer.js
File metadata and controls
87 lines (65 loc) · 2.07 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const BOARD_OFFSET_X = 16
const BOARD_OFFSET_Y = 16
class Renderer {
constructor (options) {
const canvas = document.getElementById(options.canvasId);
this.ctx = canvas.getContext('2d');
this.sprites = options.sprites;
this.state = options.state;
this.onReady = options.onReady;
this.prepare();
}
prepare () {
this.bgCanvas = document.createElement('canvas')
this.bgCanvas.width = 192
this.bgCanvas.height = 192
this.bgCtx = this.bgCanvas.getContext('2d');
this.drawBg();
this.bgImage = this.bgCtx.getImageData(0, 0, 192, 192)
this.onReady(this)
}
drawBg () {
this.bgCtx.fillStyle = '#eee';
this.bgCtx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
for(let y = 0; y < CANVAS_HEIGHT / SPRITE_SIZE; y++) {
for(let x = 0; x < CANVAS_WIDTH / SPRITE_SIZE; x++) {
this.spr(this.bgCtx, 10, 0, x * SPRITE_SIZE, y * SPRITE_SIZE)
}
}
for(let y = 0; y < BOARD_HEIGHT; y++) {
for(let x = 0; x < BOARD_WIDTH; x++) {
this.spr(this.bgCtx, 4, 0, x * SPRITE_SIZE + BOARD_OFFSET_X, y * SPRITE_SIZE + BOARD_OFFSET_Y)
}
}
}
spr (ctx, spriteX, spriteY, x, y) {
const spriteImage = this.sprites.getSpriteImageData(spriteX, spriteY);
ctx.putImageData(spriteImage, x, y);
}
render () {
this.ctx.putImageData(this.bgImage, 0, 0);
this.drawMinos();
}
drawMino (mino) {
mino.blocks.forEach((block) => {
let sX, sY
if(this.state.board.rowsToClear.includes(block.y) && (this.state.board.stateFrame) % 2 === 0) {
console.log("block in row to clear");
sX = 2
sY = 0
} else {
sX = spriteMap[block.col][0]
sY = spriteMap[block.col][1]
}
this.spr(this.ctx,
sX ,sY,
block.entity.x * SPRITE_SIZE + BOARD_OFFSET_X, block.entity.y * SPRITE_SIZE + BOARD_OFFSET_Y);
});
}
drawMinos () {
this.state.board.minos.forEach(this.drawMino.bind(this));
if(this.state.board.newMino) {
this.drawMino(this.state.board.newMino)
}
}
}