petmate
petmate copied to clipboard
flood fill tool
This is basically the fill method that paint programs use. I've implemented this for Spritemate, too. Would be a fun tool for Petmate.
This might get you started:
floodfill(pos)
{
// https://stackoverflow.com/questions/22053759/multidimensional-array-fill
// get target value
let x = pos.x;
let y = pos.y;
var data = this.all.sprites[this.all.current_sprite].pixels;
// multicolor check
var stepping = 1;
var is_multi = this.all.sprites[this.all.current_sprite].multicolor;
if (is_multi) stepping = 2;
if (is_multi && x%2 !== 0) x=x-1;
var target = data[y][x];
function flow(x,y,pen)
{
// bounds check what we were passed
if (y >= 0 && y < data.length && x >= 0 && x < data[y].length)
{
if (is_multi && x%2 !== 0) x=x-1;
if (data[y][x] === target && data[y][x] != pen)
{
data[y][x] = pen;
flow(x-stepping, y, pen); // check up
flow(x+stepping, y, pen); // check down
flow(x, y-1, pen); // check left
flow(x, y+1, pen); // check right
}
}
}
flow(x,y,this.all.pen);
this.all.sprites[this.all.current_sprite].pixels = data;
}
This makes a lot of sense for normal pixel data. But what's a sensible way to do this with PETSCII? There's both pixel (screencode) and color information per block. What's the traverse condition? Treat all non-space chars as a boundary?
I had it in mind like:
- flood fill all characters that match the char code of the character the user clicked on
- depending on the chosen tool: replace char+color, replace color, replace char
But I must admit I'm not sure if that turns out to be a power feature. Might be kept best in the backlog for a bit...