function World()
{
//config
var pointsToWin = 11;
//
var cam;
var meshFloor;
var food_list = [];
var keyboard = {};
var distance;
var updatedPosX;
var updatedPosZ;
var egg = [];
var hasEgg = false;
var play;
var player_base;
var ai_base;
var AiFoodCount = 0;
var playerFoodCount=0;
var pspan;
var pspan2;
var Aspan;
var Aspan1;
var birdsView = false;
var disFood;
var botEgg = false;
var eggSpawned = false;
var playerEgg = false;
var eggTimer = 10000;
var aiCaptures = 1;
var playerCaptures = 1;
//Ai vars
//camera
//player
var objLoader = new THREE.OBJLoader();
objLoader.setPath("/uploads/tuitef2/");
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath("/uploads/tuitef2/");
var objLoader1 = new THREE.OBJLoader();
objLoader1.setPath("/uploads/tuitef2/");
var mtlLoader1 = new THREE.MTLLoader();
mtlLoader1.setPath("/uploads/tuitef2/");
var objLoader2 = new THREE.OBJLoader();
objLoader2.setPath("/uploads/tuitef2/");
var mtlLoader2 = new THREE.MTLLoader();
mtlLoader2.setPath("/uploads/tuitef2/");
var objLoader3 = new THREE.OBJLoader();
objLoader3.setPath("/uploads/tuitef2/");
var mtlLoader3 = new THREE.MTLLoader();
mtlLoader3.setPath("/uploads/tuitef2/");
var objLoader4 = new THREE.OBJLoader();
objLoader4.setPath("/uploads/tuitef2/");
var mtlLoader4 = new THREE.MTLLoader();
mtlLoader4.setPath("/uploads/tuitef2/");
var botSpeed = 0.3;
var update_speed = 0.2;//
var player = { height:2, speed:0.3, turnSpeed:0.09}; //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;
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, 3.5, -160); //position.x,position.y,position.z
cam.lookAt(scene.position);
}
function create_player(){
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( {color: "#c91092"} );
play = new THREE.Mesh( geometry, material );
play.scale.multiplyScalar(2);
play.position.set(0,player.height,-160);
scene.add(play);
}
function ground(){
var texture = new THREE.TextureLoader().load( 'uploads/tuitef2/ground_texture1.png' );
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.offset.set( 0, 0 );
texture.repeat.set( 2, 2 );
// 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 tree_border(i){
mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += i;
mesh.position.x += 180;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){//bottom
mesh.position.z += i;
mesh.position.x += -180;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){//right
mesh.position.z += 180;
mesh.position.x += i;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){//left
mesh.position.z += -180;
mesh.position.x += i;
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 );
});
}
function place_trees(){
mtlLoader.load("PUSHILIN_forest.mtl", function(materials) {
materials.preload();
objLoader.setMaterials(materials);
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += 120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += -120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += -130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 120;
mesh.position.x += 130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += 90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += -90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 50;
mesh.position.x += -60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 50;
mesh.position.x += 60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 50;
mesh.position.x += 100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += -100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 90;
mesh.position.x += 20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 70;
mesh.position.x += -10;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += 60;
mesh.position.x += -20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top//
mesh.position.z += -10;
mesh.position.x += -50;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top//
mesh.position.z += -10;
mesh.position.x += 50;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -120;
mesh.position.x += 120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top -
mesh.position.z += -120;
mesh.position.x += -120;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -120;
mesh.position.x += -130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -120;
mesh.position.x += 130;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += 90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += -90;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -50;
mesh.position.x += -60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -50;
mesh.position.x += 60;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -50;
mesh.position.x += 100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += -100;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -90;
mesh.position.x += 20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -70;
mesh.position.x += -10;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
objLoader.load("PUSHILIN_forest.obj", function(mesh){ //top
mesh.position.z += -60;
mesh.position.x += -20;
mesh.position.y+=5;
mesh.scale.multiplyScalar(15);
scene.add(mesh);
});
});
}
function base(){
mtlLoader3.load("materials1.mtl", function(material) { //BUILDING FOR EGG
material.preload();
objLoader3.setMaterials(material);
objLoader3.load("model-triangulated.obj", function(mesh){ //player base
mesh.position.y+=1;
// mesh.scale.multiplyScalar(5);
mesh.scale.set(15,5,5);
mesh.position.x += 0;
mesh.position.z += -160;
scene.add(mesh);
player_base = mesh;
var light = new THREE.PointLight("#c91092", 5, 100 );
light.position.set(0, 2, -160 );
scene.add( light );
});
objLoader3.load("model-triangulated.obj", function(mesh){ //AI base
mesh.position.y+=1;
mesh.scale.set(15,5,5);
mesh.position.x += 0;
mesh.position.z += 160;
scene.add(mesh);
ai_base = mesh;
var light = new THREE.PointLight("#1061a0", 5, 100 );
light.position.set(0, 2, 160 );
scene.add( light );
});
});
}
function place_structure(){
mtlLoader1.load("01.mtl", function(m) { //BUILDING FOR EGG
m.preload();
objLoader1.setMaterials(m);
objLoader1.load("01.obj", function(mesh){ //top
mesh.position.y+=2;
mesh.scale.set(0.5,0.5,0.5);
mesh.position.x += 20;
mesh.position.z +=20;
scene.add(mesh);
});
});
}
function place_egg(){
mtlLoader2.load("tfx95.mtl", function(m) { //EGG place in middle
m.preload();
objLoader2.setMaterials(m);
objLoader2.load("tfx95.obj", function(mesh){ //top
mesh.position.y+=2.3;
mesh.scale.set(1.8,1.8,1.8);
mesh.position.x += -2;
mesh.position.z += -12;
mesh.rotation.z += 2;
scene.add(mesh);
egg.push(mesh);
});
var light = new THREE.PointLight(0xff0000, 5, 150 );
light.position.set(-2, 2, -12 );
scene.add( light );
});
}
var inBaseAi = false;
var inBase = false;
function eat_egg(){
for(i=0;i<egg.length;i++){
distance1 = Math.abs((parseInt(play.position.x)) - egg[i].position.x);
distance2 = Math.abs((parseInt(play.position.z)) - egg[i].position.z);
if(distance1 <=2 && distance2 <= 2 && hasEgg !== true){
hasEgg = true;
playerEgg=true;
}
if(hasEgg === true && playerEgg === true && inBase === false){
egg[i].position.x = play.position.x+1.5;
egg[i].position.z = play.position.z+1.5;
playerPositionEgg()}
}
}
function eat_eggAi(){
for(i=0;i<egg.length;i++){
distance1 = Math.abs((parseInt(ai.position.x)) - egg[i].position.x);
distance2 = Math.abs((parseInt(ai.position.z)) - egg[i].position.z);
if(distance1 <=2 && distance2 <= 2 && hasEgg !== true){
hasEgg = true;
botEgg = true;
}
if(hasEgg === true && botEgg === true && inBaseAi === false){
egg[i].position.x = ai.position.x+1.5;
egg[i].position.z = ai.position.z+1.5;
aiPositionEgg();
}
}
}
function hide_egg(){
setTimeout(function() { //places egg after 50 seconds
place_egg();
}, 50000); //70000
}
function repositionEgg(){
hasEgg = false;
if(botEgg === true){
aiCaptures++;
botEgg = false;
}
else if(playerEgg === true){
playerCaptures++;
playerEgg = false;
}
setTimeout(function(){
for(i=0;i<egg.length;i++){
egg[i].position.x = -2;
egg[i].position.z = -12;
egg[i].position.y = 2;
inBaseAi = false;
botEgg = false;
playerEgg = false;
inBase = false;
hasEgg = false;
}
},8000);
//eat_eggAi();
//eat_egg();
}
function playerPositionEgg(){//player position egg at base
for(i=0;i<egg.length;i++){
distance2BaseX = Math.abs((parseInt(updatedPosX)) - player_base.position.x);
distance2BaseZ = Math.abs((parseInt(updatedPosZ)) - player_base.position.z);
if(distance2BaseX <=20 && distance2BaseZ <= 2){
egg[i].position.x = player_base.position.x;
egg[i].position.z = player_base.position.z;
egg[i].position.y = 3.2;
inBase = true;
eggPlayer1.innerHTML = playerCaptures;
repositionEgg();
eat_egg();
endRun();
}
}
}
function aiPositionEgg(){
for(i=0;i<egg.length;i++){
distance2BaseX = Math.abs((parseInt(ai.position.x)) - ai_base.position.x);
distance2BaseZ = Math.abs((parseInt(ai.position.z)) - ai_base.position.z);
if(distance2BaseX <=20 && distance2BaseZ <= 2){
egg[i].position.x = ai_base.position.x;
egg[i].position.z = ai_base.position.z;
egg[i].position.y = 3.2;
inBaseAi = true;
eggAi1.innerHTML = aiCaptures;
repositionEgg();
eat_eggAi();
endRun();
}
}
}
function food(){
for (i=0; i < 8; i++){ //8 foods on plane
mesh = new THREE.Mesh(
new THREE.SphereGeometry(1),
new THREE.MeshBasicMaterial({color:0xff4444}));
mesh.position.z += AB.randomIntAtoB(-160,120);
mesh.position.x += AB.randomIntAtoB(-160,120);
mesh.position.y += 2; // Move the mesh up 1 meter
mesh.scale.multiplyScalar(1.5);
scene.add(mesh);
food_list.push(mesh);
}
}
function food_color(){
setTimeout(function() { //changes color of food after 5 seconds
for(i = 0;i <food_list.length; i++){
food_list[i].material.color.set("#ffcc00"); //yellow color
}
food_positionChange();
}, 6000);
}
function food_positionChange(){
setTimeout(function() { //changes position of food after 5 seconds
for(i = 0;i <food_list.length; i++){
food_list[i].position.x = AB.randomIntAtoB(-160,120);
food_list[i].position.z = AB.randomIntAtoB(-160,120);
food_list[i].material.color.set(0xff4444);//red color
}
food_color();
bestx = 999;
bestz = 999;
smallestDist = 999;
closestFood();
}, 10000);
}
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(-160,120);
food_list[i].position.z = AB.randomIntAtoB(-160,120);//change the position of the item
food_list[i].material.color.set(0xff4444); //red color
playerFoodCount++;
player.speed += 0.01;
pspan2.innerHTML = playerFoodCount;
bestx = 999;
bestz = 999;
smallestDist = 999;
closestFood();
}
}
}
function eat_foodAI(){
for(i = 0;i <food_list.length; i++){
distanceX = Math.abs((parseInt(ai.position.x)) - food_list[i].position.x);
distanceZ = Math.abs((parseInt(ai.position.z)) - food_list[i].position.z);
if(distanceX <=2 && distanceZ <= 2 ){ //if player is close
food_list[i].position.x = AB.randomIntAtoB(-160,120);
food_list[i].position.z = AB.randomIntAtoB(-160,120);//change the position of the item
food_list[i].material.color.set(0xff4444); //red color
AiFoodCount++;
botSpeed += 0.02;
Aspan.innerHTML = AiFoodCount;
bestx = 999;
bestz = 999;
smallestDist = 999;
closestFood();
}
}
}
var bestx = 999;
var bestz = 999;
var smallestDist = 999;
function closestFood(){
var dX;
var dZ;
for(i=0;i<food_list.length;i++){
dX = (parseInt(ai.position.x)) - food_list[i].position.x;
dZ = (parseInt(ai.position.z)) - food_list[i].position.z;
disFood = Math.sqrt(dX * dX + dZ*dZ);
if (disFood < smallestDist){
bestx = food_list[i].position.x;
bestz = food_list[i].position.z;
smallestDist = disFood;
}
}
}
function collisionDection(){
var xdis = Math.abs(parseInt(play.position.x - ai.position.x));
var zdis = Math.abs(parseInt(play.position.z - ai.position.z));
var dis = Math.sqrt(xdis * xdis + zdis + zdis);
if( xdis <= 2 && zdis <= 2){
if(playerEgg === true){
playerEgg = false;
hasEgg = false;
for(i=0;i<egg.length;i++){
egg[i].position.x = -2;
egg[i].position.z = -12;
egg[i].position.y = 2;}
play.position.set(0,player.height,+160);
}
else if(botEgg === true){
botEgg = false;
hasEgg = false;
for(i=0;i<egg.length;i++){
egg[i].position.x = -2;
egg[i].position.z = -12;
egg[i].position.y = 2;}
ai.position.set(0,2,-160);
}
else if(playerFoodCount < AiFoodCount){
play.position.set(0,player.height,-160);
}
else if(playerFoodCount > AiFoodCount){
ai.position.set(0,2,160);
}
}
}
function movement(){
if(playerCaptures ===pointsToWin ){
return;
}
requestAnimationFrame(movement);
collisionDection();
setTimeout(function(){eggSpawned = true},20000);
for (j = 0; j < food_list.length; j++){
food_list[j].rotation.x += 0.01;
food_list[j].rotation.y += 0.02;
}
for(i=0;i<egg.length;i++){
egg[i].rotation.x += 0.01;
egg[i].rotation.y += 0.02;
eat_egg();
eat_eggAi();
}
// ----mind movement --//
var dPtoAiX = parseInt(play.position.x) - parseInt(ai.position.x);
var dPtoAiZ = parseInt(play.position.z) - parseInt(ai.position.z);
var disPlayer = Math.sqrt(dPtoAiX * dPtoAiX + dPtoAiZ * dPtoAiZ);
var dir = AB.randomIntAtoB(0,3);
//AI LOOKING FOR FOOD
closestFood();
if(playerEgg === true){
AiMovePlay();
}
else{
if(botEgg ===false){
if(AiFoodCount-10 >= playerFoodCount && inBaseAi === false){
getEgg();
}
else{
if(smallestDist < disPlayer){
foodMovement();
}
else{
if(playerFoodCount + 1 > AiFoodCount){
foodMovement();
eat_foodAI(ai);}
else{
AiMovePlay();
closestFood();}
}
}
}else{
AiReturn();
}
}
// Keyboard movement inputs
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);
var ai;
function createMind(){
var geometry = new THREE.BoxGeometry( 1, 1, 1 );
var material = new THREE.MeshBasicMaterial( {color: "blue"} );
ai = new THREE.Mesh( geometry, material );
ai.position.set(0,2,160);
ai.scale.multiplyScalar(2);
scene.add( ai );
}
////// CAMERA STUFFS
function hud(event){
pspan = document.getElementById("user_span1");
pspan2 = document.getElementById("user_span2");
pspan.innerHTML = "<br>Player food count: ";
pspan.style.fontSize = '20px';
pspan2.style.fontSize = '20px';
Aspan1 = document.getElementById("user_span3");
Aspan = document.getElementById("user_span4");
Aspan1.innerHTML = " AI food count: ";
Aspan1.style.padding = '5px';
Aspan1.style.fontSize = '20px';
Aspan.style.fontSize = '20px';
eggPlayer = document.getElementById("user_span5");
eggPlayer.innerHTML = "<br><br>Player Egg Count:";
eggPlayer1 = document.getElementById("user_span6");
eggAi = document.getElementById("user_span7");
eggAi.innerHTML = "AI Egg Count:";
eggAi1 = document.getElementById("user_span8");
eggPlayer.style.fontSize = 20;
eggAi.style.fontSize = 20;
let span = document.getElementById('api_span1');
let span1 = document.getElementById('api_span2');
var button = document.createElement("button");
var button2 = document.createElement("button");
let run = document.getElementById('w2m_runcontrols');
run.innerHTML = "";
span.innerHTML = 'Change view: ';
button.style.background= '#ed8c0e';
button.style.color = 'white';
button.style.border = 'none';
button.style.cursor = 'pointer';
button.style.borderRadius = '3px';
button.style.fontSize = '18';
button.style.padding = '5px';
button2.style.background = '#4CAF50';
button2.style.color = 'white';
button2.style.fontSize = '18';
button2.style.border = 'none';
button2.style.cursor = 'pointer';
button2.style.borderRadius = '3px';
button2.style.padding = '5px';
button.innerHTML = "Third Person";
button2.innerHTML = "Birds Eye";
span.appendChild(button);
span1.appendChild(button2);
button.addEventListener("click", camView1);
button2.addEventListener("click", camView2);
}
function camView1(){
birdsView = false;
cam.position.x = play.position.x ;
cam.position.z = play.position.z -5;
cam.position.y = 3.5;
cam.rotation.y = 0;
cam.rotation.z = -3.141592653589793;
cam.rotation.x = -3.1197211417647925;
play.rotation.x= 0;
play.rotation.y= 0;
play.rotation.z = 0;
}
function camView2(){
birdsView = true;
cam.position.x = play.position.x;
cam.position.z = play.position.z + 20;
cam.position.y = 60;
cam.rotation.y = 0;
cam.rotation.z = -3.141592653589793;
cam.rotation.x = 29.5;
play.rotation.x= 0;
play.rotation.y= 0;
play.rotation.z = 0;
}
//AI MOVESTUFF IS
function foodMovement(){
if(ai.position.x < bestx){
ai.position.x += botSpeed;
eat_foodAI();
closestFood();
}
if(ai.position.x > bestx){
ai.position.x -= botSpeed;
eat_foodAI();
closestFood();
}
if(ai.position.z < bestz){
ai.position.z += botSpeed;
eat_foodAI();
closestFood();
}
if(ai.position.z > bestz){
ai.position.z -= botSpeed;
eat_foodAI();
closestFood();
}
}
function AiMovePlay(){
if (updatedPosX < ai.position.x){
ai.position.x -= botSpeed;
}
if (updatedPosX > ai.position.x){
ai.position.x += botSpeed;
}
if (updatedPosZ < ai.position.z){
ai.position.z -=botSpeed;
}
if (updatedPosZ > ai.position.z){
ai.position.z += botSpeed;
}
}
function getEgg(){
for(i=0;i<egg.length;i++){
var disXegg = parseInt(ai.position.x)-parseInt(play.position.x);
var disZegg = parseInt(ai.position.z)-parseInt(play.position.z);
var disPlayer = Math.sqrt(disXegg * disXegg + disZegg * disZegg)
if( disPlayer < 10){
if (play.position.z < ai.position.x){
ai.position.x += botSpeed;
}
if (play.position.x > ai.position.x){
ai.position.x -= botSpeed;
}
if (play.position.z < ai.position.z){
ai.position.z +=botSpeed;
}
if (play.position.z > ai.position.z){
ai.position.z -= botSpeed;
}}
else{
if(egg[i].position.x <= ai.position.x){
ai.position.x -= botSpeed;
}
if(egg[i].position.x >= ai.position.x){
ai.position.x += botSpeed;
}
if(egg[i].position.z >= ai.position.z){
ai.position.z += botSpeed;
}
if(egg[i].position.z <= ai.position.z){
ai.position.z -= botSpeed;
}
}}
}
function AiReturn(){
var dPtoAiX = parseInt(play.position.x) - parseInt(ai.position.x);
var dPtoAiZ = parseInt(play.position.z) - parseInt(ai.position.z);
var disPlayer = Math.sqrt(dPtoAiX * dPtoAiX + dPtoAiZ * dPtoAiZ);
if( disPlayer < 10){
if (play.position.z < ai.position.x){
ai.position.x += botSpeed;
}
if (play.position.x > ai.position.x){
ai.position.x -= botSpeed;
}
if (play.position.z < ai.position.z){
ai.position.z +=botSpeed;
}
if (play.position.z > ai.position.z){
ai.position.z -= botSpeed;
}}
if (ai_base.position.x > ai.position.x){
ai.position.x += botSpeed;
}
if (ai_base.position.x > ai.position.x){
ai.position.x -= botSpeed;
}
if (ai_base.position.z < ai.position.z){
ai.position.z -=botSpeed;
}
if (ai_base.position.z > ai.position.z){
ai.position.z += botSpeed;
}
}
function winner(){
AB.newSplash();
let splash = document.getElementById("splash-inner");
let span = document.getElementById("splashbutton");
span.innerHTML = "Restart";
span.addEventListener("click", beginGame);
var winmessage = document.createElement("p");
winmessage.innerHTML = "<b><br>Congratulations! Winner<b>";
splash.append(winmessage);
winmessage.style.fontSize = "25px";
}
function loser(){
AB.newSplash();
let splash = document.getElementById("splash-inner");
let span = document.getElementById("splashbutton");
span.innerHTML = "Restart";
span.addEventListener("click", beginGame);
var winmessage = document.createElement("p");
winmessage.innerHTML = "<br><b>Better luck next time..<b>";
splash.append(winmessage);
winmessage.style.fontSize = "25px";
}
function splash(){
AB.newSplash();
//AB.removeSplash();
let span = document.getElementById("splashbutton");
span.addEventListener("click", beginGame);
let splash = document.getElementById("splash-inner");
var h1 = document.createElement("p");
h1.innerHTML = "How To Play!";
splash.appendChild(h1);
var p = document.createElement("p");
var p1 = document.createElement("p");
var h2 = document.createElement("p");
p.innerHTML = "The objective of the game is to race to the centre of the map which hosts an egg item,<br> which must be returned to your base (pink) while also avoiding <br> the enemy who will be attempting to snatch it from you.<br><br><b>FUN TIP:</b> You can kill the enemy by touching it. However, you must have a greater number of food!";//"The objective of this game is to capture the egg found in the center of the map<br>and bring it back to your base(pink) without the enemy obtaining it back from you.<br> If you eat more food than the enemy, you're able to kill the enemy by touching it.";
h2.innerHTML = "Controls";
p1.innerHTML = "<b>W</b>: Forward <br><b>S</b>: Backwards<br><b> A</b>: Left <br><b>D</b>: Right <br> <b>Arrow key left</b>: Turns player to the left<br><b>Arrow key right</b> : Turns player to the right";
splash.appendChild(p);
splash.appendChild(h2);
splash.appendChild(p1);
h1.style.fontSize = "25px";
h1.style.borderTop = "2px solid green";
h1.style.paddingTop = "8px";
h2.style.fontSize = "25px";
h2.style.borderTop = "2px solid green";
h2.style.paddingTop = "8px";
}
function beginGame(){
AB.removeSplash();
movement();
food_color();
}
this.newRun = function()
{
splash();
render();
create_player();
camera();
hud();
ground();
base();
createMind();
food();
eat_food();
place_structure();
hide_egg();
eat_foodAI();
//eat_eggAi();
//eat_egg();
place_trees();
for(i=-180;i<180;i++){
if(i%22===0){
tree_border(i);
}
}
AB.showRunHeader();
endRun();
};
function endRun(){
if (playerCaptures === pointsToWin){
winner();
}
else if(aiCaptures === pointsToWin){
loser();
}
}
}