handle animated tiles

This commit is contained in:
synth-ruiner
2017-12-27 17:38:13 +00:00
parent c79cca778f
commit 7d31e398c6
2 changed files with 31 additions and 7 deletions

View File

@@ -1,4 +1,7 @@
$(document).ready(function() {
// todo define things like 16x16, 128x128 etc. as constants?
// also script debounce/throttle times
var bitsyData = {};
var palette = {
@@ -132,7 +135,7 @@ $(document).ready(function() {
};
// todo: handle animated tiles properly instead of discarding the second animation frame
var tiles = input.match(/TIL (.*)\n([01]{8}\n){8}/g);
var tiles = input.match(/TIL (.*)\n([01]{8}\n){8}(>\n([01]{8}\n){8})?/g); // everything after > is an optional second animation frame
_.each(tiles, function(tile, i) {
var name = tile.match(/TIL .*/)[0].replace('TIL ', '');
@@ -141,10 +144,16 @@ $(document).ready(function() {
var bitmap = tile.match(/[01]/g);
bitsyData.tiles[name] = {
name: name,
bitmap: _.chunk(bitmap, 8)
};
var newTile = {name: name};
if (bitmap.length === 64) { // normal tile
newTile.bitmap = _.chunk(bitmap, 8);
} else if (bitmap.length === 128) { // animated tile
newTile.bitmap = _.chunk(_.take( bitmap, 64), 8);
newTile.secondAnimationFrame = _.chunk(_.takeRight(bitmap, 64), 8);
}
bitsyData.tiles[name] = newTile;
});
// set palette to first imported palette and redraw
@@ -219,7 +228,7 @@ $(document).ready(function() {
rawData[i ] = targetColour.red;
rawData[i + 1] = targetColour.green;
rawData[i + 2] = targetColour.blue;
rawData[i + 3] = 255;
rawData[i + 3] = 255; // alpha
}
// split monochrome bitmap into equal chunks for easier x:y access
@@ -254,6 +263,18 @@ $(document).ready(function() {
}
});
});
if (tile.secondAnimationFrame) {
_.each(tile.secondAnimationFrame, function(row, y) {
_.each(row, function(pixel, x) {
if (parseInt(pixel) === parseInt(pseudoTile[y][x])) {
tile.match++;
}
});
});
tile.match /= 2;
}
});
var bestMatch = _.first(_.sortBy(tilesForMatch, 'match'));