Code viewer for World: A1_Draft (clone by Dave)

// Cloned by Dave on 30 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

//const TIME_BEFORE_END = 80;            //number of seconds before the time mode ends

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, MARS, VENUS, MERCURY, JUPITER, SATURN, SATURNRING, URANUS, URANUSRING, NEPTUNE;



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 timer = AB.clockTick*TIME_BEFORE_END;

    //var strtbutton = "<p align=\"center\"><button id='startButton', style=\"height:30px;width:100px;background-color:#FF0000\"><b>START</b></button></p>";
    
    //var gameMode = "SCORE";



{
 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 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;
theEarth.radius = 5000;
theEarth.start = Date.now()

	
var Er = 0;
theEarth.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Er] } );  //TEXTURE 0 

ABWorld.scene.add(theEarth);
EARTH = theEarth;

//MARS
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 = 4;
theMars.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Mr] } );   

ABWorld.scene.add(theMars);
MARS = theMars;

//VENUS
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 = 5;
theVenus.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Vr] } );   

ABWorld.scene.add(theVenus);
VENUS = theVenus;	            


//MERCURY
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 = 6;
theMercury.material =  new THREE.MeshBasicMaterial ( { map: textureArray[MEr] } );   

ABWorld.scene.add(theMercury);
MERCURY = theMercury;


//JUPITER
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 = 7;
theJupiter.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Jr] } );   

ABWorld.scene.add(theJupiter);
JUPITER = theJupiter;	            

//SATURN
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 = 8;
theSaturn.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Sr] } );   

ABWorld.scene.add(theSaturn);
SATURN = theSaturn 

//SRING
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 = 11;
theSaturnRing.material =  new THREE.MeshBasicMaterial ( { map: textureArray[sr] } );
    
ABWorld.scene.add(theSaturnRing);
SATURNRING = theSaturnRing         

//URANUS
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 = 9;
theUranus.material =  new THREE.MeshBasicMaterial ( { map: textureArray[ur] } );   

ABWorld.scene.add(theUranus);
URANUS = theUranus

//URING  
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 = 12;
theUranusRing.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Ur] } );
    
ABWorld.scene.add(theUranusRing);
URANUSRING = theUranusRing;

//NEPTUNE
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 = 10;
theNeptune.material =  new THREE.MeshBasicMaterial ( { map: textureArray[Nr] } );   

ABWorld.scene.add(theNeptune);
NEPTUNE = theNeptune;



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>`);
						
}


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] );
   }

 }
}


function moveEarth()
{
var earthangle = Math.PI*2;
var circumference	= EARTH.radius * 2 * Math.PI; //curcumfrence
var duration	= 5000; //10 seconds per rotation
var rotSpeed	= 2 * Math.PI / duration;

        if ( EARTH )
        {
			var delta = Date.now() - EARTH.start;
		    var vector = circumference / duration;
                    // vector * data is a portion of circumference that the dot should have walked through by the duration delta.
                    // You have to normalize by your radius to get an angel (in radian).
			var angle = rotSpeed * delta;

            EARTH.position.x =  EARTH.radius * Math.cos(angle) ;        
            EARTH.position.z =  EARTH.radius * Math.sin(angle) ;
            EARTH.position.y =  EARTH.radius * Math.sin(angle) ;
            ABWorld.scene.add( EARTH );
        }
}

//function initOptions()
//{
    
    //$("#user_span1").html( "<p align=\"center\"><font color=\"#C9C9C9\"><b>"+timer/AB.clockTick+" seconds remaining</b></font><p></b></p>" );
    
   // document.getElementById("startButton").addEventListener("click", start);
//}

//function updateHUD()
///{
    //if (gameMode === TIME)
    //{
       // $("#user_span1").html( "<p align=\"center\"><b>"+Math.floor(timer/AB.clockTick)+" seconds remaining</b><p></b></p>" );
        
    //}

//}


//<p align=\"center\"><button id='startButton', style=\"height:40px;width:100px;background-color:#D3D3D3\"><b>START GAME</b></button></p>` );



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();
    moveEarth();
    
};


//--- 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);
};