-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (101 loc) · 3.55 KB
/
Copy pathapp.js
File metadata and controls
123 lines (101 loc) · 3.55 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Created by LiamF on 15/03/2015.
*/
var express = require('express'),
app = express(),
path = require('path'),
bodyParser = require('body-parser'),
http = require('http').Server(app),
io = require('socket.io')(http),
views = path.join(__dirname, 'views');
require("./src/Main.js");
AsteroidGame.main = new AsteroidGame.Main();
var main = AsteroidGame.main;
main.start();
app.get('/', function(req, res){
res.sendFile(path.join(views, 'index.html'))
});
app.get('/test', function(req, res){
res.sendFile(path.join(views, 'SpecRunner.html'));
})
app.get('/playerlocations', function(req,res){
res.json({"0": {"clientName":"John Doe", "loc": {"x":50, "y":50}, "assetType": 'player'}});
});
app.get('/getuniqueplayerid', function( req, res){
res.send(main.getUniquePlayerId().toString());
})
app.use(express.static(path.join(__dirname, 'public')));
io.on('connection', function(socket){
console.log("connection made");
socket.playerId = -1;
socket.on('player connect', function(player){
var newPlayer = new AsteroidGame.Player({
clientName: player.clientName,
id : player.id,
loc: player.loc,
vel: player.vel,
rot: player.rot,
angvel: player.angvel,
assetRef: player.assetRef
});
socket.playerId = player.id;
main.addPlayer({socket : socket, player: newPlayer});
console.log("Player "+player.clientName+" connected!");
io.emit('player connect', player);
});
socket.on('post player', function(args){
if( socket.testWoO === undefined ){
console.log("post player args");
console.log(args);
socket.testWoO = true;
}
main.postPlayerFromClient(args);
})
socket.on('get player locations', function(callback){
if( callback !== undefined ){
if( typeof callback === "function" ){
console.log("Sending player locations");
console.log(main.getPlayers());
callback(main.getPlayers());
}
}
})
socket.on('getuniqueplayerid', function(callback){
if( typeof callback == "function" ){
callback(main.getUniquePlayerId());
}
})
socket.on('disconnect', function(reason){
if( main.players[socket.playerId] !== undefined ){
console.log( "Player "+main.players[socket.playerId].player.clientName+" disconnected");
main.players[socket.playerId] = undefined;
io.emit("player disconnect", socket.playerId, reason);
}
});
updatePlayer(socket);
})
function updatePlayer(socket){
if( socket !== undefined ){
//console.log("Update");
setTimeout(updatePlayer, (1 / AsteroidGame.framesPerSecond) * 1000, socket);
var locations = [],
time = new Date().getTime();
var players = main.getPlayers();
for( var i = 0, max = players.length; i < max; i++ ){
if( players[i] !== undefined ){
locations.push({
id : players[i].id,
loc: players[i].loc,
vel: players[i].vel,
rot: players[i].rot,
angvel: players[i].angvel,
time_stamp: time
});
}
}
socket.emit('update player locations', locations)
}
}
http.listen(process.env.PORT || 8080, function(){
console.log("Listening on port "+(process.env.PORT || 8080));
});