add client side physics
by god did this take a lot of debugging to get working
This commit is contained in:
parent
65f78ec3ac
commit
1412cabbb4
19 changed files with 697 additions and 71 deletions
|
@ -41,9 +41,14 @@ class Connection {
|
|||
}
|
||||
|
||||
disconnect() {
|
||||
this.socket.disconnect();
|
||||
this.player.disconnect();
|
||||
}
|
||||
|
||||
drop() {
|
||||
this.disconnect();
|
||||
}
|
||||
|
||||
send(msg, data) {
|
||||
this.io.to(this.socket.id).emit(msg, data);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ class Player {
|
|||
this.ship = false;
|
||||
this.team = false;
|
||||
this.kickCount = 0;
|
||||
this.lastAction = Date.now();
|
||||
this.connection = connection;
|
||||
this.name = `Stupid ${fruit[Math.random() * fruit.length | 0]}`;
|
||||
this.delta = {};
|
||||
|
@ -23,6 +24,7 @@ class Player {
|
|||
left: data.left || 0,
|
||||
right: data.right || 0
|
||||
});
|
||||
this.lastAction = Date.now();
|
||||
}
|
||||
|
||||
send(msg, data) {
|
||||
|
@ -34,7 +36,7 @@ class Player {
|
|||
}
|
||||
|
||||
sendUpdate() {
|
||||
if (Object.keys(this.delta).length == 0 || Math.random() < 0) return;
|
||||
if (Object.keys(this.delta).length == 0) return;
|
||||
this.connection.send('update', this.delta);
|
||||
this.delta = {};
|
||||
}
|
||||
|
|
|
@ -38,8 +38,20 @@ class Room {
|
|||
player.team = team;
|
||||
}
|
||||
|
||||
kick(player, reason) {
|
||||
player.send('kicked', reason);
|
||||
player.connection.drop();
|
||||
}
|
||||
|
||||
update(self) {
|
||||
self.players.forEach(player => player.sendUpdate());
|
||||
//if (this.world.tickCount % 100 == 0)
|
||||
self.players.forEach(player => {
|
||||
player.sendUpdate();
|
||||
if (Date.now() - player.lastAction > 10000) {
|
||||
//this.kick(player);
|
||||
//console.log('Kicked ' + player.name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
broadcast(msg, data) {
|
||||
|
|
|
@ -13,6 +13,11 @@ class World {
|
|||
this.ships = new Map();
|
||||
this.players = new Set();
|
||||
this.room = room;
|
||||
this.tps = 0;
|
||||
this.tpsCount = 0;
|
||||
this.tpsStart = Date.now();
|
||||
|
||||
this.tickCount = 0;
|
||||
|
||||
this.bounds = {
|
||||
left: -5,
|
||||
|
@ -54,7 +59,7 @@ class World {
|
|||
}
|
||||
|
||||
populate() {
|
||||
for (var i = 0; i < 20; i++) {
|
||||
for (var i = 0; i < 40; i++) {
|
||||
let pos = {
|
||||
x: Math.random() * 2000 - 200,
|
||||
y: Math.random() * 500 - 250
|
||||
|
@ -76,10 +81,11 @@ class World {
|
|||
this.ships.delete(body);
|
||||
this.structures.delete(body);
|
||||
this.asteroids.delete(body);
|
||||
this.room.broadcast('destroy', body.id);
|
||||
}
|
||||
|
||||
start() {
|
||||
this.interval = setInterval(_ => this.tick(this), 1 / 60);
|
||||
this.interval = setInterval(_ => this.tick(this), 1000 / 60);
|
||||
}
|
||||
|
||||
stop() {
|
||||
|
@ -89,12 +95,27 @@ class World {
|
|||
tick(self) {
|
||||
self.physics.step();
|
||||
|
||||
if (Math.random() < 0.1) {
|
||||
self.bodies.forEach(body => {
|
||||
self.ships.forEach(body => {
|
||||
body.applyDelta(),
|
||||
body.tick();
|
||||
});
|
||||
|
||||
if (this.tickCount % 1 == 0) {
|
||||
self.asteroids.forEach(body => {
|
||||
body.applyDelta(),
|
||||
body.tick();
|
||||
});
|
||||
|
||||
if (Date.now() - this.tpsStart > 5000) {
|
||||
this.tps = this.tpsCount / 5 | 0;
|
||||
this.tpsCount = 0;
|
||||
this.tpsStart = Date.now();
|
||||
//console.log('TPS: ' + this.tps);
|
||||
}
|
||||
}
|
||||
|
||||
this.tpsCount++;
|
||||
this.tickCount++;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,6 @@ const Box2D = require('box2d-html5');
|
|||
|
||||
const b2Vec2 = Box2D.b2Vec2;
|
||||
|
||||
Box2D.b2Settings.b2_linearSleepTolerance = 0.0002;
|
||||
|
||||
class Physics {
|
||||
constructor() {
|
||||
this.world = new Box2D.b2World(new b2Vec2(0, 0), false);
|
||||
|
@ -43,15 +41,15 @@ class Physics {
|
|||
bodyDef.active = true;
|
||||
bodyDef.linearVelocity = new b2Vec2(body.xvel / s || 0, body.yvel / s || 0);
|
||||
bodyDef.angularVelocity = body.rvel || 0;
|
||||
bodyDef.linearDamping = body.type == 'ship' ? 0.001 : 0.0003;
|
||||
bodyDef.angularDamping = body.type == 'ship' ? 0.001 : 0.0003;
|
||||
bodyDef.linearDamping = body.type == 'ship' ? 0.01 : 0.003;
|
||||
bodyDef.angularDamping = body.type == 'ship' ? 0.01 : 0.003;
|
||||
bodyDef.type = body.type == 'structure' ?
|
||||
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
|
||||
if (body.player || true) bodyDef.allowSleep = false;
|
||||
let b2body = this.world.CreateBody(bodyDef);
|
||||
|
||||
let fixtureDef = new Box2D.b2FixtureDef();
|
||||
fixtureDef.density = 10;
|
||||
fixtureDef.density = 10.1;
|
||||
fixtureDef.friction = 1;
|
||||
fixtureDef.restitution = 1;
|
||||
|
||||
|
@ -63,7 +61,7 @@ class Physics {
|
|||
}
|
||||
|
||||
body.b2body = b2body;
|
||||
//console.log(Object.keys(b2body).sort());
|
||||
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
||||
}
|
||||
|
||||
step() {
|
||||
|
|
|
@ -30,7 +30,6 @@ class Ship extends Body {
|
|||
move(data) {
|
||||
let b = this.b2body;
|
||||
|
||||
|
||||
//console.log(b.GetLocalCenter());
|
||||
|
||||
for(var i in b) {
|
||||
|
@ -70,7 +69,9 @@ class Ship extends Body {
|
|||
type: 'ship',
|
||||
id: this.id,
|
||||
team: this.player.team,
|
||||
name: this.player.name,
|
||||
frame: this.frame,
|
||||
power: this.power,
|
||||
mounts: this.mounts,
|
||||
turrets: this.turrets,
|
||||
size: this.size,
|
||||
|
|
|
@ -3,23 +3,23 @@
|
|||
"name": "Aiwodge",
|
||||
"size": "small",
|
||||
"power": {
|
||||
"forward": 0.0015,
|
||||
"forward": 0.015,
|
||||
"back": 0,
|
||||
"rotation": 0.0001
|
||||
"rotation": 0.001
|
||||
},
|
||||
"hull": [
|
||||
[
|
||||
[3, 1],
|
||||
[3, 30],
|
||||
[12, 30],
|
||||
[29, 18],
|
||||
[12, 1],
|
||||
[29, 13],
|
||||
[12, 1]
|
||||
[29, 18],
|
||||
[12, 30],
|
||||
[3, 30],
|
||||
[3, 1]
|
||||
]
|
||||
],
|
||||
"mounts": [
|
||||
[18, 4],
|
||||
[18, 27]
|
||||
[18, 28]
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,9 @@ function minifyJs(callback) {
|
|||
|
||||
var comment = '';
|
||||
|
||||
// Remove to re-enable minifying.
|
||||
callback(cache); return;
|
||||
|
||||
try {
|
||||
cache = uglify.minify(cache, { fromString: true }).code;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue