-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
135 lines (109 loc) · 3.45 KB
/
server.js
File metadata and controls
135 lines (109 loc) · 3.45 KB
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
var shortid = require('shortid');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = 3000;
var settingUp = false;
var players = {};
var gravity = 10;
var bar = {};
bar.length = 600;
bar.width = 10;
bar.mass = bar.length * bar.width * bar.width;
bar.com = bar.length / 2;
bar.pivotPoint = 280;
bar.angle = 0; // goes +/- pi/2 radians
bar.angularVelocity = 0;
bar.angularAcceleration = 0;
bar.torque = 0;
var moment = 1 / 12 * bar.mass * bar.length * bar.length;
moment += bar.mass * (bar.length - bar.pivotPoint) * (bar.length - bar.pivotPoint);
// moment might be calculated incorrectly... for the purposes of this game.
bar.moment = moment;
var game = {};
game.players = players;
game.bar = bar;
game.gravity = gravity;
var movingPlayers = {};
server.listen(port, function () {
console.log('Server listening at port ' + port);
});
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/gd', function(req, res){
res.send(game);
});
io.on('connection', function (socket) {
console.log('Someone joined the party!');
socket.id = shortid.generate();
game.players[socket.id] = {};
game.players[socket.id].position = Math.random() * (bar.length - 200) + 100;
game.players[socket.id].mass = Math.random() * 10000 + 5000;
// game.players[socket.id].velocity = 0;
// game.players[socket.id].acceleration = 0;
socket.on('disconnect', function(){
console.log('Someone left the party.');
delete game.players[socket.id];
});
socket.on('move', function(direction) {
movingPlayers[socket.id] = direction;
});
socket.on('stop move', function() {
delete movingPlayers[socket.id];
});
io.emit('updateData', game);
socket.emit('uid', socket.id);
});
var recalculate = function() {
// calculate torque on the bar
// angular acceleration of the bar
// angular velocity of the bar
// move the damn bar
// calculate forces on each block (applied force from player, gravity, friction)
// move each damn block.
if(!settingUp){
var torque = 0;
for(block in game.players){
var displacement = game.players[block].position - bar.pivotPoint; // dist from pivot -> + - <-
var currentTorque = game.players[block].mass * gravity * Math.cos(bar.angle) * displacement;
torque += currentTorque;
}
var barDisplacement = bar.length / 2 - bar.pivotPoint;
var barTorque = bar.mass * gravity * Math.cos(bar.angle) * barDisplacement;
torque += barTorque;
bar.torque = torque;
var additionalMoment = 0;
for(block in game.players){
var disp = game.players[block].position - game.bar.pivotPoint;
additionalMoment += disp * disp * game.players[block].mass;
}
bar.angularAcceleration = bar.torque / (bar.moment + additionalMoment);
bar.angularVelocity += bar.angularAcceleration;
bar.angle += bar.angularVelocity;
}
if(bar.angle > 1.57 || bar.angle < -1.57){
theyDied();
}
for(block in movingPlayers){
if(game.players[block].position < 0){
game.players[block].position = 0;
} else if (game.players[block].position > bar.length){
game.players[block].position = bar.length;
}
game.players[block].position += movingPlayers[block] * 10;
}
io.emit('updateData', game);
};
function theyDied(){
bar.angle = 0;
bar.angularAcceleration = 0;
bar.angularVelocity = 0;
settingUp = true;
setTimeout(function(){
settingUp = false;
}, 3500)
}
setInterval(recalculate, 34);