-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
187 lines (144 loc) · 5.95 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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Aim trainer</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
<link rel="stylesheet" type="text/css" href="css/global.css"></link>
<script src="js/three.js"></script>
<script src="js/dat.gui.js"></script>
</head>
<body>
<div id="container3D"></div>
<script type="text/javascript">
const BORDER = 100
var originalColor = 0x0056b8;
var objects = [];
var scene = new THREE.Scene();
var ratio = window.innerWidth/window.innerHeight;
var camera = new THREE.OrthographicCamera( -BORDER*ratio, BORDER*ratio, BORDER, -BORDER, 0.1, 1000 );
camera.position.z = 140;
camera.lookAt(new THREE.Vector3(0, 0, 0));
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor(0x242424, 1);
document.body.appendChild( renderer.domElement );
var ambientLight = new THREE.AmbientLight(0xFFFFFF);
scene.add(ambientLight);
var pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.set(50, 50, 0);
scene.add(pointLight);
var gridHelper = new THREE.GridHelper(200, 20);
//scene.add(gridHelper);
var axesHelper = new THREE.AxesHelper(20);
//scene.add(axesHelper);
var circlesSystem = new THREE.Object3D();
scene.add(circlesSystem);
var planeGeometry = new THREE.PlaneGeometry(500, 500)
plane = new THREE.Mesh( planeGeometry, new THREE.MeshBasicMaterial( { visible: false } ) );
scene.add( plane );
objects.push(plane);
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var Difficulty = function() {
this.circleRadius = 10;
this.spawnRate = 2;
this.minimumSpawnTime = 1;
this.despawnTime = 3;
this.level = "Medium"
this.changeDifficulty = function() {
switch(this.level) {
case "Easy":
this.updateSettings(10, 3, 1, 3);
break;
case "Medium":
this.updateSettings(7, 1.5, 0.5, 1);
break;
case "Hard":
this.updateSettings(3, 0.5, 0.1, 1);
break;
}
}
this.updateSettings = function(radius, spawnRate, minSpawnRate, despawnTime) {
this.circleRadius = radius;
this.spawnRate = spawnRate;
this.minSpawnRate = minSpawnRate;
this.despawnTime = despawnTime;
}
}
var difficulty = new Difficulty();
var gui = new dat.GUI();
gui.add(difficulty, 'level', ["Easy", "Medium", "Hard"]);
function onMouseMove( event ) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
function onMouseClick(event) {
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(objects);
if (intersects.length > 0) {
var intersectObject = intersects[0].object;
if (intersectObject !== plane) {
despawnCircle(intersectObject);
}
}
}
function createCircle() {
var circleGeometry = new THREE.CircleGeometry(difficulty.circleRadius, 32);
var circleMaterial = new THREE.MeshPhongMaterial( { color: originalColor } );
return new THREE.Mesh( circleGeometry, circleMaterial );
}
function spawnCircle(time) {
var x = (Math.random() * 2 - 1) * BORDER;
var y = (Math.random() * 2 - 1) * -BORDER;
var circle = createCircle();
circle.position.set(x, y, 1);
circle.spawnTime = time;
circlesSystem.add(circle);
objects.push(circle);
}
function despawnCircle(circle) {
circlesSystem.remove(circle);
objects.splice(objects.indexOf(circle), 1);
}
var nextCircleSpawnTime = 0;
function animate(time) {
time *= 0.001;
difficulty.changeDifficulty();
updateCircles(time);
if (time > nextCircleSpawnTime) {
nextCircleSpawnTime = time + difficulty.spawnRate * Math.random() + difficulty.minimumSpawnTime;
spawnCircle(time);
}
requestAnimationFrame( animate );
renderer.render(scene, camera);
}
function updateCircles(time) {
// paint the cube with its original color and check spawn times
var circles = circlesSystem.children;
for (var i = 0; i < circles.length; i++){
var circle = circles[i];
circle.material.color.set(originalColor);
if ((time - circle.spawnTime) > difficulty.despawnTime) despawnCircle(circle);
}
// update the picking ray with the camera and mouse position
raycaster.setFromCamera( mouse, camera );
// calculate objects intersecting the picking ray
var intersects = raycaster.intersectObjects( circlesSystem.children );
if (intersects.length > 0) {
intersects[0].object.material.color.set(0x5BF0FF)
}
}
window.addEventListener( 'mousemove', onMouseMove, false );
window.addEventListener( 'click', onMouseClick, false );
animate();
</script>
</body>
</html>