-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
301 lines (258 loc) · 9.15 KB
/
index.html
File metadata and controls
301 lines (258 loc) · 9.15 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE HTML>
<head>
<title>MP7</title>
<link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
#floorNumber {color: #ff005d; z-index: 1000; position: fixed;
font-family: "Press Start 2P"; font-size: 15em;
width: 100%; height: 100%; text-align: center; margin-top: .3em;
}
#reqGrid{
z-index: 1000; position: fixed;
margin-left: 15em; margin-top: 5em;
font-family: "Monaco"; font-size: 1em;
}
td {
border: 1px solid #ff005d; color: #ff005d;
width: 2.1em; height: 2em;
text-align: center; border-collapse: collapse;
}
</style>
</head>
<body>
<div id="floorNumber">1</div>
<table id="reqGrid">
<tr>
<td id="t1"></td><td id="t2"></td><td id="t3"></td>
</tr>
<tr>
<td id="t4"></td><td id="t5"></td><td id="t6"></td>
</tr>
<tr>
<td id="t7"></td><td id="t8"></td><td id="t9"></td>
</tr>
</table>
<script src="https://threejs.org/build/three.js"></script>
<script>
var scene, camera, renderer;
var leftDoor, rightDoor;
var doorsOpen = true;
var floor = 1;
var doorDisplace = 1.1, doorDisplaceY = 2, doorDisplaceZ = 6;
var passCubes = []; //passenger meshes
var requests = []; //passenger objects
var healthBar;
var points = 50;
init();
animate();
function init(){
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x00035b );
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//lights
var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.5 );
directionalLight.position.y = 1;
directionalLight.position.z = 3;
scene.add( directionalLight );
var ambientLight = new THREE.AmbientLight( 0x3164bc );
scene.add( ambientLight );
//camera position
camera.position.z = 10;
camera.position.y = 3;
//initialize doors
var doorGeo = new THREE.BoxGeometry(2, 4, 1);
var doorMat = new THREE.MeshLambertMaterial({color: 0xebebeb});
leftDoor = new THREE.Mesh(doorGeo, doorMat);
rightDoor = new THREE.Mesh(doorGeo, doorMat);
leftDoor.position.x -= doorDisplace + 2;
rightDoor.position.x += doorDisplace + 2;
leftDoor.position.y += doorDisplaceY;
rightDoor.position.y += doorDisplaceY;
leftDoor.position.z -= doorDisplaceZ;
rightDoor.position.z -= doorDisplaceZ;
scene.add(leftDoor);
scene.add(rightDoor);
//add the ground
var eleGrid = new THREE.GridHelper(10, 3, 0xff6100, 0xff005d);
scene.add(eleGrid);
//initialize a grid of boxes
//but make them invisible until a passenger boards
var passGeo = new THREE.BoxGeometry(1.5, 1.5, 1.5);
var passMat = new THREE.MeshLambertMaterial({color:0xebebeb});
var startX = -4, startZ = -3.5;
for (var i = 0; i < 3; i++){
for (var j = 0; j < 3; j++){
var pass = new THREE.Mesh(passGeo, passMat);
pass.position.x += startX + j * 4;
pass.position.z = startZ;
passCubes.push(pass);
pass.visible = false;
scene.add(pass);
}
startZ += 3;
}
//health bar
var healthGeo = new THREE.BoxGeometry(points/10, .2, .1);
var healthMat = new THREE.MeshLambertMaterial({color:0xff005d});
healthBar = new THREE.Mesh(healthGeo, healthMat);
healthBar.position.z = 5.5;
scene.add(healthBar);
//put 2 passengers in randomly
for (var i = 0; i < 2; i++){
var rqFloor = Math.floor(Math.random() * 19) + 2;
var passNum = Math.floor(Math.random() * 9) + 1;
while (passCubes[passNum - 1].visible == true){
passNum = Math.floor(Math.random() * 9) + 1;
}
passCubes[passNum - 1].visible = true;
requests.push(new Passenger(rqFloor, passCubes[passNum - 1], passNum));
}
}
//PASSENGER PROTOTYPE
function Passenger(requestedFloor, cube, index){
this.requestedFloor = requestedFloor;
this.cube = cube;
this.anger = 0;
this.index = index;
document.getElementById("t"+(index)).innerHTML = this.requestedFloor;
}
var id;
function animate(){
if (points <= 0){
document.removeEventListener("keydown", onDocumentKeyDown);
document.getElementById("floorNumber").innerHTML = "GAME OVER";
cancelAnimationFrame( id );
}
//for loop through all passengers to update their anger
for (var i = 0; i < requests.length; i++){
requests[i].anger = requests[i].anger + .005;
//console.log(requests[i].anger);
if (requests[i].anger >= 3 && requests[i].anger < 3.05){
requests[i].cube.material.color.setHex(0xff1600);
points = points - .3;
} else if (requests[i].anger >= 2 && requests[i].anger < 2.05){
requests[i].cube.material.color.setHex(0xff5242);
points = points - .2;
} else if (requests[i].anger >= 1 && requests[i].anger < 1.05){
requests[i].cube.material.color.setHex(0xff8377);
points = points - .1;
}
}
//change length of health bar to match points
healthBar.scale.x = points/50;
id = requestAnimationFrame( animate ); //recursive
renderer.clear(); //do I need this?
renderer.render( scene, camera );
}
document.addEventListener("keydown", onDocumentKeyDown, false);
function onDocumentKeyDown(event) {
var keycode = event.which;
if (keycode == 87){
//PRESS W
if (floor < 20){
floor += 1;
}
document.getElementById("floorNumber").innerHTML = floor;
if (doorsOpen){
leftDoor.position.x += 2;
rightDoor.position.x -= 2;
doorsOpen = false;
}
}else if (keycode == 83){
//PRESS S
if (floor > 1){
floor -= 1;
}
document.getElementById("floorNumber").innerHTML = floor;
if (doorsOpen){
leftDoor.position.x += 2;
rightDoor.position.x -= 2;
doorsOpen = false;
}
} else if (keycode == 13){
//ENTER
if (doorsOpen){
leftDoor.position.x += 2;
rightDoor.position.x -= 2;
doorsOpen = false;
} else {
leftDoor.position.x -= 2;
rightDoor.position.x += 2;
doorsOpen = true;
//check for passengers that should be leaving
for (var i = 0; i < requests.length; i++){
console.log("hey "+requests.length);
console.log(requests[i].requestedFloor+", "+floor);
if (requests[i].requestedFloor == floor){
console.log("Passenger got off at floor "+floor);
requests[i].cube.visible = false;
document.getElementById("t"+(requests[i].index)).innerHTML = "";
//if you let the passenger off before really angry
if (requests[i].anger < 1){
if (points + 5 < 50){
points = points + 5;
} else {
points = 50;
}
} else if (requests[i].anger < 2){
if (points + 3 < 50){
points = points + 3;
} else {
points = 50;
}
} else if (requests[i].anger < 3){
if (points + 1 < 50){
points = points + 1;
} else {
points = 50;
}
}
//remove request entirely
requests.splice(i, 1);
}
}
var chance = Math.random() * 4;
//50% chance of no new passengers
//25% chance that 1 new passenger will board
//12.5% chance that 2 new passengers will board
//6.25% chance that 3 new passengers will board
//6.25% chance that 4 new passengers will board
//if there are no passengers...
if (requests.length == 0){
chance = Math.random() * 2;
}
if (chance <= 1){
addPassengers(1);
} else if (chance <= 1.5){
addPassengers(2);
} else if (chance <= 1.75){
addPassengers(3);
} else if (chance <= 2){
addPassengers(4);
}
}
}
}
function addPassengers(amount){
for (var i = 0; i < amount; i++){
if (requests.length < 9){
var rqFloor = Math.floor(Math.random() * 20) + 1;
var passNum = Math.floor(Math.random() * 9) + 1;
while (passCubes[passNum - 1].visible == true){
passNum = Math.floor(Math.random() * 9) + 1;
}
while (rqFloor == floor){
rqFloor = Math.floor(Math.random() * 20) + 1;
}
passCubes[passNum - 1].visible = true;
requests.push(new Passenger(rqFloor, passCubes[passNum - 1], passNum));
}
}
}
</script>
</body>