Optimized terrain collapse
This commit is contained in:
1 parent
37eab40230
commit
54ff6b6472
2 files changed
+16
-14
No files matched your search
@@ -22,7 +22,7 @@ fb.canvas.style.width = `${W * Z}px`;
|
|||||||
fb.canvas.style.height = `${H * Z}px`;
|
fb.canvas.style.height = `${H * Z}px`;
|
||||||
document.body.appendChild(fb.canvas);
|
document.body.appendChild(fb.canvas);
|
||||||
|
|
||||||
// Init aux canvases
|
// Layers
|
||||||
const sky = createSky(W, H);
|
const sky = createSky(W, H);
|
||||||
const terrain = createTerrain(W, H);
|
const terrain = createTerrain(W, H);
|
||||||
const projectiles = createCanvas(W, H);
|
const projectiles = createCanvas(W, H);
|
||||||
@@ -86,15 +86,15 @@ function update() {
|
|||||||
if (y > H || isTerrain(terrain, x, y)) {
|
if (y > H || isTerrain(terrain, x, y)) {
|
||||||
clipTerrain(terrain, (ctx) => drawExplosion(ctx, projectile.x, projectile.y, weapon.xr));
|
clipTerrain(terrain, (ctx) => drawExplosion(ctx, projectile.x, projectile.y, weapon.xr));
|
||||||
projectile.osc.stop();
|
projectile.osc.stop();
|
||||||
projectile = null;
|
state = 'land-collapse';
|
||||||
state = 'collapse-land';
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (state === 'collapse-land') {
|
else if (state === 'land-collapse') {
|
||||||
collapseTerrain(terrain);
|
collapseTerrain(terrain, projectile.x, projectile.weapon.xr);
|
||||||
|
projectile = null;
|
||||||
state = 'land-players';
|
state = 'land-players';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +117,6 @@ function update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
fb.clearRect(0, 0, W, H);
|
|
||||||
fb.drawImage(sky.canvas, 0, 0);
|
fb.drawImage(sky.canvas, 0, 0);
|
||||||
fb.drawImage(terrain.canvas, 0, 0);
|
fb.drawImage(terrain.canvas, 0, 0);
|
||||||
if (projectile) drawProjectile();
|
if (projectile) drawProjectile();
|
||||||
|
|||||||
+11
-8
@@ -15,18 +15,21 @@ export function generateTerrain(ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function collapseTerrain(ctx) {
|
export function collapseTerrain(ctx, ox, r) {
|
||||||
const {width, height} = ctx.canvas;
|
const {height} = ctx.canvas;
|
||||||
for (let x=0; x<width; x++) {
|
const width = r * 2 + 10;
|
||||||
const imageData = ctx.getImageData(x, 0, 1, height);
|
const left = ox-r-5;
|
||||||
|
const imageData = ctx.getImageData(left, 0, width, height);
|
||||||
|
|
||||||
|
for (let x=0; x<width; x++) {
|
||||||
let land = 0;
|
let land = 0;
|
||||||
for (let y=0; y<height*4; y+=4) {
|
for (let y=0; y<height; y++) {
|
||||||
if (imageData.data[y+3] > 0) land++;
|
const index = y*width*4 + x*4 +3;
|
||||||
|
if (imageData.data[index] > 0) land++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.clearRect(x, 0, 1, height);
|
ctx.clearRect(left+x, 0, 1, height);
|
||||||
drawRect(ctx, x, height-land, 1, land, 'burlywood');
|
drawRect(ctx, left+x, height-land, 1, land, 'burlywood');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user