-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResourceManager.ts
77 lines (63 loc) · 2.55 KB
/
ResourceManager.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {_} from "../main";
/**
* Created by MSI on 08.11.2017.
*/
export class ResourceManager {
public shaders: Object = {};
public spineData: Object = {};
private animFolder: string;
private spineLoaderListneners: { [key:string]:Function[]; } = {};
constructor(animFolder: string) {
this.animFolder = animFolder;
}
requestSpine(spineName: string, cb: (spineData: any)=>any) {
if (this.spineData[spineName]) {
return cb(this.spineData[spineName]);
}
if (!this.spineLoaderListneners[spineName]) {
this.spineLoaderListneners[spineName] = [cb]
} else {
this.spineLoaderListneners[spineName].push(cb);
return;
}
let loader = new PIXI.loaders.Loader();
loader
.add(spineName, this.animFolder + spineName +'.json',)
.load((loader, resources) => {
this.spineData[spineName] = resources[spineName].spineData;
for (let x in this.spineLoaderListneners[spineName]) {
this.spineLoaderListneners[spineName][x](this.spineData[spineName]);
}
this.spineLoaderListneners[spineName] = null;
});
}
loadAssets(assets: string[], onProcess: (loader: any, evt: any) => any, onComplete: () => any) {
let loader = new PIXI.loaders.Loader();
if ((<any>PIXI).compressedTextures) {
(<any>loader).before((<any>PIXI).compressedTextures.imageParser());
}
loader.add(assets);
loader.on('complete', () => {
onComplete();
});
loader.on('progress', (loader: any, evt: any) => {
if (evt.name.indexOf('shaders') >= 0) {
let result = evt.name.substring(evt.name.lastIndexOf("/") + 1);
this.shaders[result] = evt.data
}
if (evt.name.indexOf('/') > 0 && evt.type == PIXI.loaders.Resource.TYPE.XML) {
let ext = evt.name.substr(evt.name.lastIndexOf('.') + 1);
let result = evt.name.substring(evt.name.lastIndexOf("/") + 1);
result = result.substring(0, result.length - 4);
if (ext == 'tsx') {
_.lm.addTileset(result, evt.data);
}
if (ext == 'tmx') {
_.lm.add(result, evt.data);
}
}
onProcess(loader, evt)
});
loader.load();
}
}