Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shooting socket #19

Merged
merged 12 commits into from
Sep 11, 2017
Merged
9,267 changes: 0 additions & 9,267 deletions package-lock.json

This file was deleted.

11 changes: 11 additions & 0 deletions server/bulletData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let bulletData = {};

const getId = (id) => {
return id ? bulletData[id] : bulletData;
}

const set = (id, data) =>{
return bulletData[id] = data;
}

module.exports = { getId, set};
37 changes: 36 additions & 1 deletion server/socket.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const socketIo = require('socket.io');
const http = require('http');
const playerData = require('./playerData');
const bulletData = require('./bulletData');

module.exports = (app) => {

const server = http.Server(app);
const io = socketIo(server, {
pingInterval: 5000,
pingInterval: 10000,
pingTimeout: 10000
});

Expand Down Expand Up @@ -52,14 +53,40 @@ module.exports = (app) => {
});
});

// socket.emit('bullet is fired', bulletData.getId());
// // socket.emit('bullet is fired', bulletData.getId());
// bulletData.set(id, getBulletDefaults());
// socket.broadcast.emit('bullet is fired', {id});

// socket.emit('bullet is fired', bulletData.getId());
//
socket.on('bullet is fired', ({randomid, velocity, position}) => {
//
socket.broadcast.emit('bullet is fired', {randomid, velocity, position});
console.log('server-socket', randomid, velocity, position);
});

socket.on('bullet position', ({randomid, velocity, position}) => {

if (!bulletData.getId(randomid)) {
bulletData.set(randomid, {velocity, position});
}
socket.broadcast.emit('other bullet position', {
randomid, velocity, position
});
});


socket.on('disconnect', () => {
playerData.remove(id);
// getScene().remove(avatar.mesh); // need to come back here and link the avatar.mesh with the right id
socket.broadcast.emit('other player disconnected', {
id
});
console.log('player has left the room');
});


});

};
Expand All @@ -77,3 +104,11 @@ const getPlayerDefaults = () => ({
},
health: 100
});

// const getBulletDefaults = () => ({
// randomid: 'defd56d7-808e-79c0-7efd-dd2b2e2dc8d9',
// velocity: {
// x: 0.12250618072906018,
// y: 0,
// z: -0.9924677504499473 }
// });
41 changes: 26 additions & 15 deletions src/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const letsMove = require('./letsMove');
const blocker = require('./blocker');
const otherPlayers = require('./otherPlayers');
const moveOtherPlayer = require('./moveOtherPlayer');
const getBullet = require('./getBullet');
const {movements} = require('./controls');
// const testobject = require('./testobject');

const start = (options) => {
const {
Expand All @@ -23,24 +26,32 @@ const start = (options) => {
requestAnimationFrame(animate);
if (!blocker.enabled) {
const time = performance.now();
letsMove(
camera,
scene,
objects,
raycaster,
prevTime,
time,
pointerLockControls,
world,
timeStep,
health,
);
letsMove(camera,scene,objects, raycaster, prevTime, time, pointerLockControls,world,
timeStep,
health);

const player = pointLockers();
// socket.emitState(getLocalState());
//to send the players positions and the bullets
// const bullet = testobject.getObj6();
// bullet.velocity = { x: -0.17509277691430628, y: 0, z: -0.9845519384331316 };
socket.emitPlayerPosition(player.position, player.rotation);

if (movements.shooting){
const bullet = getBullet();
socket.emitBulletPosition(bullet.randomid, bullet.velocity, bullet.position);
}
const players = otherPlayers.get();

Object.keys(players).forEach((id) => {
moveOtherPlayer(id, players[id]);
});



prevTime = time;
}

// mesh.rotation.x += 0.1;
// mesh.rotation.y += 0.1;
// controls(keyboard, camera, player);
renderer.render(scene, camera);
};
animate();
Expand Down
2 changes: 1 addition & 1 deletion src/avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const createCanvases = (avatar) => {
const createPlayerObject = (avatar) => {
new THREE.Object3D();
const upperbody = avatar.upperbody = new THREE.Object3D();
new THREE.MeshBasicMaterial({color: new THREE.Color('grey')});
new THREE.MeshBasicMaterial({color: new THREE.Color('black')});

const armMaterial =
new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture('images/bodyTextures/defaultPerson/arm.png')});
Expand Down
58 changes: 58 additions & 0 deletions src/getBullet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@


const THREE = require('three');
const pointLockers = require('./pointLockers');
const getScene = require('./getScene');
// const shoot = require('./shoot.js');

const {movements} = require('./controls');
const getRaycaster = require('./getRaycaster');


const velocity = new THREE.Vector3();

function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}

const getBullet = () => {
var bullet = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 8, 8),
new THREE.MeshBasicMaterial());

const scene = getScene();

const raycaster = getRaycaster();
bullet.position.set(
raycaster.ray.origin.x,
raycaster.ray.origin.y,
raycaster.ray.origin.z
);

const player = pointLockers();

bullet.velocity = new THREE.Vector3(
-Math.sin(player.rotation._y),
0,
-Math.cos(player.rotation._y),
);

bullet.alive = true;
bullet.randomid = guid();


