Skip to content

Instantly share code, notes, and snippets.

@jafstar
Created November 21, 2011 11:04
Show Gist options
  • Save jafstar/1382331 to your computer and use it in GitHub Desktop.
Save jafstar/1382331 to your computer and use it in GitHub Desktop.
Multiplayer Wrap Around Town - Server
var fs = require('fs');
var app = require('http').createServer(function(req, response){
fs.readFile(__dirname+'/town.html', function(err, data){
response.writeHead(200, {'Content-Type':'text/html'});
response.write(data);
response.end();
});
});
app.listen(80);
var nowjs = require("now");
var everyone = nowjs.initialize(app);
//CHAT//////////////////////
//CHAT MESSAGE
//everyone.now.distributeMessage = function(message){
// everyone.now.receiveMessage(this.now.name, message);
//};
//MULTIPLAYER MAP////////////////////////////////////////////////////
//VARS
var actors = [];
//CONNECT
nowjs.on('connect', function() {
console.log("New Person Entered: " + this.user.clientId );
actors[this.user.clientId] = {x: 0, y: 0};
everyone.now.receiveMessage("*** " + this.now.name + " has joined ");
});
//DISCONNECT
nowjs.on('disconnect', function() {
console.log("Some Person Left");
everyone.now.receiveMessage("*** " + this.now.name + " has left ");
for(var i in actors) {
if(i == this.user.clientId) {
delete actors[i];
break;
}
}
});
//CHAT MESSAGE
everyone.now.distributeMessage = function(x,y,id,message){
//console.log(this.user.clientId);
//console.log(message);
var chatUpdate = {};
chatUpdate = {x:x,y:y ,msg: message, id: id};
console.log(chatUpdate);
everyone.now.msgActors(chatUpdate);
/*
actors[this.user.clientId].msg = message;
actors[this.user.clientId].id = id;
var chatUpdate = {};
//HIDE VIEW
for(var i in actors) {
chatUpdate[i] = {msg: actors[i].msg, id: actors[i].id};
}
for(var i in chatUpdate) {
nowjs.getClient(i, function(err) {
this.now.msgActors(chatUpdate);
});
}
//everyone.now.receiveMessage(this.now.name, message);
*/
};
//MOVE ACTOR
everyone.now.moveActor = function(x,y) {
//console.log(id);
//console.log(message);
actors[this.user.clientId].x = x;
actors[this.user.clientId].y = y;
//actors[this.user.clientId].id = id;
//actors[this.user.clientId].name = name;
var toUpdate = {};
//HIDE VIEW
for(var i in actors) {
if(Math.abs(x - actors[i].x) < 2500 && Math.abs(y - actors[i].y) < 2500) {
toUpdate[i] = { x:actors[i].x, y:actors[i].y };
}
}
for(var i in toUpdate) {
nowjs.getClient(i, function(err) {
this.now.positionActors(toUpdate);
});
}
//END MOVE FUNC
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment