From 82aaf7f037344bdadc0ae3b0cf2d861a093e161f Mon Sep 17 00:00:00 2001 From: tobspr Date: Mon, 7 Dec 2020 12:47:19 +0100 Subject: [PATCH] Add ability to edit constant signals, bump version --- src/js/changelog.js | 5 +++ src/js/game/hud/hud.js | 2 + src/js/game/hud/parts/constant_signal_edit.js | 33 ++++++++++++++ src/js/game/systems/constant_signal.js | 44 +++++++++++-------- version | 2 +- 5 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 src/js/game/hud/parts/constant_signal_edit.js diff --git a/src/js/changelog.js b/src/js/changelog.js index a5b7e07e..6cc50993 100644 --- a/src/js/changelog.js +++ b/src/js/changelog.js @@ -1,4 +1,9 @@ export const CHANGELOG = [ + { + version: "1.2.2", + date: "07.12.2020", + entries: ["Added the ability to edit constant signals by left clicking them"], + }, { version: "1.2.1", date: "31.10.2020", diff --git a/src/js/game/hud/hud.js b/src/js/game/hud/hud.js index 5f1bd226..d64f96a8 100644 --- a/src/js/game/hud/hud.js +++ b/src/js/game/hud/hud.js @@ -48,6 +48,7 @@ import { HUDBetaOverlay } from "./parts/beta_overlay"; import { HUDStandaloneAdvantages } from "./parts/standalone_advantages"; import { HUDCatMemes } from "./parts/cat_memes"; import { HUDTutorialVideoOffer } from "./parts/tutorial_video_offer"; +import { HUDConstantSignalEdit } from "./parts/constant_signal_edit"; export class GameHUD { /** @@ -86,6 +87,7 @@ export class GameHUD { waypoints: new HUDWaypoints(this.root), wireInfo: new HUDWireInfo(this.root), leverToggle: new HUDLeverToggle(this.root), + constantSignalEdit: new HUDConstantSignalEdit(this.root), // Must always exist pinnedShapes: new HUDPinnedShapes(this.root), diff --git a/src/js/game/hud/parts/constant_signal_edit.js b/src/js/game/hud/parts/constant_signal_edit.js new file mode 100644 index 00000000..b030ac52 --- /dev/null +++ b/src/js/game/hud/parts/constant_signal_edit.js @@ -0,0 +1,33 @@ +import { STOP_PROPAGATION } from "../../../core/signal"; +import { Vector } from "../../../core/vector"; +import { enumMouseButton } from "../../camera"; +import { BaseHUDPart } from "../base_hud_part"; + +export class HUDConstantSignalEdit extends BaseHUDPart { + initialize() { + this.root.camera.downPreHandler.add(this.downPreHandler, this); + } + + /** + * @param {Vector} pos + * @param {enumMouseButton} button + */ + downPreHandler(pos, button) { + const tile = this.root.camera.screenToWorld(pos).toTileSpace(); + const contents = this.root.map.getLayerContentXY(tile.x, tile.y, "wires"); + if (contents) { + const constantComp = contents.components.ConstantSignal; + if (constantComp) { + if (button === enumMouseButton.left) { + this.root.systemMgr.systems.constantSignal.editConstantSignal(contents, { + deleteOnCancel: false, + }); + return STOP_PROPAGATION; + } else if (button === enumMouseButton.right) { + this.root.logic.tryDeleteBuilding(contents); + return STOP_PROPAGATION; + } + } + } + } +} diff --git a/src/js/game/systems/constant_signal.js b/src/js/game/systems/constant_signal.js index aaf31a19..d698c1d5 100644 --- a/src/js/game/systems/constant_signal.js +++ b/src/js/game/systems/constant_signal.js @@ -17,7 +17,9 @@ export class ConstantSignalSystem extends GameSystemWithFilter { constructor(root) { super(root, [ConstantSignalComponent]); - this.root.signals.entityManuallyPlaced.add(this.querySigalValue, this); + this.root.signals.entityManuallyPlaced.add(entity => + this.editConstantSignal(entity, { deleteOnCancel: true }) + ); } update() { @@ -33,8 +35,10 @@ export class ConstantSignalSystem extends GameSystemWithFilter { /** * Asks the entity to enter a valid signal code * @param {Entity} entity + * @param {object} param0 + * @param {boolean=} param0.deleteOnCancel */ - querySigalValue(entity) { + editConstantSignal(entity, { deleteOnCancel = true }) { if (!entity.components.ConstantSignal) { return; } @@ -110,26 +114,28 @@ export class ConstantSignalSystem extends GameSystemWithFilter { dialog.valueChosen.add(closeHandler); // When cancelled, destroy the entity again - dialog.buttonSignals.cancel.add(() => { - if (!this.root || !this.root.entityMgr) { - // Game got stopped - return; - } + 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 entityRef = this.root.entityMgr.findByUid(uid, false); + if (!entityRef) { + // outdated + return; + } - const constantComp = entityRef.components.ConstantSignal; - if (!constantComp) { - // no longer interesting - return; - } + const constantComp = entityRef.components.ConstantSignal; + if (!constantComp) { + // no longer interesting + return; + } - this.root.logic.tryDeleteBuilding(entityRef); - }); + this.root.logic.tryDeleteBuilding(entityRef); + }); + } } /** diff --git a/version b/version index cb174d58..d2d61a7e 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.2.1 \ No newline at end of file +1.2.2 \ No newline at end of file