Code viewer for World: Coin Collector (clone by L...

// 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 floorTextureFile = "/uploads/mathias/grass.jpg"
const roofTextureFile     = "/uploads/lorcan47/newUniverse.jpg"

const MOVESPEED = 3;


//==============================================================================
//  Defines the THREE.PointerLockControls class, source at https://threejs.org/
//==============================================================================

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 World() { 

    var camera, controls;

	var objects = [];
	
	var coin_list = [];
	var coin_count = 0;

	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 distance;
	var updatedPosX;
    var updatedPosZ;
    var disCoin;
    var span;
    var meshroof;
    var play;

	
	var shape1      = new THREE.BoxGeometry ( 2000, 500, 10);
	var shape2      = new THREE.BoxGeometry (0, 500, 2000);
	var shape3      = new THREE.BoxGeometry (3000, -500, 3000);
    var material    = new THREE.MeshBasicMaterial ( { color: "#17202a"} );
    var material2   = new THREE.MeshBasicMaterial ( {color: '#5dade2'})
    
    
    
    
    var wall1   = new THREE.Mesh ( shape1, material );
    var wall2   = new THREE.Mesh ( shape1, material );
    var wall3   = new THREE.Mesh ( shape2, material );
    var wall4   = new THREE.Mesh ( shape2, material );
    var roof    = new THREE.Mesh ( shape3, material );
    
    wall1.position.x += 0;
    wall1.position.y += 0;
    wall1.position.z += 1000;
    
    wall2.position.x += 0;
    wall2.position.y += 0;
    wall2.position.z += -1000;
    
    wall3.position.x += 1000;
    wall3.position.y += 0;
    wall3.position.z += 0;
    
    wall4.position.x += -1000;
    wall4.position.y += 0;
    wall4.position.z += 0;
    
    
    
    
    var bestx = 999;
    var bestz = 999;
    var smallestDist = 999;

    function coin(){
        for(i=0; i < 10; i++){
            var sphere = new THREE.SphereGeometry(1);
            var material2 = new THREE.MeshBasicMaterial({color:'gold'});
            var coin = new THREE.Mesh (sphere, material2);
            coin.position.z += AB.randomIntAtoB(-1000,1000);
            coin.position.x += AB.randomIntAtoB(-1000,1000);
            coin.position.y += -4;
            coin.scale.multiplyScalar(1.5);
            threeworld.scene.add(coin);
            coin_list.push(coin);
        }
            
    }
    function collect_coin(){
            for(i = 0; i < coin_list.length; i++){
                distanceX = Math.abs((parseInt(updatedPosX)) - coin_list[i].position.x);
                distanceZ = Math.abs((parseInt(updatedPosZ)) - coin_list[i].position.z);
                if(distanceX <=200 && distanceZ <= 200){
                    coin_list[i].position.x = AB.randomIntAtoB(-2500,-2400);
                    coin_list[i].position.z = AB.randomIntAtoB(-2500,-2400);
                    coin_list[i].material.color.set(0xff4444);
                    coin_coint++;
                    console.log(coin_count++);
                    span.innerHTML = coin_count;
                    bestx = 999;
                    bestz = 999;
                    smallestDist = 999;
            }
        }
    }
    

	
	this.newRun = function()
	{
	    
	    camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
        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 );
		
		threeworld.scene.fog = new THREE.Fog( 0xffffff, 10, 350 );

        controls = new THREE.PointerLockControls( camera );
        threeworld.scene.add( controls.getObject());
        
        
        
    //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 += 350;
					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 );
    function moving(){
        if(keyboard[87]){ // W key
            if (play.position.x<= 200 && play.position.x>= -200){
                        play.position.x -= Math.sin(play.rotation.y) * player.speed;
                        play.position.z -= -Math.cos(play.rotation.y) * player.speed;
                        updatedPosX = play.position.x;
                        updatedPosZ = play.position.z;
                
                    }
                    else{
                        play.position.x += Math.sin(play.rotation.y) * player.speed;
                 
                    }
            if(play.position.z<= 200 && play.position.z>= -200){
                play.position.x -= Math.sin(play.rotation.y) * player.speed;
                play.position.z -= -Math.cos(play.rotation.y) * player.speed;
                updatedPosX = play.position.x;
                updatedPosZ = play.position.z;
                
            }
            else{
                play.position.z += -Math.cos(play.rotation.y) * player.speed;
                
            }
            eat_food();
        }
        if(keyboard[83]){ // S key
            if (play.position.x<= 200 && play.position.x>= -200){
                    play.position.x += Math.sin(play.rotation.y) * player.speed;
                    play.position.z += -Math.cos(play.rotation.y) * player.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x -= Math.sin(play.rotation.y) * player.speed;
                    }
            if(play.position.z<= 200 && play.position.z>= -200){
                play.position.x += Math.sin(play.rotation.y) * player.speed;
                play.position.z += -Math.cos(play.rotation.y) * player.speed;
                updatedPosX = play.position.x;
                updatedPosZ = play.position.z;
            }
            else{
                play.position.z -= -Math.cos(play.rotation.y) * player.speed;
            }
            eat_food();
        }
    
        if(keyboard[65]){ // A key
                // Redirect motion by 90 degrees
            if (play.position.x <= 200 && play.position.x >= -200){
                    play.position.x += Math.sin(play.rotation.y + Math.PI/2) * player.speed;
                    play.position.z += -Math.cos(play.rotation.y + Math.PI/2) * player.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x = Math.sin(play.rotation.y - Math.PI/2) * player.speed;
                    }
            if(play.position.z <= 200 && play.position.z >= -200){
                play.position.x += Math.sin(play.rotation.y + Math.PI/2) * player.speed;
                play.position.z += -Math.cos(play.rotation.y + Math.PI/2) * player.speed;
            }
            else{
                play.position.z = -Math.cos(play.rotation.y - Math.PI/2) * player.speed;
            }
            eat_food();
        }
        if(keyboard[68]){ // D key
            if (play.position.x<= 200 && play.position.x>= -200){
                    play.position.x += Math.sin(play.rotation.y - Math.PI/2) * player.speed;
                    play.position.z += -Math.cos(play.rotation.y - Math.PI/2) * player.speed;
                    updatedPosX = play.position.x;
                    updatedPosZ = play.position.z;
            }
                else{
                    play.position.x = Math.sin(play.rotation.y + Math.PI/2) * player.speed;
                    }
            if(play.position.z<= 200 && play.position.z>= -200){
                play.position.x += Math.sin(play.rotation.y - Math.PI/2) * player.speed;
                play.position.z += -Math.cos(play.rotation.y - Math.PI/2) * player.speed;
            }
            else{
                play.position.z = -Math.cos(play.rotation.y + Math.PI/2) * player.speed;
            }
            eat_food();
        }
                
        if(birdsView === false){    // Keyboard turn inputs// STOPS PLAYERS FROM USING ARROW KEYS WHEN IN SKY VIEW.
            if(keyboard[37]){ // left arrow key
                    cam.rotation.y -= player.turnSpeed;
                    play.rotation.y -= player.turnSpeed;
                    cam.position.z = play.position.z ;
            }
            if(keyboard[39]){ // right arrow key
                    cam.rotation.y += player.turnSpeed;
                    play.rotation.y += player.turnSpeed;
                    cam.position.z = play.position.z;
            }
            
        }
        cam.position.x = play.position.x ;
        cam.position.z = play.position.z -5;
    
        renderer.render(scene, cam);
    }
    
    
    function keyDown(event){
        keyboard[event.keyCode] = true;
    }
    
    function keyUp(event){
        keyboard[event.keyCode] = false;
    }
    window.addEventListener('keydown', keyDown); 
    window.addEventListener('keyup', keyUp);
    
	    $("#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>");
	    
	    span = document.getElementById("user_span1");
	    span.innerHTML = "<br>Coins Collected: /10";
	    span.style.fontSize = '20px';

    
    
    
    //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 = false;
					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( 2000, 2000);
        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,-5,0);
        threeworld.scene.add(floor);
        threeworld.scene.add(wall1);
        threeworld.scene.add(wall2);
        threeworld.scene.add(wall3);
        threeworld.scene.add(wall4);
        threeworld.scene.add(roof);

        coin();
        collect_coin();
		

	};


	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;
        
        //======================================================================
        
        
        
	
	};

}