-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStick.js
More file actions
61 lines (46 loc) · 1.28 KB
/
Stick.js
File metadata and controls
61 lines (46 loc) · 1.28 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
function Stick(position, onShoot){
this.position = position;
this.rotation = 0;
this.origin = CONSTANTS.stickOrigin.copy();
this.power = 0;
this.onShoot = onShoot;
this.shot = false;
}
Stick.prototype.update = function(){
if(this.shot){
return;
}
if(Mouse.left.down){
this.increasePower();
}
else if(this.power > 0){
this.shoot();
}
this.updateRotation();
}
Stick.prototype.draw = function(){
Canvas.drawImage(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(){
if(this.power > CONSTANTS.maxShotPower){
return;
}
this.power += CONSTANTS.powerInterval;
this.origin.x += CONSTANTS.originXInterval;
}
Stick.prototype.shoot = function(){
this.onShoot(this.power,this.rotation);
this.power = 0;
this.origin = CONSTANTS.stickShotOrigin.copy();
this.shot = true;
}
Stick.prototype.reposition = function(position){
this.position = position.copy();
this.origin = CONSTANTS.stickOrigin.copy();
this.shot = false;
}