Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/old_sites/threejs/10_game/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,{
            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.5;
                self.light.position.set(10, 100, 80);
                self.light.castShadow=true;
                self.scene.add(self.light);

                self.renderer = new THREE.WebGLRenderer();
                self.renderer.shadowMapEnabled = true;

                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);

        var pos = this.level.doors.get_start_pos();
        console.log('pos ',pos);
        this.add_player({
            x: pos.x,
            y: 0,
            z: pos.z,
            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);
    };
};