Code viewer for World: No brain

// Cloned by Enhanced on 12 Jun 2018 from World "No brain" by SinfulSalad 
// 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.
// ==================================================================================================================





// ===================================================================================================================
// === 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?




//describes the ammount of time between each step of the run (in milliseconds)
//33 means the program will generate 30 images per second
//note that the program will not run properly if set under 18
AB.clockTick       = 33;

//describes the number of steps the program is allowed to generate before terminating
//1 000 000 means that the program will run for approximately 9 hours, if not terminated by the user
AB.maxSteps        = 1000000;    

//take screenshot on this step. (All resources should have finished loading.)
AB.screenshotStep  = 50;   

//Regulates the speed of the animation
//1 is the default speed
//2 is two times as fast
//0.5 is 50% slower...
var SPEED = 1;

//if set to true, will apply a weird effect that changes how the camera perceive space
var EXPAND_SPACE_EFFECT = false;

//if set to true, will slightly break the symmetry of the animation
var FLOATY_EFFECT = false;

var HIGH_SPEED_EFFECT = false;
 
//all the textures displayed by the program should be placed in this array
const FILE_ARRAY = [
		"/uploads/meak/text1.jpeg",
		"/uploads/meak/text2.jpg",
		"/uploads/meak/text3.jpg",
		"/uploads/meak/text4.jpg",
		"/uploads/meak/text5.jpg"
		];


//describes the color of the sky
//0x000000 means black, and 0xffffff means white
//checkout "html color hex values" on internet for more info
const SKYCOLOR 	= 0x000000;

//describes the size of every generated object
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) ) );
}

//returns a whole number in from the [A,B[ range
function randomintAtoB ( A, B )			 
{
 return  ( Math.floor ( randomfloatAtoB ( A, B ) ) );
}
  




