-
Notifications
You must be signed in to change notification settings - Fork 0
/
qborgprototype.html
557 lines (452 loc) · 17.8 KB
/
qborgprototype.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>QBORG Viewer Prototype</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/87/three.min.js"></script>
<script src="scripts/cwspriteloader.js"></script>
<script src="scripts/KeyboardState.js"></script>
<script src="scripts/jsborg.js"></script>
<style type="text/css">
#emb
{
width: 144px;
height: 144px;
background-color: #00FFFF;
float:left;
}
#nav
{
width: 144px;
height: 144px;
background-color: #FF00FF;
float:right;
}
#hud
{
width:512px;
}
#playerPos
{
position: relative;
width: 5px;
height: 5px;
background-color: red;
}
#speed
{
width:200px;
}
#sidePanel
{
display: inline;
}
</style>
</head>
<body>
<input type="url">
<input type="button" value="Go" onclick="loadBorg()"><br>
<iframe name="sidePanel" id="sidePanel" src="CWSide.html" width="256" height="512" align="right"></iframe>
<div id="borgDisplay"></div>
<div id="hud">
<div id="emb"></div>
<input id="speed" type="range" min="10" max="500" />
<div id="nav"><div id="playerPos"></div></div>
</div>
<script>
var borg = {};
var borgDisplay = document.getElementById("borgDisplay");
var emb = document.getElementById("emb");
var nav = document.getElementById("nav");
var sidePanel = document.getElementById("sidePanel");
sidePanel.onload = function()
{
var links = this.contentDocument.links;
var contentWindow = this.contentWindow;
for(var l = 0; l < links.length; l++)
{
links[l].addEventListener("click",function(e)
{
if(!this.href.startsWith("javascript:"))
{
e.preventDefault();
updateFrame(this.href);
}
})
}
var shimScript = document.createElement("script");
var pathToBorg = document.querySelector('input[type=url]').value;
var shimVars = shimContent.toString().replace("target","\"" + pathToBorg + "\"");
shimVarsStripped = shimVars.substring(shimVars.indexOf("{") + 1, shimVars.lastIndexOf("}"));
shimScript.innerHTML = shimVarsStripped;
this.contentDocument.head.insertBefore(shimScript, this.contentDocument.head.firstChild);
}
var player = new THREE.Mesh(new THREE.BoxGeometry(SCALE / 4, SCALE / 4, SCALE / 4), new THREE.MeshBasicMaterial({ visible:false}));
var playerIndicator = document.getElementById("playerPos");
var audioCtx = new AudioContext();
var keyboard = new KeyboardState();
var raycaster = new THREE.Raycaster();
var listener = new THREE.AudioListener();
var mouse = new THREE.Vector2();
var scene = new THREE.Scene();
var currentMusic = "";
var scene = new THREE.Scene();
var renderId = null;
var borgPath = "";
var omniSound = new THREE.Audio(listener);
function loadBorg()
{
var borgUrl = document.querySelector('input[type=url]').value;
var windowWidth = 512;
var windowHeight = 512;
var borgLoader = new THREE.FileLoader();
borgLoader.load(borgUrl,
function(data)
{
borgPath = borgUrl.substring(0,borgUrl.lastIndexOf("/") + 1);
cancelAnimationFrame(renderId);
scene = new THREE.Scene();
borgDisplay.innerHTML = "";
var contents = (new DOMParser()).parseFromString( data, "text/xml").childNodes[0];
borg = parseBorg(contents);
emb.style.backgroundImage = "url('" + borgPath + "domains/" + borg.emb + "')";
nav.style.backgroundImage = "url('" + borgPath + "domains/" + borg.nav + "')";
var playerCoords = contents.getElementsByTagName("gen")[0].getElementsByTagName("pos")[0].textContent.trim().split(" ");
var playerX = (parseInt(playerCoords[0],16) / 1024) * 16;
var playerY = 16 - (parseInt(playerCoords[1],16) / 1024) * 16;
var height = parseInt(playerCoords[2],16) * 4;
var angle = -(((parseInt(playerCoords[3],16) / 4096) * 360) - 270);
if(angle < 0) angle += 360;
scene = new THREE.Scene();
player = new THREE.Mesh(new THREE.BoxGeometry(SCALE / 4, SCALE / 4, SCALE / 4), new THREE.MeshBasicMaterial({ wireframe:true}));
player.position.x = playerX * SCALE - (SCALE / 2);
player.position.z = playerY * SCALE - (SCALE / 2);
player.position.y = (SCALE / 8) - 32 + height;
scene.add(player);
var playerTrigger = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
var camera = new THREE.PerspectiveCamera(windowWidth / 17, windowWidth / windowHeight, 0.1, 4000);
camera.rotation.order = "YXZ";
camera.rotation.y = angle * (Math.PI/180);
player.add(camera);
camera.add(listener);
var bgMesh;
if(borg.bgGraphic)
{
var backgroundTexture = loader.load(borgPath + "domains/" + borg.bgGraphic,
function (skyTexture)
{
bgMesh = new THREE.Mesh(
new THREE.PlaneGeometry((skyTexture.image.width / (windowWidth / 2)), (skyTexture.image.height / (windowWidth / 2)), 0),
new THREE.MeshBasicMaterial({map: skyTexture})
);
var skyHeight = ((skyTexture.image.height / 2) / (windowWidth / 2));
skyTexture.wrapS = THREE.RepeatWrapping;
var initialOffset = skyHeight + (borg.bgHeight / (windowWidth / 2));
bgMesh.position.y += initialOffset;
bgMesh.material.depthTest = false;
bgMesh.material.depthWrite = false;
bgScene.add(bgMesh);
var skyCanvas = document.createElement("canvas");
skyCanvas.width = skyTexture.image.width;
skyCanvas.height = skyTexture.image.height;
skyCanvas.getContext('2d').drawImage(skyTexture.image, 0, 0);
var topSky = skyCanvas.getContext('2d').getImageData(0, 0, 1, 1).data;
var bottomSky = skyCanvas.getContext('2d').getImageData(0, skyTexture.image.height - 1, 1, 1).data;
var topMesh = new THREE.Mesh(
new THREE.PlaneGeometry((skyTexture.image.width / (windowWidth / 2)), (skyTexture.image.width / (windowWidth / 2)), 0),
new THREE.MeshBasicMaterial({color: new THREE.Color(topSky[0]/255,topSky[1]/255,topSky[2]/255), alpha: 0.5})
);
topMesh.position.y += initialOffset + ((skyTexture.image.width - ((skyTexture.image.width - skyTexture.image.height) / 2)) / (windowWidth / 2));
topMesh.material.depthTest = false;
topMesh.material.depthWrite = false;
bgScene.add(topMesh);
var bottomMesh = new THREE.Mesh(
new THREE.PlaneGeometry((skyTexture.image.width / (windowWidth / 2)), (skyTexture.image.width / (windowWidth / 2)), 0),
new THREE.MeshBasicMaterial({color: new THREE.Color(bottomSky[0]/255,bottomSky[1]/255,bottomSky[2]/255)})
);
bottomMesh.position.y += initialOffset - ((skyTexture.image.width - ((skyTexture.image.width - skyTexture.image.height) / 2)) / (windowWidth / 2));
bottomMesh.material.depthTest = false;
bottomMesh.material.depthWrite = false;
bgScene.add(bottomMesh);
},
function(e) {},
function(e)
{
console.error("Sky texture not found","\n",e);
});
}
var bgScene = new THREE.Scene();
var bgCam = new THREE.Camera();
bgScene.add(bgCam);
renderBorg(scene,borg,bgScene);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(windowWidth, windowHeight);
renderer.sortObjects = false;
borgDisplay.appendChild(renderer.domElement);
changeGTW2(borg.defaultPage);
renderer.domElement.addEventListener('mousemove', onMouseMove, false);
function onMouseMove(event)
{
var offset = event.target.getBoundingClientRect();
mouse.x = ((event.clientX - offset.left) / windowWidth) * 2 - 1;
mouse.y = - ((event.clientY - offset.top) / windowHeight) * 2 + 1;
}
function onMouseClick(event)
{
onMouseMove(event);
/*var mouseOvers = raycaster.intersectObjects(mouseTriggers,true);
if(mouseOvers.length > 0)
{
var clickedOn = mouseOvers[0].object;
clickedOn.onClick();
}*/
var mouseHit = getMousedOverElement();
if(mouseHit)
{
mouseHit.onClick();
}
}
renderer.domElement.addEventListener('click', onMouseClick, false);
var occupiedTriggers = [];
function getMousedOverElement()
{
//A small trick to ensure that if the mouse is over an object that happens to be in front of a mouseTrigger, that mouseTrigger will not be detected
var mouseOvers = raycaster.intersectObjects(scene.children,true);
var mouseOver = mouseOvers.find((hit) => mouseTriggers.includes(hit.object) || mouseTriggers.includes(hit.object.parent));
//var mouseOver = mouseOvers[0];
if(mouseOver) return [mouseOver.object, mouseOver.object.parent].find(thing => thing.onClick);
else return undefined;
}
var prevTime;
function render(time)
{
renderId = requestAnimationFrame(render);
var delta = time - prevTime;
prevTime = time;
playerIndicator.style.left = ((player.position.x / SCALE) / 16) * 144 + 2.5 + "px";
playerIndicator.style.top = ((player.position.z / SCALE) / 16) * 144 + 2.5 + "px";
var sprites = scene.children.filter(child => child.type == "Sprite" || child.type == "SpriteOnWall");
raycaster.setFromCamera(mouse, camera);
/*var mouseOvers = raycaster.intersectObjects(mouseTriggers).filter(function(hit)
{
if(hit.object.type == "Sprite")
{
return true;
}
else return true;
});*/
mouseTriggers.map(object =>
{
//object.material.color.set("white");
borgDisplay.style.cursor = "auto";
});
var mouseHit = getMousedOverElement();
if(mouseHit)
{
//mouseHit.material.color.set("red");
borgDisplay.style.cursor = "pointer";
}
sprites.map(sprite =>
{
var positionDifference = new THREE.Vector3;
positionDifference.subVectors(player.position,sprite.position);
var direction = (Math.atan2(positionDifference.z,positionDifference.x) / (Math.PI / 180)) + 180;
if(direction < 0) direction += 360;
if(direction > 360) direction -= 360;
if(sprite.material.update) sprite.material.update(delta,direction)
});
var camDir = camera.getWorldDirection();
keyboard.update();
if (keyboard.pressed("left"))
{
camera.rotation.y += 0.02;
if(camera.rotation.y > (2 * Math.PI)) camera.rotation.y -= (2 * Math.PI);
}
if (keyboard.pressed("right"))
{
camera.rotation.y -= 0.02;
if(camera.rotation.y < 0) camera.rotation.y += (2 * Math.PI);
}
if (keyboard.pressed("up"))
{
player.position.addScaledVector(new THREE.Vector3(camDir.x,0,camDir.z),0.1 * SCALE);
}
if (keyboard.pressed("down"))
{
player.position.addScaledVector(new THREE.Vector3(camDir.x,0,camDir.z),-0.1 * SCALE);
}
if (keyboard.pressed("A"))
{
player.position.x -= 5 * Math.cos(camera.rotation.y);
player.position.z += 5 * Math.sin(camera.rotation.y);
}
if (keyboard.pressed("D"))
{
player.position.x += 5 * Math.cos(camera.rotation.y);
player.position.z -= 5 * Math.sin(camera.rotation.y);
}
if (keyboard.pressed("pageup") && camera.rotation.x < (Math.PI / 5))
{
camera.rotation.x += 0.015;
bgCam.position.y += 0.015 * 3.84;
}
if (keyboard.pressed("pagedown") && camera.rotation.x > -(Math.PI / 5))
{
camera.rotation.x -= 0.015;
bgCam.position.y -= 0.015 * 3.84;
}
playerTrigger.setFromObject(player);
if(bgMesh) bgMesh.material.map.offset.x = -camera.rotation.y / (0.5 * Math.PI) + 0.5;
var originPoint = player.position.clone();
for(var v = 0; v < player.geometry.vertices.length; v++)
{
var localVertex = player.geometry.vertices[v].clone();
var globalVertex = localVertex.applyMatrix4(player.matrix);
var directionVector = globalVertex.sub(player.position);
var ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize());
var collisionResults = ray.intersectObjects(walls);
collisionResults.map(result =>
{
if(result.distance < borg.initialSpeed)
{
var force = (directionVector.length() - result.distance) / player.geometry.vertices.length;
var collisionDir = result.face.normal;
player.position.addScaledVector(new THREE.Vector3(collisionDir.x,0,collisionDir.z),force);
}
})
}
var triggerRay = new THREE.Raycaster(originPoint, new THREE.Vector3(0,-1,0));
var steppingOnTriggers = triggerRay.intersectObjects(triggers).map(hit => hit.object);
var steppedOffTriggers = triggers.filter(trigger => !steppingOnTriggers.includes(trigger) && occupiedTriggers.includes(trigger) && trigger.triggered == true);
if(omniSound.isPlaying)
{
var steppingOnAudio = steppingOnTriggers.filter(trigger => trigger.soundRef || trigger.soundBox);
steppingOnAudio.map(trigger => trigger.triggered = false);
steppingOnTriggers = steppingOnTriggers.filter(trigger => !steppingOnAudio.includes(trigger));
if(player.position.x < omniSound.startingPosition.x - (SCALE / 2) || player.position.x > omniSound.startingPosition.x + (SCALE / 2) || player.position.z < omniSound.startingPosition.z - (SCALE / 2) || player.position.z > omniSound.startingPosition.z + (SCALE / 2)) omniSound.stop();
}
steppedOffTriggers.map(steppedOffTrigger =>
{
steppedOffTrigger.checkCluster(function(cluster)
{
var stillActive = cluster.some(otherTrigger => steppingOnTriggers.includes(otherTrigger));
if(!stillActive)
{
steppedOffTrigger.onLeave(player);
occupiedTriggers.splice(occupiedTriggers.indexOf(steppedOffTrigger),1);
}
});
});
steppingOnTriggers.map(trigger =>
{
if(trigger.triggered == false) occupiedTriggers.push(trigger);
trigger.onEnter(player);
})
updateAll(scene);
renderer.autoClear = false;
renderer.clear();
renderer.render(bgScene, bgCam);
renderer.render(scene, camera);
}
render();
function updateAll(origin)
{
if(origin.update && origin.type != "Sprite") origin.update();
origin.children.map(updateAll);
}
},
function(xhr)
{
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
function(err)
{
console.error(err);
}
);
}
function changeGTW(shortcut)
{
getShortcut(shortcut).then(path =>
{
if(path.endsWith(".borg")) window.location.href = path;
else
{
var GTWDisplay = document.createElement("iframe");
GTWDisplay.width = windowWidth;
GTWDisplay.height = windowHeight;
GTWDisplay.src = path;
borgDisplay.replaceChild(GTWDisplay,borgDisplay.firstChild);
switched = true;
}
},console.error)
}
function getShortcut(shortcutPath)
{
return new Promise(function(resolve,reject)
{
if(shortcutPath.endsWith(".html")) resolve(borgPath + shortcutPath);
else if(shortcutPath.endsWith(".url"))
{
var test = new XMLHttpRequest();
test.onload = function() {
if(this.status == 200)
{
var path = this.response.match(/URL=(\S+)/)[1];
if(path.startsWith("file:")) path = path.substring(8);
else path = path.substring(7);
var fullBorgPath = new URL(borgPath,window.location.href).href;
path = new URL(path,fullBorgPath + "domains/").href;
var target = null;
var targetMatch = this.response.match(/TARGET="(\S+)"/);
if(targetMatch) target = targetMatch[1];
resolve({path:path, target:target});
}
else reject(shortcutPath + " not found");
}
test.onerror = reject;
test.open("GET", borgPath + "domains/" + shortcutPath, true);
test.send();
}
else resolve(new URL(shortcutPath + ".borg",window.location.href).href);
})
}
function changeGTW2(shortcut)
{
console.log("Looking at",borgPath + "domains/" + shortcut);
getShortcut(shortcut).then(updateFrame,console.error);
}
function updateFrame(path)
{
if(path.target == null && sidePanel.src != path.path) sidePanel.src = path.path;
else if(sidePanel.contentDocument && sidePanel.contentDocument.getElementsByName(path.target)[0]) sidePanel.contentDocument.getElementsByName(path.target)[0].src = path.path;
}
function playOmni(buffer)
{
var stillPlaying = scene.children.filter(function(child)
{
var grandchild = child.children.find(grandchild => grandchild.type == "Audio");
if(!grandchild) return false;
return grandchild.isPlaying;
}).map(child => child.children.find(grandchild => grandchild.type == "Audio"));
if(omniSound.isPlaying) omniSound.stop();
//Will be used as a reference for the tile the player is on when the sound plays. If the player steps away from this tile, the sound will automatically stop
omniSound.startingPosition = new THREE.Vector3(Math.round(player.position.x / SCALE) * SCALE,0,Math.round(player.position.z / SCALE) * SCALE);
console.log(buffer);
omniSound.setBuffer(buffer);
omniSound.play();
stillPlaying.map(sound => sound.stop());
markLine(omniSound.startingPosition,"red");
}
function changeMusic(music)
{
console.log("Changing to",music);
}
function clearLines()
{
var lines = scene.children.filter(child => child.type =="Line");
lines.map(line => scene.remove(line));
}
</script>
</body>
</html>