From 54ff6b64724e02b01a93bb2d020dd419e929fbf6 Mon Sep 17 00:00:00 2001 From: zenoamaro Date: Mon, 24 Jun 2019 02:15:54 +0200 Subject: [PATCH] Optimized terrain collapse --- main.js | 11 +++++------ terrain.js | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/main.js b/main.js index b152ae5..6a1b4b2 100644 --- a/main.js +++ b/main.js @@ -22,7 +22,7 @@ fb.canvas.style.width = `${W * Z}px`; fb.canvas.style.height = `${H * Z}px`; document.body.appendChild(fb.canvas); -// Init aux canvases +// Layers const sky = createSky(W, H); const terrain = createTerrain(W, H); const projectiles = createCanvas(W, H); @@ -86,15 +86,15 @@ function update() { if (y > H || isTerrain(terrain, x, y)) { clipTerrain(terrain, (ctx) => drawExplosion(ctx, projectile.x, projectile.y, weapon.xr)); projectile.osc.stop(); - projectile = null; - state = 'collapse-land'; + state = 'land-collapse'; return; } } } - else if (state === 'collapse-land') { - collapseTerrain(terrain); + else if (state === 'land-collapse') { + collapseTerrain(terrain, projectile.x, projectile.weapon.xr); + projectile = null; state = 'land-players'; } @@ -117,7 +117,6 @@ function update() { } function draw() { - fb.clearRect(0, 0, W, H); fb.drawImage(sky.canvas, 0, 0); fb.drawImage(terrain.canvas, 0, 0); if (projectile) drawProjectile(); diff --git a/terrain.js b/terrain.js index 294d407..2af9ecd 100644 --- a/terrain.js +++ b/terrain.js @@ -15,18 +15,21 @@ export function generateTerrain(ctx) { } } -export function collapseTerrain(ctx) { - const {width, height} = ctx.canvas; - for (let x=0; x 0) land++; + for (let y=0; y 0) land++; } - ctx.clearRect(x, 0, 1, height); - drawRect(ctx, x, height-land, 1, land, 'burlywood'); + ctx.clearRect(left+x, 0, 1, height); + drawRect(ctx, left+x, height-land, 1, land, 'burlywood'); } }