Code viewer for World: Minecraft with cursor

// Cloned by Mathias Bazin on 29 May 2018 from World "MineCraft" by Starter user 
// Please leave this clone trail here.
 



// ==== Starter World ===============================================================================================
// (c) Ancient Brain Ltd. All rights reserved.
// This code is only for use on the Ancient Brain site.
// This code may be freely copied and edited by anyone on the Ancient Brain site.
// This code may not be copied, re-published or used on any other website.
// To include a run of this code on another website, see the "Embed code" links provided on the Ancient Brain site.
// ==================================================================================================================




// "MineCraft" World 
// user adds blocks 


// --- not using nextStep() loop for actions, only for rendering: ------------------------ 
// all actions are taken whenever user hits keys and not any other time
// however, still need to use nextStep() loop for render update

 

 
 
 
 

// ===================================================================================================================
// === Start of tweaker's box ======================================================================================== 
// ===================================================================================================================

// The easiest things to modify are in this box.
// You should be able to change things in this box without being a JavaScript programmer.
// Go ahead and change some of these. What's the worst that could happen?



// These 3 have default values, so this section is optional:

AB.clockTick       = 100;    

	// Speed of run: Step every n milliseconds. Default 100.
	
AB.maxSteps        = 1000000;    

	// 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/minecraft.1.jpg",
		"/uploads/starter/minecraft.2.jpg"
		];
		
 const SOUND_BLOCK = '/uploads/starter/chamber.mp3' ;


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

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

const shortDistance = 2; //used for checking if two vectors are close to be the same

//--- change threeworld defaults: -------------------------------

threehandler.MAXCAMERAPOS = MAXPOS * 10 ;			// allow camera go far away 


// ===================================================================================================================
// === End of tweaker's box ==========================================================================================
// ===================================================================================================================


// You will need to be some sort of JavaScript programmer to change things below the tweaker's box.










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

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




