Code viewer for World: A1_Draft (clone by Sergiu)

// Cloned by Sergiu on 29 Nov 2022 from World "A1_Draft" by Dave 
// Please leave this clone trail here.
 

AB.clockTick       = 80;    

	// Speed of run: Step every n milliseconds. Default 100.
	
AB.maxSteps        = 10000;    

	// Length of run: Maximum length of run in steps. Default 1000.

AB.screenshotStep  = 50;   
  
	// Take screenshot on this step. (All resources should have finished loading.) Default 50.

 const FILE_ARRAY = [
        
		"/uploads/dg/earth.png",
		"/uploads/dg/asteroid_1.jpeg",  // 
		"/uploads/dg/asteroid_4.jpeg",
		"/uploads/dg/asteroid_5.jpeg",
		"/uploads/dg/marmar.jpeg",
		"/uploads/dg/venven.jpeg",
		"/uploads/dg/mermer.jpeg",
		"/uploads/dg/jupii.jpeg",
		"/uploads/dg/satsat.jpeg",
		"/uploads/dg/uraura.jpeg",
		"/uploads/dg/nepnep.jpeg",
		"/uploads/dg/rings.png",
		"/uploads/dg/ringu.png",
		
		
        "/uploads/dg/right.jpg", //skybox images=
        "/uploads/dg/left.jpg",
        "/uploads/dg/top.jpg",
        "/uploads/dg/bot.jpg",
        "/uploads/dg/front.jpg",
        "/uploads/dg/back.jpg",
        
		];
	

const SKYCOLOR 	= 0xffffff;	

const ARMYSIZE = 60, EARTHSIZE = 1, MARSSIZE = 1, VENUSSIZE = 1, MERCURYSIZE = 1, JUPITERSIZE = 1, SATURNSIZE = 1, SATURNSIZERING = 1, URANUSSIZE = 1, URANUSSIZERING = 1, NEPTUNESIZE = 1;
//The amount of asteroids and planets

const objectsize = AB.randomIntAtoB(200,250) ; //size of each asteroid -> Random each time you refresh

     
const objectsizeE = 400, objectsizeM = 300, objectsizeV = 350, objectsizeME = 200, objectsizeJ = 900, objectsizeS = 600, objectsizeSR = 600,  objectsizeU = 300, objectsizeUR = 600, objectsizeN = 300;
//Size of each planet + rings

const WALKSTEP = 100;  // bounds of the random move per timestep 

const MAXPOS                = 4000 ;            // start things within these bounds                    
const startRadiusConst	 	= MAXPOS * 1.5 ;	// distance from centre to start the camera at
const maxRadiusConst 		= MAXPOS * 5 ;		// maximum distance from camera we will render things  

const skyboxConst			= MAXPOS * 4 ;

ABHandler.MAXCAMERAPOS 		= MAXPOS * 1.5 ;			// allow camera go far away -> Slight zoom available

ABWorld.drawCameraControls 	= false; 

AB.drawRunControls 			= false;

// declaring all the variables
var THEARMY = new Array( ARMYSIZE );	

var textureArray = new Array ( FILE_ARRAY.length );
	
var EARTH = new Array( EARTHSIZE );
	
var MARS = new Array( MARSSIZE);
	
var VENUS = new Array( VENUSSIZE );
	
var MERCURY = new Array( MERCURYSIZE );

var JUPITER = new Array( JUPITERSIZE );
	
var SATURN = new Array( SATURNSIZE );
var SATURNRING = new Array( SATURNSIZERING );
    
    
var URANUS = Array( URANUSSIZE );
var URANUSRING = Array( URANUSSIZERING );
    
var NEPTUNE= Array( NEPTUNESIZE );
    
var time = AB.clockTick*3;
var strtbutton = "<p align=\"center\"><button id='startButton', style=\"height:30px;width:100px;background-color:#FF0000\"><b>START</b></button></p>";
    


