Code viewer for World: World2.0 (clone by Jon Ortiz)

// Cloned by Jon Ortiz on 23 Nov 2022 from World "World2.0" by tuitef2 
// 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 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/dannydevito/");
    var mtlLoader1 = new THREE.MTLLoader();
    mtlLoader1.setPath("/uploads/dannydevito/");
    
    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 playerFoodCount=0;
    
    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("test.mtl", function(m) { //BUILDING FOR EGG
            m.preload();
            objLoader1.setMaterials(m);
            
            objLoader1.load("test.obj", function(mesh){ //top
                    mesh.position.y+=2;
                    mesh.scale.set(100,100,100);
                    mesh.position.x += 20;
                    mesh.position.z +=20;
                    scene.add(mesh);
               });
               
        });
    }
    
    function place_egg(){
        mtlLoader2.load("tfx95.mtl", function(m) { //EGG
            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(updatedPosX)) - egg[i].position.x);
            distance2 =  Math.abs((parseInt(updatedPosZ)) - egg[i].position.z);
            if(distance1 <=2 && distance2 <= 2 && hasEgg !== true){
                hasEgg = true;
            }
            if(hasEgg === true){
                egg[i].position.x = updatedPosX+1.5;
                egg[i].position.z = updatedPosZ+1.5;
                console.log(egg[i].position.x, updatedPosX);
                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);
            distance2BaseY =  Math.abs((parseInt(updatedPosZ)) - player_base.position.z);
            if(distance2BaseX <=2 && distance2BaseY <= 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;
            }
        }
    }
    
    
    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 movement(){
        requestAnimationFrame(movement);

        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++){
            console.log("egg1",egg.length);
            egg[i].rotation.x += 0.01;
            egg[i].rotation.y += 0.02;
            eat_egg();
        }
       // ----mind movement --//
        
        
        
        // ----Player 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();
        }
                
        // Keyboard turn inputs
        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;
        cam.position.y = 3.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(0,2,160);
        ai.scale.multiplyScalar(2);
        scene.add( ai );
    }
    
    function splash(){
        var s = document.getElementById("splash-inner")
        var p = document.createElement("p");
        document.s.appendChild(p);
    }
    
	this.newRun = function()
	{
	    //splash(); 
		render();
		create_player();
        camera();
        ground();
        base();
        createMind();
        movement();
        food();
        eat_food();
        food_color();
        place_structure();
        hide_egg();
        eat_egg();
        player_speed();
        // place_trees();
        for(i=-180;i<180;i++){
            if(i%22===0){
                tree_border(i);
            }
        }
        AB.showRunHeader(); 	
    };


	this.takeAction = function ( action )		 
	{
	   createMind();
	   var a = 5;
	};

	this.endRun = function()
	{
	};


	this.getState = function()
	{
	  return ( 5 );  				 
	};

	
	this.getScore = function()
 	{
	  return ( 0 );		
	};

}