-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbackend.js
169 lines (133 loc) · 4.26 KB
/
backend.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
163
164
165
166
167
168
169
const express = require('express')
const app = express()
// socket.io setup
const http = require('http')
const server = http.createServer(app)
const { Server } = require('socket.io')
const io = new Server(server, { pingInterval: 2000, pingTimeout: 5000 })
const port = 3000
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html')
})
const backEndPlayers = {}
const backEndProjectiles = {}
const SPEED = 5
const RADIUS = 10
const PROJECTILE_RADIUS = 5
let projectileId = 0
io.on('connection', (socket) => {
console.log('a user connected')
io.emit('updatePlayers', backEndPlayers)
socket.on('shoot', ({ x, y, angle }) => {
projectileId++
const velocity = {
x: Math.cos(angle) * 5,
y: Math.sin(angle) * 5
}
backEndProjectiles[projectileId] = {
x,
y,
velocity,
playerId: socket.id
}
console.log(backEndProjectiles)
})
socket.on('initGame', ({ username, width, height }) => {
backEndPlayers[socket.id] = {
x: 1024 * Math.random(),
y: 576 * Math.random(),
color: `hsl(${360 * Math.random()}, 100%, 50%)`,
sequenceNumber: 0,
score: 0,
username
}
// where we init our canvas
backEndPlayers[socket.id].canvas = {
width,
height
}
backEndPlayers[socket.id].radius = RADIUS
})
socket.on('disconnect', (reason) => {
console.log(reason)
delete backEndPlayers[socket.id]
io.emit('updatePlayers', backEndPlayers)
})
socket.on('keydown', ({ keycode, sequenceNumber }) => {
const backEndPlayer = backEndPlayers[socket.id]
if (!backEndPlayers[socket.id]) return
backEndPlayers[socket.id].sequenceNumber = sequenceNumber
switch (keycode) {
case 'KeyW':
backEndPlayers[socket.id].y -= SPEED
break
case 'KeyA':
backEndPlayers[socket.id].x -= SPEED
break
case 'KeyS':
backEndPlayers[socket.id].y += SPEED
break
case 'KeyD':
backEndPlayers[socket.id].x += SPEED
break
}
const playerSides = {
left: backEndPlayer.x - backEndPlayer.radius,
right: backEndPlayer.x + backEndPlayer.radius,
top: backEndPlayer.y - backEndPlayer.radius,
bottom: backEndPlayer.y + backEndPlayer.radius
}
if (playerSides.left < 0) backEndPlayers[socket.id].x = backEndPlayer.radius
if (playerSides.right > 1024)
backEndPlayers[socket.id].x = 1024 - backEndPlayer.radius
if (playerSides.top < 0) backEndPlayers[socket.id].y = backEndPlayer.radius
if (playerSides.bottom > 576)
backEndPlayers[socket.id].y = 576 - backEndPlayer.radius
})
})
// backend ticker
setInterval(() => {
// update projectile positions
for (const id in backEndProjectiles) {
backEndProjectiles[id].x += backEndProjectiles[id].velocity.x
backEndProjectiles[id].y += backEndProjectiles[id].velocity.y
const PROJECTILE_RADIUS = 5
if (
backEndProjectiles[id].x - PROJECTILE_RADIUS >=
backEndPlayers[backEndProjectiles[id].playerId]?.canvas?.width ||
backEndProjectiles[id].x + PROJECTILE_RADIUS <= 0 ||
backEndProjectiles[id].y - PROJECTILE_RADIUS >=
backEndPlayers[backEndProjectiles[id].playerId]?.canvas?.height ||
backEndProjectiles[id].y + PROJECTILE_RADIUS <= 0
) {
delete backEndProjectiles[id]
continue
}
for (const playerId in backEndPlayers) {
const backEndPlayer = backEndPlayers[playerId]
const DISTANCE = Math.hypot(
backEndProjectiles[id].x - backEndPlayer.x,
backEndProjectiles[id].y - backEndPlayer.y
)
// collision detection
if (
DISTANCE < PROJECTILE_RADIUS + backEndPlayer.radius &&
backEndProjectiles[id].playerId !== playerId
) {
if (backEndPlayers[backEndProjectiles[id].playerId])
backEndPlayers[backEndProjectiles[id].playerId].score++
console.log(backEndPlayers[backEndProjectiles[id].playerId])
delete backEndProjectiles[id]
delete backEndPlayers[playerId]
break
}
}
}
io.emit('updateProjectiles', backEndProjectiles)
io.emit('updatePlayers', backEndPlayers)
}, 15)
server.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
console.log('server did load')