1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

Wireless Displays!!

This commit is contained in:
TcePrepK 2020-10-26 12:48:36 +03:00
parent 5df1c330fd
commit ee28cb8435
9 changed files with 191 additions and 38 deletions

View File

@ -208,7 +208,15 @@ export class FormElementItemChooser extends FormElement {
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
item.drawFullSizeOnCanvas(context, 128);
if (typeof item == "string") {
context.fillStyle = "#000000";
context.font = "64px sans-serif"
context.textAlign = "center";
context.fillText(item, 32, 32, 128);
console.log(context);
} else {
item.drawFullSizeOnCanvas(context, 128);
}
this.element.appendChild(canvas);
const detector = new ClickDetector(canvas, {});

View File

@ -6,11 +6,6 @@ import { GameRoot } from "../root";
import { WirelessDisplayComponent } from "../components/wireless_display";
import { enumHubGoalRewards } from "../tutorial_goals";
import { formatItemsPerSecond, generateMatrixRotations } from "../../core/utils";
import { ItemAcceptorComponent } from "../components/item_acceptor";
import { ItemEjectorComponent } from "../components/item_ejector";
import { enumItemProcessorTypes, ItemProcessorComponent } from "../components/item_processor";
import { T } from "../../translations";
import { BeltUnderlaysComponent } from "../components/belt_underlays";
/** @enum {string} */
@ -65,7 +60,7 @@ export class MetaWirelessDisplayBuilding extends MetaBuilding {
* @param {Entity} entity
*/
setupEntityComponents(entity) {
entity.addComponent(new WirelessDisplayComponent({}));
}
/**

View File

@ -1,7 +1,40 @@
import { Component } from "../component";
import { types } from "../../savegame/serialization";
import { BaseItem } from "../base_item";
import { typeItemSingleton } from "../item_resolver";
export class WirelessDisplayComponent extends Component {
static getId() {
return "Wiresless_Display";
return "WirelessDisplay";
}
static getWirelessCode() {
return 0;
}
static getSchema() {
return {
signal: types.nullable(typeItemSingleton),
};
}
/**
* Copy the current state to another component
* @param {WirelessDisplayComponent} otherComponent
*/
copyAdditionalStateTo(otherComponent) {
otherComponent.signal = this.signal;
}
/**
*
* @param {object} param0
* @param {BaseItem=} param0.signal The signal to store
*/
constructor({ signal = null }) {
super();
this.signal = signal;
}
}

View File

@ -64,6 +64,11 @@ export class Entity extends BasicSerializableObject {
* @type {string} */
this.destroyReason;
/**
* Stores wireless code of this entity
*/
this.wireless_code = 0;
/* typehints:end */
}

View File

@ -89,6 +89,9 @@ export class GameSystemManager {
/** @type {DisplaySystem} */
display: null,
/** @type {WirelessDisplaySystem} */
wirelessDisplay: null,
/** @type {ItemProcessorOverlaysSystem} */
itemProcessorOverlays: null,
@ -163,6 +166,7 @@ export class GameSystemManager {
add("beltReader", BeltReaderSystem);
add("display", DisplaySystem);
add("wirelessDisplay", WirelessDisplaySystem);
add("itemProcessorOverlays", ItemProcessorOverlaysSystem);

View File

@ -68,6 +68,7 @@ export class MapChunkView extends MapChunk {
systems.staticMapEntities.drawChunk(parameters, this);
systems.lever.drawChunk(parameters, this);
systems.display.drawChunk(parameters, this);
systems.wirelessDisplay.drawChunk(parameters, this);
systems.storage.drawChunk(parameters, this);
systems.itemProcessorOverlays.drawChunk(parameters, this);
}

View File

@ -99,7 +99,6 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
}
if (itemInput.chosenItem) {
console.log(itemInput.chosenItem);
constantComp.signal = itemInput.chosenItem;
} else {
constantComp.signal = this.parseSignalCode(signalValueInput.getValue());

View File

@ -7,11 +7,23 @@ import { GameSystemWithFilter } from "../game_system_with_filter";
import { isTrueItem } from "../items/boolean_item";
import { ColorItem, COLOR_ITEM_SINGLETONS } from "../items/color_item";
import { MapChunkView } from "../map_chunk_view";
import trim from "trim";
import { THIRDPARTY_URLS } from "../../core/config";
import { DialogWithForm } from "../../core/modal_dialog_elements";
import { FormElementInput, FormElementItemChooser } from "../../core/modal_dialog_forms";
import { fillInLinkIntoTranslation } from "../../core/utils";
import { T } from "../../translations";
import { Entity } from "../entity";
import { ShapeDefinition } from "../shape_definition";
import { BOOL_FALSE_SINGLETON, BOOL_TRUE_SINGLETON } from "../items/boolean_item";
import { init } from "logrocket";
export class WirelessDisplaySystem extends GameSystemWithFilter {
constructor(root) {
super(root, [WirelessDisplayComponent]);
this.root.signals.entityManuallyPlaced.add(this.channelSignalValue, this);
/** @type {Object<string, import("../../core/draw_utils").AtlasSprite>} */
this.displaySprites = {};
@ -21,6 +33,88 @@ export class WirelessDisplaySystem extends GameSystemWithFilter {
}
this.displaySprites[colorId] = Loader.getSprite("sprites/wires/display/" + colorId + ".png");
}
this.wirelessMachineList = [];
}
/**
* Asks the entity to enter a valid signal code
* @param {Entity} entity
*/
channelSignalValue(entity) {
if (entity.components.WirelessDisplay) {
// Ok, query, but also save the uid because it could get stale
const uid = entity.uid;
const signalValueInput = new FormElementInput({
id: "channelValue",
label: fillInLinkIntoTranslation(T.dialogs.editChannel.descShortKey, THIRDPARTY_URLS.shapeViewer),
placeholder: "",
defaultValue: "",
validator: val => val,
});
const channeldialog = new DialogWithForm({
app: this.root.app,
title: T.dialogs.editChannel.title,
desc: T.dialogs.editChannel.descItems,
formElements: [signalValueInput],
buttons: ["cancel:bad:escape", "ok:good:enter"],
closeButton: false,
});
this.root.hud.parts.dialogs.internalShowDialog(channeldialog);
// When confirmed, set the signal
const closeHandler = () => {
if (!this.root || !this.root.entityMgr) {
// Game got stopped
return;
}
const entityRef = this.root.entityMgr.findByUid(uid, false);
if (!entityRef) {
// outdated
return;
}
const constantComp = entityRef.components.WirelessDisplay;
if (!constantComp) {
// no longer interesting
return;
}
if (signalValueInput.getValue() && !entity.components.WiredPins){
entity.wireless_code = signalValueInput.getValue();
} else if (signalValueInput.getValue() && entity.components.WiredPins){
entity.wireless_code = signalValueInput.getValue();
this.wirelessMachineList.push(entity);
}
};
channeldialog.buttonSignals.ok.add(closeHandler);
channeldialog.valueChosen.add(closeHandler);
// When cancelled, destroy the entity again
channeldialog.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 constantComp = entityRef.components.WirelessDisplay;
if (!constantComp) {
// no longer interesting
return;
}
this.root.logic.tryDeleteBuilding(entityRef);
});
}
}
/**
@ -60,38 +154,46 @@ export class WirelessDisplaySystem extends GameSystemWithFilter {
drawChunk(parameters, chunk) {
const contents = chunk.containedEntitiesByLayer.regular;
for (let i = 0; i < contents.length; ++i) {
const entity = contents[i];
if (entity && entity.components.Display) {
const pinsComp = entity.components.WiredPins;
const network = pinsComp.slots[0].linkedNetwork;
const entity_a = contents[i];
if (entity_a && !entity_a.components.WiredPins && entity_a.components.WirelessDisplay) {
for (let j = 0; j < this.wirelessMachineList.length; ++j) {
const entity_b = this.wirelessMachineList[j];
if (entity_a.wireless_code == entity_b.wireless_code) {
const origin = entity_a.components.StaticMapEntity.origin;
const pinsComp = entity_b.components.WiredPins;
const network = pinsComp.slots[0].linkedNetwork;
if (!network || !network.hasValue()) {
continue;
}
const value = this.getDisplayItem(network.currentValue);
if (!value) {
continue;
}
if (!network || !network.hasValue()) {
continue;
}
const value = this.getDisplayItem(network.currentValue);
if (!value) {
continue;
}
const origin = entity.components.StaticMapEntity.origin;
if (value.getItemType() === "color") {
this.displaySprites[/** @type {ColorItem} */ (value).color].drawCachedCentered(
parameters,
(origin.x + 0.5) * globalConfig.tileSize,
(origin.y + 0.5) * globalConfig.tileSize,
globalConfig.tileSize
);
} else if (value.getItemType() === "shape") {
value.drawItemCenteredClipped(
(origin.x + 0.5) * globalConfig.tileSize,
(origin.y + 0.5) * globalConfig.tileSize,
parameters,
30
);
if (value.getItemType()) {
if (value.getItemType() === "color") {
this.displaySprites[/** @type {ColorItem} */ (value).color].drawCachedCentered(
parameters,
(origin.x + 0.5) * globalConfig.tileSize,
(origin.y + 0.5) * globalConfig.tileSize,
globalConfig.tileSize
);
} else if (value.getItemType() === "shape") {
value.drawItemCenteredClipped(
(origin.x + 0.5) * globalConfig.tileSize,
(origin.y + 0.5) * globalConfig.tileSize,
parameters,
30
);
}
}
}
}
}
}
}
}

View File

@ -255,6 +255,12 @@ dialogs:
titleEdit: Edit Marker
desc: Give it a meaningful name, you can also include a <strong>short key</strong> of a shape (Which you can generate <link>here</link>)
editChannel:
title: Set Channel
descItems: >-
Enter used <strong>channel name</strong>
descShortKey: or enter new <strong>channel name</strong>
editSignal:
title: Set Signal
descItems: >-