Code viewer for World: The universe








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


const ARMYSIZE = 1000;                   // Number of planets in the universe 

const gridsize = 130;					// number of squares along side of world	   
const squaresize = 80;					// 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 k = randomintAtoB(1,gridsize-2); 
    
  var shape = new THREE.SphereGeometry ( squaresize, 15, 15 ); 
  
  // 	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 = translate ( k * squaresize );	
 
 	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(-1,1) ;       // clone, change 20 to randomintAtoB(a,b), Q. What should a be? 
        THEARMY[i].position.z =  THEARMY[i].position.z + randomintAtoB(-1,1) ;
        THEARMY[i].position.y =  THEARMY[i].position.y + randomintAtoB(-1,1) ;
        threeworld.scene.add( THEARMY[i] );
   }
 }
}


function paintArmy(  )
{
      
  var textureArray = [
 	( THREE.ImageUtils.loadTexture( "/uploads/starter/earth.5.jpg" ) ), 
 	
 	
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet1.png" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet1.png" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet1.png" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet1.png" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet1.png" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet1.png" ) ),
 	
 	
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet2.png" ) ), 
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet2.png" ) ), 
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet2.png" ) ), 
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet2.png" ) ), 
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet2.png" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet2.png" ) ),
 	
 	
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet3.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet3.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet3.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet3.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet3.jpg" ) ),
 	( THREE.ImageUtils.loadTexture( "/uploads/james/planet3.jpg" ) ),
 	
 	
 	( THREE.ImageUtils.loadTexture( "/uploads/james/sun.jpg" ) ),
 	
 	
 	( THREE.ImageUtils.loadTexture( "" ) ),
  	];
 	
 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;
};

}