function load_resources()		// asynchronous file loads - call initScene() when all finished 
{
	for ( var i = 0; i < FILE_ARRAY.length; i++ ) 
	  startFileLoad ( i );						// launch n asynchronous file loads

}


	
function startFileLoad ( n )				// asynchronous file load of texture n 
{
	var loader = new THREE.TextureLoader();

	loader.load ( FILE_ARRAY[n], function ( thetexture )  	 
	{
		thetexture.minFilter  = THREE.LinearFilter;
		textureArray[n] = thetexture;
		if ( asynchFinished() ) initArmy();
	});	

}
 
function asynchFinished()		// all file loads returned 
{
	for ( var i = 0; i < FILE_ARRAY.length; i++ ) 
		if ( ! textureArray[i] ) 
			return false;
		
	return true;
}



function initArmy()		 // called when all textures ready 
{
 var t = 0;

 for ( var c=1 ; c <= ARMYSIZE ; c++ )
 {

    var shape = new THREE.CubeGeometry( objectsize, objectsize, objectsize ); //creates all cubes the size of the declared size
  
  	var theobject  = new THREE.Mesh( shape );

  	theobject.position.x = AB.randomIntAtoB ( -MAXPOS, MAXPOS );   	
  	theobject.position.z = AB.randomIntAtoB ( -MAXPOS, MAXPOS );   	
  	theobject.position.y = AB.randomIntAtoB ( -MAXPOS, MAXPOS ); 
	
 	var r = AB.randomIntAtoB ( 1, 3 );    // random texture 
    theobject.material =  new THREE.MeshBasicMaterial ( { map: textureArray[r] } );   

 	ABWorld.scene.add(theobject);
	THEARMY[t] = theobject;	            	// save it for later
	t++; 
 }
 var t2 = 0;
 
 for ( var f=1 ; f <= EARTHSIZE ; f++ )
 {
 
    var shapeEarth = new THREE.SphereGeometry( objectsizeE, objectsizeE, objectsizeE );
  
  	var theEarth  = new THREE.Mesh( shapeEarth );

  	theEarth.position.x = -1000; 	
  	theEarth.position.z = 0;  	
  	theEarth.position.y = 0;	
	
 	var Er = AB.randomIntAtoB (0, 0);   // random texture   			            // random one of the earths
    theEarth.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Er] } );   

 	ABWorld.scene.add(theEarth);
	EARTH[t2] = theEarth;	            	// save it for later
	t2++; 
 }
 
var t3 = 0;
 
 for ( var j=1 ; j<= MARSSIZE ; j++ )
 {
        var shapeMars = new THREE.SphereGeometry ( objectsizeM, objectsizeM, objectsizeM ); 

  
  	var theMars  = new THREE.Mesh( shapeMars );

  	theMars.position.x = 0;
  	theMars.position.z = 0;
  	theMars.position.y = 0;	
	
 	var Mr = AB.randomIntAtoB ( 4, 4);    			 // random texture 
    theMars.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Mr] } );   

    ABWorld.scene.add(theMars);
	MARS[t3] = theMars;	            	// save it for later
	t3++; 
 }

var t4 = 0;
 
 for ( var k=1 ; k<= VENUSSIZE ; k++ )
 {
        var shapeVenus= new THREE.SphereGeometry ( objectsizeV, objectsizeV, objectsizeV); 

  
  	var theVenus = new THREE.Mesh( shapeVenus );

  	theVenus.position.x = -2000;
  	theVenus.position.z = 0;
  	theVenus.position.y = 0;	
	
 	var Vr = AB.randomIntAtoB ( 5, 5);    			 // random texture 
    theVenus.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Vr] } );   

    ABWorld.scene.add(theVenus);
	VENUS[t4] = theVenus;	            	// save it for later
	t4++; 
 }

var p5 = 0;
 
 for ( var x=1 ; x<= MERCURYSIZE ; x++ )
 {
        var shapeMercury = new THREE.SphereGeometry ( objectsizeME, objectsizeME, objectsizeME ); 

  
  	var theMercury = new THREE.Mesh( shapeMercury );

  	theMercury.position.x = -2750;
  	theMercury.position.z = 0;
  	theMercury.position.y = 0;	
	
 	var MEr = AB.randomIntAtoB ( 6, 6);    			 // random texture 
    theMercury.material =  new THREE.MeshBasicMaterial ( { map: textureArray[MEr] } );   

    ABWorld.scene.add(theMercury);
	MERCURY[p5] = theMercury;	            	// save it for later
	p5++; 
 }

