-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
187 lines (159 loc) · 4.8 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
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
var STEREO = true;
var STEREO_SEP = -0.4;
var USE_GYRO = false;
var VR = false;
var materials;
var camera, scene, group, renderer;
var controls;
var sections = [];
var sectionOffset = 0;
var position = 0;
var lastTime = window.performance.now();
var lastGamepadBtns = 0;
var score = 0;
var gameRunning = false;
var spritey; // game over text
function resetGame() {
while (sections.length) {
group.remove(sections[0].group);
sections.shift();
}
if (spritey) {
group.remove(spritey);
spritey = undefined;
}
position = 0;
sectionOffset = 0;
mv({x:0,y:0});
}
function newGame() {
score = 0;
gameRunning = true;
newSection(0);
newSection(0);
newSection();
}
function stopGame() {
console.log("STOPPED GAME");
resetGame();
gameRunning = false;
var canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 128;
var context = canvas.getContext('2d');
context.textAlign = "center";
context.strokeStyle = "white";
context.fillStyle = "white";
context.font = "48px Arial";
context.fillText( "Game Over!", 128, 48);
context.font = "32px Arial";
context.fillText( "Score: "+Math.round(score*10), 128, 96);
context.font = "24px Arial";
context.fillText( "(Click to restart)", 128, 128);
var texture = mapC = new THREE.Texture(canvas);
texture.needsUpdate = true;
var spriteMaterial = new THREE.MeshBasicMaterial( { map: texture, color: 0xffffff } );
spriteMaterial.side = THREE.DoubleSide;
var geo = new THREE.PlaneGeometry( -2, 1 );
spritey = new THREE.Mesh( geo, spriteMaterial );
spritey.position.set(0,0,3);
group.add( spritey );
}
function newSection(forceSection) {
var section = new Section(forceSection);
section.sectionStart = sectionOffset;
section.sectionEnd = sectionOffset + section.sectionLength;
section.group.position.z = section.sectionStart;
sectionOffset += section.sectionLength;
group.add(section.group);
setTimeout(function() {
section.start();
}, 500);
sections.push(section);
}
function init() {
document.getElementsByTagName("body")[0].overflow = "hidden";
var container = document.createElement( 'div' );
document.body.appendChild( container );
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0, 5, 10);
materials = {
sq : new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader().load( 'border.png' ) } ),
circ : new THREE.MeshBasicMaterial( { map: new THREE.TextureLoader().load( 'bordercirc.png' ) } )
};
for (var i in materials)
materials[i].side = THREE.DoubleSide;
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 100 );
scene.add( camera );
camera.position.y = 1.5; // overwritten by VR if it's set
group = new THREE.Group();
group.rotation.y = Math.PI;
groupoffset = new THREE.Group();
groupoffset.add(group);
groupoffset.position.y = 1.5;
scene.add(groupoffset);
resetGame();
newGame();
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
if (STEREO) {
effect = new THREE.StereoEffect( renderer );
effect.eyeSeparation = STEREO_SEP;
effect.setSize( window.innerWidth, window.innerHeight );
} else if (VR) {
renderer.vr.enabled = true;
document.body.appendChild( WEBVR.createButton( renderer ) );
}
container.appendChild( renderer.domElement );
//
setupControls();
setupWindow();
// kick off rendering
renderer.setAnimationLoop( render );
}
function render() {
// update timing
var time = window.performance.now();
var timeDiff = time - lastTime;
lastTime = time;
if (timeDiff>100) timeDiff=100; // in case paused
if (controls)
controls.update(timeDiff);
checkControls();
TWEEN.update();
if (gameRunning) {
score += timeDiff/1000;
position += (1+score/100)*4*timeDiff/1000;
// now remove old sections
if (position > sections[0].sectionEnd) {
group.remove(sections[0].group);
sections.shift();
newSection();
}
group.position.z = position;
// collision
var directionVector = new THREE.Vector3(0,0,-1);
var ray = new THREE.Raycaster( camera.position, directionVector );
var collisionResults = ray.intersectObjects( group.children, true);
var collisionDists = [];
collisionResults.forEach(function(c) {
if (collisionDists.indexOf(c.distance)<0)
collisionDists.push(c.distance);
});
if (collisionDists.length&1) {
console.log("Collision! "+collisionDists.length+" at "+position);
if (position>2) stopGame();
}
} else {
group.position.z = position;
}
if (spritey) {
spritey.rotation.x = Math.sin(time / 400)/10;
spritey.rotation.y = Math.sin(time / 567)/10;
}
if (STEREO)
effect.render( scene, camera );
else
renderer.render( scene, camera );
}