|
@@ -36,25 +36,37 @@ addObjects(); |
|
|
gameLoop(); |
|
|
gameLoop(); |
|
|
|
|
|
|
|
|
function setupMatter() { |
|
|
function setupMatter() { |
|
|
engine = Matter.Engine.create(); |
|
|
|
|
|
|
|
|
engine = Matter.Engine.create({ enableSleeping: true }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function addObjects() { |
|
|
function addObjects() { |
|
|
boxes.push( |
|
|
boxes.push( |
|
|
new Box(engine.world, 250, 20, 40, 40), |
|
|
|
|
|
new Box(engine.world, 300, 350, 40, 40), |
|
|
|
|
|
new Box(engine.world, 320, 70, 40, 40), |
|
|
|
|
|
new Box(engine.world, 0, 400, 800, 60, { isStatic: true }) |
|
|
|
|
|
|
|
|
new Box(engine.world, 250, 20, 40, 40, { restitution: 0.8 }), |
|
|
|
|
|
new Box(engine.world, 300, 350, 40, 40, { restitution: 0.8 }), |
|
|
|
|
|
new Box(engine.world, 320, 70, 40, 40, { restitution: 0.8 }), |
|
|
|
|
|
new Box(engine.world, 0, 400, 1200, 60, { isStatic: true }), |
|
|
|
|
|
new Box(engine.world, 260, 50, 40, 40), |
|
|
|
|
|
new Box(engine.world, 330, 300, 40, 40), |
|
|
|
|
|
new Box(engine.world, 350, 170, 40, 40), |
|
|
|
|
|
new Box(engine.world, 160, 150, 40, 40), |
|
|
|
|
|
new Box(engine.world, 190, 300, 40, 40), |
|
|
|
|
|
new Box(engine.world, 250, 270, 40, 40), |
|
|
); |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function gameLoop() { |
|
|
function gameLoop() { |
|
|
// update the physics world |
|
|
// update the physics world |
|
|
Matter.Engine.update(engine, 1000 / 60); |
|
|
Matter.Engine.update(engine, 1000 / 60); |
|
|
|
|
|
var sleepingCount = 0; |
|
|
// update the DOM world |
|
|
// update the DOM world |
|
|
for (let b of boxes) { |
|
|
for (let b of boxes) { |
|
|
b.update(); |
|
|
b.update(); |
|
|
|
|
|
if (b.physicsBox.isSleeping) |
|
|
|
|
|
{ |
|
|
|
|
|
sleepingCount += 1; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
console.debug("sleeping count:", sleepingCount); |
|
|
requestAnimationFrame(() => this.gameLoop()); |
|
|
requestAnimationFrame(() => this.gameLoop()); |
|
|
} |
|
|
} |
|
|
|
|
|
|