-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteJsonLoader.ts
47 lines (42 loc) · 1.38 KB
/
SpriteJsonLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Image from './Image';
import Sprite from './Sprite';
import Frame from './Frame';
import Animation from './Animation';
import LocalFileLoader from './LocalFileLoader';
interface SpriteJsonFrame {
x: number;
y: number;
width: number;
height: number;
}
interface SpriteJsonAnimation {
id: string;
duration: number;
frames: SpriteJsonFrame[];
}
interface SpriteJson {
image: string;
animations: SpriteJsonAnimation[];
}
export default class SpriteJsonLoader {
public static load(filename: string, onload: (sprite: Sprite | null) => any): void {
const loader = new LocalFileLoader();
loader.loadAsText(filename, (data: string | null): any => {
if (data) {
const spritedata: SpriteJson = JSON.parse(data);
const image = new Image(spritedata.image);
const sprite = new Sprite(image);
for (const animation of spritedata.animations) {
const anim = new Animation(animation.id, animation.duration);
for (const frame of animation.frames) {
anim.addFrame(new Frame(frame.x, frame.y, frame.width, frame.height));
}
sprite.addAnimation(anim);
}
onload(sprite);
} else {
onload(null);
}
});
}
}