var p6 = 0;
 
 for ( var z=1 ; z<= JUPITERSIZE ; z++ )
 {
    var shapeJupiter = new THREE.SphereGeometry ( objectsizeJ, objectsizeJ, objectsizeJ ); 

  
  	var theJupiter = new THREE.Mesh( shapeJupiter );

  	theJupiter.position.x = 1500;
  	theJupiter.position.z = 0;
  	theJupiter.position.y = 0;	
	
 	var Jr = AB.randomIntAtoB (7, 7);    			 // random texture 
    theJupiter.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Jr] } );   

    ABWorld.scene.add(theJupiter);
	JUPITER[p6] = theJupiter;	            	// save it for later
	p6++; 
 }

var p7 = 0;
 
 for ( var y=1 ; y<= SATURNSIZE ; y++ )
 {
    var shapeSaturn = new THREE.SphereGeometry ( objectsizeS, objectsizeS, objectsizeS);

  
  	var theSaturn = new THREE.Mesh( shapeSaturn );

  	theSaturn.position.x = 3500;
  	theSaturn.position.z = 0;
  	theSaturn.position.y = 0;	
	
 	var Sr = AB.randomIntAtoB ( 8, 8);    			 // random texture 
    theSaturn.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Sr] } );   

    ABWorld.scene.add(theSaturn);
	SATURN[p7] = theSaturn         	// save it for later
	p7++; 
 }
 
var s_r = 0;
 
 for ( var y2=1 ; y2<= SATURNSIZERING ; y2++ )
 {
    var shapeSaturnRing = new THREE.TorusGeometry (800, 100, 100, 100); 

  
  	var theSaturnRing = new THREE.Mesh( shapeSaturnRing );

    theSaturnRing.rotation.x = 200;
    theSaturnRing.rotation.y = 100;
    theSaturnRing.rotation.z = 100;

  	theSaturnRing.position.x = 3500;
  	theSaturnRing.position.z = 0;
  	theSaturnRing.position.y = 0;
	
 	var sr = AB.randomIntAtoB ( 11, 11);    			 // random texture 
    theSaturnRing.material =  new THREE.MeshBasicMaterial ( { map: textureArray[sr] } );
    
    ABWorld.scene.add(theSaturnRing);
	SATURNRING[s_r++] = theSaturnRing         	// save it for later
	s_r++; 
 }

var p8 = 0;
 
 for ( var w=1 ; w<= URANUSSIZE ; w++ )
 {
    var shapeUranus = new THREE.SphereGeometry ( objectsizeU, objectsizeU, objectsizeU);

  
  	var theUranus = new THREE.Mesh( shapeUranus );

  	theUranus.position.x = 5000;
  	theUranus.position.z = 0;
  	theUranus.position.y = 0;	
	
 	var ur = AB.randomIntAtoB ( 9, 9);    			 // random texture 
    theUranus.material =  new THREE.MeshBasicMaterial ( { map: textureArray[ur] } );   

    ABWorld.scene.add(theUranus);
	URANUS[p8] = theUranus         	// save it for later
	p8++; 
 }

var u_r = 0;
 
 for ( var w2=1 ; w2<= URANUSSIZERING ; w2++ )
 {
    var shapeUranusRing = new THREE.TorusGeometry (500, 20, 20, 20); 

  
  	var theUranusRing = new THREE.Mesh( shapeUranusRing );

    theUranusRing.rotation.x = 0;
    theUranusRing.rotation.y = 350;
    theUranusRing.rotation.z = 0;

  	theUranusRing.position.x = 5000;
  	theUranusRing.position.z = 0;
  	theUranusRing.position.y = 0;
	
 	var Ur = AB.randomIntAtoB ( 12, 12);    			 // random texture 
    theUranusRing.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Ur] } );
    
    ABWorld.scene.add(theUranusRing);
	URANUSRING[u_r] = theUranusRing         	// save it for later
	u_r++; 
 }

