forked from phaserjs/phaser-by-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (45 loc) · 1.98 KB
/
server.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
const express = require("express");
const app = express();
const http = require("http").Server(app);
const io = require("socket.io")(http);
/*
We set up a very basic express server. By default, we will serve the index.html page built by our Webpack build on the front end. Anything below dist will be served automatically, so the game JavaScript and assets will be served statically as well.
*/
app.get("/", function (request, response) {
response.sendFile(__dirname + "/dist/index.html");
});
app.use("/", express.static("dist"));
http.listen(5001, function () {
console.log("Server ready, listening on port ", 5001);
});
const players = {};
/*
This is where we set all the events that our server will listen to. It will react like this:
- Is a new user connected? Notify it to the rest of the players to generate it as a new Enemy.
- Is a player disconnected? Notify the rest of the players to destroy this enemy.
Has the player moved? Notify it to the rest of the players so they can update the position of the enemy.
For debugging purposes, we left some logs on these events.
*/
io.on("connection", function (socket) {
socket.on("newPlayer", function (newPlayer) {
console.log("New player joined with state:", newPlayer);
const [name, key] = newPlayer.name.split(":");
players[key] = newPlayer;
socket.emit("currentPlayers", players);
socket.broadcast.emit("newPlayer", newPlayer);
});
socket.on("playerDisconnected", function (key) {
delete players[key];
console.log("Serve > player destroyed: ", key);
socket.broadcast.emit("playerDisconnected", key);
});
socket.on("playerIsMoving", function (position_data) {
console.log("Server> playerMoved> Player moved to ", position_data);
const key = position_data?.key;
if (players[key] == undefined) return;
players[key].x = position_data.x;
players[key].y = position_data.y;
players[key].rotation = position_data.rotation;
socket.broadcast.emit("playerMoved", players[key]);
});
});