-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.js
35 lines (35 loc) · 1.04 KB
/
sprite.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
function sprite (options) {
var gravity = 1.4;
var that = {};
that.context = options.context;
that.width = options.width;
that.height = options.height;
that.x = options.x;
that.y = options.y;
that.velocity = 0;
that.color = "rgb(" + options.red + "," + options.green + "," + options.blue + ")";
console.log(that.color);
that.update = function (delta, i, blocks) {
var column = Math.floor(that.x / that.width);
var hasNoBlock = blocks.hasNoBlock(column);
var currentBottomY = that.y + that.height;
if(blocks.falling(column, currentBottomY)) {
that.velocity += gravity * delta;
that.y += that.velocity;
}
else {
if(hasNoBlock) {
that.y = canvas.height - that.height;
}
else {
that.y = blocks.firstBlock(column).y - that.height;
}
blocks.landed(column, i);
}
};
that.render = function () {
that.context.fillStyle = that.color;
that.context.fillRect(that.x, that.y, that.width, that.height);
};
return that;
}