-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
95 lines (76 loc) · 2.96 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var express = require('express'),
app = express.createServer(),
io = require('socket.io').listen(app),
_ = require("./static/js/utils/underscore"),
world = require("./static/js/world/world");
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
app.configure(function(){
app.use(express["static"](__dirname + '/static'));
//app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.bodyParser());
});
app.listen(process.env.C9_PORT || process.env.PORT || 3000);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/static/simpleClient.html');
});
world.setIo(io);
world.setPlayerDiedCallback(function(player) {
io.sockets.emit('playerDied', {id: player.id, name: player.name, color: player.color})
});
world.setResetGameCallback(function(player) {
io.sockets.emit('rejoinGame',player);
});
io.sockets.on('connection', function (socket) {
// Update our list of players Add me
var player = {id: socket.id, pos:{x:0, y:0}, dir:{x:0, y:0}, newDir:{x:0, y:0}, name:"Anon"};
world.addPlayer(player);
io.sockets.emit('playerJoined', player);
world.init();
socket.broadcast.emit('playerCount', {players: world.howManyPlayers()});
socket.emit('update', {"playaData": world.getGameState()});
socket.on('move', function(data) {
// Update where this player wants to "move"
data.id = socket.id;
//socket.broadcast.emit('move', data);
});
// Initialize player (join game)
socket.on('name', function(data) {
data.id = socket.id;
data.oldName = world.getPlayerNameById(socket.id);
// Only update player name if it has changed
if (data.name !== data.oldName) {
data.color = world.getPlayerColorById(socket.id);
io.sockets.emit('updateName', data);
world.updatePlayerById(socket.id, {"name":data.name, "oldName":data.oldName});
}
});
// God commands
socket.on('god', function(data){
world.godSays(data);
});
socket.on('dir', function(data) {
console.log(data);
world.updatePlayerDirById(socket.id, data.dir);
});
socket.on('chat', function(data) {
console.log("Chat: " + data.message);
var msg = world.addChatMessage(data.message, socket.id);
console.log(msg);
socket.broadcast.emit('chat', msg);
socket.emit('chat', msg);
});
socket.on('resetGame', function() {
io.sockets.emit('resetGame');
world.resetGame();
});
socket.on('disconnect', function() {
console.log('player disconnected');
world.removePlayerById(socket.id);
socket.broadcast.emit('died', {id: socket.id});
});
});