Tilemap.prototype._drawAutotile = function(bitmap, tileId, dx, dy) {
var autotileTable = Tilemap.FLOOR_AUTOTILE_TABLE;
var kind = Tilemap.getAutotileKind(tileId);
var shape = Tilemap.getAutotileShape(tileId);
var tx = kind % 8;
var ty = Math.floor(kind / 8);
var bx = 0;
var by = 0;
var setNumber = 0;
var isTable = false;
if (Tilemap.isTileA1(tileId)) {
// here you can set your animation cycle
const ANIMATION_CYCLE = [0, 1, 2, 3, 4, 3, 2, 1];
var waterSurfaceIndex = ANIMATION_CYCLE[this.animationFrame % ANIMATION_CYCLE];
setNumber = 0;
if (kind === 0) {
bx = waterSurfaceIndex * 2;
by = 0;
} else if (kind === 1) {
bx = waterSurfaceIndex * 2;
by = 3;
} else if (kind === 2) {
bx = 6;
by = 0;
} else if (kind === 3) {
bx = 6;
by = 3;
} else {
bx = Math.floor(tx / 4) * 8;
by = ty * 6 + Math.floor(tx / 2) % 2 * 3;
if (kind % 2 === 0) {
bx += waterSurfaceIndex * 2;
}
else {
bx += 6;
autotileTable = Tilemap.WATERFALL_AUTOTILE_TABLE;
by += this.animationFrame % 3;
}
}
} else if (Tilemap.isTileA2(tileId)) {
setNumber = 1;
bx = tx * 2;
by = (ty - 2) * 3;
isTable = this._isTableTile(tileId);
} else if (Tilemap.isTileA3(tileId)) {
setNumber = 2;
bx = tx * 2;
by = (ty - 6) * 2;
autotileTable = Tilemap.WALL_AUTOTILE_TABLE;
} else if (Tilemap.isTileA4(tileId)) {
setNumber = 3;
bx = tx * 2;
by = Math.floor((ty - 10) * 2.5 + (ty % 2 === 1 ? 0.5 : 0));
if (ty % 2 === 1) {
autotileTable = Tilemap.WALL_AUTOTILE_TABLE;
}
}
var table = autotileTable[shape];
var source = this.bitmaps[setNumber];
if (table && source) {
var w1 = this._tileWidth / 2;
var h1 = this._tileHeight / 2;
for (var i = 0; i < 4; i++) {
var qsx = table[i][0];
var qsy = table[i][1];
var sx1 = (bx * 2 + qsx) * w1;
var sy1 = (by * 2 + qsy) * h1;
var dx1 = dx + (i % 2) * w1;
var dy1 = dy + Math.floor(i / 2) * h1;
if (isTable && (qsy === 1 || qsy === 5)) {
var qsx2 = qsx;
var qsy2 = 3;
if (qsy === 1) {
qsx2 = [0,3,2,1][qsx];
}
var sx2 = (bx * 2 + qsx2) * w1;
var sy2 = (by * 2 + qsy2) * h1;
bitmap.bltImage(source, sx2, sy2, w1, h1, dx1, dy1, w1, h1);
dy1 += h1/2;
bitmap.bltImage(source, sx1, sy1, w1, h1/2, dx1, dy1, w1, h1/2);
} else {
bitmap.bltImage(source, sx1, sy1, w1, h1, dx1, dy1, w1, h1);
}
}
}
};