module Omni { export class Block { private state:number; private mesh:Physijs.BoxMesh; static states:number[]; static mesh_file:string = ""; //will take the correct state as params? constructor(private geometry:THREE.Geometry, private materials:THREE.Material[]) { this.mesh = new Physijs.BoxMesh(geometry, materials[0], 0); } getState():number { return this.state; } setState(state:number):void { this.state = state; } setPosition(pos:THREE.Vector3):void { this.mesh.position.copy(pos); } getPosition():THREE.Vector3 { return this.mesh.position.clone(); } getObject():THREE.Object3D { return this.mesh; } } /** * Plain block without any puzzles. */ export class PlainBlock extends Block { static mesh_file = "plainBlock.json"; static states:number[] = []; constructor(loader:BlockLoader) { super(loader.getMesh(PlainBlock.mesh_file), loader.getMaterials(PlainBlock.mesh_file)); } } /** * Button block with 2 states */ export class ButtonBlock extends Block { //need to make good design choices here, with the handling of states.. static mesh_file = "buttonBlock.json"; static states:number[] = [0, 1]; constructor(loader:BlockLoader) { super(loader.getMesh(ButtonBlock.mesh_file), loader.getMaterials(ButtonBlock.mesh_file)); } } /** * Lever block with 4 positions/states */ export class LeverBlock extends Block { static mesh_file = "leverBlock.json"; static states:number[] = [0, 1, 2, 3]; constructor(loader:BlockLoader) { super(loader.getMesh(LeverBlock.mesh_file), loader.getMaterials(LeverBlock.mesh_file)); } } /** * */ export class AzimuthBlock extends Block { static mesh_file = "azimuthBlock.json"; static states:number[] = [0, 1, 2, 3]; } /** * */ export class PullBlock extends Block { static mesh_file = "pullBlock.json"; static states:number[] = []; } export class BlockLoader { private manager:THREE.LoadingManager = new THREE.LoadingManager(); private loader:THREE.JSONLoader = new THREE.JSONLoader(this.manager); private geometries:{[file_name:string]:THREE.Geometry} = {}; private materials:{[file_name:string]:THREE.Material[]} = {}; private loaded:boolean = false; static URL_PREFIX:string = "json/"; static BLOCKS:string[] = [ PlainBlock.mesh_file, LeverBlock.mesh_file, //ButtonBlock.mesh_file, //AzimuthBlock.mesh_file, //PullBlock.mesh_file, ]; /** * @param files what files to load() */ constructor(private files:string[]) { this.manager.onLoad = () => { this.loaded = true; console.debug("BlockLoader: loaded"); }; } /** * Load all the geometries and materials from mesh_files */ load():void { console.debug("BlockLoader: loading..."); this.files.forEach((file) => { this.loadOne(file); }); } /** * Load additional geometries and mats * @param file what file to load. Will be prefixed with URL_PREFIX */ loadFile(file:string):void { if (this.files.indexOf(file) == -1) { this.files.push(file); this.loadOne(file); } } private loadOne(file:string) { this.loader.load(BlockLoader.URL_PREFIX + file, (geometry, materials) => { this.geometries[file] = geometry; this.materials[file] = materials; }); } /** * Dispose of all the geometries and mats */ dispose():void { console.debug("BlockLoader: disposing..."); if (this.loaded) { this.files.forEach((file) => { this.geometries[file].dispose(); this.materials[file].forEach((material) => { material.dispose(); }); }); this.loaded = false; } } isLoaded():boolean { return this.loaded; } meshLoaded(file:string) { return this.geometries[file] !== undefined; } matsLoaded(file:string) { return this.materials[file] !== undefined; } getMesh(file:string):THREE.Geometry { return this.geometries[file]; } getMaterials(file:string):THREE.Material[] { return this.materials[file]; } } }