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
|
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Omni;
(function (Omni) {
/**
* Base game class, will handle the game loop, rendering,
*/
var Game = (function () {
/**
*
*/
function Game() {
var _this = this;
this.ticks = 0;
this.delta = 0;
this.lastFrame = 0;
this.timestep = 1000 / 60;
this.maxFPS = 60;
/**
*
*/
this.start = function () {
console.debug("Game: start");
//wait around till block_loader is done loading assets
if (!_this.blockLoader.isLoaded()) {
setTimeout(_this.start, 10);
return;
}
//build the level here
_this.cube.init(0);
_this.unpause();
};
/**
* When leaving the page.
*/
this.stop = function () {
_this.pause();
console.debug("Game: stop");
_this.blockLoader.dispose();
_this.renderer.dispose();
};
this.onWindowResize = function () {
_this.camera.aspect = window.innerWidth / window.innerHeight;
_this.camera.updateProjectionMatrix();
_this.renderer.setSize(window.innerWidth, window.innerHeight);
};
this.onVisibilityChange = function () {
console.debug("visibility change");
};
this.renderer = new THREE.WebGLRenderer({
antialias: true
});
this.renderer.setClearColor(0xbababa);
this.renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(this.renderer.domElement);
window.addEventListener("resize", this.onWindowResize, false);
window.addEventListener("visibilitychange", this.onVisibilityChange, false);
this.camera = new THREE.PerspectiveCamera(Game.CAMERA_FOV, window.innerWidth / window.innerHeight, Game.CAMERA_NEAR, Game.CAMERA_FAR);
this.blockLoader = new Omni.BlockLoader(Omni.BlockLoader.BLOCKS);
}
/**
*
*/
Game.prototype.init = function () {
console.debug("Game: init");
this.blockLoader.load();
};
/**
*
*/
Game.prototype.unpause = function () {
console.debug("Game: unpause");
this.keepRunning = true;
this.run();
};
/**
*
* @param delta
*/
Game.prototype.tick = function (delta) {
this.ticks++;
this.cube.tick(delta);
};
/**
*
*/
Game.prototype.render = function () {
this.renderer.render(this.cube, this.camera);
};
/**
*
* @param timestamp
*/
Game.prototype.run = function (timestamp) {
var _this = this;
if (!timestamp) {
timestamp = performance.now();
}
if (timestamp < this.lastFrame + (1000 / this.maxFPS)) {
if (this.keepRunning) {
requestAnimationFrame(function () { return _this.run(); });
}
return;
}
this.delta += timestamp - this.lastFrame;
this.lastFrame = timestamp;
var numUpdateSteps = 0;
while (this.delta >= this.timestep) {
this.tick(this.timestep);
this.delta -= this.timestep;
if (++numUpdateSteps >= 240) {
// panic here, reset delta
this.delta = 0;
break;
}
}
this.render();
if (this.keepRunning) {
requestAnimationFrame(function (time) { return _this.run(time); });
}
};
/**
* When releasing pointer lock/on menu. Menu is HTML based.
*/
Game.prototype.pause = function () {
console.debug("Game: pause");
this.keepRunning = false;
};
Game.CAMERA_FOV = 55;
Game.CAMERA_NEAR = 1;
Game.CAMERA_FAR = 1000;
return Game;
}());
Omni.Game = Game;
})(Omni || (Omni = {}));
var game;
var locker;
window.onload = function () {
game = new Omni.Game();
locker = new Omni.PointerLocker(game.start, game.stop);
game.init();
locker.enable();
};
window.onunload = function () {
locker.disable();
game.stop();
};
var Omni;
(function (Omni) {
var Keyboard = (function () {
function Keyboard() {
}
return Keyboard;
}());
Omni.Keyboard = Keyboard;
var Mouse = (function () {
function Mouse() {
}
return Mouse;
}());
Omni.Mouse = Mouse;
})(Omni || (Omni = {}));
var Omni;
(function (Omni) {
var Block = (function () {
//will take the correct state as params?
function Block(geometry, materials) {
this.geometry = geometry;
this.materials = materials;
this.mesh = new Physijs.BoxMesh(geometry, materials[0], 0);
}
Block.prototype.getState = function () {
return this.state;
};
Block.prototype.setState = function (state) {
this.state = state;
};
Block.prototype.setPosition = function (pos) {
this.mesh.position.copy(pos);
};
Block.prototype.getPosition = function () {
return this.mesh.position.clone();
};
Block.prototype.getObject = function () {
return this.mesh;
};
Block.mesh_file = "";
return Block;
}());
Omni.Block = Block;
/**
* Plain block without any puzzles.
*/
var PlainBlock = (function (_super) {
__extends(PlainBlock, _super);
function PlainBlock(loader) {
_super.call(this, loader.getMesh(PlainBlock.mesh_file), loader.getMaterials(PlainBlock.mesh_file));
}
PlainBlock.mesh_file = "plainBlock.json";
PlainBlock.states = [];
return PlainBlock;
}(Block));
Omni.PlainBlock = PlainBlock;
/**
* Button block with 2 states
*/
var ButtonBlock = (function (_super) {
__extends(ButtonBlock, _super);
function ButtonBlock(loader) {
_super.call(this, loader.getMesh(ButtonBlock.mesh_file), loader.getMaterials(ButtonBlock.mesh_file));
}
//need to make good design choices here, with the handling of states..
ButtonBlock.mesh_file = "buttonBlock.json";
ButtonBlock.states = [0, 1];
return ButtonBlock;
}(Block));
Omni.ButtonBlock = ButtonBlock;
/**
* Lever block with 4 positions/states
*/
var LeverBlock = (function (_super) {
__extends(LeverBlock, _super);
function LeverBlock(loader) {
_super.call(this, loader.getMesh(LeverBlock.mesh_file), loader.getMaterials(LeverBlock.mesh_file));
}
LeverBlock.mesh_file = "leverBlock.json";
LeverBlock.states = [0, 1, 2, 3];
return LeverBlock;
}(Block));
Omni.LeverBlock = LeverBlock;
/**
*
*/
var AzimuthBlock = (function (_super) {
__extends(AzimuthBlock, _super);
function AzimuthBlock() {
_super.apply(this, arguments);
}
AzimuthBlock.mesh_file = "azimuthBlock.json";
AzimuthBlock.states = [0, 1, 2, 3];
return AzimuthBlock;
}(Block));
Omni.AzimuthBlock = AzimuthBlock;
/**
*
*/
var PullBlock = (function (_super) {
__extends(PullBlock, _super);
function PullBlock() {
_super.apply(this, arguments);
}
PullBlock.mesh_file = "pullBlock.json";
PullBlock.states = [];
return PullBlock;
}(Block));
Omni.PullBlock = PullBlock;
var BlockLoader = (function () {
/**
* @param files what files to load()
*/
function BlockLoader(files) {
var _this = this;
this.files = files;
this.manager = new THREE.LoadingManager();
this.loader = new THREE.JSONLoader(this.manager);
this.geometries = {};
this.materials = {};
this.loaded = false;
this.manager.onLoad = function () {
_this.loaded = true;
console.debug("BlockLoader: loaded");
};
}
/**
* Load all the geometries and materials from mesh_files
*/
BlockLoader.prototype.load = function () {
var _this = this;
console.debug("BlockLoader: loading...");
this.files.forEach(function (file) {
_this.loadOne(file);
});
};
/**
* Load additional geometries and mats
* @param file what file to load. Will be prefixed with URL_PREFIX
*/
BlockLoader.prototype.loadFile = function (file) {
if (this.files.indexOf(file) == -1) {
this.files.push(file);
this.loadOne(file);
}
};
BlockLoader.prototype.loadOne = function (file) {
var _this = this;
this.loader.load(BlockLoader.URL_PREFIX + file, function (geometry, materials) {
_this.geometries[file] = geometry;
_this.materials[file] = materials;
});
};
/**
* Dispose of all the geometries and mats
*/
BlockLoader.prototype.dispose = function () {
var _this = this;
console.debug("BlockLoader: disposing...");
if (this.loaded) {
this.files.forEach(function (file) {
_this.geometries[file].dispose();
_this.materials[file].forEach(function (material) {
material.dispose();
});
});
this.loaded = false;
}
};
BlockLoader.prototype.isLoaded = function () {
return this.loaded;
};
BlockLoader.prototype.meshLoaded = function (file) {
return this.geometries[file] !== undefined;
};
BlockLoader.prototype.matsLoaded = function (file) {
return this.materials[file] !== undefined;
};
BlockLoader.prototype.getMesh = function (file) {
return this.geometries[file];
};
BlockLoader.prototype.getMaterials = function (file) {
return this.materials[file];
};
BlockLoader.URL_PREFIX = "json/";
BlockLoader.BLOCKS = [
PlainBlock.mesh_file,
LeverBlock.mesh_file,
];
return BlockLoader;
}());
Omni.BlockLoader = BlockLoader;
})(Omni || (Omni = {}));
var Omni;
(function (Omni) {
var Puzzle = (function () {
//contains elements,
/**
* takes elements, and their correct state.
*/
function Puzzle() {
this.elements = [];
}
/**
* Whether this puzzle is currently in the correct state
*/
Puzzle.prototype.isCorrect = function () {
//true if states of all elements match the correct state-> correct state can be more.
return false;
};
return Puzzle;
}());
Omni.Puzzle = Puzzle;
})(Omni || (Omni = {}));
var Omni;
(function (Omni) {
var Cube = (function (_super) {
__extends(Cube, _super);
function Cube(loader) {
_super.call(this);
this.loader = loader;
}
Cube.prototype.init = function (level) {
//cube size, 4*4*4
//generate corners first
var half = Cube.SIZE / 2;
var corners = [
new THREE.Vector3(-1, -1, -1),
new THREE.Vector3(1, -1, -1),
new THREE.Vector3(-1, -1, 1),
new THREE.Vector3(1, -1, 1),
new THREE.Vector3(-1, 1, -1),
new THREE.Vector3(1, 1, -1),
new THREE.Vector3(-1, 1, 1),
new THREE.Vector3(1, 1, 1)
];
for (var _i = 0, corners_1 = corners; _i < corners_1.length; _i++) {
var corner = corners_1[_i];
var vertex = corner.clone().multiplyScalar(half);
var block = new Omni.PlainBlock(this.loader);
block.setPosition(vertex.add(corner.clone().multiplyScalar(0.5)));
this.add(block.getObject());
this.blocks.push(block);
}
//then edges
//then fill in the rest
};
Cube.prototype.tick = function (delta) {
this.simulate(delta);
};
Cube.SIZE = 4;
return Cube;
}(Physijs.Scene));
Omni.Cube = Cube;
})(Omni || (Omni = {}));
var Omni;
(function (Omni) {
var PointerLocker = (function () {
function PointerLocker(onGain, onLose) {
var _this = this;
this.onGain = onGain;
this.onLose = onLose;
this.hasLock = false;
this.blocker = document.getElementById("block");
this.instructions = document.getElementById("instructions");
this.onChange = function (event) {
var element = document.body;
var doc = document;
if (doc.pointerLockElement === element || doc.mozPointerLockElement === element || doc.webkitPointerLockElement === element) {
//gained
_this.hasLock = true;
_this.hide();
_this.onGain();
}
else {
//lost
_this.hasLock = false;
_this.show();
_this.onLose();
}
};
this.onError = function (event) {
};
this.onClick = function (event) {
var element = document.body;
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
_this.instructions.style.display = "none";
element.requestPointerLock();
};
}
PointerLocker.prototype.enable = function () {
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElsement' in document || 'webkitPointerLockElement' in document;
if (!havePointerLock) {
return;
}
document.addEventListener("pointerlockchange", this.onChange, false);
document.addEventListener("mozpointerlockchange", this.onChange, false);
document.addEventListener("webkitpointerlockchange", this.onChange, false);
document.addEventListener("pointerlockerror", this.onError, false);
document.addEventListener("mozpointerlockerror", this.onError, false);
document.addEventListener("webkitpointerlockerror", this.onError, false);
this.blocker.addEventListener("click", this.onClick, false);
};
PointerLocker.prototype.disable = function () {
document.removeEventListener("pointerlockchange", this.onChange, false);
document.removeEventListener("mozpointerlockchange", this.onChange, false);
document.removeEventListener("webkitpointerlockchange", this.onChange, false);
document.removeEventListener("pointerlockerror", this.onError, false);
document.removeEventListener("mozpointerlockerror", this.onError, false);
document.removeEventListener("webkitpointerlockerror", this.onError, false);
this.blocker.removeEventListener("click", this.onClick, false);
};
PointerLocker.prototype.hide = function () {
this.blocker.style.display = "none";
};
PointerLocker.prototype.show = function () {
this.blocker.style.display = '-webkit-box';
this.blocker.style.display = '-moz-box';
this.blocker.style.display = 'box';
this.instructions.style.display = "";
};
return PointerLocker;
}());
Omni.PointerLocker = PointerLocker;
})(Omni || (Omni = {}));
/// <reference path="../ts/three.d.ts" />
/// <reference path="../ts/physijs.d.ts" />
/// <reference path="game.ts" />
/// <reference path="interface.ts" />
/// <reference path="input.ts" />
/// <reference path="block.ts" />
/// <reference path="puzzle.ts" />
/// <reference path="cube.ts" />
/// <reference path="pointerlock.ts" />
//# sourceMappingURL=omni.js.map
|