Code viewer for World: new graphics
function World(){   
    var cam;
    var meshFloor;
    var food_list = [];
    var keyboard = {};
    var distance;
    var updatedPosX;
    var updatedPosZ;
    
    var objLoader = new THREE.OBJLoader();
    objLoader.setPath("/uploads/tuitef2/");
    var mtlLoader = new THREE.MTLLoader();
    mtlLoader.setPath("/uploads/tuitef2/");
    
    var update_speed = 0.2;//
    var player = { height:3.5, speed:update_speed, turnSpeed:0.2}; //to get player position get cam position
    var playerFoodCount=0;
    
    var scene = new THREE.Scene(); //CREATE SCENE
    var sky_texture = new THREE.TextureLoader().load( 'uploads/tuitef2/skyyy.png' );
    scene.background = sky_texture;
    //scene.fog = new THREE.Fog("#a6e0ca", 0.0010, 90);
    
    
    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, 60, -160); //position.x,position.y,position.z
    cam.lookAt(new THREE.Vector3(0,60,0));
    }
    
    function ground(){
    var texture = new THREE.TextureLoader().load( 'uploads/tuitef2/ground_texture1.png' );
    
    // 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 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();
    }, 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++;
            document.getElementById("user_span1").innerHTML = playerFoodCount;
        }
    
    }
    }
    
    function player_speed(){
    if (playerFoodCount %8===0){
        update_speed++;
    }
    }
    
    function createMind(){
    var geometry = new THREE.BoxGeometry( 1, 1, 1 );
    var material = new THREE.MeshBasicMaterial( {color: "blue"} );
    var ai = new THREE.Mesh( geometry, material );
    scene.add( ai );
    }
    function movement(){
    requestAnimationFrame(movement);
    	// Keyboard movement inputs
    for (j = 0; j < food_list.length; j++){
        food_list[j].rotation.x += 0.01;
        food_list[j].rotation.y += 0.02;
    }
        
        // Keyboard movement inputs
        if(keyboard[87]){ // W key
            if (cam.position.x<= 200 && cam.position.x>= -200){
                        cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
                        cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
                        updatedPosX = cam.position.x;
                        updatedPosZ = cam.position.z;
                
                    }
                    else{
                        cam.position.x += Math.sin(cam.rotation.y) * player.speed;
                 
                    }
            if(cam.position.z<= 200 && cam.position.z>= -200){
                cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
                cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
                
            }
            else{
                cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
                
            }
            eat_food();
        }
        if(keyboard[83]){ // S key
            if (cam.position.x<= 200 && cam.position.x>= -200){
                    cam.position.x += Math.sin(cam.rotation.y) * player.speed;
                    cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
            }
                else{
                    cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
                    }
            if(cam.position.z<= 200 && cam.position.z>= -200){
                cam.position.x += Math.sin(cam.rotation.y) * player.speed;
                cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
            }
            else{
                cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
            }
            eat_food();
        }
    
        if(keyboard[65]){ // A key
                // Redirect motion by 90 degrees
            if (cam.position.x <= 200 && cam.position.x >= -200){
                    cam.position.x += Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
                    cam.position.z += -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
                    updatedPosX = cam.position.x;
                    updatedPosZ = cam.position.z;
            }
                else{
                    cam.position.x = Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
                    }
            if(cam.position.z <= 200 && cam.position.z >= -200){
                cam.position.x += Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
                cam.position.z += -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
            }
            else{
                cam.position.z = -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
            }
            eat_food();
                
        }
        if(keyboard[68]){ // D key
            if (cam.position.x<= 200 && cam.position.x>= -200){
                    cam.position.x += Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
                    cam.position.z += -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
                    updatedPosX = cam.position.x;
                    updatedPosZ = cam.position.z;
            }
                else{
                    cam.position.x = Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
                    }
            if(cam.position.z<= 200 && cam.position.z>= -200){
                cam.position.x += Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
                cam.position.z += -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
            }
            else{
                cam.position.z = -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
            }
            eat_food();
        }
                
        // Keyboard turn inputs
        if(keyboard[37]){ // left arrow key
                cam.rotation.y -= player.turnSpeed;
        }
        if(keyboard[39]){ // right arrow key
                cam.rotation.y += player.turnSpeed;
        }
        
        renderer.render(scene, cam);
    }
    function keyDown(event){
    keyboard[event.keyCode] = true;
    }
    
    function keyUp(event){
    keyboard[event.keyCode] = false;
    }
    
    function main(){
    render();
    camera();
    ground();
    movement();
    food();
    eat_food();
    food_color();
    player_speed();
    place_trees();
    for(i=-180;i<180;i++){
        if(i%22===0){
            tree_border(i);
        }
    }
    AB.showRunHeader();
    createMind();
    }
    window.addEventListener('keydown', keyDown); 
    window.addEventListener('keyup', keyUp);
    window.onload = main;
}