Code viewer for World: Interstellar cubes

// Cloned by Thomas Mc Cann on 16 Oct 2019 from World "One Cube World (P5) (clone by TMC)" by Thomas Mc Cann 
// Please leave this clone trail here.
 


// Cloned by Thomas Mc Cann on 16 Oct 2019 from World "One Cube World (P5)" by Starter user 
// Please leave this clone trail here.


var zoom = .4;
var backGround;

const MUSICFILE = "uploads/midnightsky/Interstellar.mp3";
//const MUSICFILE = '/uploads/starter/SuspenseStrings.mp3';
AB.backgroundMusic(MUSICFILE); 
 
const objectsize    = 100;      // size of object   

const anglechange   = 0.01;     // how much the rotate angle changes each step 

var angle = 0;                  // rotate angle starts at 0  

var grow = 0;

var numboxes = 100;

var blackhole;
var clickAllowed = false;
var clickCount = 0;

var images = new Array(4);

var boxPositions = new Array(numboxes);         	// array of the box positions



for ( var i=0; i < numboxes; i++ )   	// set up the array
{
    
    if(i%3 === 0){
        boxPositions[i] = [ AB.randomIntAtoB(-200,200), AB.randomIntAtoB(-200,200), AB.randomIntAtoB(-200,200) ];
    }
    else{
        boxPositions[i] = [ AB.randomIntAtoB(200,-200), AB.randomIntAtoB(200,-200), AB.randomIntAtoB(200,-200) ];
    }
    
    // boxPositions[i] = [ AB.randomIntAtoB(-200,200), AB.randomIntAtoB(-200,200), AB.randomIntAtoB(-200,200) ];
    
}

var boxSizes = new Array(numboxes);         	// array of the box positions

for ( var i=0; i < numboxes; i++ )   	// set up the array
{
    boxSizes[i] = AB.randomIntAtoB(10,100);
}


function preload() 
{
    images[0] = loadImage (  "/uploads/midnightsky/synapse.jpg" );
     images[1] = loadImage (  "/uploads/midnightsky/brain1.jpg" );
     images[2] = loadImage (  "/uploads/midnightsky/synapses2.jpg" );
     images[3] = loadImage (  "/uploads/midnightsky/brain2.jpg" );
     blackhole = loadImage (  "/uploads/midnightsky/blackhole2.jpg" );
     backGround = loadImage (  "/uploads/midnightsky/space1.jpg" );
     
}


function setup()        // "setup" is called once at start of run 
{
    
    createCanvas ( ABWorld.fullwidth(), ABWorld.fullheight(),  WEBGL );
 
    setTimeout(function() {clickAllowed = true; console.log("\n\n4 minutes have passed.\nYou may now try clicking...... to peel away space and time.")}, 240000); // Change 240000 to 100 to allow instance clicking
    setTimeout(function() {console.log("\n\n1 minute has passed")}, 60000);
    setTimeout(function() {console.log("\n\n2 minutes have passed\nSpace and time are collapsing.")}, 120000);
    setTimeout(function() {console.log("\n\n3 minutes have passed\nYou move inside the tesseract, lost in between space and time. ")}, 180000);
    
    AB.msg ( "<p>Press <strong>F12 to view console!!</strong></p><p>Then try clicking and check the console</p><p>Or cheat and press <strong>z</strong> to cycle zoom levels or the <strong>space</strong> bar to allow instant clicking</p> " );
    
}

function draw()         // "draw" is called every timestep during run 
{
    scale(zoom - grow/1500);
    //background("#292b47");    // background color #0c4f66 #114152 #5a6a70 #0c0d1c #034b66 #242863 #292b47
    background(0);
    
    /*texture(backGround);
    box(screen.width*(zoom - grow/1500),screen.height*(zoom - grow/1500));*/
           
    rotateX(angle);             // set each dimension rotation angle to "angle"
    rotateY(angle);
    rotateZ(angle);
  
    //texture(images[0]);  
  
    for ( var i=0; i < numboxes; i++ )
    {
        if(i === 0){
            texture(blackhole);
            //translate( boxPositions[i][0], boxPositions[i][1], boxPositions[i][2] );		// get box position i 
            box(boxSizes[i]+grow);
        }
        else if(i === 1){
            texture(images[1]);
            box(boxSizes[i-1]+(grow*1.2));
        }
        else{
            texture(images[i%4]);
            translate( boxPositions[i][0], boxPositions[i][1], boxPositions[i][2] );		// get box position i 
            box(boxSizes[i]+grow);
        }
        
        
    }

    
    angle = angle + anglechange ;       // change angle each step to get rotate movement    
    
    grow = grow + .3; // Grow cubes by 30%
}

// mousePressed function 
// Excuse the state of this code. It's needs a tidy.

function mousePressed() {
    if(numboxes > 1 && clickAllowed === true ){
        clickCount++;
        numboxes--;
        if(numboxes == 1){
            console.clear();
            console.log("\n\n\n\n\nIt's just you and nothing now.... floating in space, staring in to the event horizon.");
        }
        else{
            if(clickCount > 78 ){
            console.log("Slow down a little and enjoy the journey but keep clicking....You are nearly there.");
            }
            else {
                console.log("Keep clicking.....");
            }
        }
        
    }
    else if(clickAllowed === true){
        console.clear();
            console.log("\n\n\n\n\nIt's just you and nothing now.... floating in space, staring in to the event horizon.");
    }
    else{
        //console.clear();
        
        console.log("\n\nYou must wait and go on a journey.....\nRelax and watch as space and time collapse.\nYou will see when the time is right.");
    }
    
}


document.addEventListener("keypress", magicBypass);

// Bypass the wait time and change zoom/scale level to allow instance clicking and viewing from differnt perspectives
// space bar press allows clicking which reduces the number of cubes
// z will change the zoom cycling through 9 different scale levels from (from deafult .3 to 1,2,3,4,5,6,7,8) - 7/8 should be inside the event horizon

const zoomMin = 1;
const zoomMax = 7;

function magicBypass(e) {
  var key = event.which || event.keyCode;  // Use either which or keyCode, depending on browser support
  
  if(key == 32)
  {
        clickAllowed = true;
        
        console.log("You pressed the magic 'space bar' key. You can now click anytime.");
        AB.msg ("<p>Clicking reduces the number of cubes by 1. <strong>You can't remove the last special cube</strong>.</p><Pressing z will cycle through 8 zoom levels.</p>");
  }
  else if(key == 122 )
  {
        //console.clear();
        console.log("You pressed the magic key. Press 'z' key. Use 'z' to change your perspective.");
        AB.msg ("</p>Pressing z will cycle through <strong>8</strong> zoom levels. <strong></p><p>CURRENT ZOOM LEVEL: " + zoom + "</strong><p>Clicking reduces the number of cubes by 1. <strong>You can't remove the last special cube</strong>.</p>");
        
      if(zoom < zoomMin)
      {
         zoom = 1; 
      }
      else if (zoom > zoomMax)
      {
            zoom = .3;
        }
        else
        {
            zoom++;
        }
  }
}