Code viewer for World: Ian Gilligan Castle World

// Cloned by Ian Gilligan on 5 Feb 2019 from World "Castle World" 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 3D model
// OBJ plus MTL

// A "Simple World" like chase inside an invisible arena inside the castle 
// Chase uses x,y,z rather than grid of squares 

 // user_span1 - Status line  

  
  
  

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


AB.clockTick       = 100;    

	// Speed of run: Step every n milliseconds.  
	
AB.maxSteps        = 500;    

	// Length of run: Maximum length of run in steps.  

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


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



// credits:
// https://commons.wikimedia.org/wiki/Category:Pac-Man_icons
// https://commons.wikimedia.org/wiki/Category:Skull_and_crossbone_icons


	const OBJPATH = "/uploads/gilligi2/";	// path of OBJ and MTL 
	const OBJNAME = "PoolTable.obj";
	const MTLNAME = "PoolTable.obj.mtl";

// castle credit
// https://www.script-tutorials.com/webgl-with-three-js-lesson-6/
// http://www.script-tutorials.com/demos/409/index2.html

	

	
const SKYCOLOR 		= 0xffffcc ;				// a number, not a string 
const LIGHTCOLOR 	= 0xffffff ;

const squaresize 	= 50;							// size of cube length  


// basic castle model size 
// see debug line to compute model size below 
// Xsize: 6158.792236328125 Ysize: 703.5784759521484 Zsize: 3791.05908203125

const MODELLENGTH 			= 11054;							
const MODELWIDTH			= 6279; 

const SCALE_CASTLE			= 1;							// scale it by this		 
const SCALEDMODELLENGTH 	= MODELLENGTH * SCALE_CASTLE;	 			 
const SCALEDMODELWIDTH	 	= MODELWIDTH  * SCALE_CASTLE;	 			 

const startRadiusConst 		= 100 //SCALEDMODELLENGTH; 
const maxRadiusConst 		= 2500 //SCALEDMODELLENGTH; 


	// camera points at 0,0 
	// where to put castle? 
	// fit bottom LHS corner to here:
	
	const CX 	= - ( SCALEDMODELLENGTH * 0.3 );		 
	const CZ 	=   ( SCALEDMODELWIDTH  * 0.3 );		 

// how close do they have to be to lose score 

const BADCLOSE = squaresize * 5;



//--- change ABWorld defaults: -------------------------------

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

ABWorld.drawCameraControls = false; 


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


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








	
 // we have a splash screen
 // behind the splash screen, newRun is running, loading resources 
 // do not start the run loop until resources ready AND splash screen is dismissed 

	var resourcesLoaded = false;
	var splashClicked = false;
	
	



	


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

var thecastle;

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

	
	

function loadResources()		// asynchronous file loads - call initScene when finished 
{

	// load castle model OBJ and materials MTL (which reference JPEGs)
	
	var m = new THREE.MTLLoader();
	m.setTexturePath ( OBJPATH );
	m.setPath        ( OBJPATH );
	
	m.load ( MTLNAME, function ( materials ) 
	{
		materials.preload(); 
		var o = new THREE.OBJLoader();
		o.setMaterials ( materials );
		o.setPath ( OBJPATH );
		
		o.load ( OBJNAME, function ( object ) 
		{
			thecastle = object;
			if ( asynchFinished() )	initScene();		 
		});
	});

}


function asynchFinished()		 
{
	if ( thecastle)  return true;  
	else return false;
}	

function initScene()		// file loads have returned 
{
	
 // add castle object to scene  

	thecastle.scale.multiplyScalar ( SCALE_CASTLE );    	  

    thecastle.position.y = 0				// adjust so cubes (centred on 0) appear exactly above castle floor 
    thecastle.position.x = 0
    thecastle.position.z = 0  
 
   	ABWorld.scene.add ( thecastle );
	console.log ( "Xsize: " + ABWorld.objectXsize(thecastle) + " Ysize: " + ABWorld.objectYsize(thecastle) + " Zsize: " + ABWorld.objectZsize(thecastle) );

	// calculate OBJ object size:
	//	console.log ( "Xsize: " + ABWorld.objectXsize(thecastle) + " Ysize: " + ABWorld.objectYsize(thecastle) + " Zsize: " + ABWorld.objectZsize(thecastle) );
	 
	 
 // ready to start run loop? 
	
	ABWorld.render();

	console.log ( "Resources loaded." );
	resourcesLoaded = true;
	
	if ( resourcesLoaded && splashClicked ) 
		ABRun.runReady = true;  		// start run loop 
	
}
 

// --- take actions -----------------------------------


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


this.newRun = function() 
{

	ABWorld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR  ); 

	// newRun can run behind splash screen 
	// do not start run loop until resources ready AND splash screen is dismissed 
	
	ABRun.runReady = false; 
	
	loadResources();		// aynch file loads		
							// calls initScene() when it returns 
 
    var thelight = new THREE.DirectionalLight ( LIGHTCOLOR, 1 );
    thelight.position.set ( 0, 100, 0 );
    ABWorld.scene.add(thelight);
		
};

}

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

// --- Splash screen --------------------------------------------------------------
// Splash screen is to get audio started on mobile/Chrome by user interaction.
// This should works on all platforms - plays background music.

	
	// display standard splash screen (World title, World image, Start button) 
	
	AB.newSplash();

	
	// when user clicks/touches button on splash screen, audio starts and run starts:
		
	$("#splashbutton").click ( function ()        
	{
		AB.removeSplash();			// remove splash screen 
		
		// ready to start run loop? 
		
		splashClicked = true;
		
		if ( resourcesLoaded && splashClicked ) 
			ABRun.runReady = true;  		// start run loop 
		
	});