1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Add basic logic gate and improve wires in general

This commit is contained in:
tobspr
2020-08-13 10:23:50 +02:00
parent 93186cbb9f
commit 75ab655998
44 changed files with 1101 additions and 926 deletions

View File

@@ -9,6 +9,7 @@ import trim from "trim";
import { BOOL_TRUE_SINGLETON, BOOL_FALSE_SINGLETON } from "../items/boolean_item";
import { ShapeDefinition } from "../shape_definition";
import { ShapeItem } from "../items/shape_item";
import { BaseItem } from "../base_item";
export class ConstantSignalSystem extends GameSystemWithFilter {
constructor(root) {
@@ -40,7 +41,7 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
const uid = entity.uid;
const signalValueInput = new FormElementInput({
id: "markerName",
id: "signalValue",
label: null,
placeholder: "",
defaultValue: "",
@@ -51,9 +52,11 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
title: "Set Signal",
desc: "Enter a shape code, color or '0' or '1'",
formElements: [signalValueInput],
buttons: ["cancel", "ok:good"],
buttons: ["cancel:bad:escape", "ok:good:enter"],
});
this.root.hud.parts.dialogs.internalShowDialog(dialog);
// When confirmed, set the signal
dialog.buttonSignals.ok.add(() => {
if (!this.root || !this.root.entityMgr) {
// Game got stopped
@@ -74,18 +77,47 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
constantComp.signal = this.parseSignalCode(signalValueInput.getValue());
});
// 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);
});
}
/**
* Tries to parse a signal code
* @param {string} code
* @returns {BaseItem}
*/
parseSignalCode(code) {
code = trim(code);
if (enumColors[code]) {
return new ColorItem(code);
const codeLower = code.toLowerCase();
if (enumColors[codeLower]) {
return new ColorItem(codeLower);
}
if (code === "1" || code === "true") {
if (code === "1" || codeLower === "true") {
return BOOL_TRUE_SINGLETON;
}
if (code === "0" || code === "false") {
if (code === "0" || codeLower === "false") {
return BOOL_FALSE_SINGLETON;
}

View File

@@ -1,7 +1,7 @@
import { globalConfig } from "../../core/config";
import { DrawParameters } from "../../core/draw_parameters";
import { Loader } from "../../core/loader";
import { Vector } from "../../core/vector";
import { Vector, enumDirectionToAngle } from "../../core/vector";
import { enumPinSlotType, WiredPinsComponent } from "../components/wired_pins";
import { Entity } from "../entity";
import { GameSystemWithFilter } from "../game_system_with_filter";
@@ -169,13 +169,16 @@ export class WiredPinsSystem extends GameSystemWithFilter {
const worldPos = tile.toWorldSpaceCenterOfTile();
const effectiveRotation = Math.radians(
staticComp.rotation + enumDirectionToAngle[slot.direction]
);
drawRotatedSprite({
parameters,
sprite: this.pinSprites[slot.type],
x: worldPos.x,
y: worldPos.y,
angle: Math.radians(staticComp.rotation),
size: globalConfig.tileSize,
angle: effectiveRotation,
size: globalConfig.tileSize + 2,
offsetX: 0,
offsetY: 0,
});
@@ -183,7 +186,9 @@ export class WiredPinsSystem extends GameSystemWithFilter {
// Draw contained item to visualize whats emitted
const value = slot.value;
if (value) {
value.draw(worldPos.x, worldPos.y, parameters, 10);
const offset = new Vector(0, -5).rotated(effectiveRotation);
value.draw(worldPos.x + offset.x, worldPos.y + offset.y, parameters, 12);
}
}
}