/
tunnel.js
258 lines (229 loc) · 5.83 KB
/
tunnel.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
Game = (function() {
//Interval that will call tick.
var interval = null;
var player = null;
var playerSpeed = 7;
//Representation of the path through which the player travels.
var path = [];
//Pixel tickness, or hight of block of tunnel.
var thickness = 10;
//Starting width of the tunnel.
var baseWidth = 200;
//Which index of the path is currently at the bottom of the screen.
var index = 0;
//The canvas.
var cv = null;
//Remember if touching.
var touching = false;
var gameOverCallback = null;
/**
* Starts the game.
* Initializes everything.
*/
function startGame() {
index = 0;
path = [];
window.onkeydown = keyDownHandler;
window.onkeyup = keyUpHandler;
generatePlayer();
generateInitialTunnel();
interval = setInterval(tick, 25);
cv = document.getElementById('canvas');
cv.ontouchmove = touchMoveHandler;
cv.ontouchstart = touchStartHandler;
cv.ontouchend = touchEndHandler;
document.getElementById("endgame").style.display = "none";
document.getElementById("endgame").style.opacity = "0";
//Draw the first frame.
drawFrame();
console.log(player.x + player.width/2)
}
function setGameOverCallback(func) {
gameOverCallback = func;
}
/**
* Generates the player Entity and saves it as player.
*/
function generatePlayer() {
var height = thickness * 5;
var width = height / 3;
var x = window.innerWidth / 2 - width / 2;
var y = window.innerHeight - height;
var color = 'green';
player = new Entity(x, y, width, height, color);
player.right = false;
player.left = false;
}
/**
* Generates the initial part of the tunnel which is straight.
*/
function generateInitialTunnel() {
var center = window.innerWidth / 2;
for(var i = 0; i < 15; i++) {
var elm = new Entity(center - baseWidth / 2, window.innerHeight - i * thickness, baseWidth, thickness, 'black');
path.push(elm);
}
for(var i = 0; i < window.innerHeight / thickness; i++) {
discoverPath();
}
}
/**
* The tick of the game, called often.
*/
function tick() {
handleMovements();
discoverPath();
drawFrame();
checkCollision();
index++;
document.getElementById('distance').innerHTML = index;
}
/**
* Discovers (generates) the next block of the path.
*/
function discoverPath() {
var last = path[path.length - 1];
var rand = Math.random();
var x = last.x;
if(rand < 0.5) {
x -= thickness;
} else {
x += thickness;
}
var y = last.y - thickness;
var color = 'black';
var width = baseWidth - index/20 + rand * thickness;
var height = thickness;
if(x < 0) x = 0;
if(x > window.innerWidth - width) x = window.innerWidth - width;
var elm = new Entity(x, y, width, height, color);
path.push(elm);
}
/**
* Checks if the player collides with the tunnel.
* The player can collide with the player.height/thickness next elements on the path from the current index.
*/
function checkCollision() {
var range = Math.round(player.height / thickness) + 1;
for(var i = index; i < index+range; i++) {
if(path[i] === undefined) break;
if(player.x <= path[i].x || player.x + player.width >= path[i].x + path[i].width) {
//collision is happening.
endGame();
}
}
}
/**
* Ends the game and gives player the option to restart.
*/
function endGame() {
clearInterval(interval);
if(gameOverCallback !== null) {
var text = 'You traveled ' + index + ' m.';
gameOverCallback(text);
}
}
/**
* Check if user is moving and move player accordingly.
*/
function handleMovements() {
if(player.right) {
player.x += playerSpeed;
if(player.x+player.width > window.innerWidth) {
player.x = window.innerWidth-player.width;
}
}
if(player.left) {
player.x -= playerSpeed;
if(player.x < 0) {
player.x = 0;
}
}
}
/**
* Paints the current state unto the canvas.
*/
function drawFrame() {
cv.width = window.innerWidth;
cv.height = window.innerHeight;
var ctx = cv.getContext('2d');
//Draw player.
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
//Draw the path.
var range = Math.round(window.innerHeight / thickness) + 1;
for(var i = index; i < index+range; i++) {
if(path[i] === undefined) break;
var elm = path[i];
ctx.fillStyle = elm.color;
var y = window.innerHeight - (i-index) * thickness;
//The path saves where we shouldn't fill, so draw left and right from it.
ctx.fillRect(0, y, elm.x, thickness);
ctx.fillRect(elm.x+elm.width, y, window.innerWidth-elm.x-elm.width, thickness);
}
}
/**
* Constructor function for an entitiy.
*/
function Entity(xStart, yStart, width, height, color) {
this.x = xStart;
this.y = yStart;
this.width = width;
this.height = height;
this.color = color;
}
function keyDownHandler(e) {
if(e.keyCode === 65 || e.keyCode === 37) {
player.left = true;
}else if(e.keyCode === 68 || e.keyCode === 39) {
player.right = true;
}
}
function keyUpHandler(e) {
if(e.keyCode === 65 || e.keyCode === 37) {
player.left = false;
}else if(e.keyCode === 68 || e.keyCode === 39) {
player.right = false;
}
}
function touchStartHandler(e) {
touching = true;
translateTouch(e);
}
function touchEndHandler() {
touching = false;
player.left = false;
player.right = false;
}
function touchMoveHandler(e) {
if(e.target !== cv) return false;
if(!touching) return false;
translateTouch(e);
}
/**
* Translates touch position into left and right movement.
*/
function translateTouch(e) {
e.x = e.touches[0].pageX;
var center = player.x + player.width/2;
if(e.x < center) {
player.left = true;
player.right = false;
} else {
player.right = true;
player.left = false;
}
}
function pause() {
clearInterval(interval);
}
function resume() {
interval = setInterval(tick, 25);
}
return {
start: startGame,
pause: pause,
resume: resume,
setGameOverCallback: setGameOverCallback
}
}());