2020-08-14 11:09:10 +00:00
|
|
|
import trim from "trim";
|
2020-08-12 19:05:32 +00:00
|
|
|
import { DialogWithForm } from "../../core/modal_dialog_elements";
|
|
|
|
import { FormElementInput } from "../../core/modal_dialog_forms";
|
2020-08-14 11:09:10 +00:00
|
|
|
import { BaseItem } from "../base_item";
|
2020-08-12 19:05:32 +00:00
|
|
|
import { enumColors } from "../colors";
|
2020-08-14 11:09:10 +00:00
|
|
|
import { ConstantSignalComponent } from "../components/constant_signal";
|
|
|
|
import { Entity } from "../entity";
|
|
|
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
|
|
|
import { BOOL_FALSE_SINGLETON, BOOL_TRUE_SINGLETON } from "../items/boolean_item";
|
|
|
|
import { COLOR_ITEM_SINGLETONS } from "../items/color_item";
|
2020-08-12 19:05:32 +00:00
|
|
|
import { ShapeDefinition } from "../shape_definition";
|
|
|
|
|
|
|
|
export class ConstantSignalSystem extends GameSystemWithFilter {
|
|
|
|
constructor(root) {
|
|
|
|
super(root, [ConstantSignalComponent]);
|
|
|
|
|
|
|
|
this.root.signals.entityManuallyPlaced.add(this.querySigalValue, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
|
|
|
// Set signals
|
|
|
|
for (let i = 0; i < this.allEntities.length; ++i) {
|
|
|
|
const entity = this.allEntities[i];
|
|
|
|
const pinsComp = entity.components.WiredPins;
|
|
|
|
const signalComp = entity.components.ConstantSignal;
|
|
|
|
pinsComp.slots[0].value = signalComp.signal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks the entity to enter a valid signal code
|
|
|
|
* @param {Entity} entity
|
|
|
|
*/
|
|
|
|
querySigalValue(entity) {
|
|
|
|
if (!entity.components.ConstantSignal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, query, but also save the uid because it could get stale
|
|
|
|
const uid = entity.uid;
|
|
|
|
|
|
|
|
const signalValueInput = new FormElementInput({
|
2020-08-13 08:23:50 +00:00
|
|
|
id: "signalValue",
|
2020-08-12 19:05:32 +00:00
|
|
|
label: null,
|
|
|
|
placeholder: "",
|
|
|
|
defaultValue: "",
|
|
|
|
validator: val => this.parseSignalCode(val),
|
|
|
|
});
|
|
|
|
const dialog = new DialogWithForm({
|
|
|
|
app: this.root.app,
|
|
|
|
title: "Set Signal",
|
|
|
|
desc: "Enter a shape code, color or '0' or '1'",
|
|
|
|
formElements: [signalValueInput],
|
2020-08-13 08:23:50 +00:00
|
|
|
buttons: ["cancel:bad:escape", "ok:good:enter"],
|
2020-08-12 19:05:32 +00:00
|
|
|
});
|
|
|
|
this.root.hud.parts.dialogs.internalShowDialog(dialog);
|
2020-08-13 08:23:50 +00:00
|
|
|
|
|
|
|
// When confirmed, set the signal
|
2020-08-12 19:05:32 +00:00
|
|
|
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 constantComp = entityRef.components.ConstantSignal;
|
|
|
|
if (!constantComp) {
|
|
|
|
// no longer interesting
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
constantComp.signal = this.parseSignalCode(signalValueInput.getValue());
|
|
|
|
});
|
2020-08-13 08:23:50 +00:00
|
|
|
|
|
|
|
// When cancelled, destroy the entity again
|
|
|
|
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 constantComp = entityRef.components.ConstantSignal;
|
|
|
|
if (!constantComp) {
|
|
|
|
// no longer interesting
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.root.logic.tryDeleteBuilding(entityRef);
|
|
|
|
});
|
2020-08-12 19:05:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 08:23:50 +00:00
|
|
|
/**
|
|
|
|
* Tries to parse a signal code
|
|
|
|
* @param {string} code
|
|
|
|
* @returns {BaseItem}
|
|
|
|
*/
|
2020-08-12 19:05:32 +00:00
|
|
|
parseSignalCode(code) {
|
|
|
|
code = trim(code);
|
2020-08-13 08:23:50 +00:00
|
|
|
const codeLower = code.toLowerCase();
|
|
|
|
|
|
|
|
if (enumColors[codeLower]) {
|
2020-08-14 11:09:10 +00:00
|
|
|
return COLOR_ITEM_SINGLETONS[codeLower];
|
2020-08-12 19:05:32 +00:00
|
|
|
}
|
2020-08-13 08:23:50 +00:00
|
|
|
if (code === "1" || codeLower === "true") {
|
2020-08-12 19:05:32 +00:00
|
|
|
return BOOL_TRUE_SINGLETON;
|
|
|
|
}
|
|
|
|
|
2020-08-13 08:23:50 +00:00
|
|
|
if (code === "0" || codeLower === "false") {
|
2020-08-12 19:05:32 +00:00
|
|
|
return BOOL_FALSE_SINGLETON;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShapeDefinition.isValidShortKey(code)) {
|
2020-08-14 11:09:10 +00:00
|
|
|
return this.root.shapeDefinitionMgr.getShapeItemFromShortKey(code);
|
2020-08-12 19:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|