Code viewer for World: tuitef2 Capturetheflag

// Cloned by tuitef2 on 25 Feb 2019 from World "Capturetheflag" by tuitef2 
// Please leave this clone trail here.
 
var cam;
var meshFloor;
var food_list = [];
var keyboard = {};
var distance;
var updatedPosX;
var updatedPosZ;
var objLoader = new THREE.OBJLoader();
objLoader.setPath("/uploads/tuitef2/");
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath("/uploads/tuitef2/");

var LEVEL = [
        '# # # # # # # # # # # # # # # # # # # # # # # # # # # #',
        '# . . . . . . . . . . . . # # . . . . . . . . . . . . #',
        '# . # # # # . # # # # # . # # . # # # # # . # # # # . #',
        '# o # # # # . # # # # # . # # . # # # # # . # # # # o #',
        '# . # # # # . # # # # # . # # . # # # # # . # # # # . #',
        '# . . . . . . . . . . . . . . . . . . . . . . . . . . #',
        '# . # # # # . # # . # # # # # # # # . # # . # # # # . #',
        '# . # # # # . # # . # # # # # # # # . # # . # # # # . #',
        '# . . . . . . # # . . . . # # . . . . # # . . . . . . #',
        '# # # # # # . # # # # #   # #   # # # # # . # # # # # #',
        '          # . # # # # #   # #   # # # # # . #          ',
        '          # . # #         G           # # . #          ',
        '          # . # #   # # # # # # # #   # # . #          ',
        '# # # # # # . # #   #             #   # # . # # # # # #',
        '            .       #             #       .            ',
        '# # # # # # . # #   #             #   # # . # # # # # #',
        '          # . # #   # # # # # # # #   # # . #          ',
        '          # . # #                     # # . #          ',
        '          # . # #   # # # # # # # #   # # . #          ',
        '# # # # # # . # #   # # # # # # # #   # # . # # # # # #',
        '# . . . . . . . . . . . . # # . . . . . . . . . . . . #',
        '# . # # # # . # # # # # . # # . # # # # # . # # # # . #',
        '# . # # # # . # # # # # . # # . # # # # # . # # # # . #',
        '# o . . # # . . . . . . . P   . . . . . . . # # . . o #',
        '# # # . # # . # # . # # # # # # # # . # # . # # . # # #',
        '# # # . # # . # # . # # # # # # # # . # # . # # . # # #',
        '# . . . . . . # # . . . . # # . . . . # # . . . . . . #',
        '# . # # # # # # # # # # . # # . # # # # # # # # # # . #',
        '# . # # # # # # # # # # . # # . # # # # # # # # # # . #',
        '# . . . . . . . . . . . . . . . . . . . . . . . . . . #',
        '# # # # # # # # # # # # # # # # # # # # # # # # # # # #'
    ];

var player = { height:3.5, speed:0.5, turnSpeed:0.2}; //to get player position get cam position

var scene = new THREE.Scene(); //CREATE SCENE
var sky_texture = new THREE.TextureLoader().load( 'uploads/tuitef2/skyyy.png' );
scene.background = sky_texture;
//scene.fog = new THREE.Fog("#a6e0ca", 0.0010, 90);


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, 1000);
	cam.position.set(0, player.height, -5); //position.x,position.y,position.z
	cam.lookAt(new THREE.Vector3(0,player.height,0));
}

