Code viewer for World: AlienLab
//skybox images
const SKYBOX_ARRAY = [										 
    "/uploads/dg/right.jpg",
    "/uploads/dg/left.jpg",
    "/uploads/dg/top.jpg",
    "/uploads/dg/bot.jpg",
    "/uploads/dg/front.jpg",
    "/uploads/dg/back.jpg",
];

//sounds list
const shot = "uploads/corbard2/Untitledvideo-MadewithClipchamp1.mp3";
const fiveScore = "uploads/corbard2/Triplekill.mp3";
const thirtyScore = "uploads/corbard2/Killingspree.mp3";
const SeventyScore = "uploads/corbard2/Halo3Overkill.mp3.mp3";
const hundredScore = "uploads/corbard2/Killtacular.mp3";


document.body.style.cursor = "url('https://ancientbrain.com/uploads/corbard2/bestcrosshairever.cur'), auto"; // loads custom cursor into the world


AB.newSplash ( splashScreenStart() );
document.getElementById("splashbutton").id = "startsplash";


AB.world.newRun = function()
{        
    AB.socketStart();
    initScene();
    AB.runReady = false;
    cameraControls();
    addLight();
    loadImg();
    loadImg2();
}


function initScene()
{
    AB.headerWidth(350);
    var colour = new THREE.Color(0xff0000);
    ABWorld.init2d ( 290, 700, colour ); // camera distance, world render size, colour
    ABWorld.scene.background = new THREE.CubeTextureLoader().load ( SKYBOX_ARRAY, 	function() // load skybox images
    { 
	    ABWorld.render(); 
	 
	    AB.removeLoading();
	
	    AB.runReady = true;
    });
}


const gameClock = new THREE.Clock();
var allCircles = new Array(0);
AB.world.nextStep = function()
{
    if(Math.trunc(gameClock.getElapsedTime()) == 32)
{
    AB.runReady = false;
    AB.newSplash ( splashScreenEnd() );
    document.getElementById("splashbutton").innerHTML = "Play Again";
    document.getElementById("splashbutton").onclick = reload;
}
    if (allCircles.length < max_circles) // max circles set to 3, if the amount of current circles drops below that (2) spawn another to make it 3 again
{
    spawnCircle(); // keeps it so there is always 3 circles in world
}
    if(AB.socket)
{
    if(AB.socket.connected)
{
    AB.socketOut(p1score) // pushes out score
    AB.msg("P1 Score = " + p1score + " P2 score = " + p2score + "\n Time remaining: " + Math.trunc(32 - gameClock.getElapsedTime()) );
}
}
};


function loadImg() // loads gun model in a position that makes it look realistic first person
{
    var loader = new THREE.GLTFLoader();
    loader.load( "/uploads/corbard2/Sci-fiNailGun.glb", function ( gltf )
{
    var model = gltf.scene;
    model.scale.set(15, 15, 15);
    model.position.set(120, 150, 100);
    model.rotation.set(181, 60, 0);
    ABWorld.scene.add(model);
});
}

function loadImg2() // loads space ship model
{
    var loader2 = new THREE.GLTFLoader();
    loader2.load( "/uploads/corbard2/ufo.glb", function ( gltf )
{
    var model2 = gltf.scene;
    model2.scale.set(45, 45, 45);
    model2.position.set(-150, 90, -100);
    model2.rotation.set(181, 60, 0);
    ABWorld.scene.add(model2);
});
}


function addLight() // adds basic white light to world, called at beginning of world
{
    var light = new THREE.PointLight(0xFFFFFF, 1, 1000);
    light.position.set(0, 0, 0);
    ABWorld.scene.add(light);
}


function redLight() // adds red light to world that reflects off models to mimic gun overheat
{
    var light2 = new THREE.PointLight(0xFF0000, 0.2, 1000);
    light2.position.set(0, 0, 0);
    ABWorld.scene.add(light2);
}


function goldLight() // adds gold light to change models to gold when player hits a score of 100
{
    var light3 = new THREE.PointLight(0xFFD700, 10, 1000);
    light3.position.set(0, 0, 0);
    ABWorld.scene.add(light3);
}

var gridSetup = [[-100, 0, 100],[-100, 0, 0],[-100, 0, -100],[0, 0, 100],[0, 0, 0],[0, 0, -100],[100, 0, 100],[100, 0, 0],[100, 0, -100]]; // sets a 3x3 grid
var max_circles = 3;
function spawnCircle() // adds sphere models and adds grid
{
    var texture = new THREE.TextureLoader().load("/uploads/corbard2/sadAlien.jpg");
    var shape = new THREE.SphereGeometry(30, 64, 32);
    var cover = new THREE.MeshBasicMaterial({map: texture});
    var target = new THREE.Mesh(shape, cover);
    target.rotation.set(181, 80, 0); // rotates sphere so alien faces face forward
    
    var randomIndex = AB.randomIntAtoB(0, gridSetup.length);
    var item = gridSetup[randomIndex];
    target.position.set(item[0], item[1], item[2]);
    
    ABWorld.scene.add(target);
    gridSetup.splice(randomIndex, 1);                             
    allCircles.push(target);
}


function mouseDrag(x, y)
{
    return;
}


function cameraControls()
{
    ABHandler.initMouseDrag 	= mouseClick;
    ABHandler.mouseDrag			= mouseDrag;
}


var x = 0;
function mouseClick(x, y) // calls shot sound on every click, calls score related sounds on selected player scores
{
    audioHandler("shoot");
    if (p1score == 5)
    {
        audioHandler("fiveScore");
    }
    if (p1score == 35)
    {
        audioHandler("thirtyScore");
    }
    if (p1score == 70)
    {
        audioHandler("SeventyScore");
    }
    if (p1score == 100)
    {
        audioHandler("hundredScore");
    }
    targetHit(x, y);
    p1score--; // every click takes -1 from score, a hit circle adds +2. when circle is hit 2-1 = +1 to score, when circle missed only -1 is called
    return;
}


var p1score = 0;
var p2score = 0;
function targetHit(x, y)
{
    if(allCircles.length > 0)
    {
    for (var i = 0; i < allCircles.length; i++)
    {
    // gun heat start //
        if (p1score == 10)
        {
            redLight();
        }
        if (p1score == 20)
        {
            redLight();
        }
        if (p1score == 30)
        {
            redLight();
        }
        if (p1score == 40)
        {
            redLight();
        }
        if (p1score == 50)
        {
            redLight();
        }
        if (p1score == 60)
        {
            redLight();
        }
        if (p1score == 70)
        {
            redLight();
        }
        if (p1score == 80)
        {
            redLight();
        }
        if (p1score == 90)
        {
            redLight();
        }
        if (p1score == 100)
        {
            goldLight();
        }
    // gun heat end //
    var object = allCircles[i];
    if ( ABWorld.hitsObject ( x, y, object )) // removes hit targets and adds +2 to score, bare mind every shot is -1  so every target hit is actually only adding +1 score
    {
        var pos = [object.position.x, object.position.y, object.position.z];
        gridSetup.push(pos);
        ABWorld.scene.remove(object);
        allCircles.splice(i, 1);
        p1score = p1score + 2;
        break;
    }
}
}
    return;
}


function audioHandler(instance)
{
    var j;
    if (instance == "shoot")
    {
        j = new Audio( shot );		
        j.play();
    }
    if (instance == "fiveScore")
    {
        j = new Audio( fiveScore );		
        j.play();
    }
    if (instance == "thirtyScore")
    {
        j = new Audio( thirtyScore );		
        j.play();
    }
    if (instance == "SeventyScore")
    {
        j = new Audio( SeventyScore );		
        j.play();
    }
    if (instance == "hundredScore")
    {
        j = new Audio( hundredScore );		
        j.play();
    }
}


function splashScreenStart()
{
    var startText = "<p>Shoot as many targets as you can in 30 seconds.</p>";
    
    startText = startText + "<p>Aim with your mouse and click to shoot. Misses result in a -1 score.</p>" ;
    
    startText = startText + "<p>Use scroll wheel to adjust camera distance.</p>" ;
    
    startText = startText + "<p>Two players must start at same time to make use of 1v1 feature!!!</p>" ;
    
    startText = startText + "<p>Current High Score: 151 Set by: dylzu</p>" ;
    
    startText = startText + "<p>DM dylzu#0001 on discord with video proof for highscore submissions!</p>" ;
    
    return ( startText );
}


function splashScreenEnd()
{
    var endText = "<h2>Game Over!</h2>";
    
    if (p1score > p2score)
    {
        endText = endText + "<p>You Won!</p>" 
    }
    else if (p1score < p2score)
    {
        endText = endText + "<p>You Lost!</p>" 
    }
    else if (p1score == p2score)
    {
        endText = endText + "<p>Draw</p>" 
        endText = endText + "<p>vv Both had a score of vv</p>" + p2score
    }
    
    endText = endText + "<p>Your score:</p>" + p1score
    
    endText = endText + "<p>Opponent score:</p>" + p2score
    
    return ( endText );
}


$("#startsplash").click(function() 
{
    AB.runReady = true;
    AB.removeSplash();
});


function reload()
{
    location.reload();
}


AB.socketIn = function (score) // calls in score
{
    p2score = score
}