1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/ts/core/loader.ts

160 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-11-18 01:33:12 +00:00
import { makeOffscreenBuffer } from "./buffer_utils";
import { AtlasSprite, BaseSprite, RegularSprite, SpriteAtlasLink } from "./sprites";
import { cachebust } from "./cachebust";
import { createLogger } from "./logging";
export type Application = import("../application").Application;
export type AtlasDefinition = import("./atlas_definitions").AtlasDefinition;
2022-11-18 15:20:54 +00:00
const logger = createLogger("loader");
const missingSpriteIds = {};
2022-11-18 01:33:12 +00:00
class LoaderImpl {
public app = null;
public sprites: Map<string, BaseSprite> = new Map();
public rawImages = [];
constructor() {
}
2022-11-18 15:20:54 +00:00
linkAppAfterBoot(app: Application) {
2022-11-18 01:33:12 +00:00
this.app = app;
this.makeSpriteNotFoundCanvas();
}
/**
* Fetches a given sprite from the cache
* {}
*/
getSpriteInternal(key: string): BaseSprite {
2022-11-18 15:20:54 +00:00
const sprite = this.sprites.get(key);
2022-11-18 01:33:12 +00:00
if (!sprite) {
if (!missingSpriteIds[key]) {
// Only show error once
missingSpriteIds[key] = true;
logger.error("Sprite '" + key + "' not found!");
}
return this.spriteNotFoundSprite;
}
return sprite;
}
/**
* Returns an atlas sprite from the cache
* {}
*/
getSprite(key: string): AtlasSprite {
2022-11-18 15:20:54 +00:00
const sprite = this.getSpriteInternal(key);
2022-11-18 01:33:12 +00:00
assert(sprite instanceof AtlasSprite || sprite === this.spriteNotFoundSprite, "Not an atlas sprite");
return sprite as AtlasSprite);
}
/**
* Returns a regular sprite from the cache
* {}
*/
getRegularSprite(key: string): RegularSprite {
2022-11-18 15:20:54 +00:00
const sprite = this.getSpriteInternal(key);
2022-11-18 01:33:12 +00:00
assert(sprite instanceof RegularSprite || sprite === this.spriteNotFoundSprite, "Not a regular sprite");
return sprite as RegularSprite);
}
/**
*
* {}
*/
internalPreloadImage(key: string, progressHandler: (progress: number) => void): Promise<HTMLImageElement | null> {
return this.app.backgroundResourceLoader
2022-11-18 15:20:54 +00:00
.preloadWithProgress("res/" + key, progress => {
2022-11-18 01:33:12 +00:00
progressHandler(progress);
})
2022-11-18 15:20:54 +00:00
.then(url => {
return new Promise((resolve, reject) => {
const image = new Image();
image.addEventListener("load", () => resolve(image));
image.addEventListener("error", err => reject("Failed to load sprite " + key + ": " + err));
2022-11-18 01:33:12 +00:00
image.src = url;
});
});
}
/**
* Preloads a sprite
* {}
*/
preloadCSSSprite(key: string, progressHandler: (progress: number) => void): Promise<void> {
2022-11-18 15:20:54 +00:00
return this.internalPreloadImage(key, progressHandler).then(image => {
2022-11-18 01:33:12 +00:00
if (key.indexOf("game_misc") >= 0) {
// Allow access to regular sprites
this.sprites.set(key, new RegularSprite(image, image.width, image.height));
}
this.rawImages.push(image);
});
}
/**
* Preloads an atlas
* {}
*/
preloadAtlas(atlas: AtlasDefinition, progressHandler: (progress: number) => void): Promise<void> {
2022-11-18 15:20:54 +00:00
return this.internalPreloadImage(atlas.getFullSourcePath(), progressHandler).then(image => {
2022-11-18 01:33:12 +00:00
// @ts-ignore
image.label = atlas.sourceFileName;
return this.internalParseAtlas(atlas, image);
});
}
2022-11-18 15:20:54 +00:00
internalParseAtlas({ meta: { scale }, sourceData }: AtlasDefinition, loadedImage: HTMLImageElement) {
2022-11-18 01:33:12 +00:00
this.rawImages.push(loadedImage);
2022-11-18 15:20:54 +00:00
for (const spriteName in sourceData) {
const { frame, sourceSize, spriteSourceSize } = sourceData[spriteName];
let sprite = this.sprites.get(spriteName) as AtlasSprite);
2022-11-18 01:33:12 +00:00
if (!sprite) {
sprite = new AtlasSprite(spriteName);
this.sprites.set(spriteName, sprite);
}
if (sprite.frozen) {
continue;
}
2022-11-18 15:20:54 +00:00
const link = new SpriteAtlasLink({
2022-11-18 01:33:12 +00:00
packedX: frame.x,
packedY: frame.y,
packedW: frame.w,
packedH: frame.h,
packOffsetX: spriteSourceSize.x,
packOffsetY: spriteSourceSize.y,
atlas: loadedImage,
w: sourceSize.w,
h: sourceSize.h,
});
sprite.linksByResolution[scale] = link;
}
}
/**
* Makes the canvas which shows the question mark, shown when a sprite was not found
*/
2022-11-18 15:20:54 +00:00
makeSpriteNotFoundCanvas() {
const dims = 128;
const [canvas, context] = makeOffscreenBuffer(dims, dims, {
2022-11-18 01:33:12 +00:00
smooth: false,
label: "not-found-sprite",
});
context.fillStyle = "#f77";
context.fillRect(0, 0, dims, dims);
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "#eee";
context.font = "25px Arial";
context.fillText("???", dims / 2, dims / 2);
// TODO: Not sure why this is set here
// @ts-ignore
canvas.src = "not-found";
2022-11-18 15:20:54 +00:00
const sprite = new AtlasSprite("not-found");
["0.1", "0.25", "0.5", "0.75", "1"].forEach(resolution => {
2022-11-18 01:33:12 +00:00
sprite.linksByResolution[resolution] = new SpriteAtlasLink({
packedX: 0,
packedY: 0,
w: dims,
h: dims,
packOffsetX: 0,
packOffsetY: 0,
packedW: dims,
packedH: dims,
atlas: canvas,
});
});
this.spriteNotFoundSprite = sprite;
}
}
2022-11-18 15:20:54 +00:00
export const Loader = new LoaderImpl();