Tfe

Ongi etorri tfe-ren webgunera...

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

(Deskargatu)
var Maze = function(game, options)
{
    this.id = 'maze_'+Math.random()+'_'+Math.random();

    this.load = function()
    {
        this.doors = new Doors(game, this, options);
        return this.doors.load();
    };

    this.getObstacles = function()
    {
        return this.doors.mesh;
    };

    this.getCollisionCallbacks = function()
    {
        return this.doors.door_separators;
    };
    this.getOutsideCollisionCallbacks = function()
    {
        return this.doors.outside_separators;
    };

    this.collisionCallbacks = function(perso,collisions)
    {
        if(collisions.length===0)
        {
            return;
        }

        var self=this;
        var ids = [];
        var action = '';
        collisions.forEach(function (obj)
        {
            if(ids.indexOf(obj.callback_data.id)===-1)
            {
                if(obj.callback_data.action)
                {
                    action= obj.callback_data.action;
                }
                ids.push(obj.callback_data.id);
            }
        });
        var leaving = perso.in_rooms.filter(function(x) { return ids.indexOf(x)===-1; });
        var adding = ids.filter(function(x) { return perso.in_rooms.indexOf(x)===-1; });
        if(adding.length>0 || leaving.length>0)
        {
            perso.in_rooms = ids;
            if(perso.in_rooms.length===1 && perso.in_rooms.indexOf('cell_outside_outside')!==-1)
            {
                if(action=='leave_maze_from_start')
                {
                    game.leaveMaze(this.id, this.start_path);
                }
                else if(action=='leave_maze_from_end')
                {
                    game.leaveMaze(this.id, this.end_path);
                }
            }
            else
            {
                game.inMaze(this.id);
            }
        }
    };

    this.build = function()
    {
        var self=this;
        return new Promise(function(ok, reject)
        {
            self.load().then(function()
            {
                self.doors.build();
                ok();
            }, reject);
        });
    };
};