Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/old_sites/threejs/9_world/js/game.js

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

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

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

        this.perso = new Perso();
        promises.push(this.perso.load());
        promises.push(this.level.load(opt));

        return Promise.all(promises);
    };

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

        this.scene = new THREE.Scene();

        this.level = new Level(this.scene,{
            width: opt.game_width,
            height:opt.game_height,
            tall: opt.door_size,

            door_size: opt.door_size,
            maze_num: opt.maze_num
        });

        return new Promise(function(ok, reject)
        {
            self.load(opt).then(function()
            {
                self.camera = new THREE.PerspectiveCamera(45, 1, 0.1, 10000);
                self.scene.add(self.camera);
                self.light = new THREE.PointLight();
                self.light.intensity=1.0;
                self.light.position.set(10, 50, 80);
                self.scene.add(self.light);

                self.scene.add( new THREE.HemisphereLight( 0x111111, 0x444444 ) );

                var light = new THREE.DirectionalLight( 0xebf3ff, 1.5 );
                light.position.set( 0, 140, 500 ).multiplyScalar( 1.1 );
                self.scene.add( light );

                self.renderer = new THREE.WebGLRenderer();

                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.create= function(opt)
    {
        this.level.init(this.scene);
        this.level.add_doors(this.scene);
        //this.setFocus(this.level.game_mesh);

        this.add_player({
            x: 0,
            y: 0,
            z: 0,
            obstacles: this.level.doors.mesh
        });
    };

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

    this.getObstacles= function () {
        'use strict';
        return this.obstacles.concat(this.walls);
    }

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