-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird.js
76 lines (44 loc) · 1.15 KB
/
bird.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
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
define(function(require, exports, module) {
var foo = 'bar';
var $ = '';
function Bird(img, cvs) {
var _this = this;
cvs.addEventListener('click', function(event) {
var x = event.layerX;
var y = event.layerY;
_this.fly();
});
this.img = img;
this.x = 200;
this.y = 100;
this.index = 0;
this.speed = 0;
this.a = 0.0005;
this.waitTime = 0;
}
Bird.prototype.update = function(dt) {
this.waitTime = this.waitTime + dt;
if (this.waitTime > 100) {
this.index = (this.index + 1) % 3;
this.waitTime = this.waitTime - 100;
}
this.speed = this.speed + this.a * dt;
this.y = this.y + this.speed * dt;
};
Bird.prototype.draw = function() {
ctx.save();
ctx.translate(this.x, this.y);
var speed = this.speed > 0.3 ? 0.3 : this.speed;
var angle = speed / 0.3 * 45;
ctx.rotate(angle / 180 * Math.PI);
ctx.drawImage(this.img,
52 * this.index, 0, 52, 45,
-26, -22.5, 52, 45
);
ctx.restore();
};
Bird.prototype.fly = function() {
this.speed = -0.3;
};
module.exports = Bird;
});