Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/old_sites/threejs/13_fighting/js/maze.js

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

    this.ennemys=[];

    this.getObstacles = function()
    {
        var obstacles = [this.doors.walls];
        for(var i in this.doors.closed_doors)
        {
            obstacles.push(this.doors.closed_doors[i]);
        }
        return obstacles;
    };

    this.getCollisionCallbacks = function()
    {
        var self=this;
        if(perso.current_cell!==null)
        {
            var cbs=[];
            var cell = this.doors.cells[perso.current_cell];
            var params = cell.params;
            var doors = this.doors.near_doors(params.x, params.z);
            cell.separation_lines.forEach(function(line)
            {
                if(line.enter_leave_door && !line.outside_door)
                {
                    cbs.push(line);
                }
            });
            cbs = cbs.concat(cell.keys);
            doors.forEach(function(door)
            {
                var door_x = door[0];
                var door_z = door[1];
                var near_cell = self.doors.cells[door_x*self.doors.num_items_line+door_z];
                cbs=cbs.concat(near_cell.separation_lines)
            });
            return cbs;
        }
        else
        {
            return [];
        }
    };

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

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

        var self=this;
        var cellids = [];
        var keyids = [];
        var mazeaction = '';

        collisions.forEach(function (obj)
        {
            if(obj.callback_data.type=='change_cell')
            {
                if(cellids.indexOf(obj.callback_data.cellid)===-1)
                {
                    if(obj.callback_data.mazeaction)
                    {
                        mazeaction= obj.callback_data.mazeaction;
                    }
                    cellids.push(obj.callback_data.cellid);
                }
            }
            else if(obj.callback_data.type=='key')
            {
                obj.callback_data.callback_function();
            }
        });
        var leaving = perso.in_cells.filter(function(x) { return cellids.indexOf(x)===-1; });
        var adding = cellids.filter(function(x) { return perso.in_cells.indexOf(x)===-1; });
        if(adding.length>0 || leaving.length>0)
        {
            perso.in_cells = cellids;
            if(leaving.length>0)
            {
                leaving.forEach(function(cellid)
                {
                    console.log('leaving ',cellid);
                });
            }
            if(adding.length>0)
            {
                adding.forEach(function(cellid)
                {
                    if(cellid!='outside')
                    {
                        self.changeCell(perso,cellid);
                    }
                    else
                    {
                        game.leaveMaze();
                        console.log('entering maze ',cellid);
                    }
                });
            }
            if(mazeaction=='leave_maze_from_start')
            {
                game.leaveMaze(this.id, this.start_path);
            }
            else if(mazeaction=='leave_maze_from_end')
            {
                game.leaveMaze(this.id, this.end_path);
            }
            game.updateCollisionsCache();
        }
    };

    this.changeCell= function(changeperso,cellid)
    {
        changeperso.current_cell=cellid;
        var cell = this.doors.cells[cellid];
        console.log('entering cell ',cellid);
    };

    this.build = function()
    {
        var self=this;
        this.doors = new Doors(game, this, options);
        this.doors.build();
    };
    this.add_ennemy = function(options)
    {
        var ennemy = new Ennemy(game, 
        {
            x: options.x,
            z: options.z,
            moveable: false,
            ai: true,
            maze: this,
            patrol_positions: options.patrol_positions,
            game: game
        });
        ennemy.build();
        ennemy.name='ennemy';
        ennemy.current_cell = this.doors.cells.length-1;
        this.ennemys.push(ennemy);
        return ennemy;
    };

    this.update= function(delta)
    {
        if(this.ennemys)
        {
            this.ennemys.forEach(function(ennemy)
            {
                ennemy.update(delta);
            });
        }
    };
};