-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stick.js
46 lines (34 loc) · 1.02 KB
/
Stick.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
const STICK_ORIGIN = new Vector2(970, 11);
const STICK_SHOT_ORIGIN = new Vector2(950, 11);
function Stick(position, onShoot) {
this.position = position;
this.rotation = 0;
this.origin = STICK_ORIGIN.copy();
this.power = 0;
this.onShoot = onShoot;
}
Stick.prototype.update = function() {
if (Mouse.left.down) {
this.increasePower();
} else if (this.power > 0) {
this.onShoot();
}
this.updateRotation();
};
Stick.prototype.draw = function() {
Canvas.drawIamge(sprites.stick, this.position, this.origin, this.rotation);
};
Stick.prototype.updateRotation = function() {
let opposite = Mouse.position.y - this.position.y;
let adjacent = Mouse.position.x - this.position.x;
this.rotation = Math.atan2(opposite, adjacent);
};
Stick.prototype.increasePower = function() {
this.power += 100;
this.origin.x += 5;
};
Stick.prototype.shoot = function() {
this.onShoot(this.power, this.rotation);
this.power = 0;
this.origin = STICK_SHOT_ORIGIN.copy();
};