function World() { 


function createCursor() 
{
    //create cursor
    
    //create shape and material for the edges
    var edgeshape = new THREE.CylinderGeometry(10,10,objectsize,32);
    var material = new THREE.MeshBasicMaterial( {color: "red"} );
    
    //create edges
    var edge1 = new THREE.Mesh( edgeshape, material );
    var edge2 = new THREE.Mesh( edgeshape, material );
    var edge3 = new THREE.Mesh( edgeshape, material );
    var edge4 = new THREE.Mesh( edgeshape, material );
    var edge5 = new THREE.Mesh( edgeshape, material );
    var edge6 = new THREE.Mesh( edgeshape, material );
    var edge7 = new THREE.Mesh( edgeshape, material );
    var edge8 = new THREE.Mesh( edgeshape, material );
    var edge9 = new THREE.Mesh( edgeshape, material );
    var edge10 = new THREE.Mesh( edgeshape, material );
    var edge11= new THREE.Mesh( edgeshape, material );
    var edge12= new THREE.Mesh( edgeshape, material );
    
    
    //place edges correctly
    edge1.position.set(-objectsize/2,0,-objectsize/2);
    
    edge2.position.set(-objectsize/2,0,+objectsize/2);
    
    edge3.position.set(+objectsize/2,0,+objectsize/2);
    
    edge4.position.set(+objectsize/2,0,-objectsize/2);
    
    edge5.position.set(objectsize/2,objectsize/2,0);
    edge5.rotation.x = Math.PI / 2;
    
    edge6.position.set(-objectsize/2,objectsize/2,0);
    edge6.rotation.x = Math.PI / 2;
    
    edge7.position.set(objectsize/2,-objectsize/2,0);
    edge7.rotation.x = Math.PI / 2;
    
    edge8.position.set(-objectsize/2,-objectsize/2,0);
    edge8.rotation.x = Math.PI / 2;
    
    edge9.position.set(0,-objectsize/2,objectsize/2);
    edge9.rotation.z = Math.PI / 2;
    
    edge10.position.set(0,-objectsize/2,-objectsize/2);
    edge10.rotation.z = Math.PI / 2;
    
    edge11.position.set(0,+objectsize/2,objectsize/2);
    edge11.rotation.z = Math.PI / 2;
    
    edge12.position.set(0,+objectsize/2,-objectsize/2);
    edge12.rotation.z = Math.PI / 2;
    
    //add all edges to a group, 
    var cursor = new THREE.Group();

    cursor.add(edge1);
    cursor.add(edge2);
    cursor.add(edge3);
    cursor.add(edge4);
    cursor.add(edge5);
    cursor.add(edge6);
    cursor.add(edge7);
    cursor.add(edge8);
    cursor.add(edge9);
    cursor.add(edge10);
    cursor.add(edge11);
    cursor.add(edge12);
    
    //you can now manipulate the cursor as a unique object
    return cursor;
}


var x, y, z;        // current location 

var cursor = createCursor();

var textureArray;

var blockArray = [];


function init()		 
{
    cursor.position.set(0,0,0);
    threeworld.scene.add(cursor);
    
	textureArray = [
		( new THREE.ImageUtils.loadTexture( FILE_ARRAY[0] ) ),
		( new THREE.ImageUtils.loadTexture( FILE_ARRAY[1] ) )
		];
 	
	for ( var i = 0; i < textureArray.length; i++ )    // for all textures 
	{ 
     textureArray[i].minFilter = THREE.LinearFilter;
	}
	
	threeworld.camera.lookAt(cursor.position);
 
}


function getBlockIndexAtCursor()
{
    let index = 0;
    for (let block of blockArray)
    {
        if ( block.position.distanceTo(cursor.position) < shortDistance )
        {
            return index;
        }
        index++;
    }
    
    return -1;
}

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




// the main key handling function:

function handleKeyDown (e)
{
 
  if ( e.keyCode == 37 ) { cursor.position.x +=  - objectsize ; 	e.preventDefault(); }   // left
  if ( e.keyCode == 39 ) { cursor.position.x +=  + objectsize ; 	e.preventDefault(); }   // right
 
  if ( e.keyCode == 38 ) { cursor.position.z +=  - objectsize ; 	e.preventDefault(); }   // forward
  if ( e.keyCode == 40 ) { cursor.position.z +=  + objectsize ; 	e.preventDefault(); }   // back 
  
  if ( e.keyCode == 34 ) { cursor.position.y +=  - objectsize ; 	e.preventDefault(); }   
  if ( e.keyCode == 33 ) { cursor.position.y +=  + objectsize ;     e.preventDefault(); }
  
  
  
 
 //	console.log ( "(x,y,z) = (" + cursor.position.x + ")" );
 
//	threeworld.camera.lookAt( cursor.position );	
    
  if ( e.keyCode == 13 ) { // Enter key pressed
      
      
      
      if( getBlockIndexAtCursor() == -1) //if no block at cursor
      {
        
        //create block at cursor position
        var shape  = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
        var block  = new THREE.Mesh( shape );
    
        block.position.x = cursor.position.x;     
        block.position.y = cursor.position.y;
        block.position.z = cursor.position.z;
    
        threeworld.scene.add(block);
        paintThis ( block );
        
        // sound effect credit:
        // http://soundbible.com/1399-Chambering-A-Round.html
        var audio = new Audio( SOUND_BLOCK );
        audio.play();
        
        blockArray.push(block)
    
      }
      
    e.preventDefault();
      
  }
    
  if ( e.keyCode == 46 ) { //delete button pressed
      
    let indexBlockToRemove = getBlockIndexAtCursor();
    if (indexBlockToRemove != -1) 
    {
        threeworld.scene.remove(blockArray[indexBlockToRemove]);
        blockArray.splice(indexBlockToRemove, 1);
    }
        
    e.preventDefault();
      
  }
}
  
  
  
  


	this.endCondition = false;

 

this.newRun = function() 
{
	threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 

	var s = "<p>   Use arrow keys and PgUp, PgDn to move cursor, press Enter to plqce q block, Del to delete one. </p>";
 	$("#user_span2").html( s );


 	  init();
	   
	// set up the main key handler:
	document.addEventListener( 'keydown', handleKeyDown   );

};


 

this.nextStep = function()		// not used 
{
};


this.endRun = function()
{
};

 

}