-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
171 lines (159 loc) · 5.1 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
var express = require('express'),
app = express(),
http = require('http'),
socketIo = require('socket.io'),
crypto = require("crypto"),
SAT = require('sat'),
V = SAT.Vector,
C = SAT.Circle;
// start webserver on port 8080
var server = http.Server(app);
var io = socketIo(server);
server.listen(9000);
// add directory with our static files
app.use(express.static(__dirname + '/public'));
console.log("Server running on :9000");
function random_int(min, max) {
return Math.ceil(Math.random() * (max - min) + min);
}
function check_against_planets(spot)
{
var response = new SAT.Response();
planets.forEach(function(ptt) {
var collided = SAT.testCircleCircle(spot, ptt.c, response);
if(collided == true)
return collided;
})
return (collided) ? true :false;
}
function check_against_players(spot)
{
var response = new SAT.Response();
players.forEach(function(ptt) {
var collided = SAT.testCircleCircle(spot, ptt.c, response);
if(collided == true)
return collided;
})
return (collided) ? true : false;
}
//7680x4320 // such solution // much pixel // wow planets
const config = { planetAmount: 25, minimalSize: 100, maximalSize: 300, xSize: 7680, ySize: 4320, playersize: 9, playersafespace: 50, planetsafespace: 50}
let planets = [];
for(let i = 0; i < config.planetAmount; i++)
{
var x = random_int(config.maximalSize,config.xSize-config.maximalSize);
var y = random_int(config.maximalSize,config.ySize-config.maximalSize);
var s = random_int(config.minimalSize,config.maximalSize)
var ptt = new C(new V(x,y), s + config.playersafespace);
var collided = check_against_planets(ptt);
console.log(collided);
if(!collided)
planets.push({ x: x, y: y, s: s, c: new C(new V(x,y), s)});
else
i--;
}
function find_new_spot()
{
while(true)
{
var test = {x: random_int(0,config.xSize), y: random_int(0,config.ySize), s: config.playersize};
var ptt = new C(new V(test.x,test.y), test.s + config.playersafespace);
var collided = check_against_planets(ptt);
if(!collided)
{
var collided = check_against_players(ptt);
if(!collided)
{
return test;
}
}
}
}
function create_shot(player, pind, velocity, angle)
{
shots.push({pind: pind, c: new C(new V(player.x,player.y), 1), v: new V((velocity * Math.cos(angle / 180 * Math.PI)),(velocity * -Math.sin(angle / 180 * Math.PI)))});
//players.find(function(p){return p.username == player.username})
}
function testCircleCollision(a, b) {
var response = new SAT.Response();
var collided = SAT.testCircleCircle(a.c, b.c, response);
return {
collided: collided,
response: response
};
};
function stepper()
{
shots.forEach(function(shot,sind,sarr) {
shot.c.pos = shot.c.pos.add(shot.v)
players.forEach(function(ptt,pind,parr) {
var response = SAT.testCircleCollision(shot, ptt);
if(response.collided == true)
{
players[pind].c = find_new_spot(); // Shot trifft auf Player = Player tot
players[pind].deads++; // Shot trifft auf Player = Player tot
if(shot.pind != pind) // Shot trifft auf Player = Schütze kriegt Punkt
players[shot.pind].kills++; // allerdings nur wenn er es nicht selbst war :P
shots.indexOf(sind); // Shot trifft auf Player = Shot geht in Rente
socket.emit('player-update')
}
});
planets.forEach(function(ptt,pind,parr) {
var response = SAT.testCircleCollision(shot, ptt);
if(response.collided == true)
shots.indexOf(sind); // Shot trifft auf Planet = Shot geht garnicht mehr
});
});
}
// players
let players = [];
let shots = [];
var mapio = io.of('/map');
mapio.on('connection', function(sock){
const socket = sock;
socket.emit('clear-map');
// auth
socket.on('auth', function(data) {
socket.user_id = socket.id;
socket.username = crypto.randomBytes(15).toString('hex');
players.push({ "socket": socket.id, "username": socket.username, "c": find_new_spot(), "deads": 0, "kills": 0 });
});
// Request Map -> Send map
socket.on('request-map', function(data) {
//if(socket.username != data.username) {socket.disconnect();}
console.log("request-map");
socket.emit('send-map', {"planets": planets, dimensions: { x: config.xSize, y: config.ySize}});
});
// get Shot
socket.on('shot-fired', function(data) {
console.log("shot-fired");
create_shot(data.username,players.findIndex(function(ele,ind,arr){}),data.velocity,data.angle);
});
socket.on('reset', function(data) {
planets = [];
for(let i = 0; i < config.planetAmount; i++)
{
var x = random_int(config.maximalSize,config.xSize-config.maximalSize);
var y = random_int(config.maximalSize,config.ySize-config.maximalSize);
var s = random_int(config.minimalSize,config.maximalSize)
var ptt = new C(new V(x,y), s);
var collided = check_against_planets(ptt);
if(!collided)
planets.push({ x: x, y: y, s: s, c: new C(new V(x,y), s)});
else
i--;
}
socket.emit('send-map', {"planets": planets, dimensions: { x: config.xSize, y: config.ySize}});
})
});
// event-handler for new incoming connections
/*io.on('connection', function(socket) {
//
console.log("connected");
socket.on('getMap', function(data) {
socket.emit('map', {
"planets": planets
});
});
});
*/