mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-12-13 18:21:51 +00:00
Make mod loading simpler
This commit is contained in:
parent
41a8d4334c
commit
44c4807d74
@ -3,8 +3,17 @@
|
||||
* If you are interested in adding more logic to the game, you should also check out
|
||||
* the advanced example
|
||||
*/
|
||||
registerMod(() => {
|
||||
class MetaDemoModBuilding extends shapez.ModMetaBuilding {
|
||||
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Add new basic building",
|
||||
version: "1",
|
||||
id: "add-building-basic",
|
||||
description: "Shows how to add a new basic building",
|
||||
};
|
||||
|
||||
class MetaDemoModBuilding extends shapez.ModMetaBuilding {
|
||||
constructor() {
|
||||
super("demoModBuilding");
|
||||
}
|
||||
@ -32,24 +41,9 @@ registerMod(() => {
|
||||
// To get an idea what you can do with the builtin components, have a look
|
||||
// at the builtin buildings in <src/js/game/buildings/>
|
||||
}
|
||||
}
|
||||
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Add new basic building",
|
||||
version: "1",
|
||||
id: "add-building-basic",
|
||||
description: "Shows how to add a new basic building",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Register the new building
|
||||
this.modInterface.registerNewBuilding({
|
||||
@ -64,8 +58,7 @@ registerMod(() => {
|
||||
metaClass: MetaDemoModBuilding,
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
@ -2,15 +2,24 @@
|
||||
* This shows how to add a new extended building (A building which flips shapes).
|
||||
* Be sure to check out the "add_building_basic" example first
|
||||
*/
|
||||
registerMod(() => {
|
||||
// Declare a new type of item processor
|
||||
shapez.enumItemProcessorTypes.flipper = "flipper";
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Add a flipper building",
|
||||
version: "1",
|
||||
id: "add-building-extended",
|
||||
description:
|
||||
"Shows how to add a new building with logic, in this case it flips/mirrors shapez from top to down",
|
||||
};
|
||||
|
||||
// For now, flipper always has the same speed
|
||||
shapez.MOD_ITEM_PROCESSOR_SPEEDS.flipper = () => 10;
|
||||
// Declare a new type of item processor
|
||||
shapez.enumItemProcessorTypes.flipper = "flipper";
|
||||
|
||||
// Declare a handler for the processor so we define the "flip" operation
|
||||
shapez.MOD_ITEM_PROCESSOR_HANDLERS.flipper = function (payload) {
|
||||
// For now, flipper always has the same speed
|
||||
shapez.MOD_ITEM_PROCESSOR_SPEEDS.flipper = () => 10;
|
||||
|
||||
// Declare a handler for the processor so we define the "flip" operation
|
||||
shapez.MOD_ITEM_PROCESSOR_HANDLERS.flipper = function (payload) {
|
||||
const shapeDefinition = payload.items.get(0).definition;
|
||||
|
||||
// Flip bottom with top on a new, cloned item (NEVER modify the incoming item!)
|
||||
@ -32,10 +41,10 @@ registerMod(() => {
|
||||
payload.outItems.push({
|
||||
item: this.root.shapeDefinitionMgr.getShapeItemFromDefinition(newDefinition),
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
// Create the building
|
||||
class MetaModFlipperBuilding extends shapez.ModMetaBuilding {
|
||||
// Create the building
|
||||
class MetaModFlipperBuilding extends shapez.ModMetaBuilding {
|
||||
constructor() {
|
||||
super("modFlipperBuilding");
|
||||
}
|
||||
@ -96,25 +105,9 @@ registerMod(() => {
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Add a flipper building",
|
||||
version: "1",
|
||||
id: "add-building-extended",
|
||||
description:
|
||||
"Shows how to add a new building with logic, in this case it flips/mirrors shapez from top to down",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Register the new building
|
||||
this.modInterface.registerNewBuilding({
|
||||
@ -129,8 +122,7 @@ registerMod(() => {
|
||||
metaClass: MetaModFlipperBuilding,
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
@ -1,25 +1,18 @@
|
||||
/**
|
||||
* This is the minimal structure of a mod
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Base",
|
||||
version: "1",
|
||||
id: "base",
|
||||
description: "The most basic mod",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Start the modding here
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,24 +1,17 @@
|
||||
/**
|
||||
* This shows how to patch existing methods by making belts have a cost
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Patch Methods",
|
||||
version: "1",
|
||||
id: "patch-methods",
|
||||
description:
|
||||
"Shows how to patch existing methods to change the game by making the belts cost shapes",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
description: "Shows how to patch existing methods to change the game by making the belts cost shapes",
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Define our currency
|
||||
const CURRENCY = "CyCyCyCy:--------:CuCuCuCu";
|
||||
@ -90,8 +83,7 @@ registerMod(() => {
|
||||
return result;
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const RESOURCES = {
|
||||
"currency.png":
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
registerMod(() => {
|
||||
class DemoModComponent extends shapez.Component {
|
||||
class DemoModComponent extends shapez.Component {
|
||||
static getId() {
|
||||
return "DemoMod";
|
||||
}
|
||||
@ -15,9 +14,9 @@ registerMod(() => {
|
||||
|
||||
this.magicNumber = magicNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MetaDemoModBuilding extends shapez.MetaBuilding {
|
||||
class MetaDemoModBuilding extends shapez.MetaBuilding {
|
||||
constructor() {
|
||||
super("demoModBuilding");
|
||||
}
|
||||
@ -29,9 +28,9 @@ registerMod(() => {
|
||||
setupEntityComponents(entity) {
|
||||
entity.addComponent(new DemoModComponent(Math.floor(Math.random() * 100.0)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DemoModSystem extends shapez.GameSystemWithFilter {
|
||||
class DemoModSystem extends shapez.GameSystemWithFilter {
|
||||
constructor(root) {
|
||||
super(root, [DemoModComponent]);
|
||||
}
|
||||
@ -78,9 +77,9 @@ registerMod(() => {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return class ModImpl extends shapez.Mod {
|
||||
class Mod extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
@ -104,50 +103,9 @@ registerMod(() => {
|
||||
// }
|
||||
// `);
|
||||
|
||||
// Replace a builtin sprite
|
||||
["red", "green", "blue", "yellow", "purple", "cyan", "white"].forEach(color => {
|
||||
this.modInterface.registerSprite(
|
||||
"sprites/colors/" + color + ".png",
|
||||
RESOURCES[color + ".png"]
|
||||
);
|
||||
});
|
||||
|
||||
// Add an atlas
|
||||
this.modInterface.registerAtlas(RESOURCES["demoAtlas.png"], RESOURCES["demoAtlas.json"]);
|
||||
|
||||
// Add a new type of sub shape ("Line", short code "L")
|
||||
this.modInterface.registerSubShapeType({
|
||||
id: "line",
|
||||
shortCode: "L",
|
||||
weightComputation: distanceToOriginInChunks =>
|
||||
Math.round(20 + Math.max(Math.min(distanceToOriginInChunks, 30), 0)),
|
||||
|
||||
draw: ({ context, quadrantSize, layerScale }) => {
|
||||
const quadrantHalfSize = quadrantSize / 2;
|
||||
context.beginPath();
|
||||
context.moveTo(-quadrantHalfSize, quadrantHalfSize);
|
||||
context.arc(
|
||||
-quadrantHalfSize,
|
||||
quadrantHalfSize,
|
||||
quadrantSize * layerScale,
|
||||
-Math.PI * 0.25,
|
||||
0
|
||||
);
|
||||
context.closePath();
|
||||
},
|
||||
});
|
||||
|
||||
// Modify the theme colors
|
||||
this.signals.preprocessTheme.add(({ theme }) => {
|
||||
theme.map.background = "#eee";
|
||||
theme.items.outline = "#000";
|
||||
});
|
||||
|
||||
// Modify the goal of the first level
|
||||
this.signals.modifyLevelDefinitions.add(definitions => {
|
||||
definitions[0].shape = "LuCuLuCu";
|
||||
});
|
||||
|
||||
this.modInterface.registerTranslations("en", {
|
||||
ingame: {
|
||||
interactiveTutorial: {
|
||||
@ -236,8 +194,7 @@ registerMod(() => {
|
||||
|
||||
`);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// @notice: Later this part will be autogenerated
|
||||
|
||||
@ -1,23 +1,16 @@
|
||||
/**
|
||||
* This example shows how to add custom css
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Add custom CSS",
|
||||
version: "1",
|
||||
id: "custom-css",
|
||||
description: "Shows how to add custom css",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Notice that, since the UI is scaled dynamically, every pixel value
|
||||
// should be wrapped in '$scaled()' (see below)
|
||||
@ -40,8 +33,7 @@ registerMod(() => {
|
||||
}
|
||||
`);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const RESOURCES = {
|
||||
"cat.png":
|
||||
|
||||
@ -1,23 +1,17 @@
|
||||
/**
|
||||
* This shows how to add custom sub shapes
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Custom Sub Shapes",
|
||||
version: "1",
|
||||
id: "custom-sub-shapes",
|
||||
description: "Shows how to add custom sub shapes",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Add a new type of sub shape ("Line", short code "L")
|
||||
this.modInterface.registerSubShapeType({
|
||||
@ -49,5 +43,4 @@ registerMod(() => {
|
||||
definitions[0].shape = "LuLuLuLu";
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,23 +1,16 @@
|
||||
/**
|
||||
* This example shows how to add a new theme to the game
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Custom Game Theme",
|
||||
version: "1",
|
||||
id: "custom-theme",
|
||||
description: "Shows how to add a custom game theme",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
this.modInterface.registerGameTheme({
|
||||
id: "my-theme",
|
||||
@ -25,8 +18,7 @@ registerMod(() => {
|
||||
theme: RESOURCES["my-theme.json"],
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const RESOURCES = {
|
||||
"my-theme.json": {
|
||||
|
||||
@ -1,23 +1,17 @@
|
||||
/**
|
||||
* This shows how to modify an existing building
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Modify existing building",
|
||||
version: "1",
|
||||
id: "modify-existing-building",
|
||||
description: "Shows how to modify an existing building",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Make Rotator always unlocked
|
||||
this.modInterface.replaceMethod(shapez.MetaRotaterBuilding, "getIsUnlocked", function () {
|
||||
@ -32,5 +26,4 @@ registerMod(() => {
|
||||
return [["Awesomeness", 5]];
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@ -2,23 +2,16 @@
|
||||
* This example shows how to modify the builtin themes. If you want to create your own theme,
|
||||
* be sure to check out the "custom_theme" example
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Modify Builtin Themes",
|
||||
version: "1",
|
||||
id: "modify-theme",
|
||||
description: "Shows how to modify builtin themes",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
shapez.THEMES.light.map.background = "#eee";
|
||||
shapez.THEMES.light.items.outline = "#000";
|
||||
@ -26,5 +19,4 @@ registerMod(() => {
|
||||
shapez.THEMES.dark.map.background = "#245";
|
||||
shapez.THEMES.dark.items.outline = "#fff";
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@ -2,34 +2,24 @@
|
||||
* This example shows how to replace builtin sprites, in this case
|
||||
* the color sprites
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Replace builtin sprites",
|
||||
version: "1",
|
||||
id: "replace-builtin-sprites",
|
||||
description: "Shows how to replace builtin sprites",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Replace a builtin sprite
|
||||
["red", "green", "blue", "yellow", "purple", "cyan", "white"].forEach(color => {
|
||||
this.modInterface.registerSprite(
|
||||
"sprites/colors/" + color + ".png",
|
||||
RESOURCES[color + ".png"]
|
||||
);
|
||||
this.modInterface.registerSprite("sprites/colors/" + color + ".png", RESOURCES[color + ".png"]);
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
@ -1,23 +1,17 @@
|
||||
/**
|
||||
* Shows to add new translations
|
||||
*/
|
||||
registerMod(() => {
|
||||
return class ModImpl extends shapez.Mod {
|
||||
constructor(app, modLoader) {
|
||||
super(
|
||||
app,
|
||||
{
|
||||
|
||||
const METADATA = {
|
||||
website: "https://tobspr.io",
|
||||
author: "tobspr",
|
||||
name: "Mod Example: Translations",
|
||||
version: "1",
|
||||
id: "translations",
|
||||
description: "Shows how to add and modify translations",
|
||||
},
|
||||
modLoader
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
class Mod extends shapez.Mod {
|
||||
init() {
|
||||
// Replace an existing translation in the english language
|
||||
this.modInterface.registerTranslations("en", {
|
||||
@ -67,5 +61,4 @@ registerMod(() => {
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
4
src/js/globals.d.ts
vendored
4
src/js/globals.d.ts
vendored
@ -26,8 +26,6 @@ declare const shapez: any;
|
||||
|
||||
declare const ipcRenderer: any;
|
||||
|
||||
declare const registerMod: any;
|
||||
|
||||
// Polyfills
|
||||
declare interface String {
|
||||
replaceAll(search: string, replacement: string): string;
|
||||
@ -98,7 +96,7 @@ declare interface Window {
|
||||
cpmstarAPI: any;
|
||||
|
||||
// Mods
|
||||
registerMod: any;
|
||||
$shapez_registerMod: any;
|
||||
anyModLoaded: any;
|
||||
|
||||
shapez: any;
|
||||
|
||||
@ -7,28 +7,22 @@ import { MOD_SIGNALS } from "./mod_signals";
|
||||
|
||||
export class Mod {
|
||||
/**
|
||||
*
|
||||
* @param {Application} app
|
||||
* @param {object} metadata
|
||||
* @param {string} metadata.name
|
||||
* @param {string} metadata.version
|
||||
* @param {string} metadata.author
|
||||
* @param {string} metadata.website
|
||||
* @param {string} metadata.description
|
||||
* @param {string} metadata.id
|
||||
*
|
||||
* @param {ModLoader} modLoader
|
||||
* @param {import("./modloader").ModMetadata} meta
|
||||
*/
|
||||
constructor(app, metadata, modLoader) {
|
||||
constructor(app, modLoader, meta) {
|
||||
this.app = app;
|
||||
this.metadata = metadata;
|
||||
this.modLoader = modLoader;
|
||||
this.metadata = meta;
|
||||
|
||||
this.signals = MOD_SIGNALS;
|
||||
this.modInterface = modLoader.modInterface;
|
||||
}
|
||||
|
||||
init() {}
|
||||
init() {
|
||||
// to be overridden
|
||||
}
|
||||
|
||||
get dialogs() {
|
||||
return this.modInterface.dialogs;
|
||||
|
||||
@ -9,6 +9,17 @@ import { MOD_SIGNALS } from "./mod_signals";
|
||||
|
||||
const LOG = createLogger("mods");
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* name: string;
|
||||
* version: string;
|
||||
* author: string;
|
||||
* website: string;
|
||||
* description: string;
|
||||
* id: string;
|
||||
* }} ModMetadata
|
||||
*/
|
||||
|
||||
export class ModLoader {
|
||||
constructor() {
|
||||
LOG.log("modloader created");
|
||||
@ -23,7 +34,7 @@ export class ModLoader {
|
||||
|
||||
this.modInterface = new ModInterface(this);
|
||||
|
||||
/** @type {((Object) => (new (Application, ModLoader) => Mod))[]} */
|
||||
/** @type {({ meta: ModMetadata, modClass: typeof Mod})[]} */
|
||||
this.modLoadQueue = [];
|
||||
|
||||
this.initialized = false;
|
||||
@ -73,8 +84,11 @@ export class ModLoader {
|
||||
|
||||
this.exposeExports();
|
||||
|
||||
window.registerMod = mod => {
|
||||
this.modLoadQueue.push(mod);
|
||||
window.$shapez_registerMod = (modClass, meta) => {
|
||||
this.modLoadQueue.push({
|
||||
modClass,
|
||||
meta,
|
||||
});
|
||||
};
|
||||
|
||||
if (G_IS_STANDALONE || G_IS_DEV) {
|
||||
@ -103,6 +117,7 @@ export class ModLoader {
|
||||
|
||||
mods.forEach(modCode => {
|
||||
try {
|
||||
modCode += "\n;window.$shapez_registerMod(Mod, METADATA);";
|
||||
const func = new Function(modCode);
|
||||
func();
|
||||
} catch (ex) {
|
||||
@ -116,9 +131,9 @@ export class ModLoader {
|
||||
}
|
||||
|
||||
this.initialized = true;
|
||||
this.modLoadQueue.forEach(modClass => {
|
||||
this.modLoadQueue.forEach(({ modClass, meta }) => {
|
||||
try {
|
||||
const mod = new (modClass())(this.app, this);
|
||||
const mod = new modClass(this.app, this, meta);
|
||||
mod.init();
|
||||
this.mods.push(mod);
|
||||
} catch (ex) {
|
||||
@ -128,7 +143,7 @@ export class ModLoader {
|
||||
});
|
||||
this.modLoadQueue = [];
|
||||
|
||||
delete window.registerMod;
|
||||
delete window.$shapez_registerMod;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user