knewjade / tetris-fumen

A NPM package to Encode/Decode fumen for tetris
MIT License
14 stars 2 forks source link

Make field.fill return piece position #1

Closed eight04 closed 4 years ago

eight04 commented 4 years ago

I'd like to draw the current operation with a lightened color. I need the position after the piece is filled in the field. Example:

const pages = decoder.decode(data);
const field = pages[0].field.copy();
const changedTiles = field.fill(pages[0].operation);

for (let y = 0; y < 20; y++) {
  for (let x = 0; x < 10; x++) {
    myCanvas.draw(x, y, field.at(x, y));
  }
}
for (const [x, y] of changedTiles) {
  myCanvas.lighten(x, y);
}

Maybe we can also add this feature to field.put?

knewjade commented 4 years ago

@eight04 Thank you for your request. In v1.1.0, I added Mino class that more flexible object than operation and changed returning placed mino at Field::fill(), Field::put(). so you can write your sample as follows:

const pages = decoder.decode(data);
const field = pages[0].field.copy();
const changed = field.fill(pages[0].operation);  // `changed` is mino object as like `operation`

for (let y = 0; y < 20; y++) {
  for (let x = 0; x < 10; x++) {
    myCanvas.draw(x, y, field.at(x, y));
  }
}
for (const {x, y} of changed.positions()) {
  myCanvas.lighten(x, y);
}

If you have no problems, please close the issue.

eight04 commented 4 years ago

Here is the tool: https://github.com/eight04/fumen-svg

I'll probably build a server as an API.