Code viewer for World: Block world



 
const	 	CLOCKTICK 	= 100;				// speed of run - move things every n milliseconds
const		MAXSTEPS 	= 1000;				// length of a run before final score
 


const objectsize = 300 ;

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


const SKYCOLOR 	= 0xffffff;				// a number, not a string 
 




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

function randomintAtoB ( A, B )			 
{
 return  ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
  



function World() { 

var x, y, z;        // current location 

var textureArray;




function init()		 
{
     // create one box to start 
     
  	var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
  	var theobject  = new THREE.Mesh( shape );

  	theobject.position.x = 0; // randomintAtoB ( -MAXPOS, MAXPOS );   	
  	theobject.position.z = 0; // randomintAtoB ( -MAXPOS, MAXPOS );   	
  	theobject.position.y = 0; // randomintAtoB ( -MAXPOS, MAXPOS );	
 
  x = theobject.position.x;     // current position
  y = theobject.position.y;
  z = theobject.position.z;
 
 	threeworld.scene.add(theobject);


   textureArray = [
 	( new THREE.ImageUtils.loadTexture( "/uploads/tus/mcstone.jpg" ) )
  	];
 	
 for ( var i = 0; i < textureArray.length; i++ )    // for all textures 
 { 
    textureArray[i].minFilter = THREE.LinearFilter;
 }
 
    paintThis ( theobject );
}



  function handleKeyDown (event)
  {
   	    var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
  	var theobject  = new THREE.Mesh( shape );

  theobject.position.x = x;     // default position
  theobject.position.y = y;
  theobject.position.z = z;
 
  var code = event.keyCode;
  if (code == 37)  theobject.position.x = x - objectsize ;   // left
  if (code == 39)  theobject.position.x = x + objectsize ;   // right
 
  if (code == 38) theobject.position.z = z - objectsize ;   // forward
  if (code == 40) theobject.position.z = z + objectsize ;   // back 
  
  if (code == 17) theobject.position.y = y - objectsize ;   
  if (code == 32) theobject.position.y = y + objectsize ; 
  
  x = theobject.position.x;     // current position
  y = theobject.position.y;
  z = theobject.position.z;
 
 console.log ( "(x,y,z) = (" + x + "," + y + "," + z + ")" );
 
 
 threeworld.lookat.copy ( theobject.position );	
 
 	threeworld.scene.add(theobject);
      paintThis ( theobject );
 }
  



function paintThis( object )        // paint objects with random textures 
{
        var t = randomintAtoB ( 0, textureArray.length - 1 );     // random texture 
    
        object.material =  new THREE.MeshBasicMaterial ( { map: textureArray[t] } );   
}






	this.endCondition;			 


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

  threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 

 	  init();
	   

document.addEventListener( 'keydown', handleKeyDown   );

};



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


this.takeAction = function ( a )
{
  
};



this.endRun = function()
{
};


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

}