Skip to content

Commit

Permalink
fix: AsepriteNativeParser returns the same tagged instance of animation
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Jul 16, 2024
1 parent ee0c3b3 commit 88c2f95
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/AsepriteNativeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ export class AsepriteNativeParser {
return result;
}

private _animationCache = new Map<string, Animation>();
/**
* Optionally get an animation by name
* @param name
* @returns
*/
public getAnimation(name?: string): Animation {
if (name && this._animationCache.has(name)) {
return this._animationCache.get(name)!;
}

let type = AnimationTypes.Forward;
let frames: Frame[];
Expand Down Expand Up @@ -120,6 +124,9 @@ export class AsepriteNativeParser {
strategy,
reverse
});
if (name) {
this._animationCache.set(name, animation)
}
return animation;
}

Expand Down
57 changes: 56 additions & 1 deletion test/unit/AsepriteJsonParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,60 @@ describe('An AsepriteJsonParser', () => {
expect(clone).toBeDefined();
expect(clone).not.toBe(aseprite);
expect(clone.getAnimation('anim1')).not.toBe(aseprite.getAnimation('anim1'));
})
});

it('returns the same instance of animation', async () => {
// Load resource
const raw: AsepriteRawJson = {
meta: {
image: './path/to/image',
size: { w: 20, h: 20 },
scale: 1,
format: 'RGBA8888',
layers: [],
frameTags: [
{ name: "anim1", from: 0, to: 1, direction: "forward"},
{ name: "anim2", from: 1, to: 2, direction: "reverse"},
{ name: "anim3", from: 0, to: 2, direction: "pingpong"}
],
slices: []
},
frames: {
"frame1": {
frame: { x: 0, y: 0, w: 10, h: 10},
rotated: false,
trimmed: false,
spriteSourceSize: { x: 0, y: 0, w: 10, h: 10 },
sourceSize: { w: 10, h: 10 },
duration: 500
},
"frame2": {
frame: { x: 10, y: 0, w: 10, h: 10},
rotated: false,
trimmed: false,
spriteSourceSize: { x: 0, y: 0, w: 10, h: 10 },
sourceSize: { w: 10, h: 10 },
duration: 500
},
"frame3": {
frame: { x: 30, y: 0, w: 10, h: 10},
rotated: false,
trimmed: false,
spriteSourceSize: { x: 0, y: 0, w: 10, h: 10 },
sourceSize: { w: 10, h: 10 },
duration: 500
}
}
}
// TODO move to a new test
const parser = new AsepriteJsonParser(raw, new ImageSource('./path/to/some/image'));
parser.parse();

const aseprite = parser.getAsepriteSheet();

const anim1 = aseprite.getAnimation('anim1');
const anim1Too = aseprite.getAnimation('anim1');

expect(anim1).toBe(anim1Too);
});
});
13 changes: 13 additions & 0 deletions test/unit/AsepriteNativeParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,17 @@ describe('A AsepriteNativeParser', () => {

await expectAsync(canvas).toEqualImage('./test/unit/expected-index-layers.png');
});

it('returns the same instance of animation', async () => {
// Load resource
const resource = new Resource<ArrayBuffer>("./test/unit/beetle-rgba-multi-animation.aseprite", "arraybuffer", true);
const arraybuffer = await resource.load();

const nativeParser = new AsepriteNativeParser(arraybuffer);
await nativeParser.parse();

const loopAnim = nativeParser.getAnimation('Loop');
const loopAnim2 = nativeParser.getAnimation('Loop');
expect(loopAnim).toBe(loopAnim2);
});
});

0 comments on commit 88c2f95

Please sign in to comment.