// 1 pick 1st image
// 2 pick 2nd image
// etc.
var oldobject; // the last object we made
var currentimage = 0; // 0 is first item in array (list) of images
// human presses 1 to 6
// map to array positions 0 to 5
var camerahover;
const CLOCKTICK = 100; // speed of run - move things every n milliseconds
const MAXSTEPS = 1000; // length of a run before final score
const objectsize = 100 ;
const MAXPOS = 1000 ; // start things within these bounds
const startRadiusConst = MAXPOS * 2 ; // distance from centre to start the camera at
const maxRadiusConst = MAXPOS * 5 ; // maximum distance from camera we will render things
const SKYCOLOR = 0xffffff; // a number, not a string
function randomfloatAtoB ( A, B )
{
return ( A + ( Math.random() * (B-A) ) );
}
function randomintAtoB ( A, B )
{
return ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
function World() {
// bad name for global variable - too easy to re-use
var x, y, z; // current location
var textureArray;
function init()
{
// create one box to start
var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
var theobject = new THREE.Mesh( shape );
theobject.position.x = 0; // randomintAtoB ( -MAXPOS, MAXPOS );
theobject.position.z = 0; // randomintAtoB ( -MAXPOS, MAXPOS );
theobject.position.y = 0; // randomintAtoB ( -MAXPOS, MAXPOS );
x = theobject.position.x; // current position
y = theobject.position.y;
z = theobject.position.z;
threeworld.scene.add(theobject);
// save a link to it
oldobject = theobject;
textureArray = [
( new THREE.ImageUtils.loadTexture( "/uploads/tus/mcstone.jpg" ) ),
( new THREE.ImageUtils.loadTexture( "/uploads/tus/crafting_table_front_1447926936.png" ) ),
( new THREE.ImageUtils.loadTexture( "/uploads/tus/crafting-table_4309954_lrg.jpg" ) ),
( new THREE.ImageUtils.loadTexture( "/uploads/tus/minecraft-wallpaper-diamond-04.jpg" ) ),
( new THREE.ImageUtils.loadTexture( "/uploads/tus/_1080p__minecraft_emerald_ore_wallpaper_by_iwithered-d6nzwm0.png" ) ),
( new THREE.ImageUtils.loadTexture( "/uploads/tus/orange.jpg" ) )
];
for ( var i = 0; i < textureArray.length; i++ ) // for all textures
{
textureArray[i].minFilter = THREE.LinearFilter;
}
theobject.material.color.setHex( 0x006600 );
// paintThis ( theobject );
}
function handleKeyDown (event)
{
var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
var theobject = new THREE.Mesh( shape );
theobject.material.color.setHex( 0x006600 );
theobject.position.x = x; // default position
theobject.position.y = y;
theobject.position.z = z;
var code = event.keyCode;
if (code == 37) theobject.position.x = x - objectsize ; // left
if (code == 39) theobject.position.x = x + objectsize ; // right
if (code == 38) theobject.position.z = z - objectsize ; // forward
if (code == 40) theobject.position.z = z + objectsize ; // back
if (code == 34) theobject.position.y = y - objectsize ; // page up and down
if (code == 33) theobject.position.y = y + objectsize ;
// press numbers:
if (code == 49) currentimage = 0; // press 1
if (code == 50) currentimage = 1;
if (code == 51) currentimage = 2;
if (code == 52) currentimage = 3;
if (code == 53) currentimage = 4;
if (code == 54) currentimage = 5;
// your job: write the other 5
x = theobject.position.x; // current position
y = theobject.position.y;
z = theobject.position.z;
var string = "(x,y,z) = (" + x + "," + y + "," + z + ")" ;
console.log ( string );
$("#user_span1").html( string );
threeworld.lookat.copy ( theobject.position );
threeworld.scene.add(theobject);
paintThis ( oldobject );
oldobject = theobject;
// threeworld.lookat.copy ( theobject.position );
// threeworld.follow.copy ( theobject.position );
var x1 = theobject.position.x + 500;
var y1 = theobject.position.y + 500;
var z1 = theobject.position.z + 500;
var cameraposition = new THREE.Vector3( x1, y1, z1 );
var cstring = " camera (x,y,z) = (" + x1 + "," + y1 + "," + z1 + ")" ;
$("#user_span1").html( cstring );
threeworld.camera.position.copy( cameraposition );
}
// exercise
// when press 9 camera hovers 500 away from current block
// when press 0 turns off that feature and does default move,track etc.
// var camerahover = false;
// in key press function, 9 and 0 turn camerahover = true or false
// in function above, if ( camerahover ) { ....}
function paintThis( object ) // paint objects with random textures
{
// var t = randomintAtoB ( 0, textureArray.length - 1 ); // random texture
object.material = new THREE.MeshBasicMaterial ( { map: textureArray [ currentimage ] } );
}
// array[0]
// array[1]
// .....
this.endCondition;
this.newRun = function()
{
this.endCondition = false;
threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR );
init();
document.addEventListener( 'keydown', handleKeyDown );
};
this.getState = function()
{
return ( null );
};
this.takeAction = function ( a )
{
};
this.endRun = function()
{
};
this.getScore = function()
{
return 0;
};
}