function ground(){
    var texture = new THREE.TextureLoader().load( 'uploads/tuitef2/ground_texture1.png' );

// immediately use the texture for material creation
    meshFloor = new THREE.Mesh(
	new THREE.PlaneGeometry(400,400),
	new THREE.MeshBasicMaterial({map:texture}));
    
	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 randomize_trees(){
    mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
        materials.preload();
        objLoader.setMaterials(materials);
        
        objLoader.load("PUSHILIN_forest.obj", function(mesh){
                mesh.position.z += AB.randomIntAtoB(-90,90); 
        	    mesh.position.x += AB.randomIntAtoB(180,200);
                mesh.position.y+=5;
                mesh.scale.multiplyScalar(15);
                scene.add(mesh);
           });
           

    });
    
    mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
        materials.preload();
        objLoader.setMaterials(materials);
        
        objLoader.load("PUSHILIN_forest.obj", function(mesh){
                mesh.position.z += AB.randomIntAtoB(-90,90); 
        	    mesh.position.x += AB.randomIntAtoB(-180,-200);
                mesh.position.y+=5;
                mesh.scale.multiplyScalar(15);
                scene.add(mesh);
           });
          
    });
    
    mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
        materials.preload();
        objLoader.setMaterials(materials);
        
        objLoader.load("PUSHILIN_forest.obj", function(mesh){
                //mesh.position.z += AB.randomIntAtoB(-90,90); 
        	    mesh.position.z += AB.randomIntAtoB(-90,90);
        	    mesh.position.x += AB.randomIntAtoB(-200,200);
                mesh.position.y+=5;
                mesh.scale.multiplyScalar(15);
                scene.add(mesh);
           });
        var light = new THREE.DirectionalLight( "#7fe093", 0.3 );
        light.position.set( 0, 1, 0 );
        scene.add( light );
    });
    
    mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
        materials.preload();
        objLoader.setMaterials(materials);
        objLoader.load("tree01.obj", function(mesh){
                mesh.position.z += AB.randomIntAtoB(-90,90); 
        	    mesh.position.x += AB.randomIntAtoB(-200,200);
                mesh.position.y+=1;
                mesh.scale.multiplyScalar(4);
                scene.add(mesh);
          });
    });
    
    var light = new THREE.DirectionalLight( "#7fe093", 0.3 );
        light.position.set( 0, 1, 0 );
        scene.add( light );
}

function createMap(level) {
    var map = {};
    map.bottom = -(level.length - 1);
    map.top = 0;
    map.left = 0;
    map.right = 0;
    map.numDots = 0;
    map.pacmanSpawn = null;
    map.ghostSpawn = null;

    var x, y;
    for (var row = 0; row < level.length; row++) {
        // Set the coordinates of the map so that they match the
        // coordinate system for objects.
        y = -row;

        map[y] = {};

        // Get the length of the longest row in the level definition.
        var length = Math.floor(level[row].length / 2);
        //map.right = Math.max(map.right, length - 1);
        map.right = Math.max(map.right, length);

        // Skip every second element, which is just a space for readability.
        for (var column = 0; column < level[row].length; column += 2) {
            x = Math.floor(column / 2);

            var cell = level[row][column];
            var object = null;

            if (cell === '#') {
                object = createWall();
            }
            // } else if (cell === '.') {
            //     object = createDot();
            //     map.numDots += 1;
            // } else if (cell === 'o') {
            //     object = createPowerPellet();
            // } else if (cell === 'P') {
            //     map.pacmanSpawn = new THREE.Vector3(x, y, 0);
            // } else if (cell === 'G') {
            //     map.ghostSpawn = new THREE.Vector3(x, y, 0);
            // }

            if (object !== null) {
                object.position.set(x, y, 0);
                map[y][x] = object;
                scene.add(object);
            }
        }
    }

    map.centerX = (map.left + map.right) / 2;
    map.centerY = (map.bottom + map.top) / 2;

    return map;
}

function createWall(){

        var wallGeometry = new THREE.BoxGeometry(1, 1, 1);
        var wallMaterial = new THREE.MeshLambertMaterial({ color: 'blue' });
        var wall = new THREE.Mesh(wallGeometry, wallMaterial);
        wall.isWall = true;
        return wall;
}

function isWall(map, position) {
    var cell = getAt(map, position);
    return cell && cell.isWall === true;
}
function getAt(map, position) {
        var x = Math.round(position.x), y = Math.round(position.y);
        return map[y] && map[y][x];
    }
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 += 2; // Move the mesh up 1 meter
        mesh.scale.multiplyScalar(1.5);
        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);
    	// Keyboard movement inputs
    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<= 200 && cam.position.x>= -200){
                        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<= 200 && cam.position.z>= -200){
                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();
        }
        if(keyboard[83]){ // S key
            if (cam.position.x<= 200 && cam.position.x>= -200){
                    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<= 200 && cam.position.z>= -200){
                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();
        }
    
        if(keyboard[65]){ // A key
                // Redirect motion by 90 degrees
            if (cam.position.x <= 200 && cam.position.x >= -200){
                    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 <= 200 && cam.position.z >= -200){
                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();
                
        }
        if(keyboard[68]){ // D key
            if (cam.position.x<= 200 && cam.position.x>= -200){
                    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<= 200 && cam.position.z>= -200){
                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();
        }
                
        // 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 main(){
    render();
    camera();
    ground();
    food();
    eat_food();
    var map = createMap(scene, LEVEL);
    movement();
    for(i = 0;i<15;i++){
        randomize_trees();
    }
}
window.addEventListener('keydown', keyDown); 
window.addEventListener('keyup', keyUp);
window.onload = main;