-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
236 lines (214 loc) · 6.25 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'use strict';
const express = require('express');
const http = require('http');
const path = require('path');
const socketIO = require('socket.io');
const app = express();
const server = http.Server(app);
const io = socketIO(server);
const yargs = require('yargs').argv;
const FIELD_WIDTH = 2000, FIELD_HEIGHT = 2000;
class GameObject{
constructor(obj={}){
this.id = Math.floor(Math.random()*2000000000);
this.x = obj.x;
this.y = obj.y;
this.width = obj.width;
this.height = obj.height;
this.angle = obj.angle;
}
move(distance){
const oldX = this.x, oldY = this.y;
this.x += distance * Math.cos(this.angle);
this.y += distance * Math.sin(this.angle);
let collision = false;
if(this.x < 0 || this.x + this.width >= FIELD_WIDTH || this.y < 0 || this.y + this.height >= FIELD_HEIGHT){
collision = true;
}
if(this.intersectWalls()){
collision = true;
}
if(collision){
this.x = oldX; this.y = oldY;
}
return !collision;
}
intersect(obj){
return (this.x <= obj.x + obj.width) &&
(this.x + this.width >= obj.x) &&
(this.y <= obj.y + obj.height) &&
(this.y + this.height >= obj.y);
}
intersectWalls(){
return Object.values(walls).some((wall) => this.intersect(wall));
}
toJSON(){
return {id: this.id, x: this.x, y: this.y, width: this.width, height: this.height, angle: this.angle};
}
}
class Player extends GameObject{
constructor(obj={}){
super(obj);
this.socketId = obj.socketId;
this.nickname = obj.nickname;
this.width = 80;
this.height = 80;
this.health = this.maxHealth = 10;
this.bullets = {};
this.point = 0;
this.movement = {};
do{
this.x = Math.random() * (FIELD_WIDTH - this.width);
this.y = Math.random() * (FIELD_HEIGHT - this.height);
this.angle = Math.random() * 2 * Math.PI;
}while(this.intersectWalls());
}
shoot(){
if(Object.keys(this.bullets).length >= 10){
return;
}
const bullet = new Bullet({
x: this.x + this.width/2,
y: this.y + this.height/2,
angle: this.angle,
player: this,
});
bullet.move(this.width/2);
this.bullets[bullet.id] = bullet;
bullets[bullet.id] = bullet;
}
damage(){
this.health --;
if(this.health === 0){
this.remove();
}
}
remove(){
delete players[this.id];
io.to(this.socketId).emit('dead');
console.log(`${process.uptime()} [Server] ${this.nickname} has died. Player user id: ${this.id}`);
}
toJSON(){
return Object.assign(super.toJSON(), {health: this.health, maxHealth: this.maxHealth, socketId: this.socketId, point: this.point, nickname: this.nickname});
}
}
class Bullet extends GameObject{
constructor(obj){
super(obj);
this.width = 15;
this.height = 15;
this.player = obj.player;
}
remove(){
delete this.player.bullets[this.id];
delete bullets[this.id];
}
}
class BotPlayer extends Player{
constructor(obj){
super(obj);
this.timer = setInterval(() => {
if(! this.move(5)){
this.angle = Math.random() * Math.PI * 2;
}
if(Math.random()<0.04){
this.shoot();
}
}, 1000/30);
}
remove(){
super.remove();
clearInterval(this.timer);
setTimeout(() => {
const bot = new BotPlayer({nickname: this.nickname});
players[bot.id] = bot;
}, 3000);
}
}
class Wall extends GameObject{
}
let players = {};
let bullets = {};
let walls = {};
for(let i=0; i<5; i++){
const wall = new Wall({
x: Math.random() * FIELD_WIDTH,
y: Math.random() * FIELD_HEIGHT,
width: 200,
height: 50,
});
walls[wall.id] = wall;
}
//List of Game bots
const bot = new BotPlayer({nickname: 'RoboCop'});
players[bot.id] = bot;
const bot1 = new BotPlayer({nickname: 'Carl-bot'});
players[bot1.id] = bot1;
const bot2 = new BotPlayer({nickname: 'Bot64'});
players[bot2.id] = bot2;
const bot3 = new BotPlayer({nickname: 'Aaron Barnett'});
players[bot3.id] = bot3;
io.on('connection', function(socket) {
let player = null;
socket.on('game-start', (config) => {
player = new Player({
socketId: socket.id,
nickname: config.nickname,
});
players[player.id] = player;
});
socket.on('movement', function(movement) {
if(!player || player.health===0){return;}
player.movement = movement;
});
socket.on('shoot', function(){
if(!player || player.health===0){return;}
player.shoot();
});
socket.on('disconnect', () => {
if(!player){return;}
delete players[player.id];
player = null;
});
});
setInterval(() => {
Object.values(players).forEach((player) => {
const movement = player.movement;
if(movement.forward){
player.move(5.5);
}
if(movement.back){
player.move(-5.5);
}
if(movement.left){
player.angle -= 0.1;
}
if(movement.right){
player.angle += 0.1;
}
});
Object.values(bullets).forEach((bullet) =>{
if(! bullet.move(10)){
bullet.remove();
return;
}
Object.values(players).forEach((player) => {
if(bullet.intersect(player)){
if(player !== bullet.player){
player.damage();
bullet.remove();
bullet.player.point +=1;
}
}
});
});
io.sockets.emit('state', players, bullets, walls);
}, 1000/30);
app.use('/static', express.static(__dirname + '/static'));
app.get('/', (request, response) => {
response.sendFile(path.join(__dirname, '/static/3d.html'));
});
const port = parseInt(yargs.port) || 3000;
server.listen(port, () => {
console.log(`${process.uptime()} [Server] Done! Server starting on port ${port}`);
});