@kaldisberzins/

Multiplayer Game Example

Express

No description

fork
loading
Files
  • index.js
  • client
  • package-lock.json
  • package.json
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var express = require('express');
var app = express();
var serv = require('http').Server(app);

app.get('/',function(req, res) {
	res.sendFile(__dirname + '/client/index.html');
});
//When the page is requested it sends the index.html file
app.use('/client',express.static(__dirname + '/client'));
//^^Routing for files
serv.listen(process.env.PORT);
console.log("Server started");

var SOCKET_LIST = {};
var PLAYER_LIST = {};
//^^These are the lists that keep track of the players and connected clients
var Player = function(id){
	var self = {
		x:250,
		y:250,
		width:50,
		height:50,
		id:id,
		number:"" + Math.floor(10 * Math.random()),
		pressingRight:false,
		pressingLeft:false,
		pressingUp:false,
		pressingDown:false,	
		maxSpd:10,
		it:false,
		canBeIt:true
	};
	self.updatePosition = function(){
		if(self.pressingRight)
			self.x += self.maxSpd;
		if(self.pressingLeft)
			self.x -= self.maxSpd;
		if(self.pressingUp)
			self.y -= self.maxSpd;
		if(self.pressingDown)
			self.y += self.maxSpd;
		//^^If any of the keys are pressed down it moves the player in a direction
		//
		//Collisions
		if(self.x+self.width>800){
			self.x = 800-self.width;
		}
		if(self.x<0){
			self.x = 0;
		}
		if(self.y+self.height>500){
			self.y = 500-self.height;
		}
		if(self.y<0){
			self.y = 0;
		}
		//^^If the player is out of bounds set him to the bounds
		if(self.it){
			self.canBeIt = false;
			self.right = self.x+self.width;
			self.bottom = self.y+self.height;
			//^^The top and left values are just self.x and self.y
			for(var i in PLAYER_LIST){
				if(PLAYER_LIST[i].id !== self.id){
					var other = {
						x:PLAYER_LIST[i].x,
						y:PLAYER_LIST[i].y,
						right:PLAYER_LIST[i].x+PLAYER_LIST[i].width,
						bottom:PLAYER_LIST[i].y+PLAYER_LIST[i].height
					};
					if(self.right>other.x && self.x<other.right && self.bottom>other.y && self.y<other.bottom){
						if(PLAYER_LIST[i].canBeIt){
							PLAYER_LIST[i].it = true;
							self.it = false;
							self.canBeIt = false;
							self.setCan(1000);
						}
					}
				}
			}
		}
	};
	self.setCan = (time)=>{
		setTimeout(()=>{
			self.canBeIt = true;
		}, time);
	};
	return self;
};
//^^Function for generating players

var io = require('socket.io')(serv,{});
io.sockets.on('connection', function(socket){
	socket.id = Math.random();
	SOCKET_LIST[socket.id] = socket;
	var player = Player(socket.id);
	PLAYER_LIST[socket.id] = player;
	//On connection adds the player and client to the lists of players and clients
	console.log("Player number "+player.number+" has joined");
	if(Object.keys(PLAYER_LIST).length === 1){
		PLAYER_LIST[socket.id].it = true;
	}
	socket.on('disconnect',function(){
		delete SOCKET_LIST[socket.id];
		delete PLAYER_LIST[socket.id];
		//When the client disconnects they get removed from the lists
		if(Object.keys(PLAYER_LIST).length === 1){
			for(var i in PLAYER_LIST){
				PLAYER_LIST[i].it = true;
			}
		}
		else{
			var anyBody = false;
			for(var i in PLAYER_LIST){
				if(PLAYER_LIST[i].it){
					anyBody = true;
				}
			}
			if(!anyBody){
				var picked = false;
				for(var i in PLAYER_LIST){
					if(!picked){
						PLAYER_LIST[i].it = true;
						picked = true;
					}
				}
			}
		}
	});
	socket.on('keyPress',function(data){
		if(data.inputId === 'left')
			player.pressingLeft = data.state;
		else if(data.inputId === 'right')
			player.pressingRight = data.state;
		else if(data.inputId === 'up')
			player.pressingUp = data.state;
		else if(data.inputId === 'down')
			player.pressingDown = data.state;
	});
	//^^Once the keys that the client is pressing are sent to the server the server saves that information
	
	
});
setInterval(function(){
	var pack = [];
	for(var i in PLAYER_LIST){
		var player = PLAYER_LIST[i];
		player.updatePosition();
		pack.push({
			x:player.x,
			y:player.y,
			number:player.number,
			it:player.it
		});		
	}
	//^^Loops through all the players in the list and pushes their data to an array
	for(var n in SOCKET_LIST){
		var socket = SOCKET_LIST[n];
		socket.emit('newPositions',pack);
	}
	//^^Loops through all the clients in the list and sends them the positions of all the players
},1000/40);