-
Notifications
You must be signed in to change notification settings - Fork 34
/
effectFramesGenerator.ts
89 lines (77 loc) · 4.06 KB
/
effectFramesGenerator.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
78
79
80
81
82
83
84
85
86
87
88
89
import {DatManager} from "./modules/datFile/datManager";
import {OtbManager} from "./modules/otbFile/otbManager";
import {SpriteManager} from "./modules/sprFile/spriteManager";
import {ImageGenerator} from "./modules/imageGenerator/imageGenerator";
import {DatThingCategory, GameFeature} from "./modules/constants/const";
import {WebsiteImageGeneratorBase} from "./websiteImageGeneratorBase";
class EffectFramesGenerator extends WebsiteImageGeneratorBase {
private forceEnableExtendedSpritesCheckbox: HTMLInputElement;
private enableTransparencyCheckbox: HTMLInputElement;
init() {
this.otbRequired = false;
super.init();
this.forceEnableExtendedSpritesCheckbox = <HTMLInputElement>document.getElementById('forceEnableExtendedSprites');
this.enableTransparencyCheckbox = <HTMLInputElement>document.getElementById('enableTransparency');
}
afterSetClientVersion() {
if (this.forceEnableExtendedSpritesCheckbox.checked) {
this.client.enableFeature(GameFeature.GameSpritesU32);
}
if (this.enableTransparencyCheckbox.checked) {
this.client.enableFeature(GameFeature.GameSpritesAlphaChannel);
}
}
startImageGenerator(imageGenerator: ImageGenerator, otbManager: OtbManager, datManager: DatManager, spriteManager: SpriteManager, zip) {
this.generateEffectImage(imageGenerator, datManager, zip, 0);
}
generateEffectImage(imageGenerator: ImageGenerator, datManager: DatManager, zip, effectId: number) {
const self = this;
this.progressValue(effectId, datManager.getCategory(DatThingCategory.ThingCategoryEffect).length);
if (effectId > datManager.getCategory(DatThingCategory.ThingCategoryEffect).length) {
this.progressText('Packing images to ZIP file, please wait (it may take a while)');
zip.generateAsync({type: "blob"}).then(function (blob: Blob) {
console.log('zip size', blob.size);
self.progressText('ZIP generated, it should start download now.');
self.downloadBlob('effects.zip', blob);
});
return;
}
let effectThingType = this.datManager.getEffect(effectId);
if (!effectThingType) {
console.log('dat ID not found in dat file', effectId);
setTimeout(function () {
self.generateEffectImage(imageGenerator, datManager, zip, effectId + 1);
}, 1);
return;
}
const effectSprites = imageGenerator.generateEffectImagesById(effectId);
if (!effectSprites || effectSprites.length == 0) {
setTimeout(function () {
self.generateEffectImage(imageGenerator, datManager, zip, effectId + 1);
}, 1);
return;
}
const firstEffectSprite = effectSprites[0];
const canvas = <HTMLCanvasElement>document.createElement('canvas');
canvas.width = firstEffectSprite.getWidth() * effectSprites.length;
canvas.height = firstEffectSprite.getHeight();
document.getElementsByTagName('body')[0].appendChild(canvas);
const ctx = canvas.getContext("2d");
for (let animationFrame = 0; animationFrame < effectSprites.length; animationFrame++) {
const palette = ctx.getImageData(firstEffectSprite.getWidth() * animationFrame, 0, firstEffectSprite.getWidth(), firstEffectSprite.getHeight());
const effectSprite = effectSprites[animationFrame];
palette.data.set(new Uint8ClampedArray(effectSprite.getPixels().m_buffer.buffer));
ctx.putImageData(palette, firstEffectSprite.getWidth() * animationFrame, 0);
}
const callback = function (blob) {
canvas.remove();
zip.file('effects/' + effectId + '_' + effectSprites.length + '.png', blob);
setTimeout(function () {
self.generateEffectImage(imageGenerator, datManager, zip, effectId + 1);
}, 1);
};
canvas.toBlob(callback);
}
}
const effectFramesGenerator = new EffectFramesGenerator();
effectFramesGenerator.init();