var p9 = 0;
 
 for ( var y3=1 ; y3<= NEPTUNESIZE ; y3++ )
 {
    var shapeNeptune = new THREE.SphereGeometry ( objectsizeN, objectsizeN, objectsizeN);

  
  	var theNeptune= new THREE.Mesh( shapeNeptune );

  	theNeptune.position.x = 6000;
  	theNeptune.position.z = 0;
  	theNeptune.position.y = 0;	
	
 	var Nr = AB.randomIntAtoB ( 10, 10);    			 // random texture 
    theNeptune.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Nr] } );   

    ABWorld.scene.add(theNeptune);
	NEPTUNE[p9] = theNeptune         	// save it for later
	p9++; 
 }

var skybox_material_array = [
 	( new THREE.MeshBasicMaterial ( { map: textureArray[13], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[14], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[15], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[16], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[17], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[18], side: THREE.BackSide } ) )
 	];
	
  var skyGeometry = new THREE.CubeGeometry ( skyboxConst, skyboxConst, skyboxConst );	
  var skyMaterial = new THREE.MeshFaceMaterial ( skybox_material_array );
  var theskybox = new THREE.Mesh ( skyGeometry, skyMaterial );
//  theskybox.position.y = skyboxConst * 0.4  ;
  
  ABWorld.scene.add( theskybox );			
 
  // can start the run loop
  
	ABWorld.render();		
  
	AB.removeLoading();
	
  	AB.runReady = true; 
  	
  	AB.msg ( ` <hr> <p>Destroy all asteroids before the time runs out, but watch out other players can create more!<p>
  	<p>How to play: <p>Drag the camera and click the buttons <p>
  	        <button onclick='baby();'  class=ab-largenormbutton > Destroy Asteroid </button>  
            <button onclick='skull();'  class=ab-largenormbutton >Create Asteroid </button> </p>
            <p align=\"center\"><button id='startButton', style=\"height:40px;width:100px;background-color:#D3D3D3\"><b>START GAME</b></button></p>` );						
}


function moveArmy()		    // move all the objects 
{
 for ( var i = 0; i < THEARMY.length; i++ )
 { 
   if ( THEARMY[i] )		// in case initArmy() not called yet 
   { 
        THEARMY[i].position.x =  THEARMY[i].position.x + AB.randomIntAtoB(-WALKSTEP,WALKSTEP) ;        
        THEARMY[i].position.z =  THEARMY[i].position.z + AB.randomIntAtoB(-WALKSTEP,WALKSTEP) ;
        THEARMY[i].position.y =  THEARMY[i].position.y + AB.randomIntAtoB(-WALKSTEP,WALKSTEP) ;
        ABWorld.scene.add( THEARMY[i] );
   }

 }
}



AB.world.newRun = function() 
{
	AB.loadingScreen();

	AB.runReady = false;  

	ABWorld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 

	load_resources();		// aynch file loads		
							// calls initArmy() when it returns 
};



AB.world.nextStep = function()
{
 	moveArmy();
};

function initLights() 
// the lights are needed to show up models
{
  const ambientLight = new THREE.AmbientLight( 0xffffff, 1 );
  ABWorld.scene.add( ambientLight );

  const frontLight = new THREE.DirectionalLight( 0xffffff, 1 );
  frontLight.position.set( 10, 10, 10 );

  const backLight = new THREE.DirectionalLight( 0xffffff, 1 );
  backLight.position.set( -10, 10, -10 );

  ABWorld.scene.add( frontLight, backLight );
}

//--- Socket functionality -----------------------------------------------------


// TO DO AT THE END !!
// ADD JS INJECTION TO CHAT?
// start socket

AB.socketStart();


// functions called by buttons
// baby and skull are textures 5 and 6 in the array:

function baby()  {    changeBox(5);    AB.socketOut (5); }
function skull() {    changeBox(6);    AB.socketOut (6); }


function changeBox(n)   // change a random box to texture n (5 or 6) 
{
    var i = AB.randomIntAtoB ( 0, THEARMY.length - 1 );     // pick a random box to change 
    THEARMY[i].material =  new THREE.MeshBasicMaterial ( { map: textureArray[n] } );   
}

AB.socketIn = function(n)       // incoming data on socket, i.e. clicks of other player 
{
    if ( ! AB.runReady ) return;
    changeBox(n);
};