Repository navigation
Expand file tree
/
Copy pathAssetsManager.ts
More file actions
147 lines (115 loc) · 5.05 KB
/
Copy pathAssetsManager.ts
File metadata and controls
147 lines (115 loc) · 5.05 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var images: Array<HTMLImageElement>;
module gameui {
// Class
export class AssetsManager {
private static loader: PIXI.loaders.Loader;
private static spriteSheets: Array<any>;
private static assetsManifest: Array<any>;
private static defaultMouseEnabled: boolean = false;
public static onProgress: (progress: number) => void;
public static onComplete: () => void;
//load assets
public static loadAssets(manifest: Array<any>, path: string = "", spriteSheets?: Array<any>) {
// initialize objects
this.spriteSheets = spriteSheets ? spriteSheets : new Array();
this.assetsManifest = manifest;
if (!images) images = images ? images : new Array();
if (!this.loader) {
//creates a preload queue
this.loader = new PIXI.loaders.Loader(path);
// Adds callbacks
//this.loader.addEventListener("filestart", (evt: any) => { console.log("loading " + evt.item.src) })
this.loader.on("error ", (evt: any) => { console.log("error " + evt.item.src) })
this.loader.on("fileerror ", (evt: any) => { console.log("ferror " + evt.item.src) })
this.loader.on("progress", (evt: any) => { if (this.onProgress) this.onProgress(evt.progress) })
this.loader.on("fileload", (evt: any): boolean => {
if (evt.item.type == "image")
images[evt.item.id] = <HTMLImageElement>evt.result;
return true;
});
this.loader.on("complete", (loader, resources) => {
for (var r in resources) images[r] = resources[r].texture;
if (this.onComplete) this.onComplete();
})
}
//loads entire manifest
for (var m in manifest) {
this.loader.add(manifest[m].id, manifest[m].src);
}
}
public static reset() {
this.loader.reset();
}
// load a font spritesheet
public static loadFontSpriteSheet(id: string, fontFile: string, options: any = null) {
this.loader.add(id, fontFile, options);
}
public static loadSpriteSheet(id: string, fontFile: string) {
this.loader.add(id, fontFile);
};
public static load(callback) {
if (callback)
this.loader.load(callback);
};
// cleans all sprites in the bitmap array;
public static cleanAssets() {
if (images)
for (var i in images) {
var img = <any>images[i]
if (img.dispose) img.dispose();
delete images[i]
}
}
// return loaded image array
public static getImagesArray(): Array<HTMLImageElement> {
return images;
}
//gets a image from assets
public static getBitmap(name: string): PIXI.Sprite {
//if image is preloaded
var texture = this.getLoadedImage(name);
if (!texture)
texture = PIXI.utils.TextureCache[name];
if (texture) {
var imgobj = new PIXI.Sprite(texture);
imgobj.interactive = AssetsManager.defaultMouseEnabled;
return imgobj;
}
//or else try grab by filename
var imgobj = PIXI.Sprite.fromImage(name);
imgobj.interactive = AssetsManager.defaultMouseEnabled;
imgobj.texture["resolution"] = assetscale;
return imgobj;
}
//get a bitmap Text
public static getBitmapText(text: string, bitmapFontId: string, color: number = 0xffffff, size: number = 1): PIXI.extras.BitmapText {
var bitmapText = new PIXI.extras.BitmapText(text, { font: bitmapFontId, align: 'left' });
bitmapText.tint = color;
bitmapText.maxLineHeight = 100;
bitmapText.interactiveChildren = AssetsManager.defaultMouseEnabled;
bitmapText.scaleX = bitmapText.scaleY = size;
return bitmapText;
}
//Get a preloaded Image from assets
public static getLoadedImage(name: string): PIXI.Texture {
if (this.loader)
if (!this.loader.resources[name]) return null;
return this.loader.resources[name].texture;
}
//return a sprite according to the image
public static getMovieClip(name: string): PIXI.extras.MovieClip {
var textures = [];
var n2 = function (n) { return n > 9 ? "" + n : "0" + n; }
for (var i = 0; i < 999; i++) {
var id = name + n2(i);
if (!PIXI.utils.TextureCache[id]) break;
var texture = PIXI.Texture.fromFrame(id);
textures.push(texture);
}
var mc = new PIXI.extras.MovieClip(textures);
//mc.play();
return mc;
}
// #endregion
}
}