Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
humans now spawn in random places
Browse files Browse the repository at this point in the history
  • Loading branch information
Joanna committed Jun 14, 2014
1 parent f17f57f commit 51c27aa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
42 changes: 29 additions & 13 deletions players/collaborative.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,34 @@
scene.add(slayerTwo);

socket.on('message', function(obj) {
if('humans' in obj) {
var i;
console.log(humans.length);
for(i = 0; i < (obj.humans).length; i++) {
humans.push(obj.humans[i]);
// if('humans' in obj) {
// var i;
// console.log(humans.length);
// for(i = 0; i < (obj.humans).length; i++) {
// humans.push(obj.humans[i]);
// var geometry = new THREE.CubeGeometry(5, 5, 5);
// var material = new THREE.MeshBasicMaterial({color: 0xF5D0A9});
// humans[i] = new THREE.Mesh(geometry, material);
// humans[i].position.y = 2.5;
// humans[i].position.x = obj.humans[i].x;
// humans[i].position.z = obj.humans[i].z;
// humans[i].turned = false;
// scene.add(humans[i]);
// console.log('human is at ' + humans[i].position.x + ', ' + humans[i].position.z);
// }
// }
if('humanPos' in obj) {
console.log(JSON.stringify(obj));
humans.push(obj.humanName);
// console.log(JSON.stringifynumb(humans));
var geometry = new THREE.CubeGeometry(5, 5, 5);
var material = new THREE.MeshBasicMaterial({color: 0xF5D0A9});
humans[i] = new THREE.Mesh(geometry, material);
humans[i].position.y = 2.5;
humans[i].position.x = -40;
humans[i].position.z = 20;
humans[i].turned = false;
scene.add(humans[i]);
}
humans[obj.humanIndex] = new THREE.Mesh(geometry, material);
humans[obj.humanIndex].position.x = obj.humanPos[0];
humans[obj.humanIndex].position.z = obj.humanPos[1];
humans[obj.humanIndex].position.y = 2.5;
console.log('human is at ' + obj.humnPos[0] + ', ' + obj.humanPos[1]);
scene.add(humans[obj.humanIndex]);
}
});

Expand All @@ -242,7 +256,6 @@
socket.on('connect', function() {
console.log('connect');
socket.emit('addUser');

});

socket.on('message', function(obj) {
Expand All @@ -267,6 +280,9 @@
console.log(JSON.stringify(obj));
clienttype = obj.type;
addCamera(obj.type);
if(clienttype === 's') {
socket.send({'loadHumans': clienttype});
}
}
if('totalScore' in obj) {
console.log('other player score is ' + JSON.stringify(obj.totalScore));
Expand Down
40 changes: 35 additions & 5 deletions players/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ var io = socket.listen(server);
}

players[players.length-1].id = players.length-1;
console.log(JSON.stringify(players[players.length-1].type));
console.log(players);
// console.log(JSON.stringify(players[players.length-1].type));
// console.log(players);

client.send({'type': client.type});
console.log({'type': client.type});
Expand All @@ -91,7 +91,20 @@ var io = socket.listen(server);

});

client.send({'humans': humans});
// var i;
// for(i = 0; i < humans.length; i++) {
// console.log(spawnHuman());
// humans[i].id = spawnHuman();
// console.log(humans[i].id);
// }
// client.send({'humans': humans});
// var otherplayerid = client.id % 2 === 0 ? client.id+1 : client.id-1;
// var i;
// for(i = 0; i < humans.length; i++) {
// var humanPos = spawnHuman();
// client.send({'humanPos': humanPos, 'humanIndex': i, 'humanName': humans[i]});
// players[otherplayerid].send({'humanPos': humanPos, 'humanIndex': i, 'humanName': humans[i]});
// }

//competitive mode
client.on('message',function(message) {
Expand All @@ -106,7 +119,6 @@ var io = socket.listen(server);
console.log('slayer in room ' + client.room + JSON.stringify(message));
players[otherplayerid].send({'newPosition': message});
}

// console.log(JSON.stringify(message));

// if('zombie' in message) {
Expand Down Expand Up @@ -148,6 +160,16 @@ var io = socket.listen(server);
var otherplayerid = client.id % 2 === 0 ? client.id+1 : client.id-1;
// console.log(JSON.stringify(otherplayerid));

if('loadHumans' in message) {
console.log('loadHumans');
var i;
for(i = 0; i < humans.length; i++) {
var humanPos = spawnHuman();
client.send({'humanPos': humanPos, 'humanIndex': i, 'humanName': humans[i]});
players[otherplayerid].send({'humanPos': humanPos, 'humanIndex': i, 'humanName': humans[i]});
}
}

//Send movements to other player
if('zombieOne' in message) {
console.log('zombie in room ' + client.room + JSON.stringify(message));
Expand All @@ -158,7 +180,7 @@ var io = socket.listen(server);
console.log('slayer in room ' + client.room + JSON.stringify(message));
client.send({'newPosition': message});
players[otherplayerid].send({'newPosition': message});
}
}

if('gameOverCol' in message) {
console.log('zombieWin!!!');
Expand Down Expand Up @@ -207,4 +229,12 @@ var io = socket.listen(server);
var newZ = respawnPos[1];
return [newX, newZ];
}

function spawnHuman() {
var initX = Math.floor((Math.random()*290)+1);
var initZ = Math.floor((Math.random()*290)+1);
console.log(initX);
console.log(initZ);
return [initX, initZ];
}
});

0 comments on commit 51c27aa

Please sign in to comment.