// Cloned by Liam on 30 Nov 2022 from World "User-controlled Model World" by Starter user // Please leave this clone trail here.// ==== Starter World =================================================================================================// This code is designed for use on the Ancient Brain site.// This code may be freely copied and edited by anyone on the Ancient Brain site.// To include a working run of this program on another site, see the "Embed code" links provided on Ancient Brain.// ====================================================================================================================// User controlled (not Mind controlled) 3D model World with various features:// - User controlled UP/LEFT/RIGHT arrows move model // - "First Person View" - Camera rotates with direction you are facing// Best effect is with "Move with" camera// - Can have one or multiple ZOMBIEs chasing you // Smooth movement// UP moves forward in whatever angle you are at// LEFT/RIGHT rotate by small angle// Uses x,y,z rather than grid of squares // Has collision detection for ZOMBIE moves// Things to do: // - Initialise ZOMBIEs so they are not already colliding // - Collision detection for agent moves// ===================================================================================================================// === Start of tweaker's box ======================================================================================== // ===================================================================================================================// The easiest things to modify are in this box.// You should be able to change things in this box without being a JavaScript programmer.// Go ahead and change some of these. What's the worst that could happen?var hitCount =0;
AB.clockTick =100;// Speed of run: Step every n milliseconds. Default 100.
AB.maxSteps =10000;// Length of run: Maximum length of run in steps. Default 1000.
AB.screenshotStep =100;// Take screenshot on this step. (All resources should have finished loading.) Default 50.
AB.drawRunControls =false;// Scrap the Run/Step/Pause controls//---- global constants: -------------------------------------------------------var zombieAmount =300;// number of ZOMBIEs: const OBJ_ZOMBIE ='/uploads/goobert/zombie.obj';const floorTextureFile ="/uploads/goobert/grass2.png";// floor textureconst steveScale =70;// multiply model sizes by some amount:const TEXTURE_ZOMBIE ='/uploads/goobert/zombie.png';var SCALE_ZOMBIE = AB.randomFloatAtoB (0,0);const MUSIC_BACK ='/uploads/liam/mc.mp3';const MAXPOS =3500;// length of one side of the arena const SKYCOLOR =0xffffcc;// a number, not a string const LIGHTCOLOR =0xffffff;const startRadiusConst = MAXPOS ;// distance from centre to start the camera atconst maxRadiusConst = MAXPOS *10;// maximum distance from camera we will render things var steveSpeed =0;// how much agent moves each step var zombieSpeed =0;// how much enemy moves each step const CLOSE_LIMIT =25;// this is as close as models come to other modelsconst mixers =[];const clock =new THREE.Clock();const timeClock =new THREE.Clock();varWorldSize=10000;//--- lookat and follow -----------------------------------------------------------------// camera on "Move With" should move up/down in y axis as we re-scale objects// to place the follow camera just above the agent, something like: // to point the camera at ZOMBIE's face, sssomething like: const FOLLOW_Y = steveScale *4;// going high up (or forward/back) means we get it out of agent's hair (makes it first person)const LOOKAT_Y = SCALE_ZOMBIE *40;const CAMERASHIFT =- steveScale *40;// shift camera behind agent, looking past agent along line of sight function mainMenu(){
AB.newSplash();
let splash = document.getElementById("splash-inner");
let play = document.getElementById("splashbutton");
document.getElementById("splash").style.backgroundImage ="url('https://cdn.discordapp.com/attachments/791350055810367488/1049424119130755083/mc.gif')";var h1 = document.createElement("p");
h1.innerHTML ="HOW TO PLAY";
h1.style.fontSize ="25px";
h1.style.font ="bold 20px helvetica";
h1.style.color ="white";var p = document.createElement("p");
p.innerHTML ="Compete with another player to see who can survive the longest!<br> Select a difficulty to spawn the zombies and survive from the zombies for as long as you can! <br> The Zombies are bigger and faster depending on the difficulty you select.";
p.style.color ="white";
p.style.font =" 20px helvetica";
p.style.fontSize ="18px";var h2 = document.createElement("p");
h2.innerHTML ="CONTROLS";
h2.style.fontSize ="25px";
h2.style.font ="bold 20px helvetica";
h2.style.color ="white";
splash.appendChild(h1);
splash.appendChild(p);
splash.appendChild(h2);
play.addEventListener("click", beginGame);
splash.appendChild(play);// adds the button start button to the splash page.\if(AB.socket){if(AB.socket.connected){
console.log("SOCKET CONNECTED");// AB.socketOut("TESTING SOCKETS!!!");}}
document.getElementById("splash").style.backgroundImage ="url('https://media.tenor.com/GNSUo4LGY8AAAAAC/wall-paper.gif')";// adds background image with entire splash death screen
document.getElementById("splash").style.backgroundSize ="cover";// sets the background image to cover the entire splash.var p = document.createElement("p");var p1 = document.createElement("p");var h2 = document.createElement("p");}function splashScreen(text){return(text);}function death(){// this function is called when the player has no more lives.
steve.position.y =0;
steve.position.x =0;
steve.position.z =0;
steveSpeed =0
AB.newSplash();
let splash = document.getElementById("splash-inner");
let span = document.getElementById("splashbutton");
span.innerHTML ="Play again";
document.getElementById("splash").style.backgroundImage ="url('https://picfiles.alphacoders.com/478/478314.png')";// adds background image with entire splash death screen
document.getElementById("splash").style.backgroundSize ="cover";// sets the background image to cover the entire splash.
span.addEventListener("click", beginGame);// when the restart button is clicked the beginGame function is executed.var winmessage = document.createElement("p");
winmessage.innerHTML ="<br><b><font color=white> You ran out of lives! Your opponent has won!<b>";
splash.append(winmessage);
winmessage.style.fontSize ="25px";
zombieSpeed =0;// freezes the zombies in place until updated when game is restarted.
steve.rotation.z =Math.PI /1;// faces steve model in the default direction (forward).for(var i =0; i < zombieAmount ; i++)// loops for all zombies and changes their scale.{
zombieArray[i].scale.set(0,0,0);// sets the zombies to 0 so invisible.}}function winner(){// this function is called when the player has no more lives.
steve.position.y =0;
steve.position.x =0;
steve.position.z =0;
steveSpeed =0;
AB.newSplash();
let splash = document.getElementById("splash-inner");
let span = document.getElementById("splashbutton");
span.innerHTML ="Restart";
document.getElementById("splash").style.backgroundImage ="url('https://picfiles.alphacoders.com/478/478314.png')";// adds background image with entire splash death screen
document.getElementById("splash").style.backgroundSize ="cover";// sets the background image to cover the entire splash.
span.addEventListener("click", beginGame);// when the restart button is clicked the beginGame function is executed.var winmessage = document.createElement("p");
winmessage.innerHTML ="<br><b>YOU WONNNN!!!<b>";
splash.append(winmessage);
winmessage.style.fontSize ="25px";
zombieSpeed =0// freezes the zombies in place until updated when game is restarted.
steve.rotation.z =Math.PI /1// faces steve model in the default direction (forward).for(var i =0; i < zombieAmount ; i++)// loops for all zombies and changes their scale.{
zombieArray[i].scale.set(0,0,0);// sets the zombies to 0 so invisible.}}ABHandler.MAXCAMERAPOS = maxRadiusConst ;ABHandler.GROUNDZERO =true;// "ground" exists at altitude zeroconst SKYBOX_ARRAY =["/uploads/starter/sky_pos_z.jpg","/uploads/starter/sky_neg_z.jpg","/uploads/starter/sky_pos_y.jpg","/uploads/starter/sky_neg_y.jpg","/uploads/starter/sky_pos_x.jpg","/uploads/starter/sky_neg_x.jpg"];var zombieArray =newArray( zombieAmount );var steve, zombie;var steveRotation =0;var enemyRotation =0;var ZOMBIE_texture;function loadResources()// asynchronous file loads - call initScene() when all finished {var loader =new THREE.OBJLoader(new THREE.LoadingManager());// load ZOMBIE - OBJ that we will paint with a texture
loader.load ( OBJ_ZOMBIE,function( object ){
zombie = object;// canonical enemy object - may be copied multiple times if( asynchFinished()) initScene();});
THREE.DefaultLoadingManager.addHandler (/\.tga$/i,new THREE.TGALoader());// load textures var loader2 =new THREE.TextureLoader();
loader2.load ( TEXTURE_ZOMBIE,function( thetexture ){
thetexture.minFilter = THREE.LinearFilter;
ZOMBIE_texture = thetexture;if( asynchFinished()) initScene();});}var difficulty ="<p align=\"center\"><button id='easyButton', style=\"height:50px;width:100px;\"><b>EASY</b></button><button id='normalButton', style=\"height:50px;width:100px\"><b>NORMAL</b></button><button id='hardButton', style=\"height:50px;width:100px\"><b>HARD</b></button></p>";var chooseDifficulty ="<p align=\"center\"><div id='chooseDifficulty', <b>CHOOSE A DIFFICULTY TO PLAY:</b></div></p>";function setDifficulty(){
$("#user_span4").html( chooseDifficulty );
$("#user_span5").html( difficulty );
easyButton.style.background ='#4CAF63';
normalButton.style.background ='#FA9E2A';
hardButton.style.background ='#C43737';
document.getElementById("chooseDifficulty").style.font ="20px Helvetica ";
document.getElementById("chooseDifficulty").style.fontStyle ="bold";
document.getElementById("easyButton").style.font ="17px Helvetica ";
document.getElementById("normalButton").style.font ="17px Helvetica ";
document.getElementById("hardButton").style.font ="17px Helvetica ";
document.getElementById("easyButton").addEventListener("click", easyMode);// checks if the button with the ID of easyButton is pressed, if so the easyMode is set (function is called)
document.getElementById("normalButton").addEventListener("click", normalMode);// checks if the button with the ID of normalButton is pressed, if so the normalMode is set (function is called)
document.getElementById("hardButton").addEventListener("click", hardMode);// checks if the button with the ID of hardButton is pressed, if so the hardMode is set (function is called)}function loadModels(){const loader =new THREE.GLTFLoader();const onLoad =( gltf, position )=>{var model = gltf.scene.children[0];// the model is the first child of the scene
model.position.copy( position );// set the position of the modelvar animation = gltf.animations[1];var animationIdle = gltf.animations[0];// get the animation from the gltf objectconst mixer =new THREE.AnimationMixer( model );
mixers.push( mixer );const action = mixer.clipAction( animation );
action.play();ABWorld.scene.add( model );
steve = model;
model.scale.set(5,5,5);
initScene();
lighting();};var stevePosition =new THREE.Vector3(0,0,0);const mutantPosition =new THREE.Vector3(10,0,-60);// can load 3D models of other user:
loader.load ('/uploads/liam/steve.glb', glb => onLoad ( glb, stevePosition ));// loader.load ( '/uploads/liam/mutant.glb', glb => onLoad ( glb, mutantPosition ) );}function asynchFinished()// all file loads returned {if( ZOMBIE_texture && zombie && steve )returntrue;elsereturnfalse;}function initScene()// all file loads have returned {// add agent model start at center// ABWorld.scene.add( steve );
setLookatFollow();// set up lookat and follow in direction agent is pointing in // add enemies// first paint and size the canonical enemy object
zombie.traverse (function( child ){if( child instanceof THREE.Mesh)
child.material.map = ZOMBIE_texture ;});
zombie.scale.multiplyScalar ( SCALE_ZOMBIE );// scale it // add perhaps multiple enemy models, starting near outside of arenafor(var i =0; i < zombieAmount ; i++){var object = zombie.clone();// copy the object multiple times
object.position.y =0;
object.position.x = AB.randomPick ( AB.randomIntAtoB (-MAXPOS/2,-MAXPOS/3), AB.randomIntAtoB ( MAXPOS/3, MAXPOS/2));
object.position.z = AB.randomPick ( AB.randomIntAtoB (-MAXPOS/2,-MAXPOS/3), AB.randomIntAtoB ( MAXPOS/3, MAXPOS/2));ABWorld.scene.add( object );// adds the ZOMBIE enemies
zombieArray[i]= object;// save in array for later}}function lighting()// this function is called on the first new run of the game.{
steve.scale.multiplyScalar ( steveScale );// sets steve's scaleABWorld.scene.background =new THREE.CubeTextureLoader().load ( SKYBOX_ARRAY,function()// skybox{ABWorld.render();// skybox
AB.removeLoading();// removes loading splash
AB.runReady =true;});}// --- lookat and follow -----------------------------------// we do NOT automatically look at enemy / enemies // instead we look in direction we are facingfunction setLookatFollow()// set up lookat and follow {// follow - camera position // start with agent centre, then adjust it by small amount ABWorld.follow.copy ( steve.position );ABWorld.follow.y = FOLLOW_Y ;// shifted forward/back along line linking agent with direction it is facing ABWorld.follow.x =ABWorld.follow.x +( CAMERASHIFT *Math.sin(steveRotation));ABWorld.follow.z =ABWorld.follow.z +( CAMERASHIFT *Math.cos(steveRotation));// lookat - look at point in distance along line we are facing// start with agent centre, then adjust it along line by huge amount ABWorld.lookat.copy ( steve.position );ABWorld.lookat.y = LOOKAT_Y ;ABWorld.lookat.x =ABWorld.lookat.x +((startRadiusConst *3)*Math.sin(steveRotation));ABWorld.lookat.z =ABWorld.lookat.z +((startRadiusConst *3)*Math.cos(steveRotation));}// --- enemy move -----------------------------------// angle between two points (x1,y1) and (x2,y2)function angleTwoPoints ( x1, y1, x2, y2 ){returnMath.atan2 ( x2 - x1, y2 - y1 );}function dist ( a, b )// distance between them when a,b are Three vectors {return( a.distanceTo ( b ));}var liveCount =0;var lives =5;var lives2 =0;function collision ( proposedMove, k )// proposed move FOR ZOMBIE k{if( dist ( proposedMove, steve.position )< CLOSE_LIMIT )
hitCount +=1;// updates every tick the enemey is inside the range of the close limit// console.log(hitCount);if( hitCount ==2)// if the player has been hit 4 times, the game is over (around being hit for 0.5 second){
lives -=1;
hitCount =0;// hit count is resit back to zerovar deathSound =newAudio('/uploads/liam/damage.mp3');// play damage sound everytime hitcounter is 4.
deathSound.play();}if( lives ===0)// if the player has no lives left, the death menu is displayed.{
death();// calling the death function
lives =1000;}for(var i=0; i < zombieAmount ; i++)if( i != k )if( dist ( proposedMove, zombieArray[i].position )< CLOSE_LIMIT )returntrue;// else returnfalse;}function moveEnemy(){for(var i =0; i < zombieAmount ; i++){var e = zombieArray[i];
enemyRotation = angleTwoPoints ( e.position.x, e.position.z, steve.position.x, steve.position.z );// rotate enemy to face agent
e.rotation.set(0, enemyRotation +85,0);// move along that line, if no collision with any other model var proposedMove =new THREE.Vector3(
e.position.x +( zombieSpeed *Math.sin(enemyRotation)),
e.position.y,
e.position.z +( zombieSpeed *Math.cos(enemyRotation)));if(! collision ( proposedMove, i ))
e.position.copy ( proposedMove );// else enemy i just misses a turn}}//--- key control of agent -------------------------------------------------------------const KEY_UP =38;const KEY_LEFT =37;const KEY_RIGHT =39;const KEY_DOWN =40;const SPACEBAR =32;const KEY_W =87;const KEY_A =65;const KEY_S =83;const KEY_D =68;var pressedcount =0;// counts how long the key is pressed for.var OURKEYS =[ KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_W, KEY_A, KEY_S, KEY_D ];function ourKeys ( event ){return( OURKEYS.includes ( event.keyCode ));}function keyHandler ( event ){if(! AB.runReady )returntrue;// not ready yet // if not handling this key, send it to default: if(! ourKeys ( event ))returntrue;// else handle it and prevent default:if( event.keyCode == KEY_UP || event.keyCode == KEY_W)// if the Up arrow or W key is pressed.{
pressedcount = pressedcount +1;// updates the pressed count each time the W key is pressed
console.log(pressedcount)// logs the pressing to console.if( pressedcount >8)// only play the sound every 8 key presses to avoid the sound effect overlapping.{
pressedcount =0;// resets the pressedcountvar forwardSound =newAudio('/uploads/liam/walk_forward.mp3');// play the walking forward sound effect
forwardSound.setLoop =(false);
forwardSound.setVolume =(0.1);
forwardSound.play();// audio is played}if( steve.position.z >-(WorldSize/2)){// console.log("up", (WorldSize / 2) );
steve.rotation.z =0;// face the agent backwards
steveRotation = steve.rotation.z;
steve.position.z = steve.position.z -( steveSpeed *Math.cos(steveRotation));// moves the agent in the direction its facing (backwards)}}if( event.keyCode == KEY_DOWN || event.keyCode == KEY_S)// if the down arrow or S key is pressed.{
pressedcount = pressedcount +1;// updates the pressed count each time the W key is pressed
console.log(pressedcount)// logs the pressing to console.if( pressedcount >8)// only play the sound every 8 key presses to avoid the sound effect overlapping.{
pressedcount =0;// resets the pressedcountvar backwardsSound =newAudio('/uploads/liam/walk_backward.mp3');// play the walking forward sound effect
backwardsSound.setLoop =(false);
backwardsSound.setVolume =(0.1);
backwardsSound.play();// audio is played}if( steve.position.z <(WorldSize/2)){// console.log("down", steve.position.z);
steve.rotation.z =Math.PI /1;// face the agent forward
steveRotation = steve.rotation.z;
steve.position.z = steve.position.z -( steveSpeed *Math.cos(steveRotation));// moves the agent in the direction its facing (forward)}}if( event.keyCode == KEY_LEFT || event.keyCode == KEY_A)// if the left arrow or A key is pressed.{
pressedcount = pressedcount +1;// updates the pressed count each time the W key is pressed
console.log(pressedcount)// logs the pressing to console.if( pressedcount >8)// only play the sound every 8 key presses to avoid the sound effect overlapping.{
pressedcount =0;// resets the pressedcountvar leftSound =newAudio('/uploads/liam/walk_left.mp3');// play the walking forward sound effect
leftSound.setLoop =(false);
leftSound.setVolume =(0.1);
leftSound.play();// audio is played}if(steve.position.x >-(WorldSize/2)){// console.log("left", steve.position.x);
steve.rotation.z =Math.PI /2;// face the agent left
steveRotation = steve.rotation.z;
steve.position.x = steve.position.x -( steveSpeed *Math.sin(steveRotation));// moves the agent in the direction its facing (left)}}if( event.keyCode == KEY_RIGHT || event.keyCode == KEY_D)// if the right arrow or D key is pressed.{
pressedcount = pressedcount +1;// updates the pressed count each time the W key is pressed
console.log(pressedcount)// logs the pressing to console.if( pressedcount >8)// only play the sound every 8 key presses to avoid the sound effect overlapping.{
pressedcount =0;// resets the pressedcountvar rightSound =newAudio('/uploads/liam/walk_right.mp3');// play the walking forward sound effect
rightSound.setLoop =(false);
rightSound.setVolume =(0.1);
rightSound.play();// audio is played}if(steve.position.x <(WorldSize/2)){// console.log("right", steve.position.x);
steve.rotation.z =Math.PI /-2;// face the agent right
steveRotation = steve.rotation.z;
steve.position.x = steve.position.x -( steveSpeed *Math.sin(steveRotation));// moves the agent in the direction its facing (left) }}// if ( event.keyCode == SPACEBAR ) // {// steve.rotation.z = Math.PI / -2; // face the agent right// steveRotation = steve.rotation.z;// steve.position.y = steve.position.y - ( steveSpeed * Math.sin(steveRotation) ); // moves the agent in the direction its facing (left)// steve.position.y = steve.position.y + ( steveSpeed * Math.sin(steveRotation) ); // moves the agent in the direction its facing (left)// }
setLookatFollow();// lookat/follow depend on change in agent position/rotation: M.H
event.stopPropagation(); event.preventDefault();returnfalse;}function easyMode()// this function is called when Easy difficulty is selected.{
lives =10
steveSpeed =100
zombieSpeed =50;// makes the enemy speed 50% slower.for(var i =0; i < zombieAmount ; i++)// loops for all zombies and increases their size to 700x700x700{
zombieArray[i].scale.set(300,300,300);// initial zombie size is set.}}function normalMode()// this function is called when Normal difficulty is selected.{
lives =3
steveSpeed =100
zombieSpeed =80;// increased zombie movement speed.for(var i =0; i < zombieAmount ; i++)// loops for all zombies and increases their size to 700x700x700{
zombieArray[i].scale.set(500,500,500);}}function hardMode()// this function is called when Easy difficulty is selected.{
lives =1
steveSpeed =100
zombieSpeed =200;// makes the enemy speed much fasterfor(var i =0; i < zombieAmount ; i++){
zombieArray[i].scale.set(700,700,700);// zombie is larger then normal mode.}}
AB.world.nextStep =function(){
time_elapsed =Math.trunc(timeClock.getElapsedTime());var playtime ="<p align=\"center\"><b>Playtime: "+ time_elapsed +"s</p>";
$("#user_span6").html( playtime );
moveEnemy();// enemies moves on their own clockconst delta = clock.getDelta();
mixers.forEach(( mixer )=>{ mixer.update( delta );});if(AB.socket){if(AB.socket.connected){if( lives2 >100){// if the users currently lives are over 100 that means the user is dead. as we set it to 1000 between rounds.
AB.msg("<p align=\"center\"> <font color=red> <B> Your lives: "+ lives +"<p align=\"center\"> <font color=blue> <B> Opponent's Lives: "+" Dead </B>");}if( lives2 <100){// if the user lives are not bigger then 100, that means real lives are in play and the value can be displayed.
AB.socketOut(lives)
AB.msg("<p align=\"center\"> <font color=red> <B> Your lives: </B>"+ lives +"<p align=\"center\"> <font color=blue> <B> Opponent's Lives: </B> "+ lives2);}}}};
AB.backgroundMusic ( MUSIC_BACK );function beginGame(){// removes the splash menu when button is pressed
steve.position.y =0;
steve.position.x =0;
steve.position.z =0;
initScene();
AB.removeSplash();
lives =5}
AB.world.newRun =function(){
AB.socketStart();
mainMenu();
AB.loadingScreen();
AB.runReady =false;ABWorld.init3d ( startRadiusConst, maxRadiusConst, SKYCOLOR );
loadResources();// aynch file loads // calls initScene() when it returns
loadModels();
setDifficulty();// lightvar ambient =new THREE.AmbientLight();ABWorld.scene.add( ambient );var thelight =new THREE.DirectionalLight( LIGHTCOLOR,3);
thelight.position.set( startRadiusConst, startRadiusConst, startRadiusConst );ABWorld.scene.add(thelight);varBlockSize=500;var floorGeometry =new THREE.PlaneBufferGeometry(WorldSize,WorldSize);
floorGeometry.rotateX(-Math.PI /2);// loading the floor texturevar floorTexture =new THREE.ImageUtils.loadTexture ( floorTextureFile );
floorTexture.minFilter = THREE.LinearFilter;
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;// world floor texture size and location
floorTexture.offset.set(0,0);
floorTexture.repeat.set(WorldSize/BlockSize,WorldSize/BlockSize);var floor =new THREE.Mesh(floorGeometry,new THREE.MeshBasicMaterial({map : floorTexture}));
floor.position.set(0,0,0);
threeworld.scene.add(floor);
document.onkeydown = keyHandler;};
AB.socketUserlist =function( array ){if(array.length >4){
AB.newSplash (splashScreen("<font color=white> <h2>GAME HAS 2/2 PLAYERS PLEASE WAIT...</h1>"));
document.getElementById("splashbutton").innerHTML ="Retry";
document.getElementById("splashbutton").onclick =function(){// reload the page when the button is clicked
location.reload();};}elseif(array.length ===1){
AB.newSplash (splashScreen("<font color=white> <h2>WAITING FOR ANOTHER USER...</h1>"));// update the button from "start" to "retry"
document.getElementById("splashbutton").innerHTML ="Retry";// reload the page when the button is clicked
document.getElementById("splashbutton").onclick =function(){
location.reload();};}// ensure that when this function is called again, the user array is not passed// this will avoid the error message displaying for all users currently playing and ensure it only displays for// the new user trying to join
AB.socketUserlist =function(){};};
AB.socketIn =function(score){
lives2 = score;}