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

97 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-06-24 20:23:10 +00:00
import { makeOffscreenBuffer } from "../../../core/buffer_utils";
import { globalConfig } from "../../../core/config";
import { DrawParameters } from "../../../core/draw_parameters";
import { KEYMAPPINGS } from "../../key_action_mapper";
import { THEME } from "../../theme";
import { BaseHUDPart } from "../base_hud_part";
2020-06-28 09:44:30 +00:00
import { Loader } from "../../../core/loader";
import { lerp } from "../../../core/utils";
2020-06-24 20:23:10 +00:00
2020-06-28 09:44:30 +00:00
const wiresBackgroundDpi = 4;
2020-06-24 20:23:10 +00:00
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);
this.generateTilePattern();
2020-06-28 09:44:30 +00:00
this.currentAlpha = 0.0;
2020-06-24 20:23:10 +00:00
}
/**
* Switches between layers
*/
switchLayers() {
2020-08-15 16:39:08 +00:00
if (this.root.currentLayer === "regular") {
this.root.currentLayer = "wires";
2020-06-24 20:23:10 +00:00
} else {
2020-08-15 16:39:08 +00:00
this.root.currentLayer = "regular";
2020-06-24 20:23:10 +00:00
}
2020-06-28 17:34:10 +00:00
this.root.signals.editModeChanged.dispatch(this.root.currentLayer);
2020-06-24 20:23:10 +00:00
}
/**
* Generates the background pattern for the wires overlay
*/
generateTilePattern() {
const overlayTile = Loader.getSprite("sprites/wires/overlay_tile.png");
2020-06-24 20:23:10 +00:00
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);
2020-06-28 09:44:30 +00:00
overlayTile.draw(context, 0, 0, dims, dims);
2020-06-24 20:23:10 +00:00
this.tilePatternCanvas = canvas;
}
2020-06-28 09:44:30 +00:00
update() {
2020-08-15 16:39:08 +00:00
const desiredAlpha = this.root.currentLayer === "wires" ? 1.0 : 0.0;
2020-06-30 07:27:30 +00:00
this.currentAlpha = lerp(this.currentAlpha, desiredAlpha, 0.12);
2020-06-28 09:44:30 +00:00
}
2020-06-24 20:23:10 +00:00
/**
*
* @param {DrawParameters} parameters
*/
draw(parameters) {
2020-06-28 09:44:30 +00:00
if (this.currentAlpha < 0.02) {
2020-06-24 20:23:10 +00:00
return;
}
2020-06-28 17:34:10 +00:00
if (this.root.camera.getIsMapOverlayActive()) {
return;
}
2020-06-24 20:23:10 +00:00
if (!this.cachedPatternBackground) {
this.cachedPatternBackground = parameters.context.createPattern(this.tilePatternCanvas, "repeat");
}
const bounds = parameters.visibleRect;
parameters.context.globalAlpha = this.currentAlpha;
2020-06-24 20:23:10 +00:00
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";
2020-06-24 20:23:10 +00:00
parameters.context.scale(scaleFactor, scaleFactor);
parameters.context.fillStyle = this.cachedPatternBackground;
parameters.context.fillRect(
bounds.x / scaleFactor,
bounds.y / scaleFactor,
bounds.w / scaleFactor,
bounds.h / scaleFactor
);
parameters.context.scale(1 / scaleFactor, 1 / scaleFactor);
2020-06-28 09:44:30 +00:00
parameters.context.globalAlpha = 1;
2020-06-24 20:23:10 +00:00
}
}