This repository has been archived by the owner on Feb 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (54 loc) · 2.01 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
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const pug = require('pug')
const Dungeon = require('@mikewesthad/dungeon')
// AUTRE
app.set('view engine', 'pug')
// STATIC
app.use(express.static('./assets'))
app.use(express.static('./node_modules/phaser/dist'))
app.use(express.static('./node_modules/socket.io-client'))
app.use(express.static('./node_modules/@mikewesthad/dungeon/dist'))
// ROUTES
app.get('/', (req, res) => res.render('index'))
// MAP
const dungeon = new Dungeon({
width: 80,
height: 80,
doorPadding: 4,
rooms: {
width: { min: 8, max: 22, onlyOdd: true },
height: { min: 8, max: 14, onlyOdd: true },
maxRooms: 12
}
})
// EVENTS
const players = {}
io.on('connection', (socket) => {
socket.emit('map', dungeon)
// create a new player and add it to our players object
players[socket.id] = {
playerId: socket.id,
}
socket.emit('currentPlayers', players) // send the players object to the new player
socket.broadcast.emit('newPlayer', players[socket.id]) // update all other players of the new player
console.log('INFO: a player connected')
socket.on('disconnect', () => {
delete players[socket.id] // remove this player from our players object
io.emit('disconnect', socket.id) // emit a message to all players to remove this player
console.log('INFO: a player disconnected')
})
// when a player moves, update the player data
socket.on('playerMovement', movementData => {
players[socket.id].x = movementData.x;
players[socket.id].y = movementData.y;
players[socket.id].turn = movementData.turn;
// emit a message to all players about the player that moved
socket.broadcast.emit('playerMoved', players[socket.id]);
});
})
// Propulse le serveur sur le port choisi
const port = 3000
http.listen(process.env.PORT || port, () => console.log(`Serveur propulsé sur le port ${port}`))