You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tobspr_shapez.io/mod_examples/notification_blocks.js

315 lines
17 KiB

// @ts-nocheck
const METADATA = {
website: "https://tobspr.io",
author: "tobspr",
name: "Mod Example: Notification Blocks",
version: "1",
id: "notification-blocks",
description:
"Adds a new building to the wires layer, 'Notification Blocks' which show a custom notification when they get a truthy signal.",
minimumGameVersion: ">=1.5.0",
};
////////////////////////////////////////////////////////////////////////
// This is the component storing which text the block should show as
// a notification.
class NotificationBlockComponent extends shapez.Component {
static getId() {
return "NotificationBlock";
}
static getSchema() {
// Here you define which properties should be saved to the savegame
// and get automatically restored
return {
notificationText: shapez.types.string,
lastStoredInput: shapez.types.bool,
};
}
constructor() {
super();
this.notificationText = "Test";
this.lastStoredInput = false;
}
}
////////////////////////////////////////////////////////////////////////
// The game system to trigger notifications when the signal changes
class NotificationBlocksSystem extends shapez.GameSystemWithFilter {
constructor(root) {
// By specifying the list of components, `this.allEntities` will only
// contain entities which have *all* of the specified components
super(root, [NotificationBlockComponent]);
// Ask for a notification text once an entity is placed
this.root.signals.entityManuallyPlaced.add(entity => {
const editorHud = this.root.hud.parts.notificationBlockEdit;
if (editorHud) {
editorHud.editNotificationText(entity, { deleteOnCancel: true });
}
});
}
update() {
if (!this.root.gameInitialized) {
// Do not start updating before the wires network was
// computed to avoid dispatching all notifications
return;
}
// Go over all notification blocks and check if the signal changed
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
// Compute if the bottom pin currently has a truthy input
const pinsComp = entity.components.WiredPins;
const network = pinsComp.slots[0].linkedNetwork;
let currentInput = false;
if (network && network.hasValue()) {
const value = network.currentValue;
if (value && shapez.isTruthyItem(value)) {
currentInput = true;
}
}
// If the value changed, show the notification if its truthy
const notificationComp = entity.components.NotificationBlock;
if (currentInput !== notificationComp.lastStoredInput) {
notificationComp.lastStoredInput = currentInput;
if (currentInput) {
this.root.hud.signals.notification.dispatch(
notificationComp.notificationText,
shapez.enumNotificationType.info
);
}
}
}
}
}
////////////////////////////////////////////////////////////////////////
// The actual notification block building
class MetaNotificationBlockBuilding extends shapez.ModMetaBuilding {
constructor() {
super("notification_block");
}
static getAllVariantCombinations() {
return [
{
variant: shapez.defaultBuildingVariant,
name: "Notification Block",
description: "Shows a predefined notification on screen when receiving a truthy signal",
regularImageBase64: RESOURCES["notification_block.png"],
blueprintImageBase64: RESOURCES["notification_block.png"],
tutorialImageBase64: RESOURCES["notification_block.png"],
},
];
}
getSilhouetteColor() {
return "#daff89";
}
getIsUnlocked(root) {
return root.hubGoals.isRewardUnlocked(shapez.enumHubGoalRewards.reward_wires_painter_and_levers);
}
getLayer() {
return "wires";
}
getDimensions() {
return new shapez.Vector(1, 1);
}
getRenderPins() {
// Do not show pin overlays since it would hide our building icon
return false;
}
setupEntityComponents(entity) {
// Accept logical input from the bottom
entity.addComponent(
new shapez.WiredPinsComponent({
slots: [
{
pos: new shapez.Vector(0, 0),
direction: shapez.enumDirection.bottom,
type: shapez.enumPinSlotType.logicalAcceptor,
},
],
})
);
// Add your notification component to identify the building as a notification block
entity.addComponent(new NotificationBlockComponent());
}
}
////////////////////////////////////////////////////////////////////////
// HUD Component to be able to edit notification blocks by clicking them
class HUDNotificationBlockEdit extends shapez.BaseHUDPart {
initialize() {
this.root.camera.downPreHandler.add(this.downPreHandler, this);
}
/**
* @param {Vector} pos
* @param {enumMouseButton} button
*/
downPreHandler(pos, button) {
if (this.root.currentLayer !== "wires") {
return;
}
const tile = this.root.camera.screenToWorld(pos).toTileSpace();
const contents = this.root.map.getLayerContentXY(tile.x, tile.y, "wires");
if (contents) {
const notificationComp = contents.components.NotificationBlock;
if (notificationComp) {
if (button === shapez.enumMouseButton.left) {
this.editNotificationText(contents, {
deleteOnCancel: false,
});
return shapez.STOP_PROPAGATION;
}
}
}
}
/**
* Asks the player to enter a notification text
* @param {Entity} entity
* @param {object} param0
* @param {boolean=} param0.deleteOnCancel
*/
editNotificationText(entity, { deleteOnCancel = true }) {
const notificationComp = entity.components.NotificationBlock;
if (!notificationComp) {
return;
}
// save the uid because it could get stale
const uid = entity.uid;
// create an input field to query the text
const textInput = new shapez.FormElementInput({
id: "notificationText",
placeholder: "",
defaultValue: notificationComp.notificationText,
validator: val => val.length > 0,
});
// create the dialog & show it
const dialog = new shapez.DialogWithForm({
app: this.root.app,
title: shapez.T.mods.notificationBlocks.dialogTitle,
desc: shapez.T.mods.notificationBlocks.enterNotificationText,
formElements: [textInput],
buttons: ["cancel:bad:escape", "ok:good:enter"],
closeButton: false,
});
this.root.hud.parts.dialogs.internalShowDialog(dialog);
// When confirmed, set the text
dialog.buttonSignals.ok.add(() => {
if (!this.root || !this.root.entityMgr) {
// Game got stopped
return;
}
const entityRef = this.root.entityMgr.findByUid(uid, false);
if (!entityRef) {
// outdated
return;
}
const notificationComp = entityRef.components.NotificationBlock;
if (!notificationComp) {
// no longer interesting
return;
}
// set the text
notificationComp.notificationText = textInput.getValue();
});
// When cancelled, destroy the entity again
if (deleteOnCancel) {
dialog.buttonSignals.cancel.add(() => {
if (!this.root || !this.root.entityMgr) {
// Game got stopped
return;
}
const entityRef = this.root.entityMgr.findByUid(uid, false);
if (!entityRef) {
// outdated
return;
}
const notificationComp = entityRef.components.NotificationBlock;
if (!notificationComp) {
// no longer interesting
return;
}
this.root.logic.tryDeleteBuilding(entityRef);
});
}
}
}
////////////////////////////////////////////////////////////////////////
// The actual mod logic
class Mod extends shapez.Mod {
init() {
// Register the component
this.modInterface.registerComponent(NotificationBlockComponent);
// Register the new building
this.modInterface.registerNewBuilding({
metaClass: MetaNotificationBlockBuilding,
buildingIconBase64: RESOURCES["notification_block.png"],
});
// Add it to the regular toolbar
this.modInterface.addNewBuildingToToolbar({
toolbar: "wires",
location: "secondary",
metaClass: MetaNotificationBlockBuilding,
});
// Register our game system so we can dispatch the notifications
this.modInterface.registerGameSystem({
id: "notificationBlocks",
systemClass: NotificationBlocksSystem,
before: "constantSignal",
});
// Register our hud element to be able to edit the notification texts
this.modInterface.registerHudElement("notificationBlockEdit", HUDNotificationBlockEdit);
// This mod also supports translations
this.modInterface.registerTranslations("en", {
mods: {
notificationBlocks: {
enterNotificationText:
"Enter the notification text to show once the signal switches from 0 to 1:",
},
},
});
}
}
const RESOURCES = {
"notification_block.png":
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMAAAADACAYAAABS3GwHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA4VpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDcuMS1jMDAwIDc5LmRhYmFjYmIsIDIwMjEvMDQvMTQtMDA6Mzk6NDQgICAgICAgICI+IDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bXBNTTpPcmlnaW5hbERvY3VtZW50SUQ9InhtcC5kaWQ6NGQwZTY5MmYtOTRlNy00MDQyLWFjY2ItNmU3OGEzMGU1N2ZjIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjAyQTMyQTVGNzA1RTExRUNBQ0EzQTZCNEYxQjM5MkFBIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjAyQTMyQ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