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 = {}));
///
///
///
///
///
///
///
///
///
//# sourceMappingURL=omni.js.map