-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoader.ts
More file actions
716 lines (598 loc) · 23.9 KB
/
Loader.ts
File metadata and controls
716 lines (598 loc) · 23.9 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import {O} from "./BaseObjects/O";
import {m, Vec2} from "./Math";
import {ColorGradingShader} from "./shaders/ColorGradingShader";
import {ObjectNames} from "../ObjectsList"
import {Application, PIXI} from "./Application";
import {Stage} from "./Stage";
import {POOL_TAG_HEAVEN_SPRITE} from "./SM";
const FLIPPED_HORIZONTALLY_FLAG = 0x80000000;
const FLIPPED_VERTICALLY_FLAG = 0x40000000;
const FLIPPED_DIAGONALLY_FLAG = 0x20000000;
for (let x in ObjectNames) {
ObjectNames[x.toLowerCase()] = ObjectNames[x]
}
export type LoadContainerType = {
container: PIXI.Container;
list: Array<O>;
}
type ImageData = {
tilesetWidth: number,
tilesetHeight: number,
width: number,
height: number,
source: string,
}
type BigTileset = {
columns: number;
tilecount: number;
firstgid: number;
tw: number;
th: number;
texname: string;
}
export function extractBlendMode(bm: string): number {
switch (bm) {
case 'normal':
return PIXI.BLEND_MODES.NORMAL;
break;
case 'add':
return PIXI.BLEND_MODES.ADD;
break;
case 'multiply':
return PIXI.BLEND_MODES.MULTIPLY;
break;
case 'screen':
return PIXI.BLEND_MODES.SCREEN;
break;
case 'overlay':
return PIXI.BLEND_MODES.OVERLAY;
break;
case 'darken':
return PIXI.BLEND_MODES.DARKEN;
break;
case 'dodge':
return PIXI.BLEND_MODES.COLOR_DODGE;
break;
case 'burn':
return PIXI.BLEND_MODES.COLOR_BURN;
break;
case 'hardlight':
return PIXI.BLEND_MODES.HARD_LIGHT;
break;
case 'softlight':
return PIXI.BLEND_MODES.SOFT_LIGHT;
break;
case 'difference':
return PIXI.BLEND_MODES.DIFFERENCE;
break;
case 'exclusion':
return PIXI.BLEND_MODES.EXCLUSION;
break;
case 'hue':
return PIXI.BLEND_MODES.HUE;
break;
case 'saturation':
return PIXI.BLEND_MODES.SATURATION;
break;
case 'color':
return PIXI.BLEND_MODES.COLOR;
break;
case 'luminosity':
return PIXI.BLEND_MODES.LUMINOSITY;
break;
}
return PIXI.BLEND_MODES.NORMAL;
}
export class Loader {
removeExt(t: string): string {
return t.replace(/\.[^/.]+$/, "")
}
customGlobalParamsCallback: Function;
public loading: boolean = false;
public levels: any = {};
public objectsList: O[];
private tilesets: any = {};
static SkipSpriteExt: boolean = false;
add(name: string, data: any) {
this.levels[name] = data;
}
static extractShortSrc(src: string) {
src = src.substring(src.lastIndexOf("\\") + 1);
return src;
}
static addGfxToWorld(stage: Stage, layerName: string): PIXI.Container {
if (layerName == 'gui') return Application.One.sm.gui;
if (layerName == 'gui2') return Application.One.sm.gui2;
if (layerName == 'olgui') return Application.One.sm.olgui;
if (layerName == 'light') return Application.One.sm.light;
return stage.layers[layerName.toLowerCase()];
}
static val(obj: any, tag: string): string {
var el = obj.getElementsByTagName(tag);
if (!el.length) return;
return el[0].textContent;
}
///Idea is to load all the objects
//then translate O to owner gfx
loadToObject(stage: Stage, name: string, group: string, object: O): O[] {
let a = this.load(stage, name, null, null, null, group, true, false);
let list = [];
this.init(a, false);
for (let x of a) {
if (x.gfx) {
object.addChild(x)
}
list.push(x);
/*if (x.constructor == O && x.gfx) {
Application.One.rp(x.gfx);
object.gfx.addChild(x.gfx);
x.gfx = null;
x.killNow();
} else {
list.push(x)
}*/
}
return list
}
loadToContainer(stage: Stage, name: string, cb: Function, CameraMode = null, offs: Vec2 = null, group: string = null, container: PIXI.Container = null): LoadContainerType {
let c = container ? container : new PIXI.Container();
let a = this.load(stage, name, cb, CameraMode, offs, group);
for (let x of a) {
if (x.gfx) {
Application.One.rp(x.gfx);
c.addChild(x.gfx);
}
}
return {list: a, container: c};
}
shouldAppear(c: any): boolean {
let properties = c.getElementsByTagName('properties')[0];
if (properties) {
let propertyArray = properties.getElementsByTagName('property');
for (let p of propertyArray) {
if (p.attributes.getNamedItem('name').nodeValue.toLowerCase() == 'appear') {
let prob = parseFloat(p.attributes.getNamedItem('value').nodeValue);
if (Math.random() * 100 > prob) return false;
}
}
}
return true;
}
load(stage: Stage, name: string, preInitCB: Function = null, CameraMode = null, offs: Vec2 = null, restrictGroup: string = null, addObjects = true, doInit: boolean = true): Array<O> {
this.loading = true;
let data = this.levels[name];
if (!data) {
throw 'No such level as ' + name;
console.log('No such level as ', name);
return;
}
let bigtilesets: Array<BigTileset> = [];
let images: { [key: number]: ImageData } = {};
let map = this.levels[name].getElementsByTagName("map")[0];
let tw = parseFloat(map.attributes.getNamedItem('tilewidth').nodeValue);
let th = parseFloat(map.attributes.getNamedItem('tileheight').nodeValue);
let objectsList: Array<O> = [];
let globalProperties = this.getProps(map);
if (addObjects)
this.updateGlobalMapParams(globalProperties);
let tilesets = map.getElementsByTagName("tileset");
for (let t of tilesets) {
let firstgid = t.attributes.getNamedItem('firstgid') ? t.attributes.getNamedItem('firstgid').nodeValue : 0;
let sourceAttr = t.attributes.getNamedItem('source');
if (sourceAttr) {
let source = sourceAttr.nodeValue;
let sourceNoExt = source.substring(0, source.length - 4);
t = this.tilesets[sourceNoExt].childNodes[0]
}
let tilecount = t.attributes.getNamedItem('tilecount').nodeValue;
let columns = t.attributes.getNamedItem('columns').nodeValue;
let tiles = t.getElementsByTagName('tile');
if (!tiles[0]) {
let img = t.getElementsByTagName('image')[0];
if (img)
bigtilesets.push({
firstgid: firstgid,
tilecount: tilecount,
tw: tw,
th: th,
columns: columns,
texname: img.attributes.getNamedItem('source').nodeValue,
});
} else {
for (let t of tiles) {
let img = t.getElementsByTagName('image')[0];
if (!img) continue;
let watr = img.attributes.getNamedItem('width');
let hatr = img.attributes.getNamedItem('height');
let sourceattr = img.attributes.getNamedItem('source');
images[parseInt(t.attributes.getNamedItem('id').nodeValue) + parseInt(firstgid)] = {
tilesetWidth: tw,
tilesetHeight: th,
width: watr ? watr.nodeValue : 0,
height: hatr ? hatr.nodeValue : 0,
source: sourceattr.nodeValue.replace(/^.*[\\\/]/, ''),
};
}
}
}
let addObjectsFunc = (c: any, ox: number, oy: number) => {
if (c.nodeName == 'layer') {
let name = c.attributes.getNamedItem('name').nodeValue.toLowerCase();
let ofsXattr = c.attributes.getNamedItem('offsetx');
let ofsYattr = c.attributes.getNamedItem('offsety');
let offset: Vec2 = [ofsXattr ? parseFloat(ofsXattr.nodeValue) : 0, ofsYattr ? parseFloat(ofsYattr.nodeValue) : 0];
offset[0] += ox;
offset[1] += oy;
let layerProps = this.getProps(c);
if (!stage.layers[name]) {
stage.addLayer(name, null)
}
if (!this.shouldAppear(c)) {
return
}
if (addObjects)
objectsList = objectsList.concat(this.addLayer(stage, c, bigtilesets, images, offset, layerProps));
}
if (c.nodeName == 'objectgroup') {
let layerProps = this.getProps(c);
let name = c.attributes.getNamedItem('name').nodeValue.toLowerCase();
if (!stage.layers[name]) {
stage.addLayer(name, null)
}
if (!this.shouldAppear(c)) {
return
}
if (addObjects)
objectsList = objectsList.concat(this.addObjectGroup(stage, c, images, layerProps));
}
};
let haveRestrictedGroup = false;
for (let c of map.childNodes) {
if (c.nodeName == 'group' && (!restrictGroup || c.attributes.getNamedItem('name').nodeValue.toLowerCase() == restrictGroup.toLowerCase())) {
let offsXattr = c.attributes.getNamedItem('offsetx');
let offsYattr = c.attributes.getNamedItem('offsety');
let ox: number = offsXattr ? parseFloat(offsXattr.nodeValue) : 0;
let oy: number = offsYattr ? parseFloat(offsYattr.nodeValue) : 0;
haveRestrictedGroup = true;
for (let x of c.childNodes) {
addObjectsFunc(x, ox, oy)
}
} else {
addObjectsFunc(c, 0, 0)
}
}
if (restrictGroup != null && !haveRestrictedGroup) {
throw "No such restricted group"
}
if (offs != null) {
for (let x of objectsList) {
x.pos[0] += offs[0];
x.pos[1] += offs[1];
}
}
this.objectsList = objectsList;
//let startLoad = (new Date()).getTime();
if (preInitCB) preInitCB(objectsList, globalProperties);
if (doInit)
this.init(objectsList, CameraMode);
this.objectsList = null;
this.loading = false;
return objectsList
}
getProps(node: any): Object {
let globalProperties = [];
let props;
for (let pchildren of node.childNodes) {
if (pchildren.nodeName == 'properties') {
props = pchildren;
break;
}
}
if (props) {
let propertyArray = props.childNodes;
for (let p of propertyArray) {
if (p.nodeName == 'property')
globalProperties[p.attributes.getNamedItem('name').nodeValue] = p.attributes.getNamedItem('value').nodeValue;
}
}
return globalProperties
}
addObjectGroup(stage: Stage, objectGroup, images: any, layerProps: any): Array<O> {
let objectsList: Array<O> = [];
let name = objectGroup.attributes.getNamedItem('name').nodeValue;
let ofsXattr = objectGroup.attributes.getNamedItem('offsetx');
let ofsYattr = objectGroup.attributes.getNamedItem('offsety');
let offsetx = ofsXattr ? parseFloat(ofsXattr.nodeValue) : 0;
let offsety = ofsYattr ? parseFloat(ofsYattr.nodeValue) : 0;
let objects = objectGroup.getElementsByTagName('object');
for (let o of objects) {
let gidAttr = o.attributes.getNamedItem('gid');
let gid = gidAttr ? parseInt(gidAttr.nodeValue) : -1;
let flipped_horizontally = false;
let flipped_vertically = false;
let textureName;
let image;
if (gid > 0) {
flipped_horizontally = (gid & FLIPPED_HORIZONTALLY_FLAG) == -FLIPPED_HORIZONTALLY_FLAG;
flipped_vertically = (gid & FLIPPED_VERTICALLY_FLAG) == FLIPPED_VERTICALLY_FLAG;
if (flipped_horizontally) gid &= ~FLIPPED_HORIZONTALLY_FLAG;
if (flipped_vertically) gid &= ~FLIPPED_VERTICALLY_FLAG;
image = images[gid];
if (!image) {
console.log("Can't load texture with Tile Id: ", gid);
} else {
textureName = image.source;
if (Loader.SkipSpriteExt) {
textureName = this.removeExt(textureName)
}
}
}
let oo = this.createObject(stage, o, textureName, offsetx, offsety, image ? image.source : null, name, layerProps, flipped_horizontally, flipped_vertically);
if (oo) objectsList.push(oo);
}
if (layerProps["color"])
this.setLayerColor(objectsList, layerProps["color"]);
if (layerProps["light"])
this.setLayerLightColor(objectsList, layerProps["light"]);
return objectsList;
}
createGfx(o: any, textureName: string, x: number, y: number, frameName: string, properties: Array<string>): any {
let w = parseFloat(o.attributes.getNamedItem('width').nodeValue);
let h = parseFloat(o.attributes.getNamedItem('height').nodeValue);
let gfx: any;
if (properties['movieclip'] == 'true') {
let noExtensionFrameName = frameName.replace(/\.[^/.]+$/, "");
let noDigitsFrameName = noExtensionFrameName.replace(/[0-9]/g, '');
gfx = Application.One.cm(noDigitsFrameName);
if (properties['randomstart'] == 'true') {
gfx.gotoAndPlay(m.rint(0, gfx.totalFrames - 1));
} else {
gfx.gotoAndPlay(0);
}
gfx.loop = true;
gfx.animationSpeed = 0.35;
} else {
gfx = Application.One.cs(textureName)
}
gfx.anchor.x = .5;
gfx.anchor.y = .5;
gfx.width = w;
gfx.height = h;
gfx.position.x = 0;
gfx.position.y = 0;
gfx.alpha = properties['alpha'] ? properties['alpha'] : 1;
if (properties['blendMode']) {
gfx.blendMode = extractBlendMode(properties['blendMode'].toLowerCase());
}
return gfx
}
createObject(stage: Stage, o: any, textureName: any, offsetx: number, offsety: number, frameName: string, layerName: string, groupProps: Object, flipX: boolean, flipY: boolean): O {
let id = o.attributes.getNamedItem('id').value;
let x = parseFloat(o.attributes.getNamedItem('x').nodeValue);
let y = parseFloat(o.attributes.getNamedItem('y').nodeValue);
let watr = o.attributes.getNamedItem('width');
let hatr = o.attributes.getNamedItem('height');
let w = watr ? parseFloat(watr.nodeValue) : 0;
let h = hatr ? parseFloat(hatr.nodeValue) : 0;
let nameAttr = o.attributes.getNamedItem('name');
let typeAttr = o.attributes.getNamedItem('type');
let rotAttr = o.attributes.getNamedItem('rotation');
let name: string = nameAttr ? nameAttr.nodeValue : '';
let type: string = typeAttr ? typeAttr.nodeValue : '';
let rot = rotAttr ? rotAttr.nodeValue : 0;
rot = Math.PI * (rot / 180);
//DO THIS ONLY FOR GFX SPRITES
let offsetVec: Vec2;
if (textureName) {
offsetVec = [w / 2, -h / 2]
} else {
offsetVec = [w / 2, h / 2]
}
offsetVec = m.rv2(offsetVec, rot);
x += offsetVec[0];
y += offsetVec[1];
let polylines = o.getElementsByTagName('polyline');
let polyline;
if (polylines.length > 0) polyline = polylines[0];
let polygons = o.getElementsByTagName('polygon');
let polygon;
if (polygons.length > 0) polygon = polygons[0];
let props = o.getElementsByTagName('property');
let properties: any = {};
for (let x of props) {
let name = x.attributes.getNamedItem('name').nodeValue;
let valattr = x.attributes.getNamedItem('value');
properties[name] = valattr ? valattr.nodeValue : x.textContent;
}
for (let x in groupProps) {
properties[x] = groupProps[x]
}
if (properties.server) return;
let className = '';
if (properties["type"]) className = properties["type"];
if (type != '') {
className = type;
}
if (properties["singleton"] == 'true') {
//UniqueCheck
if (ObjectNames[className] && Application.One.sm.findByType(ObjectNames[className]).length > 0) {
return null
}
}
let obj: O;
let startPos: Vec2 = [x + offsetx, y + offsety];
if (className && className.toLowerCase() == 'skip') {
return null;
}
if (className != '') {
if (!ObjectNames[className]) {
console.log('[LevelManager] Cant find class: ', className);
}
obj = new (ObjectNames[className])(startPos);
} else {
obj = new O(startPos)
}
obj.stringID = name;
if (polygon) {
properties["polygon"] = polygon.attributes.getNamedItem('points').nodeValue;
}
if (polyline) {
properties["polyline"] = polyline.attributes.getNamedItem('points').nodeValue;
}
obj.polygon = properties['polygon'];
obj.polyline = properties['polyline'];
if (textureName) { //has gfx
obj.gfx = this.createGfx(o, textureName, 0, 0, frameName, properties);
}
let visibility: boolean = properties['visible'] == 'false' ? false : true;
if (obj.gfx) obj.gfx.visible = visibility;
let layer = Loader.addGfxToWorld(stage, layerName);
obj.layer = layer;
if (obj.gfx) layer.addChild(obj.gfx);
if (obj.gfx) {
if (flipX) obj.gfx.scale.x = -obj.gfx.scale.x;
if (flipY) {
obj.gfx.scale.y = -obj.gfx.scale.y;
}
obj.gfx.rotation = rot;
}
obj.a = rot;
obj.width = w;
obj.height = h;
obj.properties = properties;
return obj
}
addLayer(stage: Stage, layer: any, bigtilesets: Array<BigTileset>, images: any, offset: Vec2, layerProps: any): Array<O> {
let objectsList = [];
let data = layer.getElementsByTagName('data')[0];
let str = data.textContent;
str = str.replace(/\r?\n|\r/g, '');
let name = layer.attributes.getNamedItem('name').nodeValue;
let arr = str.split(',');
let len = arr.length;
let layerWidth = layer.attributes.getNamedItem('width').nodeValue;
let layerHeight = layer.attributes.getNamedItem('height').nodeValue;
for (let i = 0; i < len; i++) {
if (arr[i] > 0) {
let textureName;
let tileID = arr[i];
if (images[tileID]) {
textureName = images[tileID].source;
} else {
for (var bt of bigtilesets) {
if (bt.firstgid >= tileID && tileID < bt.firstgid + bt.tilecount) {
break
}
}
if (!bt) continue;
textureName = bt.texname;
}
if (Loader.SkipSpriteExt) {
textureName = this.removeExt(textureName);
}
let col = Math.floor(i % layerWidth);
let row = Math.floor(i / layerWidth);
let posX = col * images[tileID].tilesetWidth;
let posY = row * images[tileID].tilesetHeight - (parseFloat(images[tileID].height) - images[tileID].tilesetHeight);
let type: string = layerProps['type'];
let layername: string = layerProps['name'];
let o = this.spawnTile(stage, textureName, posX + offset[0], posY + offset[1], name, type, layername, col, row);
o.properties = layerProps;
objectsList.push(o);
}
}
if (layerProps["color"])
this.setLayerColor(objectsList, layerProps["color"]);
if (layerProps["light"])
this.setLayerLightColor(objectsList, layerProps["light"]);
return objectsList;
}
spawnTile(stage, textureName: string, posX: number, posY: number, layerName: string, type: string, layerStringID: string, col: number, row: number): O {
let sprite = Application.One.cs(textureName);
sprite.anchor.x = 0.5;
sprite.anchor.y = 0.5;
let o: O;
if (type && type != '') {
o = new ObjectNames[type.toLowerCase()]([posX, posY])
} else {
o = new O([posX, posY]);
}
o.x += sprite.width / 2;
o.y += sprite.height / 2;
o.tileColRow = [col, row];
o.stringID = layerStringID;
o.gfx = sprite;
let layer = Loader.addGfxToWorld(stage, layerName);
o.layer = layer;
layer.addChild(sprite);
return o;
}
init(list: Array<O>, CameraMode) {
for (let o of list) {
if (CameraMode) {
o.CameraMode = CameraMode;
}
o.init(o.properties);
}
}
getGroups(level: string, filter: string = null): Array<string> {
let map = this.levels[level].getElementsByTagName("map")[0];
let arr = [];
for (let c of map.childNodes) {
if (c.nodeName == 'group') {
let name = c.attributes.name.value.toLowerCase();
if (!filter) {
arr.push(name);
} else {
if (~name.indexOf(filter)) {
arr.push(name);
}
}
}
}
return arr;
}
private setLayerLightColor(objectsList: O[], color: string) {
for (let x of objectsList) {
if (x.gfx && x.gfx.color) {
let col = m.strhexToRgbNormal(color);
x.gfx.color.setDark(col[0], col[1], col[2])
}
}
}
private setLayerColor(objectsList: O[], color: string) {
for (let x of objectsList) {
if (x.gfx && x.gfx.color) {
let col = m.strhexToRgbNormal(color);
x.gfx.color.setLight(col[0], col[1], col[2])
}
}
}
loadLayersFromLevelGroup(stage: Stage, level: string, group: string) {
this.load(stage, level, null, false, null, group, false)
}
loadGFXonly(stage: Stage, level: string, offs: Vec2, container: PIXI.Container): PIXI.DisplayObject[] {
let list = this.load(stage, level, null, false, offs, null, true, false);
let retList = [];
for (let x of list) {
if (x.gfx) {
Application.One.rp(x.gfx);
x.gfx.x = x.x;
x.gfx.y = x.y;
container.addChild(x.gfx);
retList.push(x.gfx);
x.gfx = null;
}
}
Application.One.sm.removeList(list);
return retList;
}
addTileset(result: string, data) {
this.tilesets[result] = data
}
protected updateGlobalMapParams(globalProperties: Object) {
if (this.customGlobalParamsCallback) {
this.customGlobalParamsCallback(globalProperties)
}
}
}