Code viewer for World: A1_Draft (clone by Sergiu)

// Cloned by Sergiu on 24 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/starter/earth.1.jpg",  // to be changed to planets/asteroids/ufos/???
		"/uploads/starter/earth.2.jpg",
		"/uploads/starter/earth.3.jpg",
		"/uploads/starter/earth.4.jpg",
		"/uploads/starter/earth.5.jpg",
		"/uploads/starter/baby.1.png",  
		"/uploads/starter/ghost.3.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 	= 0xccffcc;				// a number, not a string 

const ARMYSIZE = 70;       // an "army" of objects (i.e amount of them)

const objectsize = AB.randomIntAtoB(200,300) ; //obvs the size of each cube

const WALKSTEP = 200;       // bounds of the random move per timestep 
                            // try 50 (vibrating sheet) versus 1000 (cloud)
         
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;


    var THEARMY 	= new Array( ARMYSIZE );	

	var textureArray = new Array ( FILE_ARRAY.length );

	

function loadResources()		// 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.SphereGeometry ( objectsize, 30, 30 ); 
   var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
  
  	var theobject  = new THREE.Mesh( shape );

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

 	ABWorld.scene.add(theobject);
	THEARMY[t] = theobject;	            	// save it for later
	t++; 
 }
 
var skybox_material_array = [
 	( new THREE.MeshBasicMaterial ( { map: textureArray[7], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[8], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[9], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[10], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[11], side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: textureArray[12], 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> Multi-user game. Pick a side. Click buttons to change boxes on all users' machines. Drag the camera. <p>
  	        <button onclick='baby();'  class=ab-largenormbutton > Baby </button>  
            <button onclick='skull();'  class=ab-largenormbutton > Skull </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  ); 

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



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






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