setTimeout(function() {
bullet.alive = false;
scene.remove(bullet);
}, 1000);

return bullet;
}

module.exports = getBullet;
8 changes: 8 additions & 0 deletions src/getRaycaster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let _raycaster;

module.exports = () =>
_raycaster;

module.exports.init = (raycaster) => {
_raycaster = raycaster;
};
5 changes: 3 additions & 2 deletions src/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getLight = require('./getLight');
const getFloor = require('./getFloor');
const cubes = require('../cubes');
const blocker = require('../blocker');

const getRaycaster = require('../getRaycaster');
// const OBJLoader = require('three-obj-loader');
// OBJLoader(THREE);
// const MTLLoader = require('three-mtl-loader');
Expand Down Expand Up @@ -90,7 +90,8 @@ const init = () => {
pointerLocks.init(pointerLockControls);

const raycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(0, -1, 0), 0, 10);
// create cubes
getRaycaster.init(raycaster);
// create cubes
const obj1 = cubes.getObj1();
const obj2 = cubes.getObj2();
const obj3 = cubes.getObj3();
Expand Down
97 changes: 34 additions & 63 deletions src/letsMove.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const THREE = require('three');
const pointLockers = require('./pointLockers');
const socket = require('./socket');
const CANNON = require('cannon');
// const shoot = require('./shoot.js');
const otherBullets = require('./otherBullets');
const getBullet = require('./getBullet');

const {
movements,
} = require('./controls');
const {movements,} = require('./controls');

const bullets = [];
const cannonBullets = [];
let bullets = [];
const velocity = new THREE.Vector3();
var lastHealthPickup = 0;
var life=30;
Expand Down Expand Up @@ -76,67 +76,37 @@ if (!document.getElementById("health").textContent) {
velocity.z -= velocity.z * 10.0 * delta;
velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass

for (let index = 0; index < bullets.length; index++) {
if (bullets[index] === undefined) {
continue;
}
if (bullets[index].alive == false) {
cannonBullets.splice(index, 1);
bullets.splice(index, 1);
world.remove(cannonBullets[index]);

for (var index= 0; index < bullets.length ; index++) {
if (bullets[index] === undefined) {
continue;
}
if (bullets[index].alive == false) {
bullets.splice(index,1);
continue;
}
bullets[index].position.add(bullets[index].velocity);
}
// bullets[index].position.add(bullets[index].velocity);
bullets[index].position.copy(cannonBullets[index].position);
bullets[index].quaternion.copy(cannonBullets[index].quaternion);
}

if (movements.shooting) {
// shoot.bullet(scene);
const bullet = new THREE.Mesh(
new THREE.SphereGeometry(1.3, 8, 8),
new THREE.MeshBasicMaterial(),
);

const shape = new CANNON.Sphere(new CANNON.Vec3(1.3));
const body = new CANNON.Body({
mass: 5,
});
body.linearDamping = 0;
body.addShape(shape);
body.position.set(
raycaster.ray.origin.x,
raycaster.ray.origin.y,
raycaster.ray.origin.z,
);
world.addBody(body);


// bullet.position.copy(world.bodies[world.bodies.length - 1].position);
// bullet.quaternion.copy(world.bodies[world.bodies.length - 1].quaternion);
// console.log(cannonBullets[cannonBullets.length - 1]);


bullet.velocity = new CANNON.Vec3(
-Math.sin(pointerLockControls.getObject().rotation._y),
0,
-Math.cos(pointerLockControls.getObject().rotation._y),
);

console.log('thing', world.bodies[world.bodies.length - 1]);
bullet.alive = true;
setTimeout(() => {
bullet.alive = false;
scene.remove(bullet);
}, 3000);
cannonBullets.push(world.bodies[world.bodies.length - 1]);
bullets.push(bullet);
scene.add(bullet);
cannonBullets[cannonBullets.length - 1].velocity = bullet.velocity;
cannonBullets[cannonBullets.length - 1].velocity.x *= 150;
cannonBullets[cannonBullets.length - 1].velocity.y += 10;
cannonBullets[cannonBullets.length - 1].velocity.z *= 150;
}
if (movements.shooting){
const bullet = getBullet();
bullets.push(bullet);
scene.add(bullet);

}

Object.keys(otherBullets.get()).forEach(function(id){
const bullet = otherBullets.get()[id];
if (bullet === undefined) {
return;
}
if (bullet.alive == false) {
delete otherBullets[id];
return;
}
bullet.position.add(bullet.velocity);
})


if (movements.forward) { velocity.z -= 2000.0 * delta; }

Expand Down Expand Up @@ -174,6 +144,7 @@ if (!document.getElementById("health").textContent) {
pointLockers().position.y = 10;
movements.canJump = true;
}

};

// this is to stop lots of tiny movements from being sent to server once
Expand Down
13 changes: 13 additions & 0 deletions src/otherBullets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let otherBullets = {};

const get = () => otherBullets;

const addBullets = (randomid, bullet) => {
otherBullets[randomid] = bullet;

};

module.exports ={
get,
addBullets
}
19 changes: 0 additions & 19 deletions src/shoot.js

This file was deleted.

Loading