var Assets = function() { this.jsons = []; this.load_textures = []; this.textures = []; this.add=function(file, name) { this.jsons.push( { file: file, name: name}); }; this.load = function() { this.add('js/meshes/wall.js','wall'); this.add('js/meshes/wall1.js','wall1'); this.add('js/meshes/door.js','door'); this.add('js/meshes/door1.js','door1'); this.add('js/meshes/dooropen.js','dooropen'); this.add('js/meshes/dooropen1.js','dooropen1'); this.add('blender/pingu_with_armature.json','perso'); this.add('blender/ennemy.json','ennemy'); this.add('blender/key.json','key'); this.add('blender/chest.json','chest'); this.add_texture('textures/path_wall.jpg','path_wall'); this.add_texture('textures/path_wall_bump.jpg','path_wall_bump'); this.add_texture('textures/wall.jpg','cell_wall'); this.add_texture('textures/path_wall_bump.jpg','cell_wall_bump'); this.add_texture('textures/path_floor.jpg','path_floor'); this.add_texture('textures/path_floor_bump.jpg','path_floor_bump'); this.add_texture('textures/path_floor.jpg','maze_floor'); this.add_texture('textures/path_floor_bump.jpg','maze_floor_bump'); return this._load(); }; this.add_texture=function(file, name) { this.load_textures.push( { file: file, name: name}); }; this._load = function() { var self=this; var promises=[]; this.jsons.forEach(function(json) { promises.push(new Promise(function(ok, reject) { var loader = new THREE.JSONLoader(); loader.load(json.file, function(geometry, mat) { self[json.name+'_geo'] = geometry; self[json.name+'_mat'] = mat; ok(); }); })); }); this.load_textures.forEach(function(json) { promises.push(new Promise(function(ok, reject) { var textureLoader = new THREE.TextureLoader(); textureLoader.load(json.file, function(tex) { self[json.name+'_texture']= tex; self[json.name+'_texture'].wrapS = self[json.name+'_texture'].wrapT = THREE.RepeatWrapping; ok(); }); })); }); promises.push(new Promise(function(ok, reject) { self.step_sound = new Audio('sounds/step.mp3'); self.step_sound.loop=true; self.step_sound.volume=1; self.step_sound.load(); ok(); })); promises.push(new Promise(function(ok, reject) { self.attack_sound = new Audio('sounds/attack.mp3'); self.attack_sound.volume=0.6; self.attack_sound.loop=false; self.attack_sound.load(); ok(); })); promises.push(new Promise(function(ok, reject) { self.chest_open_sound = new Audio('sounds/chest_open.mp3'); self.chest_open_sound.volume=0.3; self.chest_open_sound.loop=false; self.chest_open_sound.load(); ok(); })); promises.push(new Promise(function(ok, reject) { self.ennemy_hit_sound = new Audio('sounds/ennemy_hit.mp3'); self.ennemy_hit_sound.volume=0.3; self.ennemy_hit_sound.loop=false; self.ennemy_hit_sound.load(); ok(); })); promises.push(new Promise(function(ok, reject) { self.ennemy_attack_sound = new Audio('sounds/ennemy_attack.mp3'); self.ennemy_attack_sound.volume=0.6; self.ennemy_attack_sound.loop=false; self.ennemy_attack_sound.load(); ok(); })); promises.push(new Promise(function(ok, reject) { self.die_sound = new Audio('sounds/die.mp3'); self.die_sound.volume=0.4; self.die_sound.loop=false; self.die_sound.load(); ok(); })); promises.push(new Promise(function(ok, reject) { self.ennemy_die_sound = new Audio('sounds/ennemy_die.mp3'); self.ennemy_die_sound.volume=0.4; self.ennemy_die_sound.loop=false; self.ennemy_die_sound.load(); ok(); })); return Promise.all(promises); } this.getTexture = function(texture, bumpTexture) { if(!this.textures[texture+'_'+bumpTexture]) { console.log('creating texture ',texture+' / '+bumpTexture); this.textures[texture+'_'+bumpTexture] = new THREE.MeshPhongMaterial({ bumpScale:1, map: this[texture+'_texture'], bumpMap: this[bumpTexture+'_texture'] }); } return this.textures[texture+'_'+bumpTexture]; } };