Code viewer for World: tuitef2 Updated food 10

// Cloned by tuitef2 on 18 Feb 2019 from World "Updated food 10" by  Mikey Dowling 
// Please leave this clone trail here.
 


// Cloned by  Mikey Dowling on 11 Feb 2019 from World "captureTheFlag" by tuitef2 
// Please leave this clone trail here.
 
var cam;
var meshFloor;
var food_list = [];
var keyboard = {};
var distanceX;
var distanceZ;
var updatedPosX;
var updatedPosZ;
var player = { height:1.8, speed:0.3, turnSpeed:0.05}; //to get player position get cam position

var scene = new THREE.Scene(); //CREATE SCENE
var renderer = new THREE.WebGLRenderer();


function render(){
	renderer.setSize(window.innerWidth, window.innerHeight);
	document.body.appendChild(renderer.domElement);
}

function camera(){
    cam = new THREE.PerspectiveCamera(90, window.innerWidth/window.innerHeight, 0.1, 100);
	cam.position.set(0, player.height, 0); //position.x,position.y,position.z
	cam.lookAt(new THREE.Vector3(0,player.height,0));
}

function ground(){
    meshFloor = new THREE.Mesh(
	new THREE.PlaneGeometry(200,100),
	new THREE.MeshBasicMaterial({ color: "#e0e1e2"})) ;
	
	var edgeHelper = new THREE.EdgesHelper(meshFloor);
	edgeHelper.material.color.set( 0xff4444 );
    scene.add(edgeHelper);
    
	meshFloor.rotation.x -= Math.PI / 2; // Rotate the plane 90 degrees as if you don't do this it'll be like a tv screen.
	scene.add(meshFloor);
}

function food(){
    for (i=0; i < 10; i++){
        mesh = new THREE.Mesh(
    		new THREE.SphereGeometry(1),
    		new THREE.MeshBasicMaterial({color:0xff4444}));
    	mesh.position.z += AB.randomIntAtoB(-50,50); 
    	mesh.position.x += AB.randomIntAtoB(-90,90);
    	mesh.position.y += 1; // Move the mesh up 1 meter
    	scene.add(mesh);
    	food_list.push(mesh);
    }
}

function eat_food(){
    for(i = 0;i <food_list.length; i++){
        distanceX = Math.abs((parseInt(updatedPosX)) - food_list[i].position.x);
        distanceZ =  Math.abs((parseInt(updatedPosZ)) - food_list[i].position.z);
        if(distanceX <=2 && distanceZ <= 2 ){ //if player is close 
            food_list[i].position.x = AB.randomIntAtoB(-90,90);
            food_list[i].position.z = AB.randomIntAtoB(-40,40);//change the position of the item
        }

    }
}

function movement(){
	requestAnimationFrame(movement);
	
	//  food rotation
	for (j = 0; j < food_list.length; j++){
        food_list[j].rotation.x += 0.01;
        food_list[j].rotation.y += 0.02;
    }
	
    	// Keyboard movement inputs
    	if(keyboard[87]){ // W key
    	    if (cam.position.x<= 100 && cam.position.x>= -100){
        		cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
        		cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
        		updatedPosX = cam.position.x;
        		updatedPosZ = cam.position.z;
        	
        	    }
        	    else{
        	        cam.position.x += Math.sin(cam.rotation.y) * player.speed;
        	 
        	    }
    	    if(cam.position.z<= 50 && cam.position.z>= -50){
    	        cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
        		cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
        		updatedPosX = cam.position.x;
        		updatedPosZ = cam.position.z;
        	
    	    }
    	    else{
    	        cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
    	        
    	    }
    	    eat_food();
    	    get_coords();
    	}
    	if(keyboard[83]){ // S key
    	    if (cam.position.x<= 100 && cam.position.x>= -100){
    		    cam.position.x += Math.sin(cam.rotation.y) * player.speed;
    		    cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
    		    
    	    }
    	        else{
        	        cam.position.x -= Math.sin(cam.rotation.y) * player.speed;
        	    }
        	if(cam.position.z<= 50 && cam.position.z>= -50){
    	        cam.position.x += Math.sin(cam.rotation.y) * player.speed;
        		cam.position.z += -Math.cos(cam.rotation.y) * player.speed;
    	    }
    	    else{
    	        cam.position.z -= -Math.cos(cam.rotation.y) * player.speed;
    	    }
    	    eat_food()
    	    get_coords();
    	}
    
    	if(keyboard[65]){ // A key
    		// Redirect motion by 90 degrees
    	    if (cam.position.x <= 100 && cam.position.x >= -100){
        		cam.position.x += Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
        		cam.position.z += -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
        		updatedPosX = cam.position.x;
        		updatedPosZ = cam.position.z;
    	    }
    	        else{
    	            cam.position.x = Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
        	    }
        	if(cam.position.z <= 50 && cam.position.z >= -50){
    	        cam.position.x += Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
        		cam.position.z += -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
    	    }
    	    else{
    	        cam.position.z = -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
    	    }
    	    eat_food()
    	    get_coords();
    	        
    	}
    	if(keyboard[68]){ // D key
    	    if (cam.position.x<= 100 && cam.position.x>= -100){
    		    cam.position.x += Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
    		    cam.position.z += -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
    		    updatedPosX = cam.position.x;
        		updatedPosZ = cam.position.z;
    	    }
    	        else{
    	            cam.position.x = Math.sin(cam.rotation.y + Math.PI/2) * player.speed;
        	    }
        	if(cam.position.z<= 50 && cam.position.z>= -50){
    	        cam.position.x += Math.sin(cam.rotation.y - Math.PI/2) * player.speed;
        		cam.position.z += -Math.cos(cam.rotation.y - Math.PI/2) * player.speed;
    	    }
    	    else{
    	        cam.position.z = -Math.cos(cam.rotation.y + Math.PI/2) * player.speed;
    	    }
    	    eat_food()
    	    get_coords();
    	}
		
	// Keyboard turn inputs
	if(keyboard[37]){ // left arrow key
		cam.rotation.y -= player.turnSpeed;
	}
	if(keyboard[39]){ // right arrow key
		cam.rotation.y += player.turnSpeed;
	}
	
	renderer.render(scene, cam);
}

function keyDown(event){
	keyboard[event.keyCode] = true;
}

function keyUp(event){
	keyboard[event.keyCode] = false;
}


function get_coords(){
    document.getElementById("user_span1").innerHTML = parseInt((cam.position.x));
    document.getElementById("user_span2").innerHTML = parseInt((cam.position.y));
    document.getElementById("user_span3").innerHTML = parseInt((cam.position.z));
    
}


function main(){
    AB.showRunHeader();
	render();
	camera();
	food();
	eat_food();
	ground();
	movement();
	get_coords();
}

window.addEventListener('keydown', keyDown); 
window.addEventListener('keyup', keyUp);

window.onload = main;