-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
154 lines (125 loc) · 3.89 KB
/
main.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
var gl;
function initGL(canvas) {
gl = canvas.getContext("webgl");
if (!gl) {
gl = canvas.getContext("experimental-webgl");
}
if (gl) {
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} else {
alert("Could not initialise webgl...");
}
}
function loadShader(gl, type, code) {
var shader = gl.createShader(type);
gl.shaderSource(shader, code);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var controls = {
isKeyDown: function (keyCode) {
return Boolean(this.keyStates[keyCode]);
},
keyStates: [],
getGamepad: function() {
return navigator.getGamepads()[0];
}
};
document.addEventListener("keyup", function (e) {
controls.keyStates[e.keyCode] = false;
});
document.addEventListener("keydown", function (e) {
controls.keyStates[e.keyCode] = true;
});
var shaderProgram;
function initShaders() {
var vertexShader = loadShader(gl, gl.VERTEX_SHADER, shaders.vertex);
var fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, shaders.fragment);
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Cold not initialise shaders.");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute =
gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
}
var pMatrix = mat4.create();
var w, h;
function initViewport() {
var aspectRatio = gl.viewportWidth / gl.viewportHeight;
w = 40;
h = w / aspectRatio;
mat4.ortho(pMatrix, -w / 2, w / 2, -h / 2, h / 2, -10, 10);
}
matStack = {
current: mat4.create(),
stack: [],
push: function () {
this.stack.push(this.current);
this.current = mat4.clone(this.current);
},
pop: function () {
if (!this.stack.length) {
throw "Empty matrix stack";
}
this.current = this.stack.pop();
}
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, matStack.current);
}
function wrapPosition(object) {
if (object.position[0] > w / 2) { object.position[0] -= w; }
if (object.position[0] < -w / 2) { object.position[0] += w; }
if (object.position[1] > h / 2) { object.position[1] -= h; }
if (object.position[1] < -h / 2) { object.position[1] += h; }
}
function randomPosition(w, h) {
return vec2.fromValues(Math.random() * w - (w / 2), Math.random() * h - (h / 2));
}
function padString(s, n) {
var zeroes = '00000000';
s += '';
return zeroes.slice(0, Math.max(0, n - s.length)) + s;
}
var canvas = document.getElementsByTagName("canvas")[0],
scoreElement = document.getElementById('score'),
livesElement = document.getElementById('lives');
initGL(canvas);
initViewport();
initShaders();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
var states = {
'start': new Start(),
'game': new Game()
}
var nextState = states['start'];
var currentState = states['start'];
function changeState(stateCode) {
nextState = states[stateCode];
}
var lastFrameTime = new Date();
function tick() {
requestAnimationFrame(tick);
var now = new Date();
var dt = (now - lastFrameTime) / 1000;
lastFrameTime = now;
if (nextState != currentState) {
currentState = nextState;
}
currentState.update(dt);
currentState.draw();
}
tick();