// Based on MineCraft World
// Touch gestures on mobile add new blocks to the side in the direction of the gesture
const show3d = true;
// These 3 have default values, so this section is optional:
AB.clockTick = 100;
// Speed of run: Step every n milliseconds. Default 100.
AB.maxSteps = 1000000;
// Length of run: Maximum length of run in steps. Default 1000.
AB.screenshotStep = 100;
// Take screenshot on this step. (All resources should have finished loading.) Default 50.
const FILE_ARRAY = [
"/uploads/starter/minecraft.1.jpg",
"/uploads/starter/minecraft.2.jpg"
];
const SOUND_BLOCK = '/uploads/starter/chamber.mp3' ;
const SKYCOLOR = 0xffffff; // a number, not a string
const objectsize = 300 ;
const MAXPOS = 4000 ;
const startRadiusConst = MAXPOS * 0.5 ; // distance from centre to start the camera at
const maxRadiusConst = MAXPOS * 5 ; // maximum distance from camera we will render things
//--- change threeworld defaults: -------------------------------
threehandler.MAXCAMERAPOS = MAXPOS * 10 ; // allow camera go far away
function randomfloatAtoB ( A, B )
{
return ( A + ( Math.random() * (B-A) ) );
}
function randomintAtoB ( A, B )
{
return ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
function World() {
var x, y, z; // current location
var textureArray;
var BOXHEIGHT; // 3d or 2d box height
function init()
{
// create one box to start
var shape = new THREE.BoxGeometry( objectsize, BOXHEIGHT, objectsize );
var theobject = new THREE.Mesh( shape );
theobject.position.x = 0;
theobject.position.z = 0;
theobject.position.y = 0;
x = theobject.position.x; // current position
y = theobject.position.y;
z = theobject.position.z;
threeworld.scene.add(theobject);
textureArray = [
( new THREE.ImageUtils.loadTexture( FILE_ARRAY[0] ) ),
( new THREE.ImageUtils.loadTexture( FILE_ARRAY[1] ) )
];
for ( var i = 0; i < textureArray.length; i++ ) // for all textures
{
textureArray[i].minFilter = THREE.LinearFilter;
}
paintThis ( theobject );
}
function paintThis( object ) // paint objects with random textures
{
var t = randomintAtoB ( 0, textureArray.length - 1 ); // random texture
object.material = new THREE.MeshBasicMaterial ( { map: textureArray[t] } );
}
// put block in some position relative to current position:
const ACTION_LEFT = 0;
const ACTION_RIGHT = 1;
const ACTION_FORWARD = 2;
const ACTION_BACK = 3;
const ACTION_UP = 4;
const ACTION_DOWN = 5;
function newBlock ( action )
{
var shape = new THREE.BoxGeometry( objectsize, BOXHEIGHT, objectsize );
var theobject = new THREE.Mesh( shape );
theobject.position.x = x; // default position is current position - going to then move it
theobject.position.y = y;
theobject.position.z = z;
if (action == ACTION_LEFT) theobject.position.x = x - objectsize ;
if (action == ACTION_RIGHT) theobject.position.x = x + objectsize ;
if (action == ACTION_FORWARD) theobject.position.z = z - objectsize ;
if (action == ACTION_BACK) theobject.position.z = z + objectsize ;
if (action == ACTION_DOWN) theobject.position.y = y - objectsize ;
if (action == ACTION_UP) theobject.position.y = y + objectsize ;
x = theobject.position.x; // current position is now this
y = theobject.position.y;
z = theobject.position.z;
// console.log ( "(x,y,z) = (" + x + "," + y + "," + z + ")" );
// threeworld.lookat.copy ( theobject.position );
threeworld.scene.add(theobject);
paintThis ( theobject );
// sound effect credit:
// http://soundbible.com/1399-Chambering-A-Round.html
var audio = new Audio( SOUND_BLOCK );
audio.play();
}
function handleKeyDown (event)
{
var code = event.keyCode;
if (code == 37) newBlock ( ACTION_LEFT );
if (code == 39) newBlock ( ACTION_RIGHT );
if (code == 38) newBlock ( ACTION_FORWARD );
if (code == 40) newBlock ( ACTION_BACK );
if ( show3d )
{
if (code == 34) newBlock ( ACTION_DOWN );
if (code == 33) newBlock ( ACTION_UP );
}
}
var startX, startY;
var touchevents; // number of touch events in the current drag
function initTouchDrag ( x, y ) // x,y position on screen
{
startX = x;
startY = y;
touchevents = 0;
};
function touchDrag ( x, y ) // compare with previous x,y position on screen to get direction of drag
{
if ( ( touchevents % 20 ) == 0 ) // slow it down to respond to every nth touch event - too many touch events
{
if ( x > startX ) newBlock ( ACTION_RIGHT );
else if ( x < startX ) newBlock ( ACTION_LEFT );
if ( y > startY ) newBlock ( ACTION_BACK );
else if ( y < startY ) newBlock ( ACTION_FORWARD );
}
touchevents++;
startX = x;
startY = y;
};
this.endCondition = false;
this.newRun = function()
{
if ( show3d )
{
BOXHEIGHT = objectsize;
threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR );
}
else
{
BOXHEIGHT = 1;
threeworld.init2d ( startRadiusConst, maxRadiusConst, SKYCOLOR );
}
if ( AB.onDesktop() )
$("#user_span2").html( "<p> <b> Use arrow keys and PgUp, PgDn to draw. </b> </p>" );
else
$("#user_span2").html( "<p> <b> Use touch gestures to draw. </b> </p>" );
init();
// set up the main key handler:
document.addEventListener( 'keydown', handleKeyDown );
// override threehandler default (which is camera control) to use my own functions:
threehandler.initTouchDrag = initTouchDrag;
threehandler.touchDrag = touchDrag
};
this.nextStep = function() // not used
{
};
this.endRun = function()
{
};
}