-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
287 lines (248 loc) · 7.05 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//Lerp module for node
var lerp = require('lerp');
//Grid size
var mapSize = 2035;
//Players + Bots IO (input/output)
var players = [];
var botsIO = [];
var food = [];
var foodCount = 100;
var botCount = 5;
//Replacemet for requestAnimationFrame() AKA REPEAT FUNCTION!
setInterval(tick, 30);
//Constrain function
function constrain(num, min, max) {
if (num <= min) {
return min;
} else if (num >= max) {
return max;
} else {
return num;
}
}
//Random function in a range
function random(min, max) {
return Math.random() * (max - min) + min;
}
//Distance between two points
function dist(x, y, x0, y0) {
return Math.sqrt((x -= x0) * x + (y -= y0) * y);
}
//Player Info Constructor
function Player(id, x, y, size, attack) {
this.id = id;
this.x = x;
this.y = y;
this.size = size;
this.attack = attack;
}
//Food
function Food(size) {
this.x = Math.floor(random(2, 2000));
this.y = Math.floor(random(2, 2000));
this.colorNum = Math.floor(random(1, 8));
this.size = size;
}
//Server Side Bots Constructor
function Bots() {
this.x = Math.random() * 2035;
this.y = Math.random() * 2035;
this.a = 0;
this.speedx = random(1, 2);
this.speedy = random(1, 2);
this.dirx = 0;
this.diry = 0;
this.VK = false;
this.colNum = Math.floor(random(0, 7));
var R = 60;
this.field = R + 10;
this.fieldPlayer = R + 40;
this.knockback = 20;
this.attack = Math.floor(random(1, 3));
this.health = 2;
this.eat = function (posx, posy) {
//Food part!
var d = dist(this.x, this.y, posx, posy);
var gapX = posx - this.x;
var gapY = posy - this.y;
var nX = this.x + gapX;
var nY = this.y + gapY;
if (d < this.field) {
this.VK = true;
var newposx = nX;
var newposy = nY;
this.x = lerp(this.x, newposx, 0.02);
this.y = lerp(this.y, newposy, 0.02);
if (this.VK === true) {
var directionX = gapX;
var directionY = gapY;
this.dirx = lerp(this.dirx, directionX, 0.005);
this.diry = lerp(this.diry, directionY, 0.005);
this.speedx = this.dirx;
this.speedy = this.diry;
this.VK = false;
}
if (d < 5 + R / 2) {
return true;
} else {
return false;
}
}
}
this.playerInterract = function () {
//Player part!
for (var u = 0; u < players.length; u++) {
var ds = dist(this.x, this.y, players[u].x, players[u].y);
if (ds < this.fieldPlayer) {
//this.x = lerp(this.x, players[u].x, 0.05);
//this.y = lerp(this.y, players[u].y, 0.05);
if (ds < players[u].size / 2 + R / 2) {
var dy = players[u].y - this.y;
this.x += 10;
if (players[u].x >= this.x) {
var a = dy / ds;
var newdis = ds * 2;
this.y -= newdis * Math.sin(a);
this.x -= newdis * Math.cos(a);
this.health -= players[u].attack;
}
if (players[u].x <= this.x) {
var a1 = dy / -ds;
var newdis1 = ds * 2;
this.y += newdis1 * Math.sin(a1);
this.x += newdis1 * Math.cos(a1);
this.health -= players[u].attack;
}
}
}
}
}
//Bot movement + constrains
this.interact = function () {
if (this.VK === false) {
this.x += this.speedx;
this.y += this.speedy;
}
this.x = constrain(this.x, 0, mapSize);
this.y = constrain(this.y, 0, mapSize);
if (this.x === 2035) {
this.speedx = -this.speedx;
} else if (this.y === 2035) {
this.speedy = -this.speedy;
} else if (this.x === 0) {
this.speedx = -this.speedx;
} else if (this.y === 0) {
this.speedy = -this.speedy;
}
}
}
//Creating Bots from a templete (Constructor!)
for (var n = 0; n < botCount; n++) {
botsIO[n] = new Bots();
//console.log("Theres are " + botsIO[n].x + " " + botsIO[n].y + " here!");
}
//Creation food and others
for (var v = 0; v <= foodCount; v++) {
food[v] = new Food(20);
// console.log(food[v]);
}
//**********************************************************************
//Repeatable function (Uses setInterval() )
function tick() {
//Ако храната намали създай нова.
if (food.length < foodCount) {
var randomizer = Math.floor(random(1, 20));
// console.log(randomizer);
if (randomizer === 11) {
var newf = new Food(20);
food.push(newf);
}
}
for (var p = botsIO.length - 1; p >= 0; p--) {
//console.log(botsIO[0].x);
botsIO[p].interact();
botsIO[p].eat();
botsIO[p].playerInterract();
if (botsIO[p].health <= 0) {
botsIO.splice(p, 1);
}
}
for (var e = players.length - 1; e >= 0; e--) {
for (var m = food.length - 1; m >= 0; m--) {
var dis = dist(players[e].x, players[e].y, food[m].x, food[m].y);
if (dis < 5 + players[e].size / 2) {
food.splice(m, 1);
}
}
}
for (var j = botsIO.length - 1; j >= 0; j--) {
for (var o = food.length - 1; o >= 0; o--) {
if (botsIO[j].eat(food[o].x, food[o].y)) {
food.splice(o, 1);
}
}
}
if (botsIO.length < botCount) {
;
botsIO.push(new Bots());
}
}
// Using express: http://expressjs.com/
var express = require('express');
// Create the app
var app = express();
// Set up the server
// process.env.PORT is related to deploying on heroku
var server = app.listen(process.env.PORT || 3000, listen);
// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}
app.use(express.static('public'));
// WebSocket Portion
// WebSockets work with the HTTP server
var io = require('socket.io')(server);
setInterval(heartbeat, 20);
function heartbeat() {
io.sockets.emit('heartbeat', players);
if (players.length > 0) {
//console.log("Emiting " + players[0].hitUp);
}
io.sockets.emit('bots', botsIO);
io.sockets.emit('food', food);
}
// Register a callback function to run when we have an individual connection
// This is run for each individual user that connects
io.sockets.on('connection',
// We are given a websocket object in our function
function (socket) {
console.log("We have a new client: " + socket.id);
socket.on('start',
function (data) {
console.log(socket.id + " " + data.x + " " + data.y + " " + data.size + " " + data.attack);
var player = new Player(socket.id, data.x, data.y, data.size, data.attack);
players.push(player);
}
);
socket.on('update',
function (data) {
//console.log(socket.id + " " + data.x + " " + data.y + " " + data.size + " " + data.attack);
var player;
for (var i = 0; i < players.length; i++) {
if (socket.id == players[i].id) {
player = players[i];
}
}
player.x = data.x;
player.y = data.y;
player.size = data.size;
player.attack = data.attack;
}
);
socket.on('disconnect', function () {
console.log("Client has disconnected");
});
}
);