Code viewer for World: tuitef2 2.85 AI And UI com...

// Cloned by Jon Ortiz on 24 Nov 2022 from World "tuitef2 2.85 AI And UI combind" by tuitef2 
// Please leave this clone trail here.
 


// Cloned by tuitef2 on 10 Mar 2019 from World "2.85 AI And UI combind" by  Mikey Dowling 
// Please leave this clone trail here.
 


// Cloned by  Mikey Dowling on 7 Mar 2019 from World "2.6 " by tuitef2 
// Please leave this clone trail here.
 


// Cloned by tuitef2 on 7 Mar 2019 from World "Bot Following Food AI 2.5" by  Mikey Dowling 
// Please leave this clone trail here.
 
 
//Sources:
//Egg: https://poly.google.com/view/fOdgSMxvk2r
//Trees: https://poly.google.com/view/2_fv3tn3NG_
//Egg Building: https://poly.google.com/view/fj7pMmLK5jG
//Player and AI base: https://poly.google.com/view/7A18mxC9RmU


//note when player puts egg in base make animate up and down then disappear.

function World() 
{ 
    var cam;
    var meshFloor;
    var food_list = [];
    var keyboard = {};
    var distance;
    var updatedPosX;
    var updatedPosZ;
    var egg = [];
    var hasEgg = false;
    var play;
    var player_base;
    var AiFoodCount = 0;
    var playerFoodCount=0;
    var pspan;
    var pspan2;
    var Aspan;
    var Aspan1;
    var birdsView = false;
    var disFood;
    var botEgg = false;
    var playerEgg = false;
    var eggTimer = 10000;
    //Ai vars
    //camera
    //player
    var objLoader = new THREE.OBJLoader();
    objLoader.setPath("/uploads/tuitef2/");
    var mtlLoader = new THREE.MTLLoader();
    mtlLoader.setPath("/uploads/tuitef2/");
    
    var objLoader1 = new THREE.OBJLoader();
    objLoader1.setPath("/uploads/tuitef2/");
    var mtlLoader1 = new THREE.MTLLoader();
    mtlLoader1.setPath("/uploads/tuitef2/");
    
    var objLoader2 = new THREE.OBJLoader();
    objLoader2.setPath("/uploads/tuitef2/");
    var mtlLoader2 = new THREE.MTLLoader();
    mtlLoader2.setPath("/uploads/tuitef2/");
    
    var objLoader3 = new THREE.OBJLoader();
    objLoader3.setPath("/uploads/tuitef2/");
    var mtlLoader3 = new THREE.MTLLoader();
    mtlLoader3.setPath("/uploads/tuitef2/");
    
    var objLoader4 = new THREE.OBJLoader();
    objLoader4.setPath("/uploads/tuitef2/");
    var mtlLoader4 = new THREE.MTLLoader();
    mtlLoader4.setPath("/uploads/tuitef2/");
    
    var update_speed = 0.2;//
    var player = { height:2, speed:0.3, turnSpeed:0.09}; //to get player position get cam position

    
    var scene = new THREE.Scene(); //CREATE SCENE
    var sky_texture = new THREE.TextureLoader().load( 'uploads/tuitef2/skyyy.png' );
    scene.background = sky_texture;
    
    var renderer = new THREE.WebGLRenderer();
    
    function render(){
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
    }
    
    function camera(){
        cam = new THREE.PerspectiveCamera(90, window.innerWidth/window.innerHeight, 0.1, 1000);
        cam.position.set(0, 3.5, -160); //position.x,position.y,position.z
        cam.lookAt(scene.position);
    }
    
    function create_player(){

        var geometry = new THREE.BoxGeometry( 1, 1, 1 );
        var material = new THREE.MeshBasicMaterial( {color: "#c91092"} );
        play = new THREE.Mesh( geometry, material );
        play.scale.multiplyScalar(2);
        play.position.set(0,player.height,-160);
        scene.add(play);
    }
    
    function ground(){
        var texture = new THREE.TextureLoader().load( 'uploads/tuitef2/ground_texture1.png' );
        
        texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
        texture.offset.set( 0, 0 );
        texture.repeat.set( 2, 2 );
        
        // immediately use the texture for material creation
        meshFloor = new THREE.Mesh(
        new THREE.PlaneGeometry(400,400),
        new THREE.MeshBasicMaterial({map:texture}));
        
        meshFloor.rotation.x -= Math.PI / 2; // Rotate the plane 90 degrees as if you don't do this it'll be like a tv screen.
        scene.add(meshFloor);
    }
    
    function tree_border(i){
        mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
            materials.preload();
            objLoader.setMaterials(materials);
            
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += i; 
            	    mesh.position.x += 180;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
             objLoader.load("PUSHILIN_forest.obj", function(mesh){//bottom
                    mesh.position.z += i; 
            	    mesh.position.x += -180;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
             objLoader.load("PUSHILIN_forest.obj", function(mesh){//right
                    mesh.position.z += 180; 
            	    mesh.position.x += i;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
              objLoader.load("PUSHILIN_forest.obj", function(mesh){//left
                    mesh.position.z += -180; 
            	    mesh.position.x += i;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            var light = new THREE.DirectionalLight( "#7fe093", 0.3 );
            light.position.set( 0, 1, 0 );
            scene.add( light );
        });
    }
    
    function place_trees(){
        mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
            materials.preload();
            objLoader.setMaterials(materials);
            
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 120; 
            	    mesh.position.x += 120;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 120; 
            	    mesh.position.x += -120;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 120; 
            	    mesh.position.x += -130;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 120; 
            	    mesh.position.x += 130;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
               
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 90; 
            	    mesh.position.x += 90;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 90; 
            	    mesh.position.x += -90;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 50; 
            	    mesh.position.x += -60;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 50; 
            	    mesh.position.x += 60;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 50; 
            	    mesh.position.x += 100;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 90; 
            	    mesh.position.x += -100;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
             objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 90; 
            	    mesh.position.x += 20;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
             objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 70; 
            	    mesh.position.x += -10;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += 60; 
            	    mesh.position.x += -20;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top//
                    mesh.position.z += -10; 
            	    mesh.position.x += -50;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
             objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top//
                    mesh.position.z += -10; 
            	    mesh.position.x += 50;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -120; 
            	    mesh.position.x += 120;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top -
                    mesh.position.z += -120; 
            	    mesh.position.x += -120;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -120; 
            	    mesh.position.x += -130;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -120; 
            	    mesh.position.x += 130;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
               
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -90; 
            	    mesh.position.x += 90;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -90; 
            	    mesh.position.x += -90;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -50; 
            	    mesh.position.x += -60;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -50; 
            	    mesh.position.x += 60;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -50; 
            	    mesh.position.x += 100;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -90; 
            	    mesh.position.x += -100;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
             objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -90; 
            	    mesh.position.x += 20;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
             objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -70; 
            	    mesh.position.x += -10;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
            objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
                    mesh.position.z += -60; 
            	    mesh.position.x += -20;
                    mesh.position.y+=5;
                    mesh.scale.multiplyScalar(15);
                    scene.add(mesh);
               });
        });
    }
    
    function base(){
        mtlLoader3.load("materials1.mtl", function(material) { //BUILDING FOR EGG
            material.preload();
            objLoader3.setMaterials(material);
            
            objLoader3.load("model-triangulated.obj", function(mesh){ //player base
                    mesh.position.y+=1;
                   // mesh.scale.multiplyScalar(5);
                    mesh.scale.set(15,5,5);
                    mesh.position.x += 0;
                    mesh.position.z += -160;
                    scene.add(mesh);
                    player_base = mesh;
                    
                    var light = new THREE.PointLight("#c91092", 5, 100 );
                    light.position.set(0, 2, -160 );
                    scene.add( light ); 
               });
            
            objLoader3.load("model-triangulated.obj", function(mesh){ //AI base
                    mesh.position.y+=1;
                    mesh.scale.set(15,5,5);
                    mesh.position.x += 0;
                    mesh.position.z += 160;
                    scene.add(mesh);
                    
                    var light = new THREE.PointLight("#1061a0", 5, 100 );
                    light.position.set(0, 2, 160 );
                    scene.add( light ); 
              });
        });
    }
    
    function place_structure(){
        mtlLoader1.load("01.mtl", function(m) { //BUILDING FOR EGG
            m.preload();
            objLoader1.setMaterials(m);
            
            objLoader1.load("01.obj", function(mesh){ //top
                    mesh.position.y+=2;
                    mesh.scale.set(0.5,0.5,0.5);
                    mesh.position.x += 20;
                    mesh.position.z +=20;
                    scene.add(mesh);
               });
               
        });
    }
    
    function place_egg(){
        mtlLoader2.load("tfx95.mtl", function(m) { //EGG place in middle
            m.preload();
            objLoader2.setMaterials(m);
            
            objLoader2.load("tfx95.obj", function(mesh){ //top
                    mesh.position.y+=2.3;
                    mesh.scale.set(1.8,1.8,1.8);
                    mesh.position.x += -2;
                    mesh.position.z += -12;
                    mesh.rotation.z += 2;
                    scene.add(mesh);
                    egg.push(mesh);
               });
            
            var light = new THREE.PointLight(0xff0000, 5, 150 );
            light.position.set(-2, 2, -12 );
            scene.add( light );
        });
    }
    
    function eat_egg(){
            for(i=0;i<egg.length;i++){
                distance1 = Math.abs((parseInt(play.position.x)) - egg[i].position.x);
                distance2 =  Math.abs((parseInt(play.position.z)) - egg[i].position.z);
                if(distance1 <=2 && distance2 <= 2 && hasEgg !== true){
                    hasEgg = true;
                    playerEgg=true;
                }
                if(hasEgg === true && playerEgg === true){
                    egg[i].position.x = play.position.x+1.5;
                    egg[i].position.z = play.position.z+1.5;
                    positionEgg();
                }
            }}  
        function eat_eggAi(){
            for(i=0;i<egg.length;i++){
                distance1 = Math.abs((parseInt(ai.position.x)) - egg[i].position.x);
                distance2 =  Math.abs((parseInt(ai.position.z)) - egg[i].position.z);
                if(distance1 <=2 && distance2 <= 2 && hasEgg !== true){
                    hasEgg = true;
                    botEgg = true;
                }
                if(hasEgg === true && botEgg === true){
                    egg[i].position.x = ai.position.x+1.5;
                    egg[i].position.z = ai.position.z+1.5;
                    positionEgg();
                }
            }  
        }
            
    
    
    function hide_egg(){
        setTimeout(function() { //places egg after 70 seconds
            place_egg();
        }, 20000); //70000
    }
    
    function positionEgg(){ //player position egg at base
        for(i=0;i<egg.length;i++){
            distance2BaseX = Math.abs((parseInt(updatedPosX)) - player_base.position.x);
            distance2BaseZ =  Math.abs((parseInt(updatedPosZ)) - player_base.position.z);
            if(distance2BaseX <=20 && distance2BaseZ <= 2){
                egg[i].position.x = player_base.position.x;
                egg[i].position.z = player_base.position.z;
                egg[i].position.y = 3.2;
                hasEgg = false;
                hide_egg();   
            }
        }
    }
    
    
    function food(){
        for (i=0; i < 8; i++){ //8 foods on plane
            mesh = new THREE.Mesh(
                    new THREE.SphereGeometry(1),
                    new THREE.MeshBasicMaterial({color:0xff4444}));
            mesh.position.z += AB.randomIntAtoB(-160,120); 
            mesh.position.x += AB.randomIntAtoB(-160,120);
            mesh.position.y += 2; // Move the mesh up 1 meter
            mesh.scale.multiplyScalar(1.5);
            scene.add(mesh);
            food_list.push(mesh);
        }
    }
    
    function food_color(){
        setTimeout(function() { //changes color of food after 5 seconds
            for(i = 0;i <food_list.length; i++){
                    food_list[i].material.color.set("#ffcc00"); //yellow color
            }
            food_positionChange();
        }, 6000);
    }
    
    function food_positionChange(){
        setTimeout(function() { //changes position of food after 5 seconds
             for(i = 0;i <food_list.length; i++){
                food_list[i].position.x = AB.randomIntAtoB(-160,120);
                food_list[i].position.z = AB.randomIntAtoB(-160,120);
                food_list[i].material.color.set(0xff4444);//red color
             }
             food_color();
        bestx = 999;
        bestz = 999;
        smallestDist = 999;
        closestFood(); 
        }, 10000);
    }
    
    function eat_food(){
        for(i = 0;i <food_list.length; i++){
            distanceX = Math.abs((parseInt(updatedPosX)) - food_list[i].position.x);
            distanceZ =  Math.abs((parseInt(updatedPosZ)) - food_list[i].position.z);
            if(distanceX <=2 && distanceZ <= 2 ){ //if player is close 
                food_list[i].position.x = AB.randomIntAtoB(-160,120);
                food_list[i].position.z = AB.randomIntAtoB(-160,120);//change the position of the item
                food_list[i].material.color.set(0xff4444); //red color
                playerFoodCount++;
                pspan2.innerHTML = playerFoodCount;
                bestx = 999;
                bestz = 999;
                smallestDist = 999;
                
                closestFood();
            }
        
        }
    }
    function eat_foodAI(){
        for(i = 0;i <food_list.length; i++){
            distanceX = Math.abs((parseInt(ai.position.x)) - food_list[i].position.x);
            distanceZ =  Math.abs((parseInt(ai.position.z)) - food_list[i].position.z);
            if(distanceX <=2 && distanceZ <= 2 ){ //if player is close 
                food_list[i].position.x = AB.randomIntAtoB(-160,120);
                food_list[i].position.z = AB.randomIntAtoB(-160,120);//change the position of the item
                food_list[i].material.color.set(0xff4444); //red color
                AiFoodCount++;
                Aspan.innerHTML = AiFoodCount;
                bestx = 999;
                bestz = 999;
                smallestDist = 999;
                
                closestFood();
            }
        }
    }       
    function player_speed(){
        if (playerFoodCount %8===0){
                update_speed++;
            }
    }
    
    var bestx = 999;
    var bestz = 999;
    var smallestDist = 999;
    
    function closestFood(){
        var dX;
        var dZ;
        
        for(i=0;i<food_list.length;i++){
            dX =  (parseInt(ai.position.x)) - food_list[i].position.x;
            dZ =  (parseInt(ai.position.z)) - food_list[i].position.z;
            disFood = Math.sqrt(dX * dX + dZ*dZ);
            if (disFood < smallestDist){
                bestx = food_list[i].position.x;
                bestz = food_list[i].position.z;
                smallestDist = disFood;
            }
        }
    }
    

    function collisionDection(){
        var xdis = Math.abs(parseInt(play.position.x - ai.position.x));
        var zdis = Math.abs(parseInt(play.position.z - ai.position.z));
        var dis = Math.sqrt(xdis * xdis + zdis + zdis);
        
        if( xdis <= 2 && zdis <= 2){
            if(playerFoodCount < AiFoodCount){
                play.position.set(0,player.height,-160);
            }
        else if(playerFoodCount > AiFoodCount){
            ai.position.set(0,2,160);
        }
        }
    }
    function movement(){
        requestAnimationFrame(movement);
        // collisionDection();
        // for (j = 0; j < food_list.length; j++){
        //     food_list[j].rotation.x += 0.01;
        //     food_list[j].rotation.y += 0.02;
        // }
        
        // for(i=0;i<egg.length;i++){
        //     egg[i].rotation.x += 0.01;
        //     egg[i].rotation.y += 0.02;
        //     eat_egg();
        //     eat_eggAi();
        // }
       // ----mind movement --//
        // var dPtoAiX =  parseInt(play.position.x) - parseInt(ai.position.x);
        // var dPtoAiZ =  parseInt(play.position.z) - parseInt(ai.position.z);
        // var disPlayer = Math.sqrt(dPtoAiX * dPtoAiX + dPtoAiZ * dPtoAiZ);
        // var dir = AB.randomIntAtoB(0,3);
        
        // //AI LOOKING FOR FOOD
        // closestFood();
        // console.log(AiFoodCount)

        // if(smallestDist < disPlayer){
        //         foodMovement();
        //     }
            
        // else{
            // if(playerFoodCount + 1 > AiFoodCount){
            //     foodMovement();
            // eat_foodAI(ai);}
            // else{
            // if (updatedPosX < ai.position.x){
            //     ai.position.x -= 0.3;
            // }
            // if (updatedPosX > ai.position.x){
            //     ai.position.x += 0.3;
            // }
            
            // if (updatedPosZ < ai.position.z){
            //     ai.position.z -= 0.3;
            // }
            // if (updatedPosZ > ai.position.z){
            //     ai.position.z += 0.3;
            // }
                
            // }
            // closestFood();
        // }
        // Keyboard movement inputs
        
        if(keyboard[87]){ // W key
            if (play.position.x<= 200 && play.position.x>= -200){
                        play.position.x -= Math.sin(play.rotation.y) * player.speed;
                        play.position.z -= -Math.cos(play.rotation.y) * player.speed;
                        updatedPosX = play.position.x;
                        updatedPosZ = play.position.z;
                
                    }
                    else{
                        play.position.x += Math.sin(play.rotation.y) * player.speed;
                 
                    }
            if(play.position.z<= 200 && play.position.z>= -200){
                play.position.x -= Math.sin(play.rotation.y) * player.speed;
                play.position.z -= -Math.cos(play.rotation.y) * player.speed;
                updatedPosX = play.position.x;
                updatedPosZ = play.position.z;
                
            }
            else{
                play.position.z += -Math.cos(play.rotation.y) * player.speed;
                
            }
            eat_food();
        }
        if(keyboard[83]){ // S key
            if (play.position.x<= 200 && play.position.x>= -200){
                    play.position.x += Math.sin(play.rotation.y) * player.speed;
                    play.position.z += -Math.cos(play.rotation.y) * player.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x -= Math.sin(play.rotation.y) * player.speed;
                    }
            if(play.position.z<= 200 && play.position.z>= -200){
                play.position.x += Math.sin(play.rotation.y) * player.speed;
                play.position.z += -Math.cos(play.rotation.y) * player.speed;
                updatedPosX = play.position.x;
                updatedPosZ = play.position.z;
            }
            else{
                play.position.z -= -Math.cos(play.rotation.y) * player.speed;
            }
            eat_food();
        }
    
        if(keyboard[65]){ // A key
                // Redirect motion by 90 degrees
            if (play.position.x <= 200 && play.position.x >= -200){
                    play.position.x += Math.sin(play.rotation.y + Math.PI/2) * player.speed;
                    play.position.z += -Math.cos(play.rotation.y + Math.PI/2) * player.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x = Math.sin(play.rotation.y - Math.PI/2) * player.speed;
                    }
            if(play.position.z <= 200 && play.position.z >= -200){
                play.position.x += Math.sin(play.rotation.y + Math.PI/2) * player.speed;
                play.position.z += -Math.cos(play.rotation.y + Math.PI/2) * player.speed;
            }
            else{
                play.position.z = -Math.cos(play.rotation.y - Math.PI/2) * player.speed;
            }
            eat_food();
        }
        if(keyboard[68]){ // D key
            if (play.position.x<= 200 && play.position.x>= -200){
                    play.position.x += Math.sin(play.rotation.y - Math.PI/2) * player.speed;
                    play.position.z += -Math.cos(play.rotation.y - Math.PI/2) * player.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x = Math.sin(play.rotation.y + Math.PI/2) * player.speed;
                    }
            if(play.position.z<= 200 && play.position.z>= -200){
                play.position.x += Math.sin(play.rotation.y - Math.PI/2) * player.speed;
                play.position.z += -Math.cos(play.rotation.y - Math.PI/2) * player.speed;
            }
            else{
                play.position.z = -Math.cos(play.rotation.y + Math.PI/2) * player.speed;
            }
            eat_food();
        }
                
        if(birdsView === false){    // Keyboard turn inputs// STOPS PLAYERS FROM USING ARROW KEYS WHEN IN SKY VIEW.
            if(keyboard[37]){ // left arrow key
                    cam.rotation.y -= player.turnSpeed;
                    play.rotation.y -= player.turnSpeed;
                    cam.position.z = play.position.z ;
            }
            if(keyboard[39]){ // right arrow key
                    cam.rotation.y += player.turnSpeed;
                    play.rotation.y += player.turnSpeed;
                    cam.position.z = play.position.z;
            }
            
        }
        cam.position.x = play.position.x ;
        cam.position.z = play.position.z -5;
    
        renderer.render(scene, cam);
    }
    
    
    function keyDown(event){
        keyboard[event.keyCode] = true;
    }
    
    function keyUp(event){
        keyboard[event.keyCode] = false;
    }
    window.addEventListener('keydown', keyDown); 
    window.addEventListener('keyup', keyUp);
    
    var ai;
     function createMind(){
        var geometry = new THREE.BoxGeometry( 1, 1, 1 );
        var material = new THREE.MeshBasicMaterial( {color: "blue"} );
        ai = new THREE.Mesh( geometry, material );
        ai.position.set(5,2,-160);
        ai.scale.multiplyScalar(2);
        scene.add( ai );
     }
     
    ////// CAMERA STUFFS 
    function hud(event){
        pspan = document.getElementById("user_span1");
        pspan2 = document.getElementById("user_span2");
        pspan.innerHTML = "Player food count: ";
        
        pspan.style.fontSize = '20px';
        pspan2.style.fontSize = '20px';
        
        Aspan1 = document.getElementById("user_span3");
        Aspan = document.getElementById("user_span4");
        Aspan1.innerHTML = "  AI food count: ";
        
        Aspan1.style.padding = '5px';
        Aspan1.style.fontSize = '20px';
        Aspan.style.fontSize = '20px';
        
        
        let span = document.getElementById('api_span1');
        let span1 = document.getElementById('api_span2');
        var button = document.createElement("button");
        var button2 = document.createElement("button");
        let run = document.getElementById('w2m_runcontrols');
        run.innerHTML = "";
        
        span.innerHTML = 'Change view: ';

        button.style.background= '#ed8c0e';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.cursor = 'pointer';
        button.style.borderRadius = '3px';
        button.style.fontSize = '18';
        button.style.padding = '5px';
        
        
        button2.style.background = '#4CAF50';
        button2.style.color = 'white';
        button2.style.fontSize = '18';
        button2.style.border = 'none';
        button2.style.cursor = 'pointer';
        button2.style.borderRadius = '3px';
        button2.style.padding = '5px';
        
        
        button.innerHTML = "Third Person";
        button2.innerHTML = "Birds Eye";
        
        span.appendChild(button);
        span1.appendChild(button2);
        
        button.addEventListener("click", camView1);
        button2.addEventListener("click", camView2);
    }
    
    function camView1(){
        birdsView = false;
        cam.position.x = play.position.x ;
        cam.position.z = play.position.z -5;
        cam.position.y = 3.5;
        cam.rotation.y = 0;
        cam.rotation.z = -3.141592653589793;
        cam.rotation.x = -3.1197211417647925;
        play.rotation.x= 0;
        play.rotation.y= 0;
        play.rotation.z = 0;
    }
    function camView2(){
        birdsView = true;
        cam.position.x = play.position.x;
        cam.position.z = play.position.z + 20;
        cam.position.y = 60;
        cam.rotation.y = 0;
        cam.rotation.z = -3.141592653589793;
        cam.rotation.x = 29.5;
        play.rotation.x= 0;
        play.rotation.y= 0;
        play.rotation.z = 0;
        
    }
    //AI MOVESTUFF IS 
    function foodMovement(){
            if(ai.position.x < bestx){
                    ai.position.x += 0.3;
                    eat_foodAI();
                    closestFood();
                }
                 if(ai.position.x > bestx){
                    ai.position.x -= 0.3;
                    eat_foodAI();
                    closestFood();
                }
                
                if(ai.position.z < bestz){
                    ai.position.z += 0.3;
                    eat_foodAI();
                    closestFood();
                }
                  if(ai.position.z > bestz){
                    ai.position.z -= 0.3;
                    eat_foodAI();
                    closestFood();
                }
        
    }
    
    function splash(){
        AB.newSplash();
        //AB.removeSplash();
        let span = document.getElementById("splashbutton");
        span.addEventListener("click", beginGame);
        
        let splash = document.getElementById("splash-inner");
        var h1 = document.createElement("p");
        h1.innerHTML = "How To Play!";
        splash.appendChild(h1);
        
        var p = document.createElement("p");
        var p1 = document.createElement("p");
        var h2 = document.createElement("p");
        
        p.innerHTML = "The objective of the game is to race to the centre of the map which hosts an egg item,<br> which must be returned to your base (pink) while also avoiding <br> the enemy who will be attempting to snatch it from you.<br><br><b>FUN TIP:</b> You can kill the enemy by touching it. However, you must have a greater number of food!";//"The objective of this game is to capture the egg found in the center of the map<br>and bring it back to your base(pink) without the enemy obtaining it back from you.<br> If you eat more food than the enemy, you're able to kill the enemy by touching it.";
        h2.innerHTML = "Controls";
        p1.innerHTML = "<b>W</b>: Forward <br><b>S</b>: Backwards<br><b> A</b>: Left <br><b>D</b>: Right <br> <b>Arrow key left</b>: Turns player to the left<br><b>Arrow key right</b> : Turns player to the right";
        splash.appendChild(p);
        splash.appendChild(h2);
        splash.appendChild(p1);
        
        h1.style.fontSize = "25px";
        h1.style.borderTop = "2px solid green";
        h1.style.paddingTop = "8px";
        
        h2.style.fontSize = "25px";
        h2.style.borderTop = "2px solid green";
        h2.style.paddingTop = "8px";
        
    }
    
    function beginGame(){
        AB.removeSplash();
        movement();
        food_color();
    }
	this.newRun = function()
	{   
	    splash();
		render();
		create_player();
        camera();
        hud();
        ground();
        base();
        // createMind();
        food();
        // eat_food();
        // place_structure();
        // hide_egg();
        // eat_foodAI();
        // player_speed();
        // place_trees();
        // for(i=-180;i<180;i++){
        //     if(i%22===0){
        //         tree_border(i);
        //     }
        // }
        // AB.showRunHeader(); 	
    };

}