Skip to content

Commit

Permalink
cell removal wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePedroDias committed Oct 6, 2024
1 parent b7ede0f commit 1f7dee2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion cell.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
}
60 changes: 59 additions & 1 deletion logic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export function applyPill(m, p) {
}
});

markCellsToDelete(m, p);

// reset new pill
p.restore(randomPill());

Expand Down Expand Up @@ -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();
});
}
3 changes: 2 additions & 1 deletion render.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1f7dee2

Please sign in to comment.