Code viewer for World: Cloned Touch complex

// Cloned by Nathan Bonnard on 14 Jun 2018 from World "Touch complex" 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.
// ==================================================================================================================



// Demo of Mouse and Touch control of objects (rather than the default, control of camera)
 
// No Mind. User controls agent.
// Various features cloned from "Complex World" 

  
// Desktop:
// Mouse scroll camera
// Mouse drag to move agent   
// Mouse click to "zap" enemy

// Mobile:
// Touch pinch camera
// Touch drag to move agent   
// Touch tap to "zap" enemy

 
// ============================================================================================= 
// To do my own mouse/touch: 
// Over-ride certain "threehandler" functions. See below. 
// =============================================================================================


 
 
 
 
 
 

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

 
 
// Switch between 3d and 2d view:

const show3d = true;						  
 
 

AB.clockTick       = 100;    

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

	// 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.



//---- global constants: -------------------------------------------------------

 const TEXTURE_WALL 	= '/uploads/starter/door.jpg' ;
 const TEXTURE_MAZE 	= '/uploads/starter/latin.jpg' ;
 const TEXTURE_AGENT 	= '/uploads/starter/pacman.jpg' ;
 const TEXTURE_ENEMY 	= '/uploads/starter/ghost.3.png' ;
 const TEXTURE_FRIEND 	= '/uploads/starter/green.alien.png' ;
 
// credits:
// http://commons.wikimedia.org/wiki/File:Old_door_handles.jpg
// https://commons.wikimedia.org/wiki/Category:Pac-Man_icons
// https://commons.wikimedia.org/wiki/Category:Skull_and_crossbone_icons
// http://en.wikipedia.org/wiki/File:Inscription_displaying_apices_(from_the_shrine_of_the_Augustales_at_Herculaneum).jpg
// https://commons.wikimedia.org/wiki/File:Smiley_green_alien_huf.svg


const MUSIC_BACK  = '/uploads/starter/Defense.Line.mp3' ;
const SOUND_ALARM = '/uploads/starter/air.horn.mp3' ;

// credits:
// http://www.dl-sounds.com/royalty-free/defense-line/
// http://soundbible.com/1542-Air-Horn.html 


const gridsize = 20;						// number of squares along side of world	   

const NOBOXES =  Math.trunc ( (gridsize * gridsize) / 10 );
		// density of maze - number of internal boxes

const squaresize = 100;					// size of square in pixels
const MAXPOS = gridsize * squaresize;		// length of one side in pixels 
	
const SKYCOLOR 	= 0xddffdd;				// a number, not a string 
const BLANKCOLOR 	= SKYCOLOR ;			// make objects this color until texture arrives (from asynchronous file read)

 
const startRadiusConst	 	= MAXPOS * 0.8 ;		// distance from centre to start the camera at
const skyboxConst			= MAXPOS * 3 ;		// where to put skybox 
const maxRadiusConst 		= MAXPOS * 10  ;		// maximum distance from camera we will render things  



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

threehandler.MAXCAMERAPOS 	= skyboxConst / 2 ;			// keep camera inside skybox 

threehandler.GROUNDZERO		= true;						// "ground" exists at altitude zero


//--- skybox: -------------------------------

// mountain skybox, credit:
// http://stemkoski.github.io/Three.js/Skybox.html

 const SKYBOX_ARRAY = [										 
                "/uploads/starter/dawnmountain-xpos.png",
                "/uploads/starter/dawnmountain-xneg.png",
                "/uploads/starter/dawnmountain-ypos.png",
                "/uploads/starter/dawnmountain-yneg.png",
                "/uploads/starter/dawnmountain-zpos.png",
                "/uploads/starter/dawnmountain-zneg.png"
                ];
				
  
// ===================================================================================================================
// === End of tweaker's box ==========================================================================================
// ===================================================================================================================


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









const ACTION_LEFT 		= 0;		   
const ACTION_RIGHT 		= 1;
const ACTION_UP 			= 2;		 
const ACTION_DOWN 		= 3;
const ACTION_STAYSTILL 		= 4;

// in initial view, (smaller-larger) on i axis is aligned with (left-right)
// in initial view, (smaller-larger) on j axis is aligned with (away from you - towards you)



// contents of a grid square

