reduce physics cpu usage
This commit is contained in:
parent
8a059b92d5
commit
8ea6c2d937
10 changed files with 2435 additions and 34 deletions
2364
processed.txt
Normal file
2364
processed.txt
Normal file
File diff suppressed because it is too large
Load diff
|
@ -23,6 +23,7 @@ class Net {
|
|||
|
||||
this.socket.on('world', data => {
|
||||
game.world.clear();
|
||||
console.log(data);
|
||||
game.world.bounds = data.bounds;
|
||||
for (var b of data.bodies) {
|
||||
game.world.add(b);
|
||||
|
|
|
@ -77,8 +77,8 @@ class Renderer {
|
|||
this.pallet.opacity(0.05);
|
||||
for (var x = gridx - cw / 2 - 50; x < cw + 50; x += 50) {
|
||||
for (var y = gridy - ch / 2 - 50; y < ch + 50; y += 50) {
|
||||
var wx = (-cx + x) / SCALE;
|
||||
var wy = (-cy + y) / SCALE;
|
||||
var wx = ((-cx + x) / SCALE) | 0;
|
||||
var wy = ((-cy + y) / SCALE) | 0;
|
||||
var b = game.world.bounds;
|
||||
if (wx > b.right || wx < b.left || wy > b.bottom || wy < b.top) {
|
||||
this.pallet.opacity(0.2);
|
||||
|
|
|
@ -83,6 +83,9 @@
|
|||
"Tamarillo",
|
||||
"Tamarind",
|
||||
"Ugli Fruit",
|
||||
"Umbu",
|
||||
"Ximenia",
|
||||
"Xylocarp",
|
||||
"Yuzu",
|
||||
"Ziziphus"
|
||||
],
|
||||
|
@ -90,7 +93,9 @@
|
|||
"a": [
|
||||
"Awesome",
|
||||
"Amazing",
|
||||
"Awkward"
|
||||
"Awkward",
|
||||
"Absurd",
|
||||
"Allergic"
|
||||
],
|
||||
"b": [
|
||||
"Bubbly",
|
||||
|
@ -107,7 +112,8 @@
|
|||
],
|
||||
"d": [
|
||||
"Dizzy",
|
||||
"Dangerous"
|
||||
"Dangerous",
|
||||
"Dazzling"
|
||||
],
|
||||
"e": [
|
||||
"Energetic",
|
||||
|
@ -116,7 +122,10 @@
|
|||
],
|
||||
"f": [
|
||||
"Funny",
|
||||
"Fancy"
|
||||
"Fancy",
|
||||
"Fat",
|
||||
"Filthy",
|
||||
"Fizzy"
|
||||
],
|
||||
"g": [
|
||||
"Giggly",
|
||||
|
@ -134,15 +143,21 @@
|
|||
"Introverted"
|
||||
],
|
||||
"j": [
|
||||
"Jolly"
|
||||
"Jolly",
|
||||
"Jealous",
|
||||
"Joyful",
|
||||
"Jittery"
|
||||
],
|
||||
"k": [
|
||||
"Crazy",
|
||||
"Cool"
|
||||
"Keen",
|
||||
"Kind",
|
||||
"Kingly"
|
||||
],
|
||||
"l": [
|
||||
"Little",
|
||||
"Lazy"
|
||||
"Lazy",
|
||||
"Lovely",
|
||||
"Lax"
|
||||
],
|
||||
"m": [
|
||||
"Magnificent",
|
||||
|
@ -151,13 +166,16 @@
|
|||
],
|
||||
"n": [
|
||||
"Nosy",
|
||||
"Noisy"
|
||||
"Noisy",
|
||||
"Nasty",
|
||||
"Naive"
|
||||
],
|
||||
"o": [
|
||||
"Oblivious",
|
||||
"Omnipotent",
|
||||
"Omnipresent",
|
||||
"Okay"
|
||||
"Okay",
|
||||
"Obvious"
|
||||
],
|
||||
"p": [
|
||||
"Pretty",
|
||||
|
@ -175,7 +193,10 @@
|
|||
],
|
||||
"r": [
|
||||
"Radical",
|
||||
"Racist"
|
||||
"Racist",
|
||||
"Red",
|
||||
"Rare",
|
||||
"Risky"
|
||||
],
|
||||
"s": [
|
||||
"Stupid",
|
||||
|
@ -190,7 +211,8 @@
|
|||
],
|
||||
"u": [
|
||||
"Unbelievable",
|
||||
"Ugly"
|
||||
"Ugly",
|
||||
"Ultimate"
|
||||
],
|
||||
"v": [
|
||||
"Voluptuous",
|
||||
|
@ -201,11 +223,13 @@
|
|||
"Weird"
|
||||
],
|
||||
"x": [
|
||||
"Xanthous",
|
||||
"Xenophobic"
|
||||
],
|
||||
"y": [
|
||||
"Yellow",
|
||||
"Useful"
|
||||
"Yucky",
|
||||
"Yummy"
|
||||
],
|
||||
"z": [
|
||||
"Zany",
|
||||
|
|
|
@ -49,7 +49,7 @@ class Body {
|
|||
}
|
||||
|
||||
applyDelta() {
|
||||
this.world.applyDelta(this.packDelta());
|
||||
this.world.applyDelta(this.packDelta(), this.pos);
|
||||
}
|
||||
|
||||
applyForce(x, y, center) {
|
||||
|
|
|
@ -8,8 +8,10 @@ class Grapple extends Projectile {
|
|||
super(world, pos);
|
||||
|
||||
this.r = pos.r;
|
||||
this.x = pos.x * 32;
|
||||
this.y = pos.y * 32;
|
||||
this.x = pos.x;
|
||||
this.y = pos.y;
|
||||
this.xvel = pos.xvel;
|
||||
this.yvel = pos.yvel;
|
||||
this.xvel += Math.cos(this.r) * 0.25;
|
||||
this.yvel += Math.sin(this.r) * 0.25;
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@ class Missile extends Projectile {
|
|||
constructor(world, pos, source) {
|
||||
super(world);
|
||||
|
||||
this.x = pos.x * 32;
|
||||
this.y = pos.y * 32;
|
||||
this.x = pos.x;
|
||||
this.y = pos.y;
|
||||
this.xvel = pos.xvel;
|
||||
this.yvel = pos.yvel;
|
||||
this.r = pos.r;
|
||||
|
|
|
@ -15,6 +15,7 @@ class Ship extends Body {
|
|||
// Body data.
|
||||
this.x = pos.x || 0;
|
||||
this.y = pos.y || 0;
|
||||
this.r = player.team == 'b' ? Math.PI : 0;
|
||||
|
||||
this.type = 'ship';
|
||||
this.class = build.ship;
|
||||
|
@ -64,6 +65,7 @@ class Ship extends Body {
|
|||
if (this.grapple) {
|
||||
this.grapple.retract();
|
||||
} else {
|
||||
let s = this.world.scale;
|
||||
this.launchGrapple(this.inputs.mx, this.inputs.my);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,21 +22,23 @@ class World {
|
|||
this.tpsCount = 0;
|
||||
this.tpsStart = Date.now();
|
||||
|
||||
this.scale = 32;
|
||||
|
||||
this.tickCount = 0;
|
||||
|
||||
this.bounds = {
|
||||
left: -5,
|
||||
right: 50,
|
||||
top: -15,
|
||||
bottom: 15
|
||||
left: 0,
|
||||
right: 250,
|
||||
top: 0,
|
||||
bottom: 30
|
||||
}
|
||||
}
|
||||
|
||||
addPlayer(player) {
|
||||
this.players.add(player);
|
||||
let pos = {
|
||||
x: player.team == 'b' ? 200 : 0,
|
||||
y: 0
|
||||
x: player.team == 'b' ? this.bounds.right - 5 : 5,
|
||||
y: this.bounds.bottom / 2
|
||||
};
|
||||
let ship = new Ship(this, pos, player);
|
||||
player.ship = ship;
|
||||
|
@ -86,9 +88,14 @@ class World {
|
|||
this.room.broadcast('effect', copula.packFull());
|
||||
}
|
||||
|
||||
applyDelta(data) {
|
||||
applyDelta(data, bodyPos) {
|
||||
data = data.map(v => +(v.toFixed(3)));
|
||||
this.players.forEach(player => player.applyDelta(data));
|
||||
this.players.forEach(player => {
|
||||
let dx = player.ship.pos.x - bodyPos.x;
|
||||
let dy = player.ship.pos.y - bodyPos.y;
|
||||
if (dx * dx + dy * dy < 900)
|
||||
player.applyDelta(data)
|
||||
});
|
||||
}
|
||||
|
||||
explosion(pos, power) {
|
||||
|
@ -110,12 +117,12 @@ class World {
|
|||
}
|
||||
|
||||
populate() {
|
||||
for (var i = 0; i < 5; i++) {
|
||||
for (var i = 0; i < 50; i++) {
|
||||
let pos = {
|
||||
x: Math.random() * 2000 - 200,
|
||||
y: Math.random() * 500 - 250
|
||||
x: Math.random() * this.bounds.right,
|
||||
y: Math.random() * this.bounds.bottom
|
||||
};
|
||||
this.spawner.spawnAsteroid(pos.x, pos.y,Math.random() * 50 + 10);
|
||||
this.spawner.spawnAsteroid(pos.x, pos.y, Math.random() * 50 + 10);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -171,8 +178,9 @@ class World {
|
|||
this.tps = this.tpsCount / 5 | 0;
|
||||
this.tpsCount = 0;
|
||||
this.tpsStart = Date.now();
|
||||
if(this.tps < 50)
|
||||
if(this.tps < 50) {
|
||||
wingbase.warning(`${this.room.name} TPS: ${this.tps}`);
|
||||
}
|
||||
}
|
||||
|
||||
this.tpsCount++;
|
||||
|
|
|
@ -44,7 +44,7 @@ class Physics {
|
|||
let s = SCALE;
|
||||
let bodyDef = new Box2D.b2BodyDef();
|
||||
bodyDef.userData = body;
|
||||
bodyDef.position = new b2Vec2(body.x / s || 0, body.y / s || 0);
|
||||
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
|
||||
bodyDef.angle = body.r || 0;
|
||||
bodyDef.fixedRotation = false;
|
||||
bodyDef.active = true;
|
||||
|
@ -55,7 +55,7 @@ class Physics {
|
|||
bodyDef.angularDamping = body.type == 'asteroid' ? 0.003 : 0.06;
|
||||
bodyDef.type = body.type == 'structure' ?
|
||||
Box2D.b2BodyType.b2_staticBody : Box2D.b2BodyType.b2_dynamicBody;
|
||||
if (body.player || true) bodyDef.allowSleep = false;
|
||||
if (body.player) bodyDef.allowSleep = false;
|
||||
let b2body = this.world.CreateBody(bodyDef);
|
||||
|
||||
let fixtureDef = new Box2D.b2FixtureDef();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue