var cam;
var meshFloor;
var food_list = [];
var keyboard = {};
var distance;
var player = { height:1.8, speed:0.5, turnSpeed:0.2}; //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, 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(){
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 < 2; 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(){
var temp;
i=0;
while(food_list[i]){
distance = Math.sqrt((cam.position.x - food_list[i].position.x) + (cam.position.y - food_list[i].position.y) + (cam.position.z - food_list[i].position.z)) ; //distance between item position and player position
if (Math.ceil(distance) <= 2){ //if player is close
food_list[i].position.x += AB.randomIntAtoB(-90,90); //change the position of the item
}
temp = cam.position;
i++;
}
}
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;
}
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;
}
}
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;
}
}
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;
}
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;
}
}
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;
}
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;
}
}
// 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 = (cam.position.x)
document.getElementById("user_span2").innerHTML = (cam.position.y)
document.getElementById("user_span3").innerHTML = (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;