Skip to content

Commit

Permalink
ehhehe
Browse files Browse the repository at this point in the history
  • Loading branch information
sanidhya711 committed Mar 19, 2021
1 parent 071641c commit 10ac5c0
Show file tree
Hide file tree
Showing 4 changed files with 1,094 additions and 57 deletions.
5 changes: 3 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ io.on('connection', socket => {
x:data.x,
y:data.y,
z:data.z,
username:socket.usernamse
username:socket.username
}
console.log(cubes);
var dataToEmit = {
x:data.x,
y:data.y,
Expand All @@ -46,4 +47,4 @@ app.get("/",(req,res)=>{
res.sendFile("index.html");
});

server.listen(process.env.PORT);
server.listen(3000);
991 changes: 991 additions & 0 deletions public/ammo.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="/ammo.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
Expand Down
154 changes: 99 additions & 55 deletions public/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as THREE from 'https://unpkg.com/[email protected]/build/three.module.js';
import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js';

// (function(){var script=document.createElement('script');script.onload=function(){var stats=new Stats();document.body.appendChild(stats.dom);requestAnimationFrame(function loop(){stats.update();requestAnimationFrame(loop)});};script.src='//mrdoob.github.io/stats.js/build/stats.min.js';document.head.appendChild(script);})()
const socket = io();

Expand All @@ -12,7 +13,6 @@ socket.on("newCube", username => {
});

socket.on("positions",data=>{
console.log(data);
Object.keys(data).forEach(function(key){
cubes[data[key].username] = new Cube(data[key].x,data[key].y,data[key].z);
});
Expand All @@ -26,49 +26,12 @@ socket.on("username",data=>{
username = data;
});

var scene,renderer,camera,controls;
var scene,renderer,camera,controls,thirdPersonCamera,userCube;
var keyboard = {};
var speed = 0.3;
var cubeMaterial = new THREE.MeshBasicMaterial({wireframe:true});
var cubeGeometry = new THREE.BoxGeometry(10,10,10,3,3,3);


function init(){
var WIDTH = window.innerWidth,HEIGHT = window.innerHeight;

scene = new THREE.Scene();

renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(WIDTH,HEIGHT);
renderer.setClearColor(0x333F47,1);
document.body.appendChild(renderer.domElement);

camera = new THREE.PerspectiveCamera(45,WIDTH/HEIGHT,1,10000);
camera.position.set(0,20,100);
camera.lookAt(0,0,0);
scene.add(camera);

var material = new THREE.MeshBasicMaterial({wireframe:true,color:"black"});

var floorGeometry = new THREE.PlaneGeometry(200,200,20,20);
var floor = new THREE.Mesh(floorGeometry,material);
floor.rotation.x = Math.PI/2;
scene.add(floor);

controls = new OrbitControls(camera,renderer.domElement);
controls.minDistance = 50;
controls.maxDistance = 275;
animate();
}

function animate(){
requestAnimationFrame(animate);
controls.update();
renderer.render(scene,camera);
}

init();

class Cube{
constructor(positionX,positionY,positionZ){
this.positionX = positionX;
Expand All @@ -95,20 +58,13 @@ class Cube{
this.cube.position.x += speed;
break;
}

// for (var vertexIndex = 0; vertexIndex < this.cube.geometry.vertices.length; vertexIndex++){
// var localVertex = this.cube.geometry.vertices[vertexIndex].clone();
// var globalVertex = this.cube.matrix.multiplyVector3(localVertex);
// var directionVector = globalVertex.subSelf(this.cube.Player.position);
// var ray = new THREE.Ray(this.cube.Player.position,directionVector.clone().normalize());
// var collisionResults = ray.intersectObjects( collidableMeshList );
// if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()){
// console.log("collision!!");
// }
// }
}

setPosition(positionX,positionY,positionZ){
this.idealPosition = new THREE.Vector3(positionX,positionY,positionZ);
this.currentPosition = new THREE.Vector3();
this.currentPosition.lerp(this.idealPosition,0.2);
this.cube.position.copy(this.currentPosition);
this.cube.position.x = positionX;
this.cube.position.y = positionY;
this.cube.position.z = positionZ;
Expand All @@ -121,9 +77,87 @@ class Cube{
remove(){
scene.remove(this.cube);
}

get Position() {
return this.cube.position;
}

get Rotation() {
return this.cube.quaternion;
}
}

// class ThirdPersonCamera {
// constructor(params) {
// this._params = params;
// this._camera = params.camera;

// this._currentPosition = new THREE.Vector3();
// this._currentLookat = new THREE.Vector3();
// }

// _CalculateIdealOffset() {
// const idealOffset = new THREE.Vector3(0, 15,40);
// idealOffset.applyQuaternion(this._params.target.Rotation);
// idealOffset.add(this._params.target.Position);
// return idealOffset;
// }

// _CalculateIdealLookat() {
// const idealLookat = new THREE.Vector3(0, 0, 0);
// idealLookat.applyQuaternion(this._params.target.Rotation);
// idealLookat.add(this._params.target.Position);
// return idealLookat;
// }

// Update(timeElapsed) {
// const idealOffset = this._CalculateIdealOffset();
// const idealLookat = this._CalculateIdealLookat();
// const t = 1.0 - Math.pow(0.001, timeElapsed);

// this._currentPosition.lerp(idealOffset, t);
// this._currentLookat.lerp(idealLookat, t);

// this._camera.position.copy(this._currentPosition);
// this._camera.lookAt(this._currentLookat);
// }
// }


function init(){
var WIDTH = window.innerWidth,HEIGHT = window.innerHeight;

scene = new THREE.Scene();

renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(WIDTH,HEIGHT);
renderer.setClearColor(0x333F47,1);
document.body.appendChild(renderer.domElement);

camera = new THREE.PerspectiveCamera(45,WIDTH/HEIGHT,1,10000);
camera.position.set(0,15,60);
camera.lookAt(0,0,0);
scene.add(camera);

var material = new THREE.MeshBasicMaterial({wireframe:true,color:"black"});

var floorGeometry = new THREE.PlaneGeometry(200,200,20,20);
var floor = new THREE.Mesh(floorGeometry,material);
floor.rotation.x = Math.PI/2;
scene.add(floor);

controls = new OrbitControls(camera,renderer.domElement);
controls.minDistance = 10;
controls.maxDistance = 275;

userCube = new Cube(0,5.5,0);
// thirdPersonCamera = new ThirdPersonCamera({camera:camera,target:userCube});


animate();
}

var userCube = new Cube(0,5.5,0);
init();

var dirty = true;

Expand Down Expand Up @@ -173,6 +207,19 @@ socket.on("disconnected",data=>{
cubes[data].remove();
});

var previousT;

function animate(){
controls.update();
if (previousT === null) {
previousT = t;
}
// thirdPersonCamera.Update(0.2);
renderer.render(scene,camera);
requestAnimationFrame(animate);
}


//chat
document.querySelector(".chatbox input").addEventListener("focus",function(eve){
window.removeEventListener('keydown',keyDown);
Expand Down Expand Up @@ -212,11 +259,8 @@ socket.on("message",data=>{
document.querySelector(".messages").scrollTop = document.querySelector(".messages").scrollHeight;
});




document.addEventListener('visibilitychange', function(){
if(document.visibilityState=="hidden"){
keyboard = {};
}
});
});

0 comments on commit 10ac5c0

Please sign in to comment.