function World() { 

var time = 0; //will be incremented at each step

var textureArray = []; //used to paint the generated shapes with the textures added above
var blockArray = []; //each shape created will be added to this array
var weirdBlockArray = []; //some shapes require special manipulations, and are also added to this array


//each of the following variables are only needed for the function "architect()" to work properly
var mode = 3; //describes which animation is currently being generated
var trackbool = true; //allow to keep information from one call of the architect() function to another
var trackint = 0; //allow to keep information from one call of the architect() function to another
var tracktime = 0;
var yposArray = [0, 1, 1, 1, 0, -1, -1, -1]; //allow mode 3 to accurately position its shapes
var zposArray = [1, 1, 0, -1, -1, -1, 0, 1]; //allow mode 3 to accurately position its shapes


function toggleFloat()
{
    FLOATY_EFFECT = !FLOATY_EFFECT;
}

function toggleDistorsion()
{
    EXPAND_SPACE_EFFECT = !EXPAND_SPACE_EFFECT;
    if (!EXPAND_SPACE_EFFECT)
    {
        threeworld.camera.fov = 75;
        threeworld.camera.updateProjectionMatrix();
    }
}

function toggleHighSpeed()
{
    HIGH_SPEED_EFFECT = !HIGH_SPEED_EFFECT;
    
    if (HIGH_SPEED_EFFECT)
        {SPEED *= 4;}
    else
        {SPEED /= 4;}
    
}
   

function init()		 
{
    
    //SET TEXTURES
	textureArray = [FILE_ARRAY.length];
    for (let i = 0; i < FILE_ARRAY.length; i++)
    {
        textureArray[i] = new THREE.ImageUtils.loadTexture( FILE_ARRAY[i] );
    }
    
	for ( let i = 0; i < textureArray.length; i++ )    // for all textures 
	{ 
     textureArray[i].minFilter = THREE.LinearFilter;
	}
	
	
	//SET CAMERA	
	threeworld.camera.position.set(-6000,0,0);
	threeworld.camera.rotation.set(Math.PI, -Math.PI/2, Math.PI);
	
	
	//GENERATE THE FIRST SHAPES
	for (var i = 9; i>=0 ; i--)
	{
    	addBlock(-2*i*objectsize, objectsize, objectsize);
    	addBlock(-2*i*objectsize, -objectsize, objectsize);
    	addBlock(-2*i*objectsize, objectsize, -objectsize);
    	addBlock(-2*i*objectsize, -objectsize, -objectsize);
	}
	
	//SET THE MENU OPTIONS
	var s1 = "<p>Tweak the animation with the following options!</p>";
	var s2 = "<p><button id='floatButton'>Floaty</button> <button id='distorsionButton'>Distorted</button> <button id='speedButton'>Hyperspeed</button></p>"
 	$("#user_span4").html( s1 );
 	$("#user_span5").html( s2 );
 	
    document.getElementById("floatButton").addEventListener("click", toggleFloat);
    document.getElementById("distorsionButton").addEventListener("click", toggleDistorsion);
    document.getElementById("speedButton").addEventListener("click", toggleHighSpeed);
 
}


//apply a random texture to an object
function paintThis( object )
{
        var t = randomintAtoB ( 0, textureArray.length - 1 );     // random texture 
    
        object.material =  new THREE.MeshBasicMaterial ( { transparent: true, opacity: 1, map: textureArray[t] } ); 
}

//place a shape at a specified location
//if an array "dest" is specified, it will add the created shape to that specified destination 
function addBlock(x, y, z, dest=undefined)
{
    var shape  = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
    var block  = new THREE.Mesh( shape );
    
    block.position.x = x;     
    block.position.y = y;
    block.position.z = z;
    
    threeworld.scene.add(block);
    paintThis ( block );
    
    blockArray.push(block);
    
    if (dest !== undefined)
        {dest.push(block);}
        
}

//function in charge of building all the shapes
function architect()
{
    //every 600 steps, the function changes mode
    if (time != 0 && Math.floor(time%((30/AB.clockTick*1000)/SPEED)) == 0)
    {
        mode++;
        if (mode > 4)
            {mode = 1;}
        trackint = 0;
        trackbool = 0;
        if(mode == 4)
            {tracktime = 0;}
        
    }
      
   switch (mode){
       case 1 :
            //cubes are created 4 by 4, at a distance of one cube from one another
            if (Math.floor(time%((1.5/AB.clockTick*1000)/SPEED)) == 0 && time != 0)
            {
                addBlock(0, objectsize, objectsize);
                addBlock(0, -objectsize, objectsize);
                addBlock(0, objectsize, -objectsize);
                addBlock(0, -objectsize, -objectsize);
            }
            break;
            
        case 2 :
            //half of the time, the cubes are created "diagonally"
            if (trackbool)
            {
               if (Math.floor(time%((1.5/AB.clockTick*1000)/SPEED)) == 0 && time != 0)
                {
                    addBlock(0, objectsize, objectsize);
                    addBlock(0, -objectsize, objectsize);
                    addBlock(0, objectsize, -objectsize);
                    addBlock(0, -objectsize, -objectsize);
                    trackbool = !trackbool;
                } 
            }
            else
            {
                if (Math.floor(time%((1.5/AB.clockTick*1000)/SPEED)) == 0 && time != 0)
                {
                    addBlock(0, 2*objectsize, 0);
                    addBlock(0, -2*objectsize, 0);
                    addBlock(0, 0, -2*objectsize);
                    addBlock(0, 0, 2*objectsize);
                    trackbool = !trackbool;
                } 
            }
            break;
        
        case 3 :
            //generates 2 long string of cubes that are symmetric to one another
            if (Math.floor(time%((0.75/AB.clockTick*1000)/SPEED)) == 0 && time != 0)
            {
                addBlock(0, yposArray[trackint]*objectsize, zposArray[trackint]*objectsize);
                addBlock(0, -yposArray[trackint]*objectsize, -zposArray[trackint]*objectsize);
                
                if (trackbool == true)
                {
                    nextBlockPos = randomintAtoB(0,3)-1;
                    if (nextBlockPos != 0)
                    {
                        trackint += nextBlockPos;
                        if (trackint < 0)
                            {trackint = 7;}
                        if (trackint > 7)
                            {trackint = 0;}
                            
                        addBlock(0, yposArray[trackint]*objectsize, zposArray[trackint]*objectsize);
                        addBlock(0, -yposArray[trackint]*objectsize, -zposArray[trackint]*objectsize);
                        trackbool = false;
                    }
                    
                    
                }
                else
                    {trackbool = true;} 
                
            }
            break;
            
        case 4 :
            //create a long string of cubes that will spiral its way through space
            if (Math.floor(time%((0.75/AB.clockTick*1000)/SPEED)) == 0 && time != 0)
                {addBlock(0, 0, objectsize, weirdBlockArray);}
            tracktime++;
            break;
            
    }
    
}

 

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

    init();
};


 

this.nextStep = function() 
{
    
    //all shapes are drawn closer to the camera
    blockArray.forEach(function (block) {
        block.position.x -= SPEED*400*AB.clockTick/1000;
    });
    
    //manages the motion of the shapes created by mode 4
    weirdBlockArray.forEach(function (block) {
        block.position.y = (objectsize-block.position.x/9)*Math.sin(block.position.x*tracktime/300000*SPEED);
        block.position.z = (objectsize-block.position.x/9)*Math.cos(block.position.x*tracktime/300000*SPEED);
        block.rotateX(20*AB.clockTick/1000*tracktime/30000*2.3*SPEED);
    });
    
    
    //apply a weird optic effect if the constant is set to true
    if (FLOATY_EFFECT)
    {
        blockArray.forEach(function (block) {
            block.position.y += 1.3*20*AB.clockTick/1000*Math.sin(block.position.x/400);
            block.position.z += 1.3*20*AB.clockTick/1000*Math.cos(block.position.x/400);
        });
    }
    
    //apply a weird optic effect if the constant is set to true
    if (EXPAND_SPACE_EFFECT)
    {
        threeworld.camera.fov = 112+(65*Math.sin(time/55));
        threeworld.camera.updateProjectionMatrix();    
    }

    //build more shapes if necessary
    architect();
    
    
    //destroy the shapes that are far behind the camera (and that can't be seen anymore)
    while(blockArray[0].position.x <= -7000 && blockArray.length > 0)
    {
        threeworld.scene.remove(blockArray[0]);
        blockArray = blockArray.splice(1, blockArray.length-1);
    }
    
    //increment time
 	time ++;
 	console.log("nb cubes : "+blockArray.length);
};


this.endRun = function()
{
    
};

 

}