forked from mozilla/rescuefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rescuefox.js
560 lines (426 loc) · 16.2 KB
/
rescuefox.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
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
558
559
560
/*global console,paladin */
(function() {
var Game = function( options ) {
var engine = options.engine;
var CubicVR = engine.graphics.CubicVR;
// Specify a different number of asteroids via index.html?# on the url, or use default
var spawnObjs = (window.location.search.substr(1) | 0) || 100;
var generateObjects = function() {
var result = [];
var asteroidModels = CubicVR.loadCollada("../assets/asteroids/asteroids1.dae","../assets/asteroids");
var asteroid1 = asteroidModels.getSceneObject("asteroid1").getMesh().clean();
var asteroid2 = asteroidModels.getSceneObject("asteroid2").getMesh().clean();
var asteroid3 = asteroidModels.getSceneObject("asteroid3").getMesh().clean();
var asteroid4 = asteroidModels.getSceneObject("asteroid4").getMesh().clean();
// keep model data for these ones.. (no clean)
var asteroid1hull = asteroidModels.getSceneObject("asteroid1hull").getMesh();
var asteroid2hull = asteroidModels.getSceneObject("asteroid2hull").getMesh();
var asteroid3hull = asteroidModels.getSceneObject("asteroid3hull").getMesh();
var asteroid4hull = asteroidModels.getSceneObject("asteroid4hull").getMesh();
var envTex = new CubicVR.Texture("../assets/fract_reflections.jpg");
var sphereMesh = CubicVR.primitives.sphere({
radius: 1,
lat: 24,
lon: 24
});
var cylinderMesh = CubicVR.primitives.cylinder({
radius: 1.0,
height: 2,
lon: 24
});
var capsuleMesh = new CubicVR.Mesh();
capsuleMesh.booleanAdd(cylinderMesh);
capsuleMesh.booleanAdd(sphereMesh,(new CubicVR.Transform()).translate([0,1,0]));
capsuleMesh.booleanAdd(sphereMesh,(new CubicVR.Transform()).translate([0,-1,0]));
capsuleMesh.prepare();
var capsuleCollision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CAPSULE,
radius: 1,
height: 2,
restitution:0
});
var asteroid1Collision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CONVEX_HULL,
mesh: asteroid1hull,
restitution:0
});
var asteroid2Collision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CONVEX_HULL,
mesh: asteroid2hull,
restitution:0
});
var asteroid3Collision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CONVEX_HULL,
mesh: asteroid3hull,
restitution:0
});
var asteroid4Collision = new CubicVR.CollisionMap({
type: CubicVR.enums.collision.shape.CONVEX_HULL,
mesh: asteroid4hull,
restitution:0
});
result.push({mesh:capsuleMesh,collision:capsuleCollision});
result.push({mesh:asteroid1,collision:asteroid1Collision});
result.push({mesh:asteroid2,collision:asteroid2Collision});
result.push({mesh:asteroid3,collision:asteroid3Collision});
result.push({mesh:asteroid4,collision:asteroid4Collision});
return result;
};
var spawnObjects = function (scene,physics,objlist) {
var nobjs = objlist.length-1;
for (var i = 0; i < spawnObjs; i++) {
var src = objlist[i%nobjs+1];
// var isStatic = (i%4 == 0);
// var isStatic = false;
var isStatic = (i%nobjs+1 == 4);
var distFactor = isStatic?300:200;
var sceneObj = new CubicVR.SceneObject({
mesh:src.mesh,
position:[(Math.random()-0.5)*2.0*distFactor,(Math.random()-0.5)*2*distFactor,(Math.random()-0.5)*2.0*distFactor],
rotation:[Math.random()*360,Math.random()*360,Math.random()*360]
});
var rigidObj = new CubicVR.RigidBody({
sceneObject:sceneObj,
properties: {
type: (isStatic)?CubicVR.enums.physics.body.STATIC:CubicVR.enums.physics.body.DYNAMIC,
mass: (isStatic)?0:((1 + (i % 3))*20),
collision: src.collision
},
impulse: isStatic?[0,0,0]:[(Math.random()-0.5)*100.0,(Math.random()-0.5)*100.0,(Math.random()-0.5)*100.0]
});
/* if (isStatic) {
sceneObj.getInstanceMaterials()[0].color = [1,0,0];
} else {
var cdrp = rigidObj.getMass()/80;
sceneObj.getInstanceMaterials()[0].color = [1-cdrp,1-cdrp,1];
}*/
scene.bindSceneObject(sceneObj);
physics.bindRigidBody(rigidObj);
}
};
var setupPlayer = function (scene,physics,playerObj) {
var astronautCollada = CubicVR.loadCollada("../assets/spacesuit-scene.dae","../assets/");
var astronautMesh = astronautCollada.getSceneObject("astronaut").getMesh().clean();
var sceneObj = new CubicVR.SceneObject({
mesh: astronautMesh,
position:[0,0,0],
rotation:[0,0,0]
});
var rigidObj = new CubicVR.RigidBody(sceneObj, {
type: CubicVR.enums.physics.body.DYNAMIC,
mass: 0.1,
collision: playerObj.collision
});
sceneObj.getInstanceMaterials()[0].color = [1,0,1];
scene.bindSceneObject(sceneObj);
physics.bindRigidBody(rigidObj);
// rigidObj.setAngularFactor(0);
return rigidObj;
};
//----------- SCENE INIT:START -------------
CubicVR.setGlobalAmbient([0.3,0.3,0.4]);
// New scene with our canvas dimensions and default camera with FOV 80
var canvas = engine.graphics.getCanvas();
var scene = new CubicVR.Scene(canvas.width, canvas.height, 80);
// load skybox
scene.setSkyBox(new CubicVR.SkyBox({texture:"../assets/space_skybox.jpg"}));
// set initial camera position and target
scene.camera.setClip(0.1,2000);
// Add a simple directional light
scene.bindLight(new CubicVR.Light({
type: CubicVR.enums.light.type.DIRECTIONAL,
specular: [1, 1, 1],
direction: [0.5, -1, 0.5]
}));
// Create a shadowed area light, map resolution 2048
// designed for shadowing larger areas than spotlights can provide
// it represents a directional light with shadows.
// scene.bindLight(new CubicVR.Light({
// type:CubicVR.enums.light.type.AREA,
// intensity:0.9,
// mapRes:2048, // 4096 ? 8192 ? ;)
// areaCeiling:40,
// areaFloor:-40,
// areaAxis: [-2,-2], // specified in degrees east/west north/south
// distance: 60
// }));
// CubicVR.setSoftShadows(true);
var floorMaterial = new CubicVR.Material({
specular:[0,0,0],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: new CubicVR.Texture("../assets/6583-diffuse.jpg")
}
});
var floorMesh = CubicVR.primitives.box({
size: 1.0,
material: floorMaterial,
uvmapper: {
projectionMode: CubicVR.enums.uv.projection.CUBIC,
scale: [0.05, 0.05, 0.05]
}
}).prepare();
var floorObject = new CubicVR.SceneObject({
mesh: floorMesh,
scale: [100, 0.2, 100],
position: [0, -5, 0]
});
floorObject.shadowCast = false;
// init physics manager
var physics = new CubicVR.ScenePhysics();
physics.setGravity([0,0,0]);
var rigidFloor = new CubicVR.RigidBody(floorObject, {
type: CubicVR.enums.physics.body.STATIC,
collision: {
type: CubicVR.enums.collision.shape.BOX,
size: floorObject.scale
}
});
// physics.bindRigidBody(rigidFloor);
// Add SceneObject containing the mesh to the scene
// scene.bindSceneObject(floorObject);
// Add our scene to the window resize list
CubicVR.addResizeable(scene);
//----------- SCENE INIT:END -------------
//----------- PARTICLES:START -------------
var ps = null;
var maxParticles = 8000;
ps = new CubicVR.ParticleSystem(maxParticles,false,new CubicVR.Texture("../assets/beam.png"),canvas.width,canvas.height,true);
//----------- PARTICLES:END -------------
var objlist = generateObjects();
spawnObjects(scene,physics,objlist);
var player = setupPlayer(scene,physics,objlist[0]);
// uncomment to force player to stay upright.
// player.setAngularFactor(0);
player.activate(true);
player.getSceneObject().visible = true;
//----------- LAYOUT:START -------------
var layout = new CubicVR.Layout({
width:canvas.width,
height:canvas.height
});
var target1 = new CubicVR.View({
width:50,
height:50,
blend:true,
tint:[1.0,0.4,0],
texture:new CubicVR.Texture('../assets/target.png')
});
var target2 = new CubicVR.View({
width:50,
height:50,
blend:true,
tint:[0,0.4,1],
texture:new CubicVR.Texture('../assets/target.png')
});
layout.addSubview(target1);
layout.addSubview(target2);
target1.x = canvas.width/2-50;
target1.y = canvas.height/2-50;
//----------- LAYOUT:END -------------
//----------- MOUSE EVENTS:START -------------
var point1 = null, point2 = null;
var pickDist = 0;
var lastResult = false;
var downPos;
var zoom = 10;
function zoomCamera() {
var vec3 = CubicVR.vec3;
scene.camera.position = vec3.add(scene.camera.target, vec3.multiply(vec3.normalize(vec3.subtract(scene.camera.position, scene.camera.target)), zoom));
}
// initialize a mouse view controller
var mvc = new CubicVR.MouseViewController(canvas, scene.camera);
mvc.setEvents({
mouseMove: function (ctx, mpos, mdelta, keyState) {
if (!ctx.mdown) return;
mvc.orbitView( mdelta );
},
mouseWheel: function (ctx, mpos, wdelta, keyState) {
mvc.zoomView( wdelta );
},
mouseDown: function (ctx, mpos, keyState) {
downPos = mpos;
},
// mouseUp: function(ctx, mpos, keyState) {
// },
mouseUp: function (ctx,mpos,keyState) {
var dx = mpos[0]-downPos[0], dy = mpos[1]-downPos[1];
var maxPixelsMoved = 20; // Maximum number of pixels the cursor may move before it's not considered for ray testing.
if (Math.sqrt(dx*dx+dy*dy)<maxPixelsMoved) {
var rayTo = scene.camera.unProject(mpos[0],mpos[1]);
var rayFrom = scene.camera.getParentedPosition();
rayFrom = CubicVR.vec3.add(rayFrom,CubicVR.vec3.multiply(CubicVR.vec3.normalize(CubicVR.vec3.subtract(rayTo,rayFrom)),1.5));
var result = physics.getRayHit(rayFrom,rayTo,true);
lastResult = !!result;
if (point1) {
point1 = null;
point2 = null;
}
if (result && !point1) {
point1 = result;
}
}
},
keyDown: function(ctx,mpos,keyCode,keyState) {
if (keyCode == kbd.KEY_R) {
if (point1) {
// physics.removeConstraint(pickConstraint);
// pickConstraint = null;
point1 = null;
point2 = null;
}
physics.reset();
return false;
}
},
keyPress: function(ctx,mpos,keyCode,keyState) {
},
keyUp: null
});
//----------- MOUSE EVENTS:END -------------
CubicVR.addResizeable(layout);
function acquireTarget(point,target) {
var sceneObj = point.rigidBody.getSceneObject();
var proj = point.localPosition;
var projT = CubicVR.mat4.vec3_multiply(proj,sceneObj.tMatrix);
var targetLoc = scene.camera.project(projT[0],projT[1],projT[2]);
target.x = targetLoc[0]-target.width/2;
target.y = targetLoc[1]-target.height/2;
}
var kbd = CubicVR.enums.keyboard;
scene.camera.position = [ 0, 1000, 2000 ];
var mainLoop = new CubicVR.MainLoop(function(timer, gl) {
var seconds = timer.getSeconds();
if (!player.isActive()) {
player.activate();
}
var angV = player.getAngularVelocity();
angV = CubicVR.vec3.subtract(angV,CubicVR.vec3.multiply(angV,timer.getLastUpdateSeconds()*5));
player.setAngularVelocity(angV);
if (mvc.isKeyPressed(kbd.KEY_W)) {
player.applyImpulse(CubicVR.vec3.multiply(CubicVR.vec3.normalize(scene.camera.unProject(scene.camera.width/2,scene.camera.height/2)),0.001));
}
if (mvc.isKeyPressed(kbd.KEY_S)) {
player.applyImpulse(CubicVR.vec3.multiply(CubicVR.vec3.normalize(scene.camera.unProject(scene.camera.width/2,scene.camera.height/2)),-0.001));
}
if (point1) {
var tetherVec = CubicVR.vec3.subtract(CubicVR.mat4.vec3_multiply(point1.localPosition,point1.rigidBody.getSceneObject().tMatrix),player.getSceneObject().position);
var tetherDist = CubicVR.vec3.length(tetherVec);
var tetherDir = CubicVR.vec3.normalize(tetherVec);
var tetherImpulse = CubicVR.vec3.multiply(tetherDir,0.03);
player.applyImpulse(tetherImpulse);
var linV = player.getLinearVelocity();
if (tetherDist < 6) {
linV = CubicVR.vec3.subtract(linV,CubicVR.vec3.multiply(linV,timer.getLastUpdateSeconds()*10.0));
player.setLinearVelocity(linV);
} else {
// nudge the current linear velocity towards the target to prevent orbital swing
linV = CubicVR.vec3.add(linV,CubicVR.vec3.multiply(linV,timer.getLastUpdateSeconds()*-0.4));
player.setLinearVelocity(linV);
}
}
physics.stepSimulation(timer.getLastUpdateSeconds());
var playerPosition = player.getSceneObject().position,
playerLastPosition = player.getSceneObject().lposition,
playerSceneObj = player.getSceneObject(),
camPos = scene.camera.position,
dt = timer.getLastUpdateSeconds();
scene.camera.target = playerPosition;
scene.camera.trackTarget( scene.camera.target, 0.1, 2 );
scene.updateShadows();
scene.render();
// Draw grappling beam
if (point1) {
var nominalBeamStep = 0.2;
var beamVector = CubicVR.vec3.subtract(CubicVR.mat4.vec3_multiply(point1.localPosition,point1.rigidBody.getSceneObject().tMatrix),playerPosition);
var beamLength = CubicVR.vec3.length(beamVector);
var numPoints = Math.floor(beamLength/nominalBeamStep);
if (numPoints > maxParticles) numPoints = maxParticles;
ps.numParticles = numPoints;
if (numPoints != 0) {
var linStep = CubicVR.vec3.multiply(beamVector,1.0/numPoints);
var pos = playerPosition.slice(0);
for (var i = 0, iMax = numPoints*3; i < iMax; i+=3) {
ps.arPoints[i] = pos[0];
ps.arPoints[i+1] = pos[1];
ps.arPoints[i+2] = pos[2];
pos[0]+=linStep[0];
pos[1]+=linStep[1];
pos[2]+=linStep[2];
}
ps.updatePoints();
ps.draw(scene.camera.mvMatrix, scene.camera.pMatrix);
}
}
// end grappling line
if (point1) {
acquireTarget(point1,target1);
} else {
target1.x = -target1.width;
target1.y = -target1.height;
}
if (point2) {
acquireTarget(point2,target2);
} else {
target2.x = -target2.width;
target2.y = -target2.height;
}
layout.render();
});
engine.sound.Track.load({
url: "../assets/music/perfect-blind-ethernion-ii.ogg",
callback: function( track ) {
engine.sound.music.add( 'bg-music', track );
engine.sound.music.play( 'bg-music' );
}
});
function GameTimer () {
}
GameTimer.prototype = {
intervalId: null,
secondsLeft: 90,
start: function gameTimerStart() {
this.intervalId = setInterval(this.update.bind(this), 1000);
},
stop: function gameTimerStop() {
clearInterval(this.intervalId);
},
update: function gameTimerUpdate() {
this.secondsLeft -= 1;
document.getElementById("secondsLeft").textContent = this.secondsLeft;
if (!this.secondsLeft) {
this.stop();
engine.tasker.terminate();
mainLoop.setPaused(true);
CubicVR.setMainLoop(null);
}
}
};
var gameTimer = new GameTimer();
// Run the game.
this.run = function() {
gameTimer.start();
engine.run();
};
};
document.addEventListener( 'DOMContentLoaded', function( event ) {
paladin.create( { debug: true },
function( engineInstance ) {
var game = new Game( { engine: engineInstance } );
console.log( "Starting game" );
game.run();
}
);
/*
paladin.create( {debug: true },
function( engineInstance ) {
var engine = engineInstance;
var CubicVR = engine.graphics.CubicVR;
var physics = new CubicVR.ScenePhysics();
console.log( CubicVR.CollisionMap );
}
);
*/
}, false );
})();