var Game = function(opt) { var scene; var lock; var SCREEN_HEIGHT; var SCREEN_HEIGHT; var camera; var renderer; var mazes= {}; var camera_decal_x = 10; var camera_decal_y = 200; var camera_decal_z = 100; if(opt.debug_level>1) { camera_decal_x = 0; camera_decal_y = 300; camera_decal_z = 50; } var current_item_id = 0; // "Main perso view" //var camera_decal_x = -10; //var camera_decal_y = 10; //var camera_decal_z = -10; var clock = new THREE.Clock(); //var stats = new Stats(); //stats.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom //document.body.appendChild( stats.dom ); var start_num_maze = 7; var maze_increment = 1; this.load = function() { var promises = []; this.assets = new Assets(); return this.assets.load(); }; this.init = function() { var self=this; this.load().then(function() { self.init_loaded(); }, function(e) { console.error('error loading ',e); alert('Error loading some assets...'); }); }; this.init_loaded= function() { var self=this; this.opt = opt; this.static_obstacles = []; this.moving_obstacles = []; this.moving_and_static_obstacles = []; this.obstacles_with_player = []; this.collision_callbacks = []; this.scene = new THREE.Scene(); // Build basic structures var width = 10000; var height = 10000; var click_ground = new THREE.PlaneGeometry(width, height); var material = new THREE.LineBasicMaterial( { color: 0x555555, opacity:0, transparent:true } ); // Set and add the click_ground this.click_ground = new THREE.Mesh(click_ground, material); this.click_ground.name='click_ground'; this.click_ground.rotation.x = -Math.PI / 2; this.scene.add(this.click_ground); this.camera = new THREE.PerspectiveCamera(45, 1, 0.1, 10000); this.scene.add(this.camera); this.ambient_light = new THREE.PointLight(); this.ambient_light.intensity=opt.debug_level>1 ? 2.0 : 1.0; this.ambient_light.position.set(100, 200, 80); this.scene.add(this.ambient_light); //this.scene.fog = new THREE.FogExp2( 0x333355, 0.0055 ); this.renderer = new THREE.WebGLRenderer({ antialias: true}); this.renderer.setClearColor(opt.debug_level>1 ? 0x000000 : 0x000000, 1); this.renderer.shadowMapEnabled=true; this.renderer.shadowMapSoft=true; window.addEventListener( 'resize', onWindowResize, false ); function onWindowResize(){ self.camera.aspect = window.innerWidth / window.innerHeight; self.camera.updateProjectionMatrix(); self.renderer.setSize( window.innerWidth, window.innerHeight ); } this.container = opt.root; this.setAspect(); this.container.appendChild(this.renderer.domElement); this.start(); this.render(); this.updateCollisionsCache(); }; this.setAspect= function() { var w = this.container.offsetWidth; // Fit the initial visible area's height h = this.container.offsetHeight; // Update the renderer and the camera this.renderer.setSize(w, h); this.camera.aspect = w / h; this.camera.updateProjectionMatrix(); }; this.setFocus = function(object) { this.ambient_light.position.set(100+object.position.x, 200, 80+object.position.z); this.camera.position.x = object.position.x + camera_decal_x; this.camera.position.y = object.position.y + camera_decal_y; this.camera.position.z = object.position.z + camera_decal_z; this.camera.lookAt(object.position); }; this.start = function() { var self=this; this.direction=0; this.focus_perso = self.add_character({ x: 10, y: 0, z: 10, moveable: true, ai: false, game: self }); this.focus_perso.name='main character'; // Create start path var path = new Path(this, {level: 1, direction: this.direction, x: 0, z: 0}); path.build(); var pos = path.get_start_pos(); var angle = Math.radians(30); pos.x += Math.cos(angle) * 20; pos.z += Math.sin(angle) * 20; this.current_item = path; path.enter(); // Look at player this.resetCamera(); }; this.add_character= function(params) { return new Perso(this, params); }, this.add_key= function(params) { return new Key(this, params); }, this.reload = function() { this.current_item.reload(); var pos = this.current_item.get_start_pos(); game.focus_perso.reload(pos); }, // Camera refresh animation this.update = function(delta) { if(this.zoomOut) { } else if(this.zoominDestination) { this.zoominAngle-= 0.01; this.current_radius = Math.min(this.current_radius+1, game.opt.door_size); this.current_camera_decal_x *=0.96; this.current_camera_decal_y *=0.96; this.current_camera_decal_z *=0.96; this.camera.position.x = game.focus_perso.container.position.x + this.current_camera_decal_x + this.current_radius * Math.cos( this.zoominAngle); this.camera.position.y *= 0.99; this.camera.position.z = game.focus_perso.container.position.z + this.current_camera_decal_z + this.current_radius * Math.sin( this.zoominAngle); this.camera.lookAt(game.focus_perso.container.position); if(this.camera.position.y0.070 && game.opt.debug_level>10) { console.warn('SLOW RENDERING DELTA: ',delta); delta=0.070; } //delta=0.030; this.update(delta); this.focus_perso.update(delta); this.current_item.update(delta); this.updating=0; } requestAnimationFrame(this.render_fct); //stats.end(); }; this.render_fct = this.render.bind(this); this.getStaticObstacles = function() { return this.static_obstacles; }; this.getMovingObstacles = function() { return this.moving_obstacles; }; this.getMovingAndStaticObstacles = function() { return this.moving_and_static_obstacles; }; this.getHovers = function() { return this.hovers; }; this.getHoversWithClickGround = function() { return this.hovers_with_ground; }; this.getObstaclesWithPlayer = function() { return this.obstacles_with_player; }; this.getCollisionCallbacks = function() { return this.collision_callbacks; } this.generateCollisionCallbacks = function() { return this.current_item.getCollisionCallbacks(); }; this.callCollisionCallbacks = function(perso,collisions) { collisions.forEach(function (obj) { obj.callback(); }); }; this.updateCollisionsCache = function() { this.hovers=this.current_item.getHovers(); this.static_obstacles = this.current_item.getStaticObstacles(); this.moving_obstacles = this.current_item.getMovingObstacles(); this.obstacles_with_player = [].concat(this.static_obstacles, this.focus_perso.container_mesh); this.moving_and_static_obstacles = [].concat(this.static_obstacles).concat(this.moving_obstacles); this.hovers_with_ground= this.hovers.concat([this.click_ground]); this.collision_callbacks = this.generateCollisionCallbacks(); }; this.getNewId = function() { return 'item_'+(++current_item_id); }; this.zoomInCircle = function(callback) { this.zoominAngle = 0; this.zoominDestination=50; this.zoominCallback=callback; this.current_radius = 0; this.current_camera_decal_x = camera_decal_x; this.current_camera_decal_y = camera_decal_y; this.current_camera_decal_z = camera_decal_z; }; this.resetCamera = function() { this.camera.position.x = game.focus_perso.container.position.x + camera_decal_x; this.camera.position.y = game.focus_perso.container.position.y + camera_decal_y; this.camera.position.z = game.focus_perso.container.position.z + camera_decal_z; this.camera.lookAt(game.focus_perso.container.position); } this.setItem = function(item) { if(this.current_item.id!==item.id) { this.current_item.leave(); this.current_item=item; this.current_item.enter(); } }; };