-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.js
47 lines (46 loc) · 1.57 KB
/
blocks.js
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
function blocks (options) {
var that = {};
that.canvas = canvas;
that.movingBlocks = new Array();
that.landedBlocks = new Array();
that.render = function (delta) {
for (var i=0; i<that.movingBlocks.length; i++) {
that.movingBlocks[i].update(delta, i, that);
that.movingBlocks[i].render();
}
for (var i=0; i<that.landedBlocks.length; i++) {
if(that.landedBlocks[i] != undefined) {
for (var j=0; j<that.landedBlocks[i].length; j++) {
that.landedBlocks[i][j].render();
}
}
}
};
that.addBlock = function (sprite) {
that.movingBlocks.push(sprite);
};
that.landed = function (column, movingIndex) {
if (that.hasNoBlock(column)) {
that.landedBlocks[column] = new Array();
}
that.landedBlocks[column].unshift(that.movingBlocks.splice(movingIndex, 1).pop());
};
that.hasNoBlock = function (column) {
return that.landedBlocks[column] == undefined || that.landedBlocks[column][0] == undefined;
};
that.falling = function (column, currentBottomY) {
var hasNoBlock = that.hasNoBlock(column);
return !hasNoBlock && currentBottomY < that.landedBlocks[column][0].y || hasNoBlock && currentBottomY < that.canvas.height
};
that.firstBlock = function (column) {
return that.landedBlocks[column][0];
};
that.renderWithOffset = function (delta, offsetX) {
var context = that.canvas.getContext("2d");
context.save();
context.translate(offsetX, 0);
that.render(delta);
context.restore();
}
return that;
}