From 1f7dee2c3b31d55c3990c393cb3ab1ce1f99e763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro=20Dias?= Date: Sun, 6 Oct 2024 23:22:38 +0100 Subject: [PATCH] cell removal wip --- README.md | 3 ++- cell.mjs | 18 +++++++++++++++- logic.mjs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++- render.mjs | 3 ++- 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 07d49e8..24b5560 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,8 @@ Implemented in vanilla js and rendered using canvas API. ## TODO -- detect cells to remove +- properly remove cells +- drop remaining ones - generate decent level - hint leaving cells visually - display next piece diff --git a/cell.mjs b/cell.mjs index 288e1ed..d04c8b1 100644 --- a/cell.mjs +++ b/cell.mjs @@ -1,11 +1,27 @@ +import { COLOR_NONE, KIND_EMPTY } from './constants.mjs'; + export class Cell { constructor(color, kind, rotation) { this.color = color; this.kind = kind; this.rotation = rotation; // only relevant for pills + this.leaving = false; } clone() { - return new Cell(this.color, this.kind, this.rotation); + const c = new Cell(this.color, this.kind, this.rotation); + c.leaving = this.leaving; + return c; + } + + toRemove() { + this.leaving = true; + } + + clearLeaving() { + if (!this.leaving) return; + this.leaving = false; + this.color = COLOR_NONE; + this.kind = KIND_EMPTY; } } diff --git a/logic.mjs b/logic.mjs index 3f837ff..5043af1 100644 --- a/logic.mjs +++ b/logic.mjs @@ -57,6 +57,8 @@ export function applyPill(m, p) { } }); + markCellsToDelete(m, p); + // reset new pill p.restore(randomPill()); @@ -119,9 +121,65 @@ export function rotateCCW(m, p) { } export function markCellsToDelete(m, p) { + const combos = []; + + for (let x = 0; x < m.w; ++x) { + let prev = COLOR_NONE; + let combo; + for (let y = 0; y < m.h; ++y) { + const c = m.getValue([x, y]).color; + if (c && !combo || (c && combo && c !== prev)) { + combo = [[x, y]]; + prev = c; + } else if (combo && (!c || c !== prev)) { + if (combo.length > 3) combos.push(combo); + combo = undefined; + prev = c; + } else if (c === prev && combo) { + combo.push([x, y]); + } + } + if (combo && combo.length > 3) combos.push(combo); + } + + for (let y = 0; y < m.h; ++y) { + let prev = COLOR_NONE; + let combo; + for (let x = 0; x < m.w; ++x) { + const c = m.getValue([x, y]).color; + if (c && !combo || (c && combo && c !== prev)) { + combo = [[x, y]]; + prev = c; + } else if (combo && (!c || c !== prev)) { + if (combo.length > 3) combos.push(combo); + combo = undefined; + prev = c; + } else if (c === prev && combo) { + combo.push([x, y]); + } + } + if (combo && combo.length > 3) combos.push(combo); + } + + if (combos.length > 0) { + console.warn('combos', combos); + combos.forEach((c) => { + c.forEach((pos) => { + const v = m.getValue(pos); + v.toRemove(); + }); + }); + // TODO HACkY + setTimeout(() => removeMarkedCells(m, p), 2000); + } + return combos.length > 0; } export function removeMarkedCells(m, p) { - + console.log('to remove...'); + m.values().forEach((v) => { + //console.log('v', v); + v.clearLeaving(); + }); } diff --git a/render.mjs b/render.mjs index 2ac7b53..79736a1 100644 --- a/render.mjs +++ b/render.mjs @@ -86,9 +86,10 @@ function render(el, m, p, { bg, viruses, pills }) { const ctx = el.getContext('2d'); ctx.clearRect(0, 0, S * m.w, S * m.h); ctx.drawImage(bg, 0, 0); - m.entries().forEach(([[x, y], { color, kind, rotation }]) => { + m.entries().forEach(([[x, y], { color, kind, rotation, leaving }]) => { x *= S; y *= S; + ctx.globalAlpha = leaving ? 0.5 : 1; if (kind === KIND_VIRUS) { ctx.drawImage(viruses[color], x, y); } else if (kind === KIND_PILL) {