1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/js/game/hud/parts/wires_overlay.js

192 lines
6.9 KiB
JavaScript
Raw Normal View History

2020-08-29 20:52:52 +00:00
import { makeOffscreenBuffer } from "../../../core/buffer_utils";
import { globalConfig } from "../../../core/config";
import { DrawParameters } from "../../../core/draw_parameters";
import { Loader } from "../../../core/loader";
import { lerp } from "../../../core/utils";
2021-06-25 17:45:47 +00:00
import { enumDirectionToAngle, Vector } from "../../../core/vector";
2020-09-28 12:45:53 +00:00
import { SOUNDS } from "../../../platform/sound";
2021-06-25 17:45:47 +00:00
import { enumPinSlotType } from "../../components/wired_pins";
2020-09-28 12:45:53 +00:00
import { KEYMAPPINGS } from "../../key_action_mapper";
2020-09-24 15:14:15 +00:00
import { enumHubGoalRewards } from "../../tutorial_goals";
2020-09-28 12:45:53 +00:00
import { BaseHUDPart } from "../base_hud_part";
2020-08-29 20:52:52 +00:00
2020-09-28 12:45:53 +00:00
const copy = require("clipboard-copy");
2020-08-29 20:52:52 +00:00
const wiresBackgroundDpi = 4;
export class HUDWiresOverlay extends BaseHUDPart {
createElements(parent) {}
initialize() {
// Probably not the best location, but the one which makes most sense
this.root.keyMapper.getBinding(KEYMAPPINGS.ingame.switchLayers).add(this.switchLayers, this);
2020-09-28 12:45:53 +00:00
this.root.keyMapper.getBinding(KEYMAPPINGS.placement.copyWireValue).add(this.copyWireValue, this);
2020-08-29 20:52:52 +00:00
this.generateTilePattern();
this.currentAlpha = 0.0;
}
/**
* Switches between layers
*/
switchLayers() {
Puzzle DLC (#1172) * Puzzle mode (#1135) * Add mode button to main menu * [WIP] Add mode menu. Add factory-based gameMode creation * Add savefile migration, serialize, deserialize * Add hidden HUD elements, zone, and zoom, boundary constraints * Clean up lint issues * Add building, HUD exclusion, building exclusion, and refactor - [WIP] Add ConstantProducer building that combines ConstantSignal and ItemProducer functionality. Currently using temp assets. - Add pre-placement check to the zone - Use Rectangles for zone and boundary - Simplify zone drawing - Account for exclusion in savegame data - [WIP] Add puzzle play and edit buttons in puzzle mode menu * [WIP] Add building, component, and systems for producing and accepting user-specified items and checking goal criteria * Add ingame puzzle mode UI elements - Add minimal menus in puzzle mode for back, next navigation - Add lower menu for changing zone dimenensions Co-authored-by: Greg Considine <gconsidine@users.noreply.github.com> * Performance optimizations (#1154) * 1.3.1 preparations * Minor fixes, update translations * Fix achievements not working * Lots of belt optimizations, ~15% performance boost * Puzzle mode, part 1 * Puzzle mode, part 2 * Fix missing import * Puzzle mode, part 3 * Fix typo * Puzzle mode, part 4 * Puzzle Mode fixes: Correct zone restrictions and more (#1155) * Hide Puzzle Editor Controls in regular game mode, fix typo * Disallow shrinking zone if there are buildings * Fix multi-tile buildings for shrinking * Puzzle mode, Refactor hud * Puzzle mode * Fixed typo in latest puzzle commit (#1156) * Allow completing puzzles * Puzzle mode, almost done * Bump version to 1.4.0 * Fixes * [puzzle] Prevent pipette cheats (miners, emitters) (#1158) * Puzzle mode, almost done * Allow clearing belts with 'B' * Multiple users for the puzzle dlc * Bump api key * Minor adjustments * Update * Minor fixes * Fix throughput * Fix belts * Minor puzzle adjustments * New difficulty * Minor puzzle improvements * Fix belt path * Update translations * Added a button to return to the menu after a puzzle is completed (#1170) * added another button to return to the menu * improved menu return * fixed continue button to not go back to menu * [Puzzle] Added ability to lock buildings in the puzzle editor! (#1164) * initial test * tried to get it to work * added icon * added test exclusion * reverted css * completed flow for building locking * added lock option * finalized look and changed locked building to same sprite * removed unused art * added clearing every goal acceptor on lock to prevent creating impossible puzzles * heavily improved validation and prevented autocompletion * validation only checks every 100 ticks to improve performance * validation only checks every 100 ticks to improve performance * removed clearing goal acceptors as it isn't needed because of validation * Add soundtrack, puzzle dlc fixes Co-authored-by: Greg Considine <gconsidine@users.noreply.github.com> Co-authored-by: dengr1065 <dengr1065@gmail.com> Co-authored-by: Sense101 <67970865+Sense101@users.noreply.github.com>
2021-05-23 14:32:05 +00:00
if (!this.root.gameMode.getSupportsWires()) {
return;
}
2020-08-29 20:52:52 +00:00
if (this.root.currentLayer === "regular") {
2020-09-29 09:14:27 +00:00
if (
this.root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_wires_painter_and_levers) ||
(G_IS_DEV && globalConfig.debug.allBuildingsUnlocked)
) {
2020-09-24 15:14:15 +00:00
this.root.currentLayer = "wires";
}
2020-08-29 20:52:52 +00:00
} else {
this.root.currentLayer = "regular";
}
this.root.signals.editModeChanged.dispatch(this.root.currentLayer);
}
/**
* Generates the background pattern for the wires overlay
*/
generateTilePattern() {
const overlayTile = Loader.getSprite("sprites/wires/overlay_tile.png");
const dims = globalConfig.tileSize * wiresBackgroundDpi;
const [canvas, context] = makeOffscreenBuffer(dims, dims, {
smooth: false,
reusable: false,
label: "wires-tile-pattern",
});
context.clearRect(0, 0, dims, dims);
overlayTile.draw(context, 0, 0, dims, dims);
this.tilePatternCanvas = canvas;
}
update() {
const desiredAlpha = this.root.currentLayer === "wires" ? 1.0 : 0.0;
2020-09-28 12:47:23 +00:00
// On low performance, skip the fade
if (this.root.entityMgr.entities.length > 5000 || this.root.dynamicTickrate.averageFps < 50) {
this.currentAlpha = desiredAlpha;
} else {
this.currentAlpha = lerp(this.currentAlpha, desiredAlpha, 0.12);
}
2020-08-29 20:52:52 +00:00
}
2020-09-28 12:45:53 +00:00
/**
* Copies the wires value below the cursor
*/
copyWireValue() {
if (this.root.currentLayer !== "wires") {
return;
}
const mousePos = this.root.app.mousePosition;
if (!mousePos) {
return;
}
const tile = this.root.camera.screenToWorld(mousePos).toTileSpace();
const contents = this.root.map.getLayerContentXY(tile.x, tile.y, "wires");
if (!contents) {
return;
}
let value = null;
if (contents.components.Wire) {
const network = contents.components.Wire.linkedNetwork;
if (network && network.hasValue()) {
value = network.currentValue;
}
2021-06-25 17:45:47 +00:00
} else if (contents.components.Display) {
const pinsComp = contents.components.WiredPins;
const network = pinsComp.slots[0].linkedNetwork;
2020-09-28 12:45:53 +00:00
2021-06-25 17:45:47 +00:00
if (network && network.hasValue()) {
value = network.currentValue;
}
}
// else if (contents.components.ConstantSignal) {
// value = contents.components.ConstantSignal.signal;
// }
else if (contents.components.WiredPins) {
const pinComp = contents.components.WiredPins;
const staticComp = contents.components.StaticMapEntity;
// Go over all slots and see if they are close to mouse or not
const pinSlots = pinComp.slots;
for (let i = 0; i < pinSlots.length; ++i) {
const slot = pinSlots[i];
// Check if the type matches
if (slot.type != enumPinSlotType.logicalEjector) {
continue;
}
// Check if slot is close to mouse
const mouseTilePos = this.root.camera.screenToWorld(mousePos);
// Dirty math that I don't like the look of
const slotPos = staticComp.localTileToWorld(slot.pos).toWorldSpaceCenterOfTile();
const effectiveRotation = Math.radians(
staticComp.rotation + enumDirectionToAngle[slot.direction]
);
const valueSpritePos = slotPos.add(new Vector(0, -9.1).rotated(effectiveRotation));
const length = mouseTilePos.sub(valueSpritePos).length();
// If it is closer than 8 we can copy that value
if (length <= 8) {
value = slot.value;
}
}
2020-09-28 12:45:53 +00:00
}
if (value) {
copy(value.getAsCopyableKey());
this.root.soundProxy.playUi(SOUNDS.copy);
} else {
copy("");
this.root.soundProxy.playUiError();
}
}
2020-08-29 20:52:52 +00:00
/**
*
* @param {DrawParameters} parameters
*/
draw(parameters) {
if (this.currentAlpha < 0.02) {
return;
}
const hasTileGrid = !this.root.app.settings.getAllSettings().disableTileGrid;
if (hasTileGrid && !this.cachedPatternBackground) {
2020-08-29 20:52:52 +00:00
this.cachedPatternBackground = parameters.context.createPattern(this.tilePatternCanvas, "repeat");
}
const bounds = parameters.visibleRect;
parameters.context.globalAlpha = this.currentAlpha;
const scaleFactor = 1 / wiresBackgroundDpi;
parameters.context.globalCompositeOperation = "overlay";
parameters.context.fillStyle = "rgba(50, 200, 150, 1)";
parameters.context.fillRect(bounds.x, bounds.y, bounds.w, bounds.h);
parameters.context.globalCompositeOperation = "source-over";
parameters.context.scale(scaleFactor, scaleFactor);
parameters.context.fillStyle = hasTileGrid
? this.cachedPatternBackground
: "rgba(78, 137, 125, 0.75)";
2020-08-29 20:52:52 +00:00
parameters.context.fillRect(
bounds.x / scaleFactor,
bounds.y / scaleFactor,
bounds.w / scaleFactor,
bounds.h / scaleFactor
);
parameters.context.scale(1 / scaleFactor, 1 / scaleFactor);
parameters.context.globalAlpha = 1;
}
}