Code viewer for World: The big bang


   
// No actual problem
// Just blank space for three.js 
   
   
 
const	 	CLOCKTICK 	= 100;				// speed of run - move things every n milliseconds
const		MAXSTEPS 	= 1000;				// length of a run before final score
 


const ARMYSIZE = 1000;

const gridsize = 100;					// number of squares along side of world	   
const squaresize = 100;					// size of square in pixels
const MAXPOS = gridsize * squaresize;		// length of one side in pixels 
	
const SKYCOLOR 	= 0x000000;				// a number, not a string 
 

const startRadiusConst	 	= MAXPOS * 0.8 ;		// distance from centre to start the camera at
const maxRadiusConst 		= MAXPOS * 3 ;		// maximum distance from camera we will render things  

const skyboxConst			= MAXPOS * 3 ;

const ACTION_LEFT 		= 0;		   
const ACTION_RIGHT 		= 1;
const ACTION_UP 			= 2;		 
const ACTION_DOWN 		= 3;
const ACTION_STAYSTILL 		= 4;


 


function randomfloatAtoB ( A, B )			 
{
 return ( A + ( Math.random() * (B-A) ) );
}

function randomintAtoB ( A, B )			 
{
 return  ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
  
function randomBoolean()			 
{
 if ( Math.random() < 0.5 ) { return false; }
 else { return true; }
}





function World() { 

    var  step;

  	var self = this;					  

var THEARMY 	= new Array( ARMYSIZE );	


function translate ( x ) 
{
 return ( x - ( MAXPOS/2 ) );
}




function initSkybox() 
{

// x,y,z positive and negative faces have to be in certain order in the array 
 
// mountain skybox, credit:
// http://stemkoski.github.io/Three.js/Skybox.html

  var materialArray = [
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_pos_z.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_neg_z.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_pos_y.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_neg_y.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_pos_x.jpg" ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/starter/sky_neg_x.jpg" ), side: THREE.BackSide } ) ),
 	];
  
  var skyGeometry = new THREE.CubeGeometry ( skyboxConst, skyboxConst, skyboxConst );	
  var skyMaterial = new THREE.MeshFaceMaterial ( materialArray );
  var theskybox = new THREE.Mesh ( skyGeometry, skyMaterial );
  threeworld.scene.add( theskybox );						// We are inside a giant Cube
}



function initArmy()		 
{
 var t = 0;
 
 for ( var c=1 ; c <= ARMYSIZE ; c++ )
 {
  	var i = randomintAtoB(1,gridsize-2);	 
  	var j = randomintAtoB(1,gridsize-2);
  
  var shape = new THREE.SphereGeometry ( squaresize, 20, 10 ); 
  
  // 	var shape    = new THREE.BoxGeometry( squaresize, squaresize, squaresize );
  
  	var thecube  = new THREE.Mesh( shape );

  	thecube.position.x = translate ( i * squaresize );   	
  	thecube.position.z = translate ( j * squaresize );   	
  	thecube.position.y =  0;	
 
 	threeworld.scene.add(thecube);
	THEARMY[t] = thecube;		// save it for later
	t++; 
 }
}


 
function walkArmy()		 
{
 for ( var i = 0; i < THEARMY.length; i++ )
 { 
   if ( THEARMY[i] )
   { 
        THEARMY[i].position.x =  THEARMY[i].position.x + randomintAtoB(-1000,1000) ;       // clone, change 20 to randomintAtoB(a,b), Q. What should a be? 
        THEARMY[i].position.z =  THEARMY[i].position.z + randomintAtoB(-1000,1000) ;
        THEARMY[i].position.y =  THEARMY[i].position.y + randomintAtoB(-1000,1000) ;
        threeworld.scene.add( THEARMY[i] );
   }
 }
}


function paintArmy(  )
{
      
  var textureArray = [
 	( THREE.ImageUtils.loadTexture( "/uploads/starter/earth.1.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/starter/earth.2.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/starter/earth.3.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/starter/earth.4.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/starter/earth.5.jpg" ) )
  	];
 	
 for ( var i = 0; i < textureArray.length; i++ )
 { 
    	textureArray[i].minFilter = THREE.LinearFilter;
 }
 
 	
 for ( var i = 0; i < THEARMY.length; i++ )
 { 
   if ( THEARMY[i] )
   { 
        var t = randomintAtoB ( 0, textureArray.length-1 );
        THEARMY[i].material =  new THREE.MeshBasicMaterial ( { map: textureArray[t] } );   
   }
 }    	 
}


	this.endCondition;			 



this.newRun = function() 
{
  this.endCondition = false;
  step = 0;

  threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 

     initArmy();
	  paintArmy();		 
  	
    initSkybox();
  	
};



this.getState = function()
{
  return ( null );  
};


this.takeAction = function ( a )
{
  step++;
  walkArmy();
};



this.endRun = function()
{
};


this.getScore = function()
{
 return 0;
};

}