function initSkybox()
{
// x,y,z positive and negative faces have to be in certain order in the array
var materialArray = [
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/dg/corona_ft.png" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/dg/corona_bk.png" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/dg/corona_up.png" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/dg/corona_dn.png" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/dg/corona_rt.png" ), side: THREE.BackSide } ) ),
( new THREE.MeshBasicMaterial ( { map: THREE.ImageUtils.loadTexture( "/uploads/dg/corona_lf.png" ), 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
}
// World must define these:
const CLOCKTICK = 100; // speed of run - move things every n milliseconds
const MAXSTEPS = 1000; // length of a run before final score
const SCREENSHOT_STEP = 50;
//---- global constants: -------------------------------------------------------
const gridsize = 30; // number of squares along side of world
const NOBOXES = Math.trunc ( (gridsize * gridsize) / 10 );
// density of maze - number of internal boxes
// (bug) use trunc or can get a non-integer
const squaresize = 100; // size of square in pixels
const MAXPOS = gridsize * squaresize; // length of one side in pixels
const SKYCOLOR = 0xF0B27A ; // a number, not a string
const BLANKCOLOR = SKYCOLOR ; // make objects this color until texture arrives (from asynchronous file read)
const LIGHTCOLOR = 0xffffff ;
const show3d = true; // Switch between 3d and 2d view (both using Three.js)
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
//--- Mind can pick one of these actions -----------------
const ACTION_LEFT = 0;
const ACTION_RIGHT = 1;
const ACTION_UP = 2;
const ACTION_DOWN = 3;
const ACTION_STAYSTILL = 4;
const ACTION_JUMPFORWARD = 5; //"cheat code"
// 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; //People moving. User is the agent
var flag; //Aim of the game is to get as many flags as possible before being caught.
//position on squares
var ei, ej, ai, aj; //enemy and agent
var fi, fj; //flag
var badsteps;
var goodsteps;
var step;
var flags; //keeps track of how many flags you find.
var self = this; // needed for private fn to call public fn - see below
//---------------------- Grid -------------------------------------------------------------
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 ( ( fi == i ) && ( fj == j ) ) return true; //make sure flag is not placed on piece of maze.
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 ) );
}
// --- asynchronous load textures from file ----------------------------------------
/* credits:
Characters: Mark Humphrys male02.obj
flag: http://tf3dm.com/3d-model/usmc-flag-67417.html
ALl other images: Google images.
*/
// First textures load when game starts up first.
function loadTexturesFIRST()
{
var manager = new THREE.LoadingManager();
var loader = new THREE.OBJLoader( manager );
loader.load( "/uploads/starter/male02.obj", buildenemy );
loader.load( "/uploads/starter/male02.obj", buildagent);
loader.load( "/uploads/meabhhoran1/usmcflag.obj", buildflag);
var loader1 = new THREE.TextureLoader();
loader1.load ( '/uploads/meabhhoran1/rust.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintWalls ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
var loader2 = new THREE.TextureLoader();
loader2.load ( '/uploads/meabhoran1/grey.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintMaze ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
}
//Second textures are for when the flag is found and reloads maze and new flag position
function loadTexturesSECOND()
{
var manager = new THREE.LoadingManager();
var loader = new THREE.OBJLoader( manager );
loader.load( "/uploads/meabhhoran1/usmcflag.obj", buildflag);
var loader1 = new THREE.TextureLoader();
loader1.load ( '/uploads/meabhhoran1/rust.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintWalls ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
var loader2 = new THREE.TextureLoader();
loader2.load ( '/uploads/meabhhoran1/grey.jpg', function ( thetexture ) {
thetexture.minFilter = THREE.LinearFilter;
paintMaze ( new THREE.MeshBasicMaterial( { map: thetexture } ) );
} );
}
// --- build and paint ----------------------------------------
function buildenemy ( object ) {
object.scale.multiplyScalar ( 2.5 ); // make 3d object n times bigger
object.traverse( paintEnemy );
theenemy = object;
threeworld.scene.add( theenemy );
}
function paintEnemy ( child ) {
if ( child instanceof THREE.Mesh ) {
child.material.map = THREE.ImageUtils.loadTexture( "/uploads/meabhhoran1/redcol.jpg" );
}
}
function buildagent ( object ) {
object.scale.multiplyScalar ( 2.5 ); // make 3d object n times bigger
object.traverse( paintAgent );
theagent = object;
threeworld.scene.add( theagent );
}
function paintAgent ( child ) {
if ( child instanceof THREE.Mesh ) {
child.material.map = THREE.ImageUtils.loadTexture( "/uploads/meabhhoran1/armycol.jpg" );
}
}
function buildflag ( object ) {
object.scale.multiplyScalar ( 5 ); // make 3d object n times bigger
object.traverse( paintFlag );
flag = object;
threeworld.scene.add( flag );
}
function paintFlag ( child ) {
if ( child instanceof THREE.Mesh ) {
child.material.map = THREE.ImageUtils.loadTexture( "/uploads/meabhhoran1/flagirish.jpg" );
}
}