Code viewer for World: New World (clone by Adam Gray)

// Cloned by Adam Gray on 8 Nov 2022 from World "New World" by Niall Kelly 
// Please leave this clone trail here.
 
const start_r = 100 //camera start pos
const max_r = 250   // Max render distance

var p1score = 0;
var p2score = 0;

var alltargets = new Array(0);  // array of all targets currently in play

var packet = new Array(0);
 
const SKYBOX_ARRAY = [										 
    "/uploads/kellyn88/sbleft.jpg",    // left
    "/uploads/kellyn88/sbr.png",    // right 
    "/uploads/kellyn88/test.png",
    "/uploads/kellyn88/skybox_base.png",    //base
    "/uploads/kellyn88/test.png",
    "/uploads/kellyn88/test.png"
    ];
                
var gridSetup = [
    [-160, -100, 70], [-80, -100, 70], [0, -100, 70], [80, -100, 70], [160, -100, 70],
    [-160, -100, -10], [-80, -100, -10], [0, -100, -10], [80, -100, -10], [160, -100, -10], // Layout of poosible target spawn positions
    [-160, -100, -90], [-80, -100, -90], [0, -100, -90], [80, -100, -90], [160, -100, -90]
    ]
                
// !!!BACKGROUND SOUND UNCOMMENT WHEN DONE!!!
const MUSICFILE = '/uploads/kellyn88/ambience.mp3';
AB.backgroundMusic ( MUSICFILE );

const shoot_short = "uploads/kellyn88/shoot_short.wav";
const shoot_long = "uploads/kellyn88/shoot_long.wav";

 AB.newSplash ( splashScreen() );

                
	AB.world.newRun = function()
	{
	    
		// Code for Three.js initial drawing of objects.
		// Should include one of:
	 	// ABWorld.init2d ( arguments );
        
        initScene();
        AB.runReady = false;
        cameraControls();
	 	
	};
	
	function initScene(){
	    var color = new THREE.Color()
	 	ABWorld.init2d ( start_r, max_r, color );
	 	ABWorld.scene.background = new THREE.CubeTextureLoader().load ( SKYBOX_ARRAY );
	 	
	 	var light = new THREE.AmbientLight("white", 0.8);
        ABWorld.scene.add(light);
	};
	
	AB.world.nextStep = function()		 
	{
	    if (alltargets.length < 3){
    	    spawnTarget();
	        wait(100);
	    }
	    if(AB.socket){
	        if(AB.socket.connected){
	            console.log(packet);
	            packet = [];
	            packet.push(gridSetup);
	            packet.push(alltargets);
	            packet.push(p1score);
        	    AB.socketOut(packet);
        	    console.log(packet);
	        }
	    }
	   // else{
	   //     AB.runReady = false;
	   // }
	};
	
	function spawnTarget(){
	    var texture = new THREE.TextureLoader().load("/uploads/kellyn88/target.png");
	 	var shape = new THREE.CylinderGeometry(30, 30, 5, 32);
        var cover = new THREE.MeshBasicMaterial({map: texture});
        var target = new THREE.Mesh(shape, cover);
	 	ABWorld.scene.add(target);                                      // Target creation
	 	
	 	var randomIndex = Math.floor(Math.random() * gridSetup.length);
        var item = gridSetup[randomIndex];
	 	target.position.set(item[0], item[1], item[2]);                 // Select Random spaen location
	 	
	 	gridSetup.splice(randomIndex, 1);
	 	alltargets.push(target);
	 	
	}
	
	function cameraControls()
    {
    		//ABHandler.initTouchDrag 	= {};   // Mobile controls, will change if I have time.
    		//ABHandler.touchDrag	    = {};
    		
    		ABHandler.initMouseDrag 	= mouseClick;
    		ABHandler.mouseDrag			= mouseDrag;
    		ABHandler.mouseZoom         = mouseZoom;
    		
    }
    
    function mouseClick(x, y)
    {
        audioHandler("shoot");
        targetHit(x, y);
        return;
    }
    
    function mouseDrag(x, y){
        return;
    }
    
    function mouseZoom(x){
        return;
    }
    
    function targetHit(x, y)
    {
        if(alltargets.length > 0){
            for (var i = 0; i < alltargets.length; i++){
                var target = alltargets[i];
                if ( ABWorld.hitsObject ( x, y, target ) ){
                    var pos = [target.position.x, target.position.y, target.position.z];
                    gridSetup.push(pos);
                    ABWorld.scene.remove(target);
                    alltargets.splice(i, 1);
                    p1score++;
                    AB.runReady = true;
                    break;
                }
            }
        }
        return;
    }
    
    function wait(ms) {
        var start = new Date().getTime();
        for (var i = 0; i < 1e7; i++) {
            if ((new Date().getTime() - start) > ms){
                break;
            }
        }
    }
    
    function audioHandler(instance){
        if (instance == "shoot"){
            var a = new Audio( shoot_short );		
	        a.play();
        }
    }

	AB.world.endRun = function()
	{
	};
	
	function splashScreen()		// HTML format string of instructions for splash screen 
    {
    	var s = "Shoot targets as they appear to earn points.\n\n";
    	
    	if ( AB.onDesktop() ) 	s = s + " Desktop instructions: Use your mouse to aim and click to shoot.\n\n" ;
    	else 					s = s + " Mobile instructions: Tap to shoot.\n\n" ;
    
    	s = s + "1 point is awarded for each target destroyed. Keep an eye out for ducks. They are more difficult to hit but are worth 10 points. Earn more points than your opponent to win!";
    	return ( s );
    }
    
    AB.splashClick ( function ()        
	{		

        AB.runReady = true;
		AB.removeSplash();			// remove splash screen
	
	});
	
	AB.socketStart();
	
	AB.socketIn = function (packet){
	    gridSetup = packet[0];
	    alltargets = packet[1];
	    p2score = packet[2];
	}
    
    AB.socketUserlist = function ( array ) {
        console.log(array);
    };