-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGame.js
64 lines (49 loc) · 1.41 KB
/
Game.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 Player = require('./Player')
class Game {
constructor(io) {
this.io = io
this.handlePlayerConnection()
setInterval(() => {
this.update()
}, 1000/30)
console.log('Game')
}
handlePlayerConnection() {
this.io.on('connection', (socket) => {
if (!this.player1) {
this.player1 = new Player(socket, 55, 1, 1)
console.log('Player 1 connected', socket.id)
} else if (!this.player2) {
this.player2 = new Player(socket, 500 - 55, -1, 2)
console.log('Player 2 connected', socket.id)
}
if (this.player2) {
this.player2.enemy = this.player1
}
if (this.player1) {
this.player1.enemy = this.player2
}
socket.on('disconnect', () => {
console.log('disc socket', socket.id)
console.log('player1', this.player1 && this.player1.socket.id)
console.log('player2', this.player2 && this.player2.socket.id)
if (this.player1 && this.player1.socket.id === socket.id) {
this.player1 = null
console.log('Player 1 disconnected')
} else if (this.player2 && this.player2.socket.id === socket.id) {
this.player2 = null
console.log('Player 2 disconnected')
}
})
})
}
update() {
if (this.player1) {
this.player1.update()
}
if (this.player2) {
this.player2.update()
}
}
}
module.exports = Game