// Cloned by Mathias Bazin on 30 May 2018 from World "Minecraft with cursor" by Mathias Bazin
// Please leave this clone trail here.
// Cloned by Mathias Bazin on 29 May 2018 from World "MineCraft" 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.
// ==================================================================================================================
// "MineCraft" World
// user adds blocks
// --- not using nextStep() loop for actions, only for rendering: ------------------------
// all actions are taken whenever user hits keys and not any other time
// however, still need to use nextStep() loop for render update
// ===================================================================================================================
// === 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?
// 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 = 50;
// 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 CURSORCOLOR = "red"
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
const shortDistance = 2; //used for checking if two vectors are close to be the same
//--- change threeworld defaults: -------------------------------
threehandler.MAXCAMERAPOS = MAXPOS * 10 ; // allow camera go far away
// ===================================================================================================================
// === End of tweaker's box ==========================================================================================
// ===================================================================================================================
// You will need to be some sort of JavaScript programmer to change things below the tweaker's box.
function randomfloatAtoB ( A, B )
{
return ( A + ( Math.random() * (B-A) ) );
}
function randomintAtoB ( A, B )
{
return ( Math.round ( randomfloatAtoB ( A, B ) ) );
}
function World() {
function createCursor()
{
//create cursor
//create shape and material for the edges
var edgeshape = new THREE.CylinderGeometry(10,10,objectsize,32);
var material = new THREE.MeshBasicMaterial( {color: CURSORCOLOR} );
//create edges
var edge1 = new THREE.Mesh( edgeshape, material );
var edge2 = new THREE.Mesh( edgeshape, material );
var edge3 = new THREE.Mesh( edgeshape, material );
var edge4 = new THREE.Mesh( edgeshape, material );
var edge5 = new THREE.Mesh( edgeshape, material );
var edge6 = new THREE.Mesh( edgeshape, material );
var edge7 = new THREE.Mesh( edgeshape, material );
var edge8 = new THREE.Mesh( edgeshape, material );
var edge9 = new THREE.Mesh( edgeshape, material );
var edge10 = new THREE.Mesh( edgeshape, material );
var edge11= new THREE.Mesh( edgeshape, material );
var edge12= new THREE.Mesh( edgeshape, material );
//place edges correctly
edge1.position.set(-objectsize/2,0,-objectsize/2);
edge2.position.set(-objectsize/2,0,+objectsize/2);
edge3.position.set(+objectsize/2,0,+objectsize/2);
edge4.position.set(+objectsize/2,0,-objectsize/2);
edge5.position.set(objectsize/2,objectsize/2,0);
edge5.rotation.x = Math.PI / 2;
edge6.position.set(-objectsize/2,objectsize/2,0);
edge6.rotation.x = Math.PI / 2;
edge7.position.set(objectsize/2,-objectsize/2,0);
edge7.rotation.x = Math.PI / 2;
edge8.position.set(-objectsize/2,-objectsize/2,0);
edge8.rotation.x = Math.PI / 2;
edge9.position.set(0,-objectsize/2,objectsize/2);
edge9.rotation.z = Math.PI / 2;
edge10.position.set(0,-objectsize/2,-objectsize/2);
edge10.rotation.z = Math.PI / 2;
edge11.position.set(0,+objectsize/2,objectsize/2);
edge11.rotation.z = Math.PI / 2;
edge12.position.set(0,+objectsize/2,-objectsize/2);
edge12.rotation.z = Math.PI / 2;
//add all edges to a group,
var cursor = new THREE.Group();
cursor.add(edge1);
cursor.add(edge2);
cursor.add(edge3);
cursor.add(edge4);
cursor.add(edge5);
cursor.add(edge6);
cursor.add(edge7);
cursor.add(edge8);
cursor.add(edge9);
cursor.add(edge10);
cursor.add(edge11);
cursor.add(edge12);
//you can now manipulate the cursor as a unique object
return cursor;
}
var x, y, z; // current location
var cursor = createCursor();
var textureArray;
var blockArray = [];
// State of the Camera depending of the position
var currentCameraState;
// Inputs to instantiate Box. they depend of the current State of the camera
var leftInput ;
var rightInput ;
var forwardInput ;
var backwardInput ;
var topInput ;
var bottonInput ;
function init()
{
cursor.position.set(0,0,0);
threeworld.scene.add(cursor);
textureArray = [FILE_ARRAY.length];
for (let i = 0; i < FILE_ARRAY.length; i++)
{
textureArray[i] = new THREE.ImageUtils.loadTexture( FILE_ARRAY[i] );
}
currentCameraState = 1;
leftInput = 37;
rightInput = 39;
forwardInput = 38;
backwardInput = 40;
topInput = 33;
bottomInput = 34;
for ( let i = 0; i < textureArray.length; i++ ) // for all textures
{
textureArray[i].minFilter = THREE.LinearFilter;
}
threeworld.camera.lookAt(cursor.position);
}
function getBlockIndexAtCursor()
{
let index = 0;
for (let block of blockArray)
{
if ( block.position.distanceTo(cursor.position) < shortDistance )
{
return index;
}
index++;
}
return -1;
}
function paintThis( object ) // paint objects with random textures
{
var t = randomintAtoB ( 0, textureArray.length - 1 ); // random texture
object.material = new THREE.MeshBasicMaterial ( { transparent: true, opacity: 1, map: textureArray[t] } );
}
// the main key handling function:
function handleKeyDown (e)
{
if ( e.keyCode == leftInput ) { cursor.position.x += - objectsize ; e.preventDefault(); } // left
if ( e.keyCode == rightInput ) { cursor.position.x += + objectsize ; e.preventDefault(); } // right
if ( e.keyCode == forwardInput ) { cursor.position.z += - objectsize ; e.preventDefault(); } // forward
if ( e.keyCode == backwardInput ) { cursor.position.z += + objectsize ; e.preventDefault(); } // back
if ( e.keyCode == bottomInput ) { cursor.position.y += - objectsize ; e.preventDefault(); }
if ( e.keyCode == topInput ) { cursor.position.y += + objectsize ; e.preventDefault(); }
// console.log ( "(x,y,z) = (" + cursor.position.x + ")" );
// threeworld.camera.lookAt( cursor.position );
if ( e.keyCode == 13 ) { // Enter key pressed
if( getBlockIndexAtCursor() == -1) //if no block at cursor
{
//create block at cursor position
var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
var block = new THREE.Mesh( shape );
block.position.x = cursor.position.x;
block.position.y = cursor.position.y;
block.position.z = cursor.position.z;
threeworld.scene.add(block);
paintThis ( block );
// sound effect credit:
// http://soundbible.com/1399-Chambering-A-Round.html
var audio = new Audio( SOUND_BLOCK );
audio.play();
blockArray.push(block)
}
e.preventDefault();
}
if ( e.keyCode == 46 ) { //delete button pressed
let indexBlockToRemove = getBlockIndexAtCursor();
if (indexBlockToRemove != -1)
{
threeworld.scene.remove(blockArray[indexBlockToRemove]);
blockArray.splice(indexBlockToRemove, 1);
}
e.preventDefault();
}
}
this.endCondition = false;
this.newRun = function()
{
threeworld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR );
var s = "<p> Use arrow keys and PgUp, PgDn to move cursor, press Enter to plqce q block, Del to delete one. </p>";
$("#user_span2").html( s );
$("#user_span3").html( );
init();
// set up the main key handler:
document.addEventListener( 'keydown', handleKeyDown );
};
this.nextStep = function() // not used
{
if(threeworld.camera.rotation.y > Math.PI / 4)
{
if(Math.abs(threeworld.camera.rotation.x) > 2 * Math.PI / 4)
{
leftInput = 38;
rightInput = 40;
forwardInput = 39;
backwardInput = 37;
currentCameraState = 1;
}
else
{
leftInput = 38;
rightInput = 40;
forwardInput = 39;
backwardInput = 37;
currentCameraState = 1;
}
}
else if(threeworld.camera.rotation.y > 0)
{
if(Math.abs(threeworld.camera.rotation.x) > 2 * Math.PI / 4)
{
leftInput = 39;
rightInput = 37;
forwardInput = 40;
backwardInput = 38;
currentCameraState = 2;
}
else
{
leftInput = 37;
rightInput = 39;
forwardInput = 38;
backwardInput = 40;
currentCameraState = 2;
}
}
else if(threeworld.camera.rotation.y > - Math.PI / 4)
{
if(Math.abs(threeworld.camera.rotation.x) > 2 * Math.PI / 4)
{
leftInput = 39;
rightInput = 37;
forwardInput = 40;
backwardInput = 38;
currentCameraState = 3;
}
else
{
leftInput = 37;
rightInput = 39;
forwardInput = 38;
backwardInput = 40;
currentCameraState = 3;
}
}
else if(threeworld.camera.rotation.y > - 2 * Math.PI / 4)
{
if(Math.abs(threeworld.camera.rotation.x) > 2 * Math.PI / 4)
{
leftInput = 40;
rightInput = 38;
forwardInput = 37;
backwardInput = 39;
currentCameraState = 4;
}
else
{
leftInput = 40;
rightInput = 38;
forwardInput = 37;
backwardInput = 39;
currentCameraState = 4;
}
}
};
this.endRun = function()
{
};
}