-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple.js
107 lines (79 loc) · 3.37 KB
/
apple.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
var Apple = (function () {
function Apple(data) {
this.position = data.position;
this.score = 10;
this.rotation = 0;
this.bounceAngle = 0;
}
Apple.prototype.draw = function() {
mvPushMatrix();
//draw in its position, with its rotation, at the appropriate scale
mat4.translate(mvMatrix, this.position);
mat4.rotate(mvMatrix, this.rotation, [0,1,0]);
mat4.scale(mvMatrix, [0.15,0.15,0.15]);
drawApple(teapotTex);
mvPopMatrix();
}
Apple.prototype.animate = function (elapsed) {
//add a bounce and a spin
this.bounceAngle += elapsed*speed;
this.rotation += elapsed;
vec3.add(this.position, [0,Math.sin(this.bounceAngle)/5,0]);
//if snake eats apple
if (hasCollided(head.position,this.position, 2)){
//move to new place, update score, add to snake
this.position = vec3.create([27 - Math.random()*54,27 - Math.random()*54,27 - Math.random()*54]);
score += this.score;
updateScore();
addToConsole("TEAPOT GET!");
addToSnake();
speed++;
}
}
return Apple;
}());
var teapotVertexPositionBuffer;
var teapotVertexTextureCoordBuffer;
var teapotVertexIndexBuffer;
//create buffers from the teapot JSON data
function handleLoadedTeapot(teapotData) {
teapotVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(teapotData.vertexTextureCoords), gl.STATIC_DRAW);
teapotVertexTextureCoordBuffer.itemSize = 2;
teapotVertexTextureCoordBuffer.numItems = teapotData.vertexTextureCoords.length / 2;
teapotVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(teapotData.vertexPositions), gl.STATIC_DRAW);
teapotVertexPositionBuffer.itemSize = 3;
teapotVertexPositionBuffer.numItems = teapotData.vertexPositions.length / 3;
teapotVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, teapotVertexIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(teapotData.indices), gl.STATIC_DRAW);
teapotVertexIndexBuffer.itemSize = 1;
teapotVertexIndexBuffer.numItems = teapotData.indices.length;
}
//load teapot
function loadTeapot() {
var request = new XMLHttpRequest();
request.open("GET", "teapot.json");
request.onreadystatechange = function () {
if (request.readyState == 4) {
handleLoadedTeapot(JSON.parse(request.responseText));
}
}
request.send();
}
//with the given texture, draw Teapot
function drawApple(texture){
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, teapotVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexTextureCoordBuffer);
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, teapotVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, teapotVertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, teapotVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}