-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCue.js
66 lines (56 loc) · 1.85 KB
/
Cue.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
"use strict";
function Cue(sprite, position, origin) {
this.sprite = sprite;
this.position = position;
this.origin = origin;
this.rotation = 0;
this.old_rotation = 0;
this.old_origin = origin.copy();
this.visible = true;
}
Cue.prototype.aim = function (power) {
this.origin.x = this.old_origin.x + power*2;
}
Cue.prototype.shoot = function (power) {
if(!this.visible)
return;
this.origin = new Vector2(this.old_origin.copy().x, this.old_origin.copy().y);
if(power<=2) {
return;
}
Game.gameWorld.cueball.shoot(power, this.rotation);
var temp_cue = this;
//this.visible = false;
setTimeout(function(){temp_cue.visible = false;}, 300);
}
Cue.prototype.calculate_rotation = function(input_initial, input_current) {
var initial_angle = Math.atan2(input_initial.x - this.position.x, input_initial.y - this.position.y);
var angle = Math.atan2(input_current.x - this.position.x, input_current.y - this.position.y);
return (angle - initial_angle);
}
Cue.prototype.handleInput = function (cueball_position) {
if(Touch.isTouchDevice) {
if(Touch.isTouching) {
var touch_position = Touch.getPosition(0);
var touch_initial_position = Touch.getInitialPosition(0);
if(Table.contains(touch_initial_position)) {
this.rotation = this.old_rotation + Mouse.inverse*this.calculate_rotation(touch_initial_position, touch_position);
}
} else if(!Touch.isTouching) {
this.old_rotation = this.rotation;
}
} else {
if(Mouse.left.down && Table.contains(Mouse.initial)) {
this.rotation = this.old_rotation + Mouse.inverse*this.calculate_rotation(Mouse.initial, Mouse.position);
} else if(!Mouse.left.down) {
this.old_rotation = this.rotation;
}
}
};
Cue.prototype.update = function () {
};
Cue.prototype.draw = function () {
if(!this.visible)
return;
Canvas2D.drawImage(this.sprite, this.position, this.rotation, 1, this.origin);
};