Skip to content

Commit

Permalink
Half CPU usage of game
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowserinator committed Jul 27, 2018
1 parent 489073f commit 607c478
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 1 addition & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ Create a simulation (calculations only)
https://stackoverflow.com/questions/29429219/how-put-a-texture-on-pixi-js-polygon


Optimize planets
split int osections or something
Only body needs to be optimized
Speed up sector rendering by computing once for both graphics and planet


If rocket is inside planet teleport it out
Expand Down
4 changes: 2 additions & 2 deletions src/game-components/planet-sector.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ class PlanetSector {
Matter.Body.setStatic(this.body, true);

// Debugging
let graphics = new PIXI.Graphics();
/* let graphics = new PIXI.Graphics();
for (let i=1;i<vert.length;i++) {
graphics.lineStyle(20, 0xffffff)
.moveTo(vert[i-1].x, vert[i-1].y)
.lineTo(vert[i].x, vert[i].y);
}
sim.stage.addChild(graphics);
sim.stage.addChild(graphics); */
}
}

Expand Down
21 changes: 18 additions & 3 deletions src/game-components/planet.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Planet {

this.sectors = {};
this.texture_sectors = {};
this.sectors_to_delete = []; // Optimization for deleting matter.js sectors

// Science and info
this.desc = 'Default planet desc';
Expand Down Expand Up @@ -85,17 +86,31 @@ class Planet {
// Sector already exists
if (this.texture_sectors[angle2]) continue;

this.texture_sectors[angle2] = new PlanetSectorGraphic(angle2, this, stage_handler.getCurrentStage().stage);
this.texture_sectors[angle2] = new PlanetSectorGraphic(angle2, this, sim.stage);
}

/* Trim extra angles that are too far away */
if (added) {
for (let a of Object.keys(this.sectors)) {
//TODO aslso remove graphics
if (Math.abs(angle - a) > config.planet_sector_size * 5) {
Matter.Composite.remove(sim.engine.world, this.sectors[a].body);
// Matter.Composite.remove(sim.engine.world, this.sectors[a].body);
Matter.Sleeping.set(this.sectors[a].body, true);
this.sectors[a].body.collision
this.sectors_to_delete.push(this.sectors[a].body);
delete this.sectors[a];
}

if (Math.abs(angle - a) > config.planet_graphic_sector_size * 5) {
sim.stage.removeChild(this.texture_sectors[a].body);
delete this.texture_sectors[a];
}
}
}

/* Delete matter.js bodies in one swoop */
if (this.sectors_to_delete.length > 30 || Math.random() < 0.05) {
for (let sector of this.sectors_to_delete) {
Matter.Composite.remove(sim.engine.world, sector);
}
}
}
Expand Down

0 comments on commit 607c478

Please sign in to comment.