const GRID_BLANK 	= 0;
const GRID_WALL 	= 1;
const GRID_MAZE 	= 2;
 
 

 
// --- some useful random functions  -------------------------------------------


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

function randomintAtoB ( A, B )			 
{
 return  ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
  
function randomBoolean()			 
{
 if ( Math.random() < 0.5 ) { return false; }
 else { return true; }
}







//---- start of World class -------------------------------------------------------
 
function World() { 


var BOXHEIGHT;		// 3d or 2d box height 


var GRID 	= new Array(gridsize);			// can query GRID about whether squares are occupied, will in fact be initialised as a 2D array   
var WALLS 	= new Array ( 4 * gridsize );		// need to keep handles to wall and maze objects so can find them later to paint them 
var MAZE 	= new Array ( NOBOXES );
var theagent, theenemy;

var agentmaterial, enemymaterial, friendmaterial;


// enemy and agent position on squares
var ei, ej, ai, aj;

var badsteps;
var goodsteps;
var  step;

 	var self = this;						// needed for private fn to call public fn - see below  

 


function initGrid()
{
 for (var i = 0; i < gridsize ; i++) 
 {
  GRID[i] = new Array(gridsize);		// each element is an array 

  for (var j = 0; j < gridsize ; j++) 
  {
   GRID[i][j] = GRID_BLANK ;
  }
 }
}

function occupied ( i, j )		// is this square occupied
{
 if ( ( ei == i ) && ( ej == j ) ) return true;		// variable objects 
 if ( ( ai == i ) && ( aj == j ) ) return true;

 if ( GRID[i][j] == GRID_WALL ) return true;		// fixed objects	 
 if ( GRID[i][j] == GRID_MAZE ) return true;		 
	 
 return false;
}

 
// logically, coordinates are: y=0, x and z all positive (no negative)    
// logically my dimensions are all positive 0 to MAXPOS
// to centre everything on origin, subtract (MAXPOS/2) from all dimensions 

function translate ( x ) 
{
 return ( x - ( MAXPOS/2 ) );
}





//--- skybox ----------------------------------------------------------------------------------------------


function initSkybox() 
{
  var materialArray = [
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( SKYBOX_ARRAY[0] ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( SKYBOX_ARRAY[1] ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( SKYBOX_ARRAY[2] ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( SKYBOX_ARRAY[3] ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( SKYBOX_ARRAY[4] ), side: THREE.BackSide } ) ),
 	( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( SKYBOX_ARRAY[5] ), side: THREE.BackSide } ) )
 	];

  var skyGeometry = new THREE.CubeGeometry ( skyboxConst, skyboxConst, skyboxConst );	
  var skyMaterial = new THREE.MeshFaceMaterial ( materialArray );
  var theskybox = new THREE.Mesh ( skyGeometry, skyMaterial );
  threeworld.scene.add( theskybox );						// We are inside a giant cube
}







// --- asynchronous load textures from file ----------------------------------------
 

function loadTextures()
{

 var loader1 = new THREE.TextureLoader();
 loader1.load ( TEXTURE_WALL,		function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		paintWalls ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
	} ); 

 var loader2 = new THREE.TextureLoader();
 loader2.load ( TEXTURE_MAZE,		function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		paintMaze ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
 	} ); 

 var loader3 = new THREE.TextureLoader();
 loader3.load ( TEXTURE_AGENT,		function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		agentmaterial =  new THREE.MeshBasicMaterial( { map: thetexture } );
		theagent.material = agentmaterial;
	} ); 

 var loader4 = new THREE.TextureLoader();
 loader4.load ( TEXTURE_ENEMY,		function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		enemymaterial =  new THREE.MeshBasicMaterial( { map: thetexture } );
		theenemy.material = enemymaterial;
	} ); 

 var loader5 = new THREE.TextureLoader();
 loader5.load ( TEXTURE_FRIEND,		function ( thetexture ) {			 
		thetexture.minFilter = THREE.LinearFilter;
		friendmaterial =  new THREE.MeshBasicMaterial( { map: thetexture } );
	} ); 
	
}

 
 


// --- add fixed objects ---------------------------------------- 
   
 
function initLogicalWalls()		// set up logical walls in data structure	
{
 for (var i = 0; i < gridsize ; i++) 
  for (var j = 0; j < gridsize ; j++) 
   if ( ( i==0 ) || ( i==gridsize-1 ) || ( j==0 ) || ( j==gridsize-1 ) )
   {
    	GRID[i][j] = GRID_WALL ;		 
   }
}


function initThreeWalls()		// set up blank boxes, painted later 	
{
 var t = 0;
 for (var i = 0; i < gridsize ; i++) 
  for (var j = 0; j < gridsize ; j++) 
   if ( GRID[i][j] == GRID_WALL )
   {
 	 var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
 	 var thecube  = new THREE.Mesh( shape );
	 thecube.material.color.setHex( BLANKCOLOR  );			  
 
    	 thecube.position.x = translate ( i * squaresize );   		// translate my simple (i,j) block-numbering coordinates to three.js (x,y,z) coordinates 
    	 thecube.position.z = translate ( j * squaresize );   	
    	 thecube.position.y =  0;	
 
 	 threeworld.scene.add(thecube);
	 WALLS[t] = thecube;				// save it for later
	 t++; 
   }
}


function paintWalls ( material )		 
{
 for ( var i = 0; i < WALLS.length; i++ )
 { 
   if ( WALLS[i] )  WALLS[i].material = material;
 }
}





function initLogicalMaze()		 
{
 for ( var c=1 ; c <= NOBOXES ; c++ )
 {
  	var i = randomintAtoB(1,gridsize-2);	// inner squares are 1 to gridsize-2
  	var j = randomintAtoB(1,gridsize-2);
    	GRID[i][j] = GRID_MAZE ;		 
 }
}


function initThreeMaze()		  	
{
 var t = 0;
 for (var i = 0; i < gridsize ; i++) 
  for (var j = 0; j < gridsize ; j++) 
   if ( GRID[i][j] == GRID_MAZE )
   {
   	var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
  	var thecube  = new THREE.Mesh( shape );
	thecube.material.color.setHex( BLANKCOLOR  );			  

  	thecube.position.x = translate ( i * squaresize );   	
  	thecube.position.z = translate ( j * squaresize );   	
  	thecube.position.y =  0;	
 
 	threeworld.scene.add(thecube);
	MAZE[t] = thecube;		// save it for later
	t++; 
   }
}


function paintMaze ( material )		 
{
 for ( var i = 0; i < MAZE.length; i++ )
 { 
   if ( MAZE[i] )  MAZE[i].material = material;
 }
}







// --- enemy functions -----------------------------------


function drawEnemy()	// given ei, ej, draw it 
{
  var x = translate ( ei * squaresize );   	
  var z = translate ( ej * squaresize );   	
  var y =  0;	

 theenemy.position.x = x;
 theenemy.position.y = y;
 theenemy.position.z = z;
 threeworld.scene.add(theenemy);

 threeworld.lookat.copy ( theenemy.position );		// if camera moving, look back at where the enemy is  
}


function initLogicalEnemy()
{
// start in random location:
 var i, j;
 do
 {
  i = randomintAtoB(1,gridsize-2);
  j = randomintAtoB(1,gridsize-2);
 }
 while ( occupied(i,j) );  	  // search for empty square 

 ei = i;
 ej = j;
}


function initThreeEnemy()
{
 var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
 theenemy = new THREE.Mesh( shape );
 theenemy.material.color.setHex( BLANKCOLOR  );	
 drawEnemy();		  
}


function moveLogicalEnemy()
{ 
// move towards agent 
// put some randomness in so it won't get stuck with barriers 

 var i, j;
 if ( ei < ai ) i = randomintAtoB(ei, ei+1); 
 if ( ei == ai ) i = ei; 
 if ( ei > ai ) i = randomintAtoB(ei-1, ei); 

 if ( ej < aj ) j = randomintAtoB(ej, ej+1); 
 if ( ej == aj ) j = ej; 
 if ( ej > aj ) j = randomintAtoB(ej-1, ej); 
 
 if ( ! occupied(i,j) )  	// if no obstacle then move, else just miss a turn
 {
  ei = i;
  ej = j;
 }
}





// --- agent functions -----------------------------------


function drawAgent()	// given ai, aj, draw it 
{
  var x = translate ( ai * squaresize );   	
  var z = translate ( aj * squaresize );   	
  var y =  0;	

 theagent.position.x = x;
 theagent.position.y = y;
 theagent.position.z = z;
 threeworld.scene.add(theagent);

 threeworld.follow.copy ( theagent.position );		// follow vector = agent position (for camera following agent)
}


function initLogicalAgent()
{
// start in random location:
 var i, j;
 do
 {
  i = randomintAtoB(1,gridsize-2);
  j = randomintAtoB(1,gridsize-2);
 }
 while ( occupied(i,j) );  	  // search for empty square 

 ai = i;
 aj = j;
}

function initThreeAgent()
{
 var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
 theagent = new THREE.Mesh( shape );
 theagent.material.color.setHex( BLANKCOLOR );	
 drawAgent(); 		  
}


function moveLogicalAgent( a )			 
{ 
 var i = ai;
 var j = aj;		 

      if ( a == ACTION_LEFT ) 	i--;
 else if ( a == ACTION_RIGHT ) 	i++;
 else if ( a == ACTION_UP ) 		j++;
 else if ( a == ACTION_DOWN ) 	j--;

 if ( ! occupied(i,j) ) 
 {
  ai = i;
  aj = j;
 }
}






// --- score and status: -----------------------------------
// score uses user_span5 to user_span7


function badstep()			// is the enemy within one square of the agent
{
 if ( ( Math.abs(ei - ai) < 2 ) && ( Math.abs(ej - aj) < 2 ) ) return true;
 else return false;
}


function agentBlocked()			// agent is blocked on all sides, run over
{
 return ( 	occupied (ai-1,aj) 		&& 
		occupied (ai+1,aj)		&&
		occupied (  ai,aj+1)		&&
		occupied (  ai,aj-1) 	);		
} 


function updateStatusBefore() 
{
 var x = self.getState();
 var status = " Step: <b> " + step + " </b> &nbsp; x = (" + x.toString() + ") "; 

 $("#user_span5").html( status );
}


function   updateStatusAfter()		// agent and enemy have moved, can calculate score
{
 // new state after both have moved
 var y = self.getState();
 var status = " &nbsp; y = (" + y.toString() + ") <BR> "; 
 $("#user_span6").html( status );

 var score = self.getScore();

 var status = "   Bad steps: " + badsteps + 
		" &nbsp; Good steps: " + goodsteps + 
		" &nbsp; Score: " + score.toFixed(2) + "% "; 

 $("#user_span7").html( status );
}





// --- keyboard and touch handling functions: ----------------------------------------


function keyHandler ( e )		
{
    if (e.keyCode == 37)  { moveLogicalAgent ( ACTION_LEFT 		); 	e.preventDefault(); }
    if (e.keyCode == 38)  { moveLogicalAgent ( ACTION_DOWN  	); 	e.preventDefault(); }
    if (e.keyCode == 39)  { moveLogicalAgent ( ACTION_RIGHT 	); 	e.preventDefault(); }
    if (e.keyCode == 40)  { moveLogicalAgent ( ACTION_UP		); 	e.preventDefault(); }
};



var startX, startY; 

var dragevents;		// number of events in the current drag 


function initDrag ( x, y )		// x,y position on screen
{
	startX 	= x;						 
	startY 	= y;
	dragevents = 0;
	
	if ( threeworld.hitsObject ( x, y, theenemy ) )	      // check if this is a tap not a drag - and if it hits the enemy  
		zapEnemy();
};



function drag ( x, y )			// compare with previous x,y position on screen to get direction of drag
{
	if ( ( dragevents % 4 ) == 0 )		 // slow it down to respond to every nth event - too many events
	{
			 if ( x > startX ) 	moveLogicalAgent ( ACTION_RIGHT 	);
		else if ( x < startX ) 	moveLogicalAgent ( ACTION_LEFT 		);

			 if ( y > startY ) 	moveLogicalAgent ( ACTION_UP 		);
		else if ( y < startY ) 	moveLogicalAgent ( ACTION_DOWN		);
	}
	
	dragevents++;
	
	startX 	= x;						 
	startY 	= y;
};
 
 

function zapEnemy()
// we hit the enemy with a touch or click
{
    // this enemy converts to "dead enemy", stationary 
    // draw dead enemy block at current enemy position

   	var shape    = new THREE.BoxGeometry( squaresize, BOXHEIGHT, squaresize );			 
  	var thecube  = new THREE.Mesh( shape );

  	thecube.position.x = translate ( ei * squaresize );   	
  	thecube.position.z = translate ( ej * squaresize );   	
  	thecube.position.y =  0;	
 
    thecube.material = friendmaterial;
 	threeworld.scene.add(thecube);
 	
	GRID[ei][ej] = GRID_MAZE ;      // obstacle - basically part of the maze now 

    // move the moving enemy to random new position

    initLogicalEnemy();
}
    
    
    

