-
Notifications
You must be signed in to change notification settings - Fork 0
/
HaxEmu.js
executable file
·104 lines (97 loc) · 3.04 KB
/
HaxEmu.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
96
97
98
99
100
101
102
103
104
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server),
options = {
"port" : 3000
},
players = [];
server.listen(options.port);
io.set('log level', 1);
app.configure(function(){
app.use(express.static(__dirname + '/'));
});
// app.get('/', function (request, response) {
// response.sendfile('index.html');
// });
io.sockets.on('connection', function (socket) {
console.log("connnect");
var player = {id: "player" + players.length, xPos : 50, yPos : 50, kicking: false};
players.push(player);
socket.player = players[players.indexOf(player)];
socket.broadcast.emit('addplayer', players[players.indexOf(player)]);
for(var i = 0; i < players.length; i++)
{
socket.emit('addplayer', players[i]);
}
socket.on('disconnect', function () {
console.log("disconnect");
delete players[players.indexOf(socket.player)];
io.sockets.emit('delplayer', socket.player);
});
// socket.emit("pong",{txt:"Connected to server"});
socket.on('ping', function (data) {
//console.log('Ping request: ' + data);
socket.emit("pong",data);
});
function movement () {
var p = socket.player;
if (movedata[87]) {
socket.player.yPos -= 1;
players[players.indexOf(p)] = socket.player;
}
if (movedata[65]) {
socket.player.xPos -= 1;
players[players.indexOf(p)] = socket.player;
}
if (movedata[83]) {
socket.player.yPos += 1;
players[players.indexOf(p)] = socket.player;
}
if (movedata[68]) {
socket.player.xPos += 1;
players[players.indexOf(p)] = socket.player;
}
io.sockets.emit('moveclient', {oldp: p, newp: players[players.indexOf(socket.player)]});
};
//var moveinterval = setInterval(movement({87: false, 65:false, 83: false, 68: false}), 10);
var moveinterval;
socket.on('move', function (data) {
switch (data.dir) {
case 'up':
var p = socket.player;
socket.player.yPos -= 10;
players[players.indexOf(p)] = socket.player;
io.sockets.emit('moveclient', {oldp: p, newp: players[players.indexOf(p)]});
break;
case 'left':
var p = socket.player;
socket.player.xPos -= 10;
players[players.indexOf(p)] = socket.player;
socket.emit('moveclient', {oldp: p, newp: players[players.indexOf(p)]});
break;
case 'down':
var p = socket.player;
socket.player.yPos += 10;
players[players.indexOf(p)] = socket.player;
socket.emit('moveclient', {oldp: p, newp: players[players.indexOf(p)]});
break;
case 'right':
var p = socket.player;
socket.player.xPos += 10;
players[players.indexOf(p)] = socket.player;
socket.emit('moveclient', {oldp: p, newp: players[players.indexOf(p)]});
break;
}
});
var movedata;
socket.on('updatemovement', function (data) {
//clearInterval(moveinterval);
movedata = data;
//moveinterval = setInterval(movement, 10);
movement();
//setInterval(movement, 10);
});
});
console.log('Express server listenting on port ' + options.port);