Skip to content

Commit

Permalink
fix: Fixing animation optimization issue
Browse files Browse the repository at this point in the history
When a tile was used several times in the same animation, it would be improperly
renumbered if the tile was already optimized before.
This prevented animations going back and forth to work correctly.
  • Loading branch information
moufmouf committed Jun 19, 2024
1 parent ff47c0d commit 74919b5
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/Optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ sharp.cache(false);

export class Optimizer {
private optimizedMap: ITiledMap;
private optimizedTiles: Map<number, number>;
/**
* A map mapping the old tile id to the new tile id (global) and to the new tile id in the current tileset
* @private
*/
private optimizedTiles: Map<number, { global: number; local: number }>;
private optimizedTilesets: ITiledMapEmbeddedTileset[];
private currentTilesetOptimization: ITiledMapEmbeddedTileset;
private currentExtractedTiles: Promise<Buffer>[];
Expand All @@ -30,7 +34,7 @@ export class Optimizer {
private readonly outputPath: string
) {
this.optimizedMap = map;
this.optimizedTiles = new Map<number, number>();
this.optimizedTiles = new Map<number, { global: number; local: number }>();
this.optimizedTilesets = [];
this.tileSize = options?.tile?.size ?? 32;
this.outputSize = options?.output?.tileset?.size ? options?.output?.tileset?.size : 512;
Expand Down Expand Up @@ -219,7 +223,7 @@ export class Optimizer {

const unflippedTileId = tileId - minBitId;

const existantNewTileId = this.optimizedTiles.get(unflippedTileId);
const existantNewTileId = this.optimizedTiles.get(unflippedTileId)?.global;

if (existantNewTileId) {
return existantNewTileId + minBitId;
Expand Down Expand Up @@ -265,7 +269,10 @@ export class Optimizer {
if (tileData && tileData.animation) {
if (tileData.animation.length + this.currentExtractedTiles.length > this.tilesetMaxTileCount) {
for (let i = 1; i < this.tilesetMaxTileCount - this.currentExtractedTiles.length; i++) {
this.optimizedTiles.set(-1, this.optimizedTiles.size + i);
this.optimizedTiles.set(-1, {
global: this.optimizedTiles.size + i,
local: 0,
});
}

await this.currentTilesetRendering();
Expand All @@ -275,7 +282,10 @@ export class Optimizer {

const newTileId = this.optimizedTiles.size + 1;

this.optimizedTiles.set(unflippedTileId, newTileId);
this.optimizedTiles.set(unflippedTileId, {
global: newTileId,
local: this.currentExtractedTiles.length,
});

let newTileData: ITiledMapTile | undefined = undefined;

Expand Down Expand Up @@ -322,10 +332,14 @@ export class Optimizer {
newTileData.animation = [];
for (const frame of tileData.animation) {
await this.optimizeNewTile(oldFirstgid + frame.tileid);
const newTile = this.optimizedTiles.get(oldFirstgid + frame.tileid)?.local;
if (newTile === undefined) {
throw new Error(`Undefined tile in animation for ${oldFirstgid + frame.tileid}`);
}

newTileData.animation.push({
duration: frame.duration,
tileid: this.currentExtractedTiles.length - 1,
tileid: newTile,
});
}
}
Expand Down

0 comments on commit 74919b5

Please sign in to comment.