-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
283 lines (227 loc) · 10.4 KB
/
index.html
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Snake3D</title>
<style>
html, body {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<script src="./three.js"></script>
<script src="dat.gui.min.js"></script>
<script src="./OrbitControls.js"></script>
<script>
var WIDTH = window.innerWidth, HEIGHT = window.innerHeight;
var aspectRatio = WIDTH / HEIGHT;
var renderer = new THREE.WebGLRenderer(), camera = new THREE.PerspectiveCamera(45, aspectRatio, 0.1, 1000), scene = new THREE.Scene();
var controls = new THREE.OrbitControls(camera, renderer.domElement);
var clock = new THREE.Clock(), text = document.createElement("div");
controls.enableKeys = false;
var mov = 5;
var delta = 1 / mov;
var tetha = 0.0, edgeSize = 20, padding = 0.15;
var cubeSize = edgeSize + (edgeSize - 1) * padding;
var halfCubeSize = cubeSize/2;
var BODY_COLOR = 0x388e3c, HEAD_COLOR = 0x004D40, score = 0;
var lightPos = [new THREE.Vector3(0,50,20), new THREE.Vector3(0,15,-20), new THREE.Vector3(-20,15,20), new THREE.Vector3(20,-15,0)];
var end = false, keysQueue = [];
var snake = [], apple;
var cube = new THREE.BoxGeometry( 1, 1, 1 );
var gameCube = new THREE.BoxGeometry( cubeSize, cubeSize, cubeSize );
var direction = new THREE.Vector3(1, 0, 0);
camera.position.z = 30;
camera.position.y = 30;
cube.center();
function init(){
renderer.setSize(WIDTH, HEIGHT);
document.body.appendChild(renderer.domElement);
lightPos.forEach(function(v){
var light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(v.x, v.y, v.z);
scene.add(light)
});
for(var i = 0; i < 5; i++){
var snakeCubeMaterial = new THREE.MeshPhongMaterial( { color: (i == 4) ? HEAD_COLOR : BODY_COLOR} );
snake.push(new Cube( new THREE.Vector3((i + i * padding) -halfCubeSize + 0.5 , 0.5 + padding / 2, 0.5 + padding / 2 ), snakeCubeMaterial, scene));
}
// texture bakground:
const loader = new THREE.TextureLoader();
const bgTexture = loader.load('https://threejsfundamentals.org/threejs/resources/images/daikanyama.jpg');
scene.background = bgTexture;
// creating a helper for color gui
class ColorGUIHelper {
constructor(object, prop) {
this.object = object;
this.prop = prop;
}
get value() {
return `#${this.object[this.prop].getHexString()}`;
}
set value(hexString) {
this.object[this.prop].set(hexString);
}
}
// Ambient color
const color1 = 0xFFFFFF;
const intensity1 = 1;
const light1 = new THREE.AmbientLight(color1, intensity1);
scene.add(light1);
// Hemisphere light
const skyColor = 0xB1E1FF; // light blue
const groundColor = 0xB97A20; // brownish orange
const intensity2 = 1;
const light2 = new THREE.HemisphereLight(skyColor, groundColor, intensity2);
scene.add(light2);
// Directional light
const color3 = 0xFFFFFF;
const intensity3 = 1;
const light3 = new THREE.DirectionalLight(color3, intensity3);
light3.position.set(0, 10, 0);
light3.target.position.set(-5, 0, 0);
scene.add(light3);
scene.add(light3.target);
// using gui for controlling
// Ambient
const gui = new dat.GUI();
gui.addColor(new ColorGUIHelper(light1, 'color'), 'value').name('color');
gui.add(light1, 'intensity', 0, 20, 0.01);
// Hemisphere:
const gui2 = new dat.GUI();
gui2.addColor(new ColorGUIHelper(light2, 'color'), 'value').name('skyColor');
gui2.addColor(new ColorGUIHelper(light2, 'groundColor'), 'value').name('groundColor');
gui2.add(light2, 'intensity', 0, 15, 0.01);
// Directional:
const gui3 = new dat.GUI();
gui3.addColor(new ColorGUIHelper(light3, 'color'), 'value').name('color');
gui3.add(light3, 'intensity', 0, 25, 0.01);
gui3.add(light3.target.position, 'x', -10, 10);
gui3.add(light3.target.position, 'z', -10, 10);
gui3.add(light3.target.position, 'y', 0, 10);
var appleCubeMaterial = new THREE.MeshPhongMaterial( { color: 0xe53935} );
apple = new Cube(spawnAppleVector(), appleCubeMaterial, scene);
var edgesMaterial = new THREE.LineBasicMaterial( { color: 0x000000 , linewidth: 10.0 } );
new Cube(new THREE.Vector3(0,0,0), edgesMaterial, scene, gameCube, true).setPosition(0,0,0);
text.style.position = "absolute";
text.style.width = 200;
text.style.height = 100;
text.innerHTML = "Score: " + score;
text.style.top = 20 + "px";
text.style.left = 20 + "px";
text.style.fontSize = 50 + "px";
document.body.appendChild(text);
clock.startTime = 0.0;
render();
}
function restart(){
while(snake.length > 5) scene.remove(snake.shift().mesh);
for(var i = 0; i < snake.length; i++){
snake[i].setPosition((i + i * padding) -halfCubeSize + 0.5 , 0.5 + padding / 2, 0.5 + padding / 2 );
}
end = false;
direction = new THREE.Vector3(1,0,0);
text.innerHTML = "Score: " + 0;
score = 0;
}
document.onload = init();
function spawnAppleVector(){
var x = randInRange(0, edgeSize - 1), y = randInRange(0, edgeSize - 1), z = randInRange(0, edgeSize - 1);
return new THREE.Vector3(x + x*padding -halfCubeSize + 0.5, y + y * padding -halfCubeSize + 0.5, z + z * padding -halfCubeSize + 0.5);
}
function Cube(vec, material, scene, geometry, renderWireframe){
this.geometry = typeof geometry === 'undefined' ? cube : geometry;
this.mesh = new THREE.Mesh(this.geometry, material);
if(typeof renderWireframe === 'undefined' || !renderWireframe){
this.mesh.position.set(vec.x, vec.y, vec.z);
scene.add(this.mesh);
}
else {
var edges = new THREE.EdgesGeometry( this.mesh.geometry );
scene.add(new THREE.LineSegments( edges, material ));
}
this.setPosition = function(vec){
this.mesh.position.set(vec.x, vec.y, vec.z);
};
}
function randInRange(a, b){
return a + Math.floor((b - a) * Math.random());
}
function render(){
requestAnimationFrame(render);
tetha += clock.getDelta();
if(tetha > delta){
var tail = snake.shift();
var head = snake[snake.length - 1];
head.mesh.material.color.setHex(BODY_COLOR);
tail.mesh.material.color.setHex(HEAD_COLOR);
direction = keysQueue.length > 0 ? keysQueue.pop(0) : direction;
var newPosition = new THREE.Vector3(head.mesh.position.x + direction.x + Math.sign(direction.x) * padding, head.mesh.position.y + direction.y + Math.sign(direction.y) * padding, head.mesh.position.z + direction.z + Math.sign(direction.z) * padding);
tail.setPosition(newPosition);
snake.push(tail);
head = tail;
for(var i = snake.length - 2; i > -1; i--){
if(head.mesh.position.distanceTo(snake[i].mesh.position) < 1){
end = true;
break;
}
}
if(end) {
restart();
}
if(head.mesh.position.distanceTo(apple.mesh.position) < 1){
apple.setPosition(spawnAppleVector());
text.innerHTML = "Score: " + (++score);
snake.unshift(new Cube( new THREE.Vector3(snake[0].mesh.position.x, snake[0].mesh.position.y, snake[0].mesh.position.z), new THREE.MeshPhongMaterial( { color: 0x388e3c} ), scene));
}
if(head.mesh.position.x < -halfCubeSize){
head.mesh.position.x = halfCubeSize - 0.5;
}
else if(head.mesh.position.x > halfCubeSize){
head.mesh.position.x = -halfCubeSize + 0.5;
}
else if(head.mesh.position.y < -halfCubeSize){
head.mesh.position.y = halfCubeSize - 0.5;
}
else if(head.mesh.position.y > halfCubeSize){
head.mesh.position.y = -halfCubeSize + 0.5;
}
else if(head.mesh.position.z < -halfCubeSize){
head.mesh.position.z = halfCubeSize - 0.5;
}
else if(head.mesh.position.z > halfCubeSize){
head.mesh.position.z = -halfCubeSize + 0.5;
}
tetha = 0;
}
renderer.render(scene, camera);
}
document.addEventListener("keydown", function(e){
switch(e.key){
case 'q':
keysQueue.push(new THREE.Vector3(0,1,0));
break;
case 'a':
keysQueue.push(new THREE.Vector3(0,-1,0));
break;
case "ArrowDown":
keysQueue.push(new THREE.Vector3(0,0,1));
break;
case "ArrowUp":
keysQueue.push(new THREE.Vector3(0,0,-1));
break;
case "ArrowLeft":
keysQueue.push(new THREE.Vector3(-1,0,0));
break;
case "ArrowRight":
keysQueue.push(new THREE.Vector3(1,0,0));
break;
}
});
</script>
</body>
</html>