// Cloned by Lorcan Dunne on 30 Nov 2022 from World "First Person Controls" by Enhanced
// Please leave this clone trail here.
// Cloned by Enhanced on 21 Jun 2018 from World "First Person Controls" by Mathias Bazin
// Please leave this clone trail here.
// Customise AB run parameters (optional).
// The following parameters can be customised. (They have default values.)
AB.clockTick = 20;
// Speed of run: Step every n milliseconds. Default 100.
AB.maxSteps = 65545;
// Length of run: Maximum length of run in steps. Default 1000.
AB.screenshotStep = 50;
// For automatic generation of World images.
// Take screenshot on this step. (All resources should have finished loading.) Default 50.
AB.drawRunControls = false;
threeworld.drawCameraControls = false;
const TEXTURE_WALL = '/uploads/starter/door.jpg' ;
const TEXTURE_MAZE = '/uploads/starter/latin.jpg' ;
const TEXTURE_AGENT = '/uploads/starter/pacman.jpg' ;
const PATHTEXTURES = "/uploads/meak/";
const floorTextureFile = "/uploads/mathias/grass.jpg";
const things = "/uploads/lorcan47/Pc_principal_tfbw.png";
const MOVESPEED = 7;
const boxcolor = 'red';
const objectsize = 40; // size of object
const ARMYSIZE = 10;
const gridsize = 50;
const squaresize = 100;
const MAXPOS = gridsize * squaresize;
const GRID_BLANK = 0;
const GRID_WALL = 1;
const GRID_MAZE = 2;
const GRID_AGENT = 3;
const GRID_BABY = 4;
const GRID_DEAD = 5;
var BOXHEIGHT;
var GRID = new Array ( gridsize );
var wall_texture, agent_texture, maze_texture;
// the object is a cube (each dimension equal):
var shape = new THREE.BoxGeometry ( objectsize, objectsize, objectsize );
var material = new THREE.MeshBasicMaterial ( { color: boxcolor.toLowerCase() } );
var theobject = new THREE.Mesh ( shape, material );
var THEARMY = new Array( ARMYSIZE );
const grid = theobject + gridsize;
// function loadResources() // asynchronous file loads - call initScene() when all finished
// {
// for ( var i = 0; i < FILE_ARRAY.length; i++ )
// startFileLoad ( i ); // launch n asynchronous file loads
// }
//==============================================================================
// Defines the THREE.PointerLockControls class, source at https://threejs.org/
//==============================================================================
function loadResources() // asynchronous file loads - call initScene() when all finished
{
var loader1 = new THREE.TextureLoader();
var loader2 = new THREE.TextureLoader();
var loader3 = new THREE.TextureLoader();
var loader4 = new THREE.TextureLoader();
var loader5 = new THREE.TextureLoader();
loader1.load ( TEXTURE_WALL, function ( thetexture )
{
thetexture.minFilter = THREE.LinearFilter;
wall_texture = thetexture;
if ( asynchFinished() ) initScene(); // if all file loads have returned
});
loader2.load ( TEXTURE_AGENT, function ( thetexture )
{
thetexture.minFilter = THREE.LinearFilter;
agent_texture = thetexture;
if ( asynchFinished() ) initScene();
});
loader3.load ( TEXTURE_BABY, function ( thetexture )
{
thetexture.minFilter = THREE.LinearFilter;
baby_texture = thetexture;
if ( asynchFinished() ) initScene();
});
loader4.load ( TEXTURE_MAZE, function ( thetexture )
{
thetexture.minFilter = THREE.LinearFilter;
maze_texture = thetexture;
if ( asynchFinished() ) initScene();
});
loader5.load ( TEXTURE_DEAD, function ( thetexture )
{
thetexture.minFilter = THREE.LinearFilter;
dead_texture = thetexture;
if ( asynchFinished() ) initScene();
});
}
function asynchFinished() // all file loads returned
{
if ( wall_texture && agent_texture && baby_texture && dead_texture && maze_texture ) return true;
else return false;
}
function initScene() // all file loads have returned
{
var i,j, shape, thecube;
// set up GRID as 2D array
for ( i = 0; i < gridsize ; i++ )
GRID[i] = new Array(gridsize);
// set up walls
for ( i = 0; i < gridsize ; i++ )
for ( j = 0; j < gridsize ; j++ )
if ( ( i==0 ) || ( i==gridsize-1 ) || ( j==0 ) || ( j==gridsize-1 ) )
{
GRID[i][j] = GRID_WALL;
shape = new THREE.BoxGeometry ( squaresize, BOXHEIGHT, squaresize );
thecube = new THREE.Mesh( shape );
thecube.material = new THREE.MeshBasicMaterial( { map: wall_texture } );
thecube.position.copy ( translate(i,j) ); // translate my (i,j) grid coordinates to three.js (x,y,z) coordinates
ABWorld.scene.add(thecube);
}
else
GRID[i][j] = GRID_BLANK;
// set up maze
// for ( var c=1 ; c <= NOBOXES ; c++ )
// {
// i = AB.randomIntAtoB(1,gridsize-2); // inner squares are 1 to gridsize-2
// j = AB.randomIntAtoB(1,gridsize-2);
// GRID[i][j] = GRID_MAZE ;
// shape = new THREE.BoxGeometry ( squaresize, BOXHEIGHT, squaresize );
// thecube = new THREE.Mesh( shape );
// thecube.material = new THREE.MeshBasicMaterial( { map: maze_texture } );
// thecube.position.copy ( translate(i,j) ); // translate my (i,j) grid coordinates to three.js (x,y,z) coordinates
// ABWorld.scene.add(thecube);
// }
// set up first baby
BABIES[0] = newBaby();
// set up agent
// start in random location
do
{
i = AB.randomIntAtoB(1,gridsize-2);
j = AB.randomIntAtoB(1,gridsize-2);
}
while ( occupied(i,j) ); // search for empty square
ai = i;
aj = j;
GRID[i][j] = GRID_AGENT;
shape = new THREE.BoxGeometry ( squaresize, BOXHEIGHT, squaresize );
theagent = new THREE.Mesh( shape );
theagent.material = new THREE.MeshBasicMaterial( { map: agent_texture } );
theagent.position.copy ( translate(ai,aj) ); // translate my (i,j) grid coordinates to three.js (x,y,z) coordinates
ABWorld.scene.add(theagent);
}
THREE.PointerLockControls = function ( camera ) {
var scope = this;
camera.rotation.set( 0, 0, 0 );
var pitchObject = new THREE.Object3D();
pitchObject.add( camera );
var yawObject = new THREE.Object3D();
yawObject.position.y = 10;
yawObject.add( pitchObject );
var PI_2 = Math.PI / 2;
var onMouseMove = function ( event ) {
if ( scope.enabled === false ) return;
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
yawObject.rotation.y -= movementX * 0.002;
pitchObject.rotation.x -= movementY * 0.002;
pitchObject.rotation.x = Math.max( - PI_2, Math.min( PI_2, pitchObject.rotation.x ) );
};
this.dispose = function () {
document.removeEventListener( 'mousemove', onMouseMove, false );
};
document.addEventListener( 'mousemove', onMouseMove, false );
this.enabled = false;
this.getObject = function () {
return yawObject;
};
this.getDirection = function () {
// assumes the camera itself is not rotated
var direction = new THREE.Vector3( 0, 0, - 1 );
var rotation = new THREE.Euler( 0, 0, 0, 'YXZ' );
return function ( v ) {
rotation.set( pitchObject.rotation.x, yawObject.rotation.y, 0 );
v.copy( direction ).applyEuler( rotation );
return v;
};
}();
};
//==============================================================================
function initArmy() // called when all textures ready
{
var t = 0;
// for ( var c=1 ; c <= ARMYSIZE ; c++ )
// {
// // var shape = new THREE.SphereGeometry ( objectsize, 30, 30 );
// var shape = new THREE.BoxGeometry( objectsize, objectsize, objectsize );
// var theobject = new THREE.Mesh( shape );
// theobject.position.x = AB.randomIntAtoB ( -MAXPOS, MAXPOS );
// theobject.position.z = AB.randomIntAtoB ( -MAXPOS, MAXPOS );
// theobject.position.y = 0;
// // var r = AB.randomIntAtoB ( 0, textureArray.length - 1 ); // random texture
// var r = AB.randomIntAtoB ( 0, 4 ); // random one of the earths
// theobject.material = new THREE.MeshBasicMaterial ( { map: textureArray[r] } );
// ABWorld.scene.add(theobject);
// THEARMY[t] = theobject; // save it for later
// t++;
// }
}
function OnClick(e)
{
if(editMode !== null)
{
if(editMode === 0)
{
selectionObject = null;
CreateObject(e);
}
else if(editMode == 1)
{
if(controls.getAttachedObject().children.length !== 0)
{
controls.getAttachedObject().remove(controls.getAttachedObject().children[1]);
controls.getAttachedObject().remove(controls.getAttachedObject().children[0]);
}
if(e !== null && e.which == 3)
{
editMode = null;
}
if(selectionObject !== null)
{
removeObject(threeworld.scene, selectionObject);
while (selectionBox.children.length)
{
selectionBox.remove(selectionBox.children[0]);
}
}
}
}
}
function World() {
var objLoader = new THREE.OBJLoader();
objLoader.setPath("/uploads/tuitef2/");
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath("/uploads/tuitef2/");
var objLoader1 = new THREE.OBJLoader();
objLoader1.setPath("/uploads/tuitef2/");
var mtlLoader1 = new THREE.MTLLoader();
mtlLoader1.setPath("/uploads/tuitef2/");
var objLoader2 = new THREE.OBJLoader();
objLoader2.setPath("/uploads/tuitef2/");
var mtlLoader2 = new THREE.MTLLoader();
mtlLoader2.setPath("/uploads/tuitef2/");
var objLoader3 = new THREE.OBJLoader();
objLoader3.setPath("/uploads/tuitef2/");
var mtlLoader3 = new THREE.MTLLoader();
mtlLoader3.setPath("/uploads/tuitef2/");
var objLoader4 = new THREE.OBJLoader();
objLoader4.setPath("/uploads/tuitef2/");
var mtlLoader4 = new THREE.MTLLoader();
mtlLoader4.setPath("/uploads/tuitef2/");
const ARMYSIZE = 10;
const gridsize = 50;
const squaresize = 100;
const MAXPOS = gridsize * squaresize;
const GRID_BLANK = 0;
const GRID_WALL = 1;
const GRID_MAZE = 2;
const GRID_AGENT = 3;
const GRID_BABY = 4;
const GRID_DEAD = 5;
var BOXHEIGHT;
var GRID = new Array ( gridsize );
var wall_texture, agent_texture, maze_texture;
// the object is a cube (each dimension equal):
// var shape = new THREE.BoxGeometry ( objectsize, objectsize, objectsize );
// var material = new THREE.MeshBasicMaterial ( { color: boxcolor.toLowerCase() } );
// var theobject = new THREE.Mesh ( shape, material );
var camera, controls;
var objects = [];
var raycaster;
var moveForward = false;
var moveBackward = false;
var moveLeft = false;
var moveRight = false;
var canJump = false;
var prevTime = performance.now();
var velocity = new THREE.Vector3();
var direction = new THREE.Vector3();
var vertex = new THREE.Vector3();
var color = new THREE.Color();
var ai;
function createMind(){
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( {color: "blue"} );
ai = new THREE.Mesh( geometry, material );
ai.position.set(0,0,0);
ai.scale.multiplyScalar(2);
scene.add( ai );
}
function place_trees(){
mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += 120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += -120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += -130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += 130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += 90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += -90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 50;
mesh.position.x += -60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 50;
mesh.position.x += 60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 50;
mesh.position.x += 100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += -100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += 20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 70;
mesh.position.x += -10;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 60;
mesh.position.x += -20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top//
mesh.position.z += -10;
mesh.position.x += -50;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top//
mesh.position.z += -10;
mesh.position.x += 50;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -120;
mesh.position.x += 120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top -
mesh.position.z += -120;
mesh.position.x += -120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -120;
mesh.position.x += -130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -120;
mesh.position.x += 130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += 90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += -90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -50;
mesh.position.x += -60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -50;
mesh.position.x += 60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -50;
mesh.position.x += 100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += -100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += 20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -70;
mesh.position.x += -10;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -60;
mesh.position.x += -20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
});
}
this.newRun = function()
{
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
threeworld.camera = camera;
threeworld.init3d ( 0,0, 0x7ec0ee );
var light = new THREE.HemisphereLight( 0xeeeeff, 0x777788, 0.75 );
light.position.set( 0.5, 1, 0.75 );
threeworld.scene.add( light );
controls = new THREE.PointerLockControls( camera );
threeworld.scene.add( controls.getObject() );
threeworld.scene.add( theobject);
place_trees();
//This will handle key presses
var onKeyDown = function ( event ) {
switch ( event.keyCode ) {
case 38: // up
case 87: // w
moveForward = true;
break;
case 37: // left
case 65: // a
moveLeft = true; break;
case 40: // down
case 83: // s
moveBackward = true;
break;
case 39: // right
case 68: // d
moveRight = true;
break;
case 32: // space
if ( canJump === true ) velocity.y += 1000;
canJump = false;
break;
}
};
var onKeyUp = function ( event ) {
switch( event.keyCode ) {
case 38: // up
case 87: // w
moveForward = false;
break;
case 37: // left
case 65: // a
moveLeft = false;
break;
case 40: // down
case 83: // s
moveBackward = false;
break;
case 39: // right
case 68: // d
moveRight = false;
break;
}
};
document.addEventListener( 'keydown', onKeyDown, false );
document.addEventListener( 'keyup', onKeyUp, false );
raycaster = new THREE.Raycaster( new THREE.Vector3(), new THREE.Vector3( 0, - 1, 0 ), 0, 10 );
$("#user_span1").html(" Use WASD or Arrows to move, mouse to look around and space to jump.");
var blocker = $("#user_span2");
blocker.html("<p><b>Click screen to enable mouse controls</b></p>");
// The following handles pointer locking when clicking the window
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
console.log(havePointerLock);
if ( havePointerLock ) {
var element = document.body;
var pointerlockchange = function ( event ) {
if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {
controls.enabled = true;
blocker.html("");
} else
{
controls.enabled = true;
blocker.html("<p><b>Click screen to enable mouse controls</b></p>");
}
};
var pointerlockerror = function ( event ) {
console.error("pointerlockerror");
};
// Hook pointer lock state change events
document.addEventListener( 'pointerlockchange', pointerlockchange, false );
document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );
document.addEventListener( 'pointerlockerror', pointerlockerror, false );
document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );
document.addEventListener( 'click', function ( event ) {
// Ask the browser to lock the pointer
// element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
}, false );
} else {
$("#user_span1").html('<p>Your browser doesn\'t seem to support Pointer Lock API</p>');
}
//The following draws a simple scene
//floor
var floorGeometry = new THREE.PlaneBufferGeometry( 20000, 20000, 1000, 1000 );
floorGeometry.rotateX( - Math.PI / 2 );
var floorTexture = new THREE.ImageUtils.loadTexture ( floorTextureFile );
floorTexture.minFilter = THREE.LinearFilter;
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.offset.set( 0, 0 );
floorTexture.repeat.set( 40, 40 );
var floor = new THREE.Mesh(floorGeometry, new THREE.MeshBasicMaterial({map : floorTexture}));
floor.position.set(0,-20,0);
threeworld.scene.add(floor, THEARMY);
};
this.nextStep = function()
{
//======================================================================
//This will handle moving the player and the camera
//======================================================================
raycaster.ray.origin.copy( controls.getObject().position );
raycaster.ray.origin.y -= 10;
var intersections = raycaster.intersectObjects( objects );
var onObject = intersections.length > 0;
var time = performance.now();
var delta = ( time - prevTime ) / 1000;
velocity.x -= velocity.x * 10.0 * delta;
velocity.z -= velocity.z * 10.0 * delta;
velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
direction.z = Number( moveForward ) - Number( moveBackward );
direction.x = Number( moveLeft ) - Number( moveRight );
direction.normalize(); // this ensures consistent movements in all directions
if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * MOVESPEED * delta;
if ( moveLeft || moveRight ) velocity.x -= direction.x * 400.0 * MOVESPEED * delta;
if ( onObject === true ) {
velocity.y = Math.max( 0, velocity.y );
canJump = true;
}
controls.getObject().translateX( velocity.x * delta );
controls.getObject().translateY( velocity.y * delta );
controls.getObject().translateZ( velocity.z * delta );
if ( controls.getObject().position.y < 10 ) {
velocity.y = 0;
controls.getObject().position.y = 10;
canJump = true;
}
prevTime = time;
//======================================================================
};
}