Code viewer for World: Capture The Egg (clone by ...

// Cloned by Lorcan Dunne on 1 Dec 2022 from World "Capture The Egg (clone by Jon Bryan Ortiz) (clone by user three)" by user three 
// Please leave this clone trail here.
 


// Cloned by user three on 1 Dec 2022 from World "Capture The Egg (clone by Jon Bryan Ortiz)" by Jon Bryan Ortiz 
// Please leave this clone trail here.
 


// Cloned by Jon Bryan Ortiz on 28 Nov 2022 from World "Capture The Egg" by tuitef2 
// Please leave this clone trail here.
 
function World() 
{ 
    
    
    //config
    var pointsToWin = 11;
    
    //
    var cam;
    var meshFloor;
    var food_list = [];
    var keyboard = {};
    var distance;
    var updatedPosX;
    var updatedPosZ;
    var egg = [];
    var hasEgg = false;
    var play;
    var AiFoodCount = 0;
    var playerFoodCount=0;
    var pspan;
    var pspan2;
    var Aspan;
    var Aspan1;
    var birdsView = false;
    var disFood;
    var botEgg = false;
    var eggSpawned = false;
    var playerEgg = false;
    var eggTimer = 10000;
    var aiCaptures = 1;
    var playerCaptures = 1;
    //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 botSpeed = 0.3;
    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(40,40),
        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 place_trees(){
    //     mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
    //         materials.preload();
    //         objLoader.setMaterials(materials);
    //     }
    // )}
            

    function base(){
        mtlLoader3.load("materials1.mtl", function(material) { //BUILDING FOR EGG
            material.preload();
            objLoader3.setMaterials(material);
        });
    }
    
   
    
    
    function food(){
        for (i=0; i < 5; 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_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++;
                console.log(playerFoodCount++);
                player.speed += 0.01;
                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++;
                botSpeed += 0.02;
                Aspan.innerHTML = AiFoodCount;
                bestx = 999;
                bestz = 999;
                smallestDist = 999;
                
                closestFood();
            }
        }
    }       
    
    
    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(playerEgg === true){
                playerEgg = false;
                hasEgg = false;
                for(i=0;i<egg.length;i++){
                    egg[i].position.x = -2;
                    egg[i].position.z = -12;
                    egg[i].position.y = 2;}
                play.position.set(0,player.height,+160);
            }
        
            
            else if(botEgg === true){
                botEgg = false;
                hasEgg = false;
                for(i=0;i<egg.length;i++){
                    egg[i].position.x = -2;
                    egg[i].position.z = -12;
                    egg[i].position.y = 2;}
                ai.position.set(0,2,-160);
            }
            else if(playerFoodCount < AiFoodCount){
                play.position.set(0,player.height,-160);
            }
            else if(playerFoodCount > AiFoodCount){
                ai.position.set(0,2,160);
            }
    }
    }
    function movement(){
        if(playerCaptures ===pointsToWin ){
            return;
        }
        requestAnimationFrame(movement);
        collisionDection();
        setTimeout(function(){eggSpawned = true},20000);
        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);
        
        
        // 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( 5, 5, 5 );
         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 );
    }
     
    ////// CAMERA STUFFS 
        function hud(event){
        pspan = document.getElementById("user_span1");
        pspan2 = document.getElementById("user_span2");
        pspan.innerHTML = "<br>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';
        
        eggPlayer = document.getElementById("user_span5");
        eggPlayer.innerHTML = "<br><br>Player Egg Count:";
        eggPlayer1 = document.getElementById("user_span6");
        
        eggAi = document.getElementById("user_span7");
        eggAi.innerHTML = "AI Egg Count:";
        eggAi1 = document.getElementById("user_span8");
        
        eggPlayer.style.fontSize = 20;
        eggAi.style.fontSize = 20;
        
        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;
        
    }
    function winner(){
         AB.newSplash();
         let splash = document.getElementById("splash-inner");
         let span = document.getElementById("splashbutton");
         span.innerHTML = "Restart";
         span.addEventListener("click", beginGame);
         var winmessage = document.createElement("p");
         winmessage.innerHTML = "<b><br>Congratulations! Winner<b>";
         splash.append(winmessage);
         
         winmessage.style.fontSize = "25px";
    }
    
    function loser(){
        AB.newSplash();
         let splash = document.getElementById("splash-inner");
         let span = document.getElementById("splashbutton");
         span.innerHTML = "Restart";
         span.addEventListener("click", beginGame);
         var winmessage = document.createElement("p");
         winmessage.innerHTML = "<br><b>Better luck next time..<b>";
         splash.append(winmessage);
         
         winmessage.style.fontSize = "25px";
    }
    
    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();
        //eat_eggAi();
        //eat_egg(); 
        place_trees();
        
        for(i=-180;i<180;i++){
            if(i%22===0){
                tree_border(i);
            }
        }
        AB.showRunHeader(); 
        endRun();
    };
    function endRun(){
        if (playerCaptures === pointsToWin){
            winner();
        }
        else if(aiCaptures === pointsToWin){
            loser();
        }
    }

}