-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
150 lines (130 loc) · 3.04 KB
/
game.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var cue = new Cue(100,100);
var context;
var mx;
var my;
var offset = $('canvas').offset();
var shooting = false;
var maxCuePull = 125;
var cueSpeedMultiplier = 0.08;
var width = 500;
var height = 500;
var pits = new Array();
pits[0] = new Pit(75,75)
pits[1] = new Pit(width-75, 75)
pits[2] = new Pit(75, height-75)
pits[3] = new Pit(width-75, height-75)
var balls = new Array();
for (i = 0; i < 10; i++){
var ballX;
var ballY;
var overlap;
do{
overlap = false;
ballX = 50+Math.ceil(Math.random()*400);
ballY = 50+Math.ceil(Math.random()*400);
$.each(balls, function(){
if (dist(this.x, this.y, ballX, ballY) < 40){
overlap = true;
}
});
if (dist(cue.x, cue.y, ballX, ballY) < 40){
overlap = true;
}
}while(overlap==true);
balls[i] = new Ball(ballX, ballY);
}
// balls getting stuck on walls
function init(){
context = myCanvas.getContext('2d');
setInterval(tick,10);
}
function tick(){
draw();
cue.move();
$.each(balls, function(){
var ball = this;
ball.move();
checkCollision(cue, ball);
$.each(pits, function(){
if (pitDeath(this, ball)) {
console.log('pit');
var i = balls.indexOf(ball);
if(i != -1) balls.splice(i, 1);
};
});
$.each(balls, function(){
if (this != ball) checkCollision(this, ball);
});
});
$.each(pits, function(){
if (pitDeath(this, cue)) {
console.log('pit');
cue = null;
};
});
}
function draw(){
context.clearRect(0,0, width,height);
$.each(pits, function(){
pit = this
context.beginPath();
context.fillStyle="#000";
context.arc(pit.x,pit.y,pit.radius,0,Math.PI*2,true);
context.closePath();
context.fill();
});
if (shooting) {
context.strokeStyle = '#f00';
context.lineWidth = 10;
context.beginPath();
context.moveTo(cue.x, cue.y);
context.lineTo(mx, my);
context.stroke();
context.closePath();
}
$.each(balls, function(){
ball = this
context.beginPath();
context.fillStyle="#00ff00";
context.arc(ball.x,ball.y,20,0,Math.PI*2,true);
context.closePath();
context.fill();
});
context.beginPath();
context.fillStyle="#0000ff";
context.arc(cue.x,cue.y,20,0,Math.PI*2,true);
context.closePath();
context.fill();
}
function dist(x, y, X, Y){
x_diff = Math.pow( (x-X), 2 );
y_diff = Math.pow( (y-Y), 2 );
dist1 = Math.sqrt( x_diff + y_diff );
if (dist1<0) {dist1 = -dist1;}
return dist1;
}
$('canvas').mousedown(function(e){
mx = e.pageX - offset.left
my = e.pageY - offset.top
if (dist(mx,my, cue.x, cue.y) < 20){
shooting = true;
}
});
$('canvas').mouseup(function(e){
if (shooting){
mx = e.pageX - offset.left
my = e.pageY - offset.top
calculateCuePull();
var dx = cue.x-mx;
var dy = cue.y-my;
cue.vx = (dx*cueSpeedMultiplier);
cue.vy = (dy*cueSpeedMultiplier);
}
shooting=false;
});
$('canvas').mousemove(function(e){
mx = e.pageX - offset.left - 10
my = e.pageY - offset.top - 10
calculateCuePull();
});
window.onload += init();