Code viewer for World: CUBE

// Cloned by Clem on 19 Sep 2023 from World "One Cube World (Three.js)" by Starter user 
// Please leave this clone trail here.
 

const skycolor          = 'lightblue';           
const boxcolor          = 'purple';

const objectsize    = 300;                  // size of object   

const startRadius   = 600;                 // distance from centre we start the camera at

const maxRadius     = startRadius * 15;     // maximum distance from camera we render things 

const circleTexture = '/uploads/clem1916/old_paper.jpg';    // Texture for the circle

const MUSICFILE = '/uploads/starter/SuspenseStrings.mp3';  // Music file


// the object is a cube (each dimension equal): 
var loader      = new THREE.TextureLoader();  
var shape       = new THREE.BoxGeometry ( objectsize, objectsize, objectsize );
var circleshape = new THREE.SphereGeometry ( objectsize - 100, objectsize, objectsize - 100 );
var material    = new THREE.MeshBasicMaterial ( { color: boxcolor.toLowerCase() } );
var theobject   = new THREE.Mesh ( shape, material );
var theobjectcircle = new THREE.Mesh ( circleshape, material );


loader.load( circleTexture, function( thetexture ){
    thetexture.minFilter = THREE.LinearFilter;
    theobjectcircle.material = new THREE.MeshBasicMaterial({ map: thetexture });
});

// Define what the World does at the start of a run: 

AB.world.newRun = function() 
{
    // start a 3D scene: 
    ABWorld.init3d ( startRadius, maxRadius, skycolor ); 

    // add the object to the scene:
    ABWorld.scene.add ( theobject );
    
    // add another object to the scene:
    ABWorld.scene.add ( theobjectcircle );
    
};



function keyHandler ( event )		
{
    
	if ( event.keyCode == 37 )  
	{ 
	    theobject.rotation.z += 0.5;
	    theobject.material.color.setHex(0x800080); //purple
	}  
    if ( event.keyCode == 38 )  
    { 
        theobject.rotation.x -= 0.5;
        theobject.material.color.setHex(0x00ff00); // green
    }	 
    if ( event.keyCode == 39 )  
    { 
        theobject.rotation.z -= 0.5; 
        theobject.material.color.setHex(0xFF0000); // red
    } 	 
    if ( event.keyCode == 40 )  
    { 
        theobject.rotation.x += 0.5;
        theobject.material.color.setHex(0x0000FF); // blue
    }   
	event.stopPropagation(); event.preventDefault(); return false;
}


document.onkeydown = keyHandler;

// Every step spin cube
AB.world.nextStep = function()
{
    theobjectcircle.rotation.y += 0.1;
    theobjectcircle.rotation.x += 0.5;
    
};

AB.backgroundMusic( MUSICFILE );