-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.js
381 lines (288 loc) · 10.3 KB
/
circle.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var canvas = document.getElementById('my_Canvas');
var gl = canvas.getContext('experimental-webgl');
var width = canvas.getAttribute("width"), height = canvas.getAttribute("height");
var shaders = [];
var circleColours = [];
var circleCoords = [];
var circleRadius = [];
var circleAlive = [];
var clickAnimationFrame = -1;
var clickAnimationX;
var clickAnimationY;
var clickAnimationRadius;
var maxSize = 200;
var score = 0;
var timer = 0;
//all possible colours to randomly choose from
/*var colours = [
[0,0,1,1],
[1,0,0,1],
[0,1,0,1],
[1,0,1,1]
]*/
// Fullscreen if not set
if (width) {
gl.maxWidth = width;
}
if (height) {
gl.maxHeight = height;
}
/*======= Defining and storing the geometry ======*/
// Create an empty buffer object
var vertex_buffer = gl.createBuffer();
var vertices = [];
var vertCount = 2;
for(var i=0.0; i<=360; i+=1){
var j = i * Math.PI / 180;
var vert1 = [
Math.sin(j),
Math.cos(j),
];
var vert2 = [
0,
0,
];
vertices = vertices.concat(vert1);
vertices = vertices.concat(vert2);
}
var n = vertices.length/vertCount;
// Bind appropriate array buffer to it
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Pass the vertex data to the buffer
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.DYNAMIC_DRAW);
// Unbind the buffer
gl.bindBuffer(gl.ARRAY_BUFFER, null);
/*=================== Shaders ====================*/
function createShaders(colour){
// Vertex shader source code
var vertCode =
'attribute vec3 coordinates;' +
'void main(void) {' +
' gl_Position = vec4(coordinates, 1.0);' +
'}';
// Create a vertex shader object
var vertShader = gl.createShader(gl.VERTEX_SHADER);
// Attach vertex shader source code
gl.shaderSource(vertShader, vertCode);
// Compile the vertex shader
gl.compileShader(vertShader);
// Fragment shader source code
var fragCode =
'void main(void) {' +
'gl_FragColor = vec4('+colour[0]+', '+colour[1]+', '+colour[2]+', '+colour[3]+');' +
'}';
// Create fragment shader object
var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
// Attach fragment shader source code
gl.shaderSource(fragShader, fragCode);
// Compile the fragmentt shader
gl.compileShader(fragShader);
// Create a shader program object to store
// the combined shader program
var shaderProgram = gl.createProgram();
// Attach a vertex shader
gl.attachShader(shaderProgram, vertShader);
// Attach a fragment shader
gl.attachShader(shaderProgram, fragShader);
// Link both the programs
gl.linkProgram(shaderProgram);
// Use the combined shader program object
gl.useProgram(shaderProgram);
/*======= Associating shaders to buffer objects ======*/
// Bind vertex buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Get the attribute location
var coord = gl.getAttribLocation(shaderProgram, "coordinates");
// Point an attribute to the currently bound VBO
gl.vertexAttribPointer(coord, vertCount, gl.FLOAT, false, 0, 0);
// Enable the attribute
gl.enableVertexAttribArray(coord);
return shaderProgram;
}
// Clear the canvas
gl.clearColor(0.5, 0.5, 0.5, 1);
// Enable the depth test
gl.enable(gl.DEPTH_TEST);
//Clear the color and depth buffer
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
for(var i = 0; i < 10; i++){
circleColours[i] = getRandomColour(0.2, 0.8);
circleCoords[i] = getRandomLoc();
circleRadius[i] = i * 10;
shaders[i] = createShaders(circleColours[i]);
circleAlive[i] = true;
//drawBacteria(i);
}
shaders[10] = createShaders([1, 1, 1, 1]);
drawBacteria();
drawMainCircle();
function drawMainCircle(){
gl.useProgram(shaders[10]);
// Set the view port
gl.viewport(canvas.width * 0.1, canvas.height * 0.1, canvas.width * 0.8, canvas.height * 0.8);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
}
function drawBacteriaCircle(i){
console.log("Drawing circle "+i);
gl.useProgram(shaders[i]);
gl.viewport(circleCoords[i][0]-circleRadius[i]/2,
circleCoords[i][1]-circleRadius[i]/2,
circleRadius[i],
circleRadius[i]);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
}
function drawBacteria() {
for(var i = 0; i < 10; i++){
console.log("Starting circle "+i);
drawBacteriaCircle(i);
}
animateBacteria();
}
function animateBacteria(){
//circleRadius += 0.2;
timer++;
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
for (i = 0; i < 10; i++){
//check intersection for clicking
var dx = clickAnimationX - circleCoords[i][0];
var dy = (height - clickAnimationY) - circleCoords[i][2];
if (circleAlive[i] && ((dx * dx) + (dy * dy) <= (circleRadius[i] + clickAnimationRadius) * (circleRadius[i] + clickAnimationRadius) / 4))
{
console.log("Poison intersecting with circle "+i);
circleAlive[i] = false;
if (timer < 1000){
score += 1000 - timer;
console.log(1000 - timer +" points added from poison collision. New score is: "+score);
}
}
}
for(var i = 0; i < 10; i++){
if (circleAlive[i] == true){
if (circleRadius[i] < maxSize) circleRadius[i] += 0.2;
//circleRadius[i] += 0.2;
gl.useProgram(shaders[i]);
gl.viewport(
circleCoords[i][0]-circleRadius[i]/2,
circleCoords[i][1]- circleRadius[i]/2,
circleRadius[i],
circleRadius[i]);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
}
}
for(var i = 0; i < 9; i++)
{
if (circleAlive[i])
for(var j = i+1; j < 10; j++)
{
if(circleAlive[j]&&checkIntersection(i, j, circleRadius[i], circleRadius[j]))
{
console.log("Circle "+(j)+" and Circle "+i+" are intersecting");
if (circleRadius[i] >= circleRadius[j]){
circleAlive[j] = false;
//circleRadius[i] += circleRadius[j]/5;
circleRadius[i] = Math.sqrt(circleRadius[i] * circleRadius[i] + circleRadius[j] * circleRadius[j]);
console.log("Circle "+i+" consumed circle "+j);
}
else{
circleAlive[i] = false;
//circleRadius[j] += circleRadius[i]/5;
circleRadius[j] = Math.sqrt(circleRadius[i] * circleRadius[i] + circleRadius[j] * circleRadius[j]);
console.log("Circle "+j+" consumed circle "+i);
}
}
}
}
if (clickAnimationFrame >= 0){
clickAnimationRadius = clickAnimationFrame
gl.useProgram(createShaders([0.2, 1, 0.2, 1]));
gl.viewport(
clickAnimationX - clickAnimationRadius/2,
clickAnimationY - clickAnimationRadius/2,
clickAnimationRadius,
clickAnimationRadius)
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
clickAnimationFrame++;
}
drawMainCircle();
if (isGameOver() == false)
requestAnimationFrame(animateBacteria);
}
function isGameOver(){
var gameOver = false;
var anyAlive = false;
var tooBigCount = 0;
for (var i = 0; i < 10; i++){
if (circleAlive[i] == true){
anyAlive = true;
}
if (circleRadius[i] >= 200){
tooBigCount ++;
console.log("Circle "+i+" is too big.");
}
}
if (anyAlive == false){
console.log("You win! Final score is: "+score);
gameOver = true;
}
if (tooBigCount >= 2){
console.log("You lose... Final score is: "+score);
gameOver = true;
}
return gameOver;
}
function getRandomLoc(){
var rand = Math.floor(20 * Math.random()) / 20.0;
var angle = (rand*Math.PI*2);
var coord = [3];
coord[0] = canvas.width / 2 + canvas.width * 0.8/2 * Math.cos(-angle);
console.log(coord[0]);
coord[1] = canvas.height / 2 + canvas.height * 0.8/2 * Math.sin(-angle);
coord[2] = (canvas.height / 2 + canvas.height * 0.8/2 * Math.sin(angle));
return coord;
}
function getRandomColour(min, max)
{
colour = [min, max, Math.random() * (max - min) + min, 1];
pos = Math.floor(Math.random() * 3);
p = colour[0];
colour[0] = colour[pos];
colour[pos] = p;
pos = Math.floor(Math.random() * 2 + 1);
p = colour[1];
colour[1] = colour[pos];
colour[pos] = p;
console.log(colour);
return colour;
}
function checkIntersection(firstCircle, secondCircle, firstRadius, secondRadius)
{
var dx = circleCoords[firstCircle][0] - circleCoords[secondCircle][0];
var dy = circleCoords[firstCircle][2] - circleCoords[secondCircle][2];
//if(Math.abs(dx) <= (firstRadius + secondRadius)/2 && Math.abs(dy) <= (firstRadius + secondRadius)/2)
return ((dx * dx) + (dy * dy) <= (firstRadius + secondRadius) * (firstRadius + secondRadius)/4)
}
var canvasLeft = canvas.offsetLeft, canvasTop = canvas.offsetTop;
canvas.addEventListener('click', function(event){
var x = event.pageX - canvas.getBoundingClientRect().left;
var y = event.pageY - canvas.getBoundingClientRect().top;
clickAnimationFrame = 0;
clickAnimationX = x;
//clickAnimationY = Math.abs(canvas.getBoundingClientRect().bottom - y);
clickAnimationY = height-y;
for(var i = 0; i < 10; i++)
{
//x-circlex
var dx = x - circleCoords[i][0];
//y-circley
var dy = y - circleCoords[i][2];
var d = Math.sqrt(dx*dx + dy*dy);
if(d <= circleRadius[i]/2 && circleAlive[i] == true && circleRadius[i]<maxSize)
{
circleAlive[i] = false;
if (timer < 1000){
score += 1000 - timer;
console.log(1000 - timer +" points added from click detection. New score is: "+score);
}
}
}
});