//--- public functions / interface / API ----------------------------------------------------------


	this.endCondition = false;			// If set to true, run will end. 



this.newRun = function() 
{
	badsteps = 0;	
	goodsteps = 0;
	step = 0;


 // for all runs:

 	initGrid();
	initLogicalWalls(); 
	initLogicalMaze();
	initLogicalAgent();
	initLogicalEnemy();


	if ( show3d )
	{
	 BOXHEIGHT = squaresize;
	 threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 	
	}	     
	else
	{
	 BOXHEIGHT = 1;
	 threeworld.init2d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 		     
	}

	initSkybox();
	
	
 	initMusic();

// notice in user_span4
 if ( AB.onDesktop() )  $("#user_span4").html( "<p>  Drag to move agent. Scroll to zoom camera. Click on enemy to kill it. </p>" );
 else 					$("#user_span4").html( "<p>  Drag to move agent. Pinch to zoom camera. Tap on enemy to kill it. </p>" );

 
	// Set up objects first:

	initThreeWalls(); 
	initThreeMaze();
	initThreeAgent();
	initThreeEnemy();

	// Then paint them with textures - asynchronous load of textures from files. 
	// The texture file loads return at some unknown future time in some unknown order.
	// Because of the unknown order, it is probably best to make objects first and later paint them, rather than have the objects made when the file reads return.
	// It is safe to paint objects in random order, but might not be safe to create objects in random order. 

	loadTextures();	
	

	
	// ---------------------------------------------------------------------------------------------------------------------------
	
	// --- redirect keyboard and touch event handling to my own functions: ----------------------------------------

		document.onkeydown 			= keyHandler;
 
	// override threehandler default (which is camera control) to use my own functions:

		threehandler.initTouchDrag 	= initDrag;
		threehandler.touchDrag		= drag
		
		threehandler.initMouseDrag 	= initDrag;
		threehandler.mouseDrag		= drag
		
	// ---------------------------------------------------------------------------------------------------------------------------

};





this.getState = function()
{
 var x = [ ai, aj, ei, ej ];
  return ( x );  
};



this.nextStep = function ()
{
  step++;

  updateStatusBefore();			// show status line before moves 

  if ( ( step % 2 ) == 0 )		// slow the enemy down to every nth step
    moveLogicalEnemy();

        // no agent move - moved by human 

  if ( badstep() )
   badsteps++;
  else
   goodsteps++;

   drawAgent();
   drawEnemy();
   updateStatusAfter();			// show status line after moves  


  if ( agentBlocked() )			// if agent blocked in, run over 
  {
	this.endCondition = true;
	goodsteps = 0;			// you score zero as far as database is concerned 			 
	musicPause();
	soundAlarm();
  }

};


// end uses user_span8

this.endRun = function()
{
  musicPause(); 
  if ( this.endCondition )
    $("#user_span8").html( " &nbsp; <font color=red> <B> Agent trapped. Final score zero. </B> </font>   "  );
  else
    $("#user_span8").html( " &nbsp; <font color=red> <B> Run over. </B> </font>   "  );
};


this.getScore = function()
{
 return ( ( goodsteps / step ) * 100 );
};




}

//---- end of World class -------------------------------------------------------





  
 

 

// --- music and sound effects ----------------------------------------


var backmusic = new Audio ( MUSIC_BACK );
 

function initMusic()	
{
	backmusic.loop = true;                 // loop forever 
	backmusic.play();

	$("#w2m_audio1").html( " <img class=audiobutton  onclick='musicPlay();'  width=25  src='images/audio.on.1.png'  > " );	
	$("#w2m_audio2").html( " <img class=audiobutton  onclick='musicPause();' width=25  src='images/audio.off.1.png' > " );	
} 
 
	
function musicPlay()   { backmusic.play();  }
function musicPause()  { backmusic.pause(); }

											 
function soundAlarm()
{
	var alarm = new Audio ( SOUND_ALARM );
	alarm.play();							// play once, no loop 
}