let bombs = []; let explosions = []; //let bomb = {x: 0, y: 0} function placeBomb(x, y) { let lastBomb = bombs.slice(-1); // return last element if (lastBomb.length === 0 || lastBomb[0].placeAt < millis() - nextBombTime) { //snap bomb on grid if (snapBomb) { x = Math.round(x / cellSize) * cellSize; y = Math.round(y / cellSize) * cellSize; } let bomb = { x, y, placeAt: millis() }; bombs.push(bomb); } } function drawBombs() { textSize(cellSize / 1.6); for (let bomb of bombs) { text("💣", bomb.x - halfCellSize, bomb.y + yOffsetDraw - halfCellSize); if(debug){ fill(255, 0, 0); circle(bomb.x, bomb.y, 8);} } } function detonateBomb() { for (let bomb of bombs) { if (bomb && bomb.placeAt < millis() - bombDelay) { //console.log(bombs.length); placeMultiExplosion(bomb.x, bomb.y); bombs.splice(bombs.indexOf(bomb), 1); explosionSound.play(); } } } function placeMultiExplosion(x, y) { for (let ix = -explodeSize; ix <= explodeSize; ix++) { for (let iy = -explodeSize; iy <= explodeSize; iy++) { const exlodesOverBorder = overBorder({ x: x, y: y }); x = exlodesOverBorder.x; y = exlodesOverBorder.y; placeExplosion(ix * cellSize + x, iy * cellSize + y); } } } function placeExplosion(x, y) { const exlodeOverBorder = overBorder({ x: x, y: y }); console.log(exlodeOverBorder); console.log(x); x = exlodeOverBorder.x; y = exlodeOverBorder.y; let explosion = { x, y, placeAt: millis() }; explosions.push(explosion); const explodePosition = positionsToCellsIdx({ x: x, y: y, }); if (grid[explodePosition.yi][explodePosition.xi] === softItem) { grid[explodePosition.yi][explodePosition.xi] = freePlace; } } function drawExplosions() { textSize(cellSize / 1); for (let explosion of explosions) { text( "💥", explosion.x - halfCellSize - 4, explosion.y + yOffsetDraw - halfCellSize + 4 ); if(debug){fill(255, 0, 0); circle(explosion.x, explosion.y, 8);}/* */ if (explosion && explosion.placeAt < millis() - expodeDelay) { //console.log(explosions.lengh); explosions.splice(explosions.indexOf(explosion), 1); } } }