1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-13 18:21:51 +00:00
tobspr_shapez.io/src/js/mods/mod_interface.js

117 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-01-13 20:20:42 +00:00
/* typehints:start */
import { Application } from "../application";
import { ModLoader } from "./modloader";
/* typehints:end */
import { createLogger } from "../core/logging";
import { AtlasSprite, SpriteAtlasLink } from "../core/sprites";
import { Mod } from "./mod";
2022-01-13 20:45:09 +00:00
import { enumShortcodeToSubShape, enumSubShape, enumSubShapeToShortcode } from "../game/shape_definition";
2022-01-13 20:20:42 +00:00
const LOG = createLogger("mod-interface");
2022-01-13 20:45:09 +00:00
/**
* @type {Object<string, (distanceToOriginInChunks: number) => number>}
*/
export const MODS_ADDITIONAL_SHAPE_MAP_WEIGHTS = {};
/**
* @typedef {{
* context: CanvasRenderingContext2D,
* quadrantSize: number,
* layerScale: number,
* }} SubShapeDrawOptions
*/
/**
* @type {Object<string, (options: SubShapeDrawOptions) => void>}
*/
export const MODS_ADDITIONAL_SUB_SHAPE_DRAWERS = {};
2022-01-13 20:20:42 +00:00
export class ModInterface {
/**
*
* @param {ModLoader} modLoader
* @param {Mod} mod
*/
constructor(modLoader, mod) {
/**
* @param {Application} app
*/
this.app = undefined;
this.modLoader = modLoader;
this.mod = mod;
}
registerCss(cssString) {
const element = document.createElement("style");
element.textContent = cssString;
element.setAttribute("data-mod-id", this.mod.metadata.id);
element.setAttribute("data-mod-name", this.mod.metadata.name);
document.head.appendChild(element);
}
registerSprite(spriteId, base64string) {
assert(base64string.startsWith("data:image"));
const img = new Image();
img.src = base64string;
const sprite = new AtlasSprite(spriteId);
const link = new SpriteAtlasLink({
w: img.width,
h: img.height,
atlas: img,
packOffsetX: 0,
packOffsetY: 0,
packedW: img.width,
packedH: img.height,
packedX: 0,
packedY: 0,
});
sprite.linksByResolution["0.25"] = link;
sprite.linksByResolution["0.5"] = link;
sprite.linksByResolution["0.75"] = link;
// @ts-ignore
sprite.modSource = this.mod;
const oldSprite = this.modLoader.lazySprites.get(spriteId);
if (oldSprite) {
LOG.error(
"Sprite '" +
spriteId +
"' is provided twice, once by mod '" +
// @ts-ignore
oldSprite.modSource.metadata.name +
"' and once by mod '" +
this.mod.metadata.name +
"'. This could cause artifacts."
);
}
this.modLoader.lazySprites.set(spriteId, sprite);
}
2022-01-13 20:45:09 +00:00
/**
*
* @param {object} param0
* @param {string} param0.id
* @param {string} param0.shortCode
* @param {(distanceToOriginInChunks: number) => number} param0.weightComputation
* @param {(options: SubShapeDrawOptions) => void} param0.shapeDrawer
*/
registerSubShapeType({ id, shortCode, weightComputation, shapeDrawer }) {
if (shortCode.length !== 1) {
throw new Error("Bad short code: " + shortCode);
}
enumSubShape[id] = id;
enumSubShapeToShortcode[id] = shortCode;
enumShortcodeToSubShape[shortCode] = id;
MODS_ADDITIONAL_SHAPE_MAP_WEIGHTS[id] = weightComputation;
MODS_ADDITIONAL_SUB_SHAPE_DRAWERS[id] = shapeDrawer;
}
2022-01-13 20:20:42 +00:00
}