Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/old_sites/threejs/12_game/js/game.js

(Deskargatu)
var Game = function(opt)
{
    var scene;
    var lock;
    var SCREEN_HEIGHT;
    var SCREEN_HEIGHT;
    var camera;
    var renderer;
    var mazes= {};

    var camera_decal_x = 0;
    var camera_decal_y = 0;
    var camera_decal_z = 0;
    var clock  = new THREE.Clock();

    var start_num_maze = 10;
    var maze_increment = 5;


    this.load = function()
    {
        var promises = [];

        this.perso = new Perso(this);
        promises.push(this.perso.load());
        //promises.push(this.maze.load());

        return Promise.all(promises);
    };

    this.init= function()
    {
        var self=this;
        this.opt = opt;
        console.log('call init',this);

        this.obstacles = [];
        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.receiveShadow=true;
        this.click_ground.castShadow=true;
        this.click_ground.rotation.x = -Math.PI / 2;

        this.scene.add(this.click_ground);

        return new Promise(function(ok, reject)
        {
            self.load().then(function()
            {
                self.camera = new THREE.PerspectiveCamera(45, 1, 0.1, 10000);
                self.scene.add(self.camera);

                self.scene.fog = new THREE.FogExp2( 0x333355, 0.0055 );

                self.ambient_light = new THREE.PointLight();
                self.ambient_light.intensity=0.8;
                self.ambient_light.position.set(100, 200, 80);
                self.scene.add(self.ambient_light);

                self.light = new THREE.AmbientLight(0x404040);
                self.scene.add(self.light);

                self.renderer = new THREE.WebGLRenderer();
                //self.renderer.shadowMapEnabled = 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 );
                }
                self.container = opt.root;

                self.setAspect();
                self.container.appendChild(self.renderer.domElement);
                ok();
            }, function()
            {
                console.error('error loading game.js');
                reject();
            });
        });
    };

    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.setCameraPos = function(x,y,z)
    {
        camera_decal_x = x;
        camera_decal_y = y;
        camera_decal_z = z;
    };

    this.setFocus = function(object)
    {
        this.camera.position.set(object.position.x + camera_decal_x, object.position.y + camera_decal_y, object.position.z + camera_decal_z);
        this.camera.lookAt(object.position);
    };

    this.start = function()
    {
        var self=this;

        this.direction=0;
        // Create start path
        var path = new Path(this, {direction: this.direction, x: 0, z: 0});
        path.build();

        this.start_path = path;
        var pos = path.get_start_pos();
        self.add_player({
            x: pos.x,
            y: 0,
            z: pos.z,
            game: self
        });

        this.enterPath(path);
    };

    this.add_player= function(params)
    {
        var self=this;
        this.perso.build(params, this);
        this.perso.moveable();
    },

    this.render = function()
    {
	    requestAnimationFrame(this.render_fct);
        var delta = clock.getDelta();
        delta=0.020;
        if(this.perso.mixer)
        {
            this.perso.update(delta);
            this.setFocus(this.perso.container);
        }
        this.renderer.render(this.scene, this.camera);
    };
    this.render_fct = this.render.bind(this);

    this.getObstacles = function()
    {
        return this.obstacles;
    };

    this.getCollisionCallbacks = function()
    {
        if(this.current_maze)
        {
            return this.current_maze.getCollisionCallbacks();
        }
        else
        {
            var coll = [];
            for (var i in mazes)
            {
                coll = coll.concat(mazes[i].getOutsideCollisionCallbacks());
            }
            return coll;
        }
    };

    this.collisionCallbacks = function(perso,collisions)
    {
        if(this.current_maze)
        {
            return this.current_maze.collisionCallbacks(perso, collisions);
        }
        else
        {
            if(collisions.length>0)
            {
                if(collisions[0].mazeid)
                {
                    this.inMaze(collisions[0].mazeid);
                }
            }
        }
    };

    this.enterPath = function(path)
    {
        var self=this;
        this.current_path=path;
        this.obstacles = this.current_path.getObstacles();
        if(!path.next_maze)
        {
            var pos = path.get_end_pos();
            start_num_maze+= maze_increment;
            var maze = new Maze(this, { maze_num: start_num_maze, x: pos.x, z: pos.z });
            path.next_maze = maze;
            maze.start_path = path;
            mazes[maze.id] = maze;
            maze.build().then(function()
            {
                var pos = maze.doors.get_end_pos();
                console.log('create end path ',pos);
                var path = new Path(self, {direction: this.direction, x: pos.x, z: pos.z});
                maze.end_path = path;
                self.end_path = path;
                path.build();
            });
        }
    }

    this.leaveMaze = function(id, path)
    {
        var self=this;
        console.log('leaving maze',id , path);
        this.current_maze = null;
        this.current_path = path;
        this.obstacles=[];
        this.collision_callbacks = [];

        if(path){
            this.enterPath(path);
        }
    };

    this.inMaze = function(id)
    {
        console.log('in maze ',id);
        this.current_path = null;
        this.current_maze = mazes[id];
        this.obstacles = this.current_maze.getObstacles();
        this.collision_callbacks = this.current_maze.getCollisionCallbacks();
    }
};