Code viewer for World: escape...
function World()
{
    AB.clockTick = 1000;
    var scene = new THREE.Scene();
    var skyTexture = new THREE.TextureLoader().load("uploads/dannydevito/night.png");
    scene.background = skyTexture;
    var fogStrength = 0.001;
    scene.fog = new THREE.FogExp2(0x916e6c, fogStrength);

    var renderer = new THREE.WebGLRenderer();
    function render()
    {
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
    }

    var cam;
    function camera()
    {
        cam = new THREE.PerspectiveCamera(90, window.innerWidth/window.innerHeight, 0.1, 1000);
        cam.position.set(0, 3.5, -160);
        cam.lookAt(scene.position);
    }

    var playerAttributes = { height:2, speed:0.3, turnSpeed:0.09};
    function player()
    {
        var geometry = new THREE.BoxGeometry(1,1,1);
        var material = new THREE.MeshBasicMaterial({color:0xffffff,transparent:true, opacity:0});
        play = new THREE.Mesh(geometry, material);
        play.scale.multiplyScalar(2);
        play.position.set(0, playerAttributes.height, -160);
        scene.add(play);
    }
    
    var collisionFlag = false;
    function collision()
    {
        var enemyPlayerX = Math.abs(parseInt(play.position.x - enemy.position.x));
        var enemyPlayerZ = Math.abs(parseInt(play.position.z - enemy.position.z));
        
        if (enemyPlayerX <= 1 && enemyPlayerZ <= 1)
        {
            play.position.set(0, playerAttributes.height, -160);
            collisionFlag = true;
            endScreen();
        }
    }

    keyboard = {};
    function keyDown(event)
    {
        keyboard[event.keyCode] = true;
    }
    function keyUp(event)
    {
        keyboard[event.keyCode] = false;
    }
    window.addEventListener('keydown', keyDown);
    window.addEventListener('keyup', keyUp);

    var updatedPosX;
    var updatedPosZ;
    var enemySpeed = 0.1
    function movement()
    {
        requestAnimationFrame(movement);
        collision();
        
        // enemy movement
      
        if (updatedPosX < enemy.position.x)
        {
            enemy.position.x -= (playerAttributes.speed + enemySpeed);
        }
        if (updatedPosX > enemy.position.x)
        {
            enemy.position.x += (playerAttributes.speed + enemySpeed);
        }
        if (updatedPosZ < enemy.position.z)
        {
            enemy.position.z -= (playerAttributes.speed + enemySpeed);
        }
        if (updatedPosZ > enemy.position.z){
            enemy.position.z += (playerAttributes.speed + enemySpeed);
        }
     
     
        // player movement

        if(keyboard[87])
        { // W key
            if (play.position.x<= 200 && play.position.x>= -460){
                        play.position.x -= Math.sin(play.rotation.y) * playerAttributes.speed;
                        play.position.z -= -Math.cos(play.rotation.y) * playerAttributes.speed;
                        updatedPosX = play.position.x;
                        updatedPosZ = play.position.z;
                
                    }
                    else{
                        play.position.x += Math.sin(play.rotation.y) * playerAttributes.speed;
                 
                    }
            if(play.position.z<= 463 && play.position.z>= -193){
                play.position.x -= Math.sin(play.rotation.y) * playerAttributes.speed;
                play.position.z -= -Math.cos(play.rotation.y) * playerAttributes.speed;
                updatedPosX = play.position.x;
                updatedPosZ = play.position.z;
                
            }
            else{
                play.position.z += -Math.cos(play.rotation.y) * playerAttributes.speed;
                
            }
            checkFound();
        }
        if(keyboard[83])
        { // S key
            if (play.position.x<= 200 && play.position.x>= -460){
                    play.position.x += Math.sin(play.rotation.y) * playerAttributes.speed;
                    play.position.z += -Math.cos(play.rotation.y) * playerAttributes.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x -= Math.sin(play.rotation.y) * playerAttributes.speed;
                    }
            if(play.position.z<= 463 && play.position.z>= -193){
                play.position.x += Math.sin(play.rotation.y) * playerAttributes.speed;
                play.position.z += -Math.cos(play.rotation.y) * playerAttributes.speed;
                updatedPosX = play.position.x;
                updatedPosZ = play.position.z;
            }
            else{
                play.position.z -= -Math.cos(play.rotation.y) * playerAttributes.speed;
            }
            checkFound();
        }
        
        if(keyboard[65])
        { // A key
                // Redirect motion by 90 degrees
            if (play.position.x <= 200 && play.position.x >= -460){
                    play.position.x += Math.sin(play.rotation.y + Math.PI/2) * playerAttributes.speed;
                    play.position.z += -Math.cos(play.rotation.y + Math.PI/2) * playerAttributes.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x = Math.sin(play.rotation.y - Math.PI/2) * playerAttributes.speed;
                    }
            if(play.position.z <= 463 && play.position.z >= -193){
                play.position.x += Math.sin(play.rotation.y + Math.PI/2) * playerAttributes.speed;
                play.position.z += -Math.cos(play.rotation.y + Math.PI/2) * playerAttributes.speed;
            }
            else{
                play.position.z = -Math.cos(play.rotation.y - Math.PI/2) * playerAttributes.speed;
            }
            checkFound();
        }
        if(keyboard[68])
        { // D key
            if (play.position.x<= 200 && play.position.x>= -460){
                    play.position.x += Math.sin(play.rotation.y - Math.PI/2) * playerAttributes.speed;
                    play.position.z += -Math.cos(play.rotation.y - Math.PI/2) * playerAttributes.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x = Math.sin(play.rotation.y + Math.PI/2) * playerAttributes.speed;
                    }
            if(play.position.z<= 463 && play.position.z>= -193){
                play.position.x += Math.sin(play.rotation.y - Math.PI/2) * playerAttributes.speed;
                play.position.z += -Math.cos(play.rotation.y - Math.PI/2) * playerAttributes.speed;
            }
            else{
                play.position.z = -Math.cos(play.rotation.y + Math.PI/2) * playerAttributes.speed;
            }
            checkFound();
        }
        
        if(keyboard[37]){ // left arrow key
                cam.rotation.y -= playerAttributes.turnSpeed;
                play.rotation.y -= playerAttributes.turnSpeed;
                cam.position.z = play.position.z;
        }
        if(keyboard[39]){ // right arrow key
                cam.rotation.y += playerAttributes.turnSpeed;
                play.rotation.y += playerAttributes.turnSpeed;
                cam.position.z = play.position.z;
        }

        cam.position.x = play.position.x ;
        cam.position.z = play.position.z;
        cam.position.y = 3.5;
        renderer.render(scene, cam);
    }

    function loadCity()
    {
        var mtlLoader = new THREE.MTLLoader();
        mtlLoader.setResourcePath("uploads/dannydevito/");
        mtlLoader.setPath("uploads/dannydevito/");
        
        mtlLoader.load("lowpoly.mtl", function(materials)
        {
            materials.preload();
            var objLoader = new THREE.OBJLoader();
            objLoader.setMaterials(materials);
            objLoader.setPath("uploads/dannydevito/");
            
            objLoader.load("lowpoly.obj", function(object)
            {
                object.scale.set(0.3,0.3,0.3);
                object.position.set(0, 37, 0);
                scene.add(object);
            });
        });
    }
    
    function lights()
    {
        var ambient = new THREE.AmbientLight(0xadadad, 0.5);
        scene.add(ambient);
    }

    function createEnemy()
    {
        var geometry = new THREE.BoxGeometry(5, 7, 1);
        var textureLoader = new THREE.TextureLoader();
    
        thetexture = textureLoader.load("/uploads/dannydevito/enemy.jpg")
        var material = new THREE.MeshBasicMaterial({map:thetexture});
        enemy = new THREE.Mesh(geometry, material);
        enemy.position.set(5,5,160);
        enemy.scale.multiplyScalar(2);
        scene.add(enemy);
    }

    var pageList = [];
    function pages()
    {
        for(i = 0; i < 11; i++)
        {
            var geometry = new THREE.BoxGeometry(1, 0.2, 2);
            var textureLoader = new THREE.TextureLoader();
            
            thetexture = textureLoader.load("/uploads/dannydevito/page.jpg")
            var material = new THREE.MeshBasicMaterial({map:thetexture});
            page = new THREE.Mesh(geometry, material);
            var road = AB.randomIntAtoB(1,6)
            if (road == 1)
            {
                page.position.z += AB.randomIntAtoB(-190,-110); 
                page.position.x += AB.randomIntAtoB(-458,195);
            }
            else if (road == 2)
            {
                page.position.z += AB.randomIntAtoB(-190,460); 
                page.position.x += AB.randomIntAtoB(195,112);
            }
            else if (road == 3)
            {
                page.position.z += AB.randomIntAtoB(-190,460); 
                page.position.x += AB.randomIntAtoB(-100,-172);
            }
            else if (road == 4)
            {
                page.position.z += AB.randomIntAtoB(-190,460); 
                page.position.x += AB.randomIntAtoB(-385,-458);
            }
            else if (road == 5)
            {
                page.position.z += AB.randomIntAtoB(105,177); 
                page.position.x += AB.randomIntAtoB(195,-458);
            }
            else
            {
                page.position.z += AB.randomIntAtoB(385,460); 
                page.position.x += AB.randomIntAtoB(195,-458);
            }
            
            page.position.y += 1.5;
            page.scale.multiplyScalar(1.5);
            pageList.push(page);
            scene.add(page);
        }
    }
    
    var pageCount = 0;
    function checkFound()
    {
        for (i=0; i < pageList.length; i++)
        {
            pagePlayerX = Math.abs((parseInt(updatedPosX)) - pageList[i].position.x);
            pagePlayerZ = Math.abs((parseInt(updatedPosZ)) - pageList[i].position.z);
            
            if (pagePlayerX <= 2 && pagePlayerZ <= 2)
            {
                pageList[i].position.set(-50,-50,-50);
                pageCount++;
                fogStrength += 0.003
                scene.fog = new THREE.FogExp2(0x916e6c, fogStrength);
                playerAttributes.speed -= 0.005;
                enemySpeed += 0.01;
            }
        }
    }
    
    function music()
    {
        AB.backgroundMusic("/uploads/dannydevito/scarymusic.mp3");
    }

    function titleScreen()
    {
        AB.newSplash();
        AB.splashClick(function()
        {
            movement();
            AB.removeSplash();
        })
        
        let splashText = document.getElementById("splash-inner");
        const h1 = document.createElement("h1");
        const p = document.createElement("p");
        h1.innerHTML = "first player to find all the pages escapes, the others are left behind...";
        p.innerHTML = "W A S D & ARROW LEFT + ARROW RIGHT TO MOVE"
        splashText.appendChild(h1);
        splashText.appendChild(p);
    }
    
    AB.socketUserlist = function (a)
    { 
    console.log (JSON.stringify (a, null, ' '));
    }
    AB.socketStart();
    var player2score = 0;
    function socket()
    {
        AB.socketIn = function(score)
        {
            player2score = score
        }
        if (AB.socket)
        {
            if (AB.socket.connected)
            {
                AB.socketOut(pageCount)
            }
        }
    }
    
    
    var element = document.getElementById("ab-threepage");
    var tag = document.createElement("p");
    var tag2 = document.createElement("p");
    function header()
    {
        element.appendChild(tag);
        tag.style = "color:white; text-align:center; font-size:50px;"
        
        element.appendChild(tag2);
        tag2.style = "color:white; text-align:center; font-size:50px;"
    }
    
    function headerUpdate()
    {
        tag.innerText = "you " + pageCount
        tag2.innerText = "them " + player2score
    }
    
    function deathSound()
    {
        const listener = new THREE.AudioListener();
        const sound = new THREE.Audio(listener);
        
        const audioLoader = new THREE.AudioLoader();
        audioLoader.load('/uploads/dannydevito/deathsound.mp3', function(buffer)
        {
        	sound.setBuffer(buffer);
        	sound.setLoop(false);
        	sound.setVolume(10);
        	sound.play();
        });
    }

    this.newRun = function()
    {
        titleScreen();
        render();
        player();
        music();
        createEnemy();
        camera();
        socket();
        loadCity();
        lights();
        pages();
        header();
    }
    
    function endScreen()
    {
        AB.abortRun = true;
        AB.newSplash();
        oldSplash = document.getElementById("splash");
        oldSplash.remove();
        splashBG = document.getElementById("splashblock");
        splashBG.style = "background:black";
        endTitle = document.createElement("h2");
        if (pageCount >= 10)
        {
            endTitle.innerText = "you escaped";
            splashBG.style = "background:white";
            endTitle.style = "color:navy; left:0; line-height: 200px; margin-top: -100px; position: absolute; text-align: center; top: 50%; width: 100%; font-size:100px;";
        }
        else if (player2score >= 10)
        {
            endTitle.innerText = "you have been left behind...";
            endTitle.style = "color:red; left:0; line-height: 200px; margin-top: -100px; position: absolute; text-align: center; top: 50%; width: 100%; font-size:100px;";
        }
        else if (collisionFlag)
        {
            endTitle.innerText = "you have been captured"
            endTitle.style = "color:red; left:0; line-height: 200px; margin-top: -100px; position: absolute; text-align: center; top: 50%; width: 100%; font-size:100px;";
        }
        
        splashBG.appendChild(endTitle)
    }
    
    this.nextStep = function()
    {
        socket();
        headerUpdate();
        if (pageCount >= 10 || player2score >= 10)
        {
            endScreen();
        }
    };
    
    this.endRun = function()
    {
        if (collisionFlag)
        {
            deathSound();
        }
        else if (pageCount >= 10)
        {
            AB.backgroundMusic("/uploads/dannydevito/celebrate.mp3");
        }
    }
}