-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (179 loc) · 4.81 KB
/
index.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
var gl;
var speed = 10;
var direction = vec3.create([0, 1, 0]);
//initialise
function webGLStart() {
var canvas = document.getElementById("canvas");
initGL(canvas);
initShaders()
initBuffers();
initTexture();
loadMaze();
loadTeapot();
initGameObjects();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
canvas.onmousedown = handleMouseDown;
document.onmouseup = handleMouseUp;
document.onmousemove = handleMouseMove;
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;
tick();
}
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
var head, skybox, apple, powerUp, maze, gameOver, camera, paused, score, highScore, initSnakeBits, degree;
var gameObjects = [];
var snake = [];
//creates gameObjects, adds them to gameObjects Array
function initGameObjects(){
initSnakeBits = 4;
degree = 0;
score = 0;
highScore = 0;
updateScore();
camera = cameraType.FIXED;
gameOver = false;
paused = false;
maze = new Maze({
size : vec3.create([5,5,5])
});
gameObjects.push(maze);
skybox = new Skybox({
size : vec3.create([30,30,30])
})
gameObjects.push(skybox);
powerUp = new PowerUp({
position : vec3.create([28 - Math.random()*56,28 - Math.random()*56,0])
});
gameObjects.push(powerUp);
for (var t in powerUpType){
console.log(t);
}
head = new Head({
position : vec3.create(),
direction : direction
});
snake.push(head);
apple = new Apple({
position : vec3.create([28 - Math.random()*56,28 - Math.random()*56,28 - Math.random()*56])
});
gameObjects.push(apple);
snake.push(new SnakeBit({
position: vec3.create([0,-5,0]),
front: head
}));
for (var i = 1; i<=initSnakeBits; i++){
addToSnake();
}
}
var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();
function mvPushMatrix() {
var copy = mat4.create();
mat4.set(mvMatrix, copy);
mvMatrixStack.push(copy);
}
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
//using the cube buffer, draw a cube with the given texture
function drawCube(texture){
//Setup texture and shaders
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}
//Draw on the screen
function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//dont draw if maze not loaded
if (mazeVertexTextureCoordBuffer == null || mazeVertexPositionBuffer == null) {
return;
}
//move camera to correct position
chooseCamera(camera);
//draw snake and gameObjects
for (var i in snake){
snake[i].draw();
}
for (var i in gameObjects) {
gameObjects[i].draw();
}
}
var lastTime = 0;
//moves gmaeObjects to the right position
function animate() {
var timeNow = new Date().getTime();
if (lastTime != 0) {
var elapsed = (timeNow - lastTime)/1000;
for (var i in snake){
snake[i].animate(elapsed);
}
for (var i in gameObjects) {
gameObjects[i].animate(elapsed);
}
}
lastTime = timeNow;
}
//called every time screen wants us to draw
function tick() {
//if game isn't over, animate and draw
if(!gameOver){
requestAnimationFrame(tick);
handleKeys();
//if game is paused, don't animate
if (!paused){
animate();
}
drawScene();
//if game over
}else{
//update score
highScore = score;
score = 0;
updateScore();
addToConsole("DEAD!");
//reset speed
speed = 10;
//shorten snake
var i = snake.length-1;
while(i>=1){
snake.pop();
i--;
}
//reset head
vec3.set([0,0,0],head.position);
snake.push(new SnakeBit({
position: vec3.create([0,-5,5]),
front: head
}));
gameOver = false;
requestAnimationFrame(tick);
}
}