Code viewer for World: Cloned xyz MineCraft




// not using these - actually user controlled 

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



const objectsize = 200 ;

const MAXPOS                = 4000 ;                                 
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 SKYCOLOR 	= 0xffffff;				// a number, not a string 
 


var currentTexture = 0 ;    // under user control!  starts at 1st texture




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;


// programmers like lowercase:
//  var theobject
// but can have too much
// var thebiggestobjectsize
// hence
// var theBiggestObjectSize
// var the_biggest_object_size 



function init()		 
{
    
   textureArray = [
 	( new THREE.ImageUtils.loadTexture( "/uploads/tus/minecraft_tree_wood.jpg" ) ),
 	( new THREE.ImageUtils.loadTexture( "/uploads/starter/minecraft.1.jpg" ) ),
 	( new THREE.ImageUtils.loadTexture( "/uploads/starter/minecraft.2.jpg" ) )
 	];
 	
 for ( var i = 0; i < textureArray.length; i++ )    // for all textures 
 { 
    textureArray[i].minFilter = THREE.LinearFilter;
 }
 
 
 x = 0;
 y = 0;
 z = 0;
 
 
 // go up the stairs:

 for ( var i = 1; i <= 10; i++ )      
 {
     var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
  	 var  theobject  = new THREE.Mesh( shape );

 // console.log(i);
 
 	theobject.position.x =  objectsize * i ;    	
  	theobject.position.z =  0; 	
  	theobject.position.y =  objectsize * i;	
 
 	threeworld.scene.add(theobject);
    paintThis ( theobject );
    
  //  makeSideRow ( 	theobject.position.x, 	theobject.position.y );
 }
 
  
  // go down the stairs:
  
 for ( i = 1; i <= 10; i++ )      
 {
      shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
  	  theobject  = new THREE.Mesh( shape );

  var startx = (objectsize*10);
  var starty = (objectsize*10);
  
 	theobject.position.x =  startx + (objectsize * i);    	
  	theobject.position.z =  0; 	
  	theobject.position.y =  starty - (objectsize * i);	
 
 	threeworld.scene.add(theobject);
    paintThis ( theobject );
    
 //   makeSideRow ( 	theobject.position.x, 	theobject.position.y );
 }
 
    
    //  exercise - make stairs up and stairs down 
  
}



function makeSideRow ( x, y )          // put 2nd for loop in separate function
{
       for ( var i = 1; i <= 10; i++ )      
 {
      var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
  	  var theobject  = new THREE.Mesh( shape );

 	theobject.position.x =  x;    	
  	theobject.position.z =  objectsize * i ; 	
  	theobject.position.y =  y;	
 
 	threeworld.scene.add(theobject);
    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 is current position - going to then move it 
  theobject.position.y = y;
  theobject.position.z = z;
 
  var code = event.keyCode;
  
   if (code == 49) currentTexture = 0 ;
   if (code == 50) currentTexture = 1 ;
   if (code == 51) currentTexture = 2 ;
  
  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 == 34) theobject.position.y = y - objectsize ;   
  if (code == 33) theobject.position.y = y + objectsize ; 
  
  x = theobject.position.x;     // current position is now this 
  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 );
      
      // sound effect credit:
      // http://soundbible.com/1399-Chambering-A-Round.html
        var audio = new Audio('/uploads/starter/chamber.mp3');
        audio.play();

 }
  





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






	this.endCondition;			 


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

  threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 

 var s = "<center> <h2> User controlled. Use arrow keys and PgUp, PgDn to draw. </h2> </center>";
 	$("#user_span2").html( s );


 	  init();
	   
document.addEventListener( 'keydown', handleKeyDown   );

};



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


this.takeAction = function ( a )
{
};


this.endRun = function()
{
};


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

}