-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·130 lines (100 loc) · 4.04 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var express = require('express')
, http = require('http')
, path = require('path');
var app = express();
var server = http.createServer(app);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
//app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.use(express.static(path.join(__dirname, 'core')));
app.use(express.static(path.join(__dirname, 'tpl/images')));
app.use(express.static(path.join(__dirname, 'tpl/css')));
app.use(express.static(path.join(__dirname, 'tpl/gamejs')));
app.get('/', function(req, res){
res.sendfile(__dirname + '/tpl/game.html');
});
app.get('/game', function(req, res){
res.sendfile(__dirname + '/tpl/gamescreen.html');
});
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
//Requires
var constants = require('./core/constants');
var io = require("socket.io").listen(server);
var Character = require('./core/character');
var SocketUtils = require('./core/socketutils');
var Server = require('./core/server');
Server = new Server();
var ChatServer = require('./core/chat');
ChatServer = new ChatServer();
console.log('Server created: ' + Server.server_id);
io.sockets.on('connection', function (socket) {
//ChatServer.emitMessages(io);
var myHero = new Character();
socket.on('new player', function (data) {
//Create a new character using user input
var new_character = Server.addUserToList(data, socket.id);
//Add to server board
Server.board.addCharacter(new_character);
//Spawn new character on a random location based on it's team
Server.board.spawnCharacter(new_character, constants.TEAM_GOODGUYS);
myHero = Server.board._get_character_by_unique_id(socket.id);
//Send message to clientes with all players
SocketUtils.sendPlayers(io, Server.board.characters);
//Send player info to client
socket.emit('myHero', myHero);
});
/*socket.on('updateHeroPos', function (data){
myHero.x = data.x;
myHero.y = data.y;
console.log(data);
Server.board.moveCharacter(myHero.unique_id, myHero.x, myHero.y);
socket.emit('myHero', myHero);
SocketUtils.sendPlayers(io, Server.board.characters);
});*/
socket.on('sendInput', function (data){
if (data.key == 'up') {
myHero.y -= myHero.speed * data.modifier;
}
if (data.key == 'down') {
myHero.y += myHero.speed * data.modifier;
}
if (data.key == 'left') {
myHero.x -= myHero.speed * data.modifier;
}
if (data.key == 'right') {
myHero.x += myHero.speed * data.modifier;
}
Server.board.moveCharacter(myHero.unique_id, myHero.x, myHero.y);
socket.emit('myHero', myHero);
SocketUtils.sendPlayers(io, Server.board.characters);
});
socket.on('sendClick', function (data) {
myHero.x = data.clickx;
myHero.y = data.clicky;
Server.board.moveCharacter(myHero.unique_id, myHero.x, myHero.y);
socket.emit('myHero', myHero);
SocketUtils.sendPlayers(io, Server.board.characters);
});
socket.on('disconnect', function () {
SocketUtils.removeFromSeek(socket.id, Server.board.seekCharacters);
SocketUtils.removeSocketFromList(socket.id, Server.board.characters);
console.log('Character '+ socket.id + ' disconnected');
SocketUtils.sendPlayers(io, Server.board.characters);
});
socket.on('receiveMessage', function (data) {
console.log('Receiving message: '+ data.message);
var character = SocketUtils.findPlayerBySocketId(socket.id, Server.user_list);
ChatServer.addMessage(character.char_name, data.message);
ChatServer.emitMessages(io);
});
});