mirror of
https://github.com/tobspr/shapez.io.git
synced 2024-10-27 20:34:29 +00:00
156 lines
4.5 KiB
JavaScript
156 lines
4.5 KiB
JavaScript
import { enumDirection, Vector } from "../../core/vector";
|
|
import { enumPinSlotType, WiredPinsComponent } from "../components/wired_pins";
|
|
import { Entity } from "../entity";
|
|
import { MetaBuilding, defaultBuildingVariant } from "../meta_building";
|
|
import { GameRoot } from "../root";
|
|
import { enumLogicGateType, LogicGateComponent } from "../components/logic_gate";
|
|
|
|
/** @enum {string} */
|
|
export const enumLogicGateVariants = {
|
|
not: "not",
|
|
xor: "xor",
|
|
or: "or",
|
|
transistor: "transistor",
|
|
};
|
|
|
|
/** @enum {string} */
|
|
export const enumVariantToGate = {
|
|
[defaultBuildingVariant]: enumLogicGateType.and,
|
|
[enumLogicGateVariants.not]: enumLogicGateType.not,
|
|
[enumLogicGateVariants.xor]: enumLogicGateType.xor,
|
|
[enumLogicGateVariants.or]: enumLogicGateType.or,
|
|
[enumLogicGateVariants.transistor]: enumLogicGateType.transistor,
|
|
};
|
|
|
|
export class MetaLogicGateBuilding extends MetaBuilding {
|
|
constructor() {
|
|
super("logic_gate");
|
|
}
|
|
|
|
getSilhouetteColor() {
|
|
return "#89dc60";
|
|
}
|
|
|
|
/**
|
|
* @param {GameRoot} root
|
|
*/
|
|
getIsUnlocked(root) {
|
|
// @todo
|
|
return true;
|
|
}
|
|
|
|
/** @returns {"wires"} **/
|
|
getLayer() {
|
|
return "wires";
|
|
}
|
|
|
|
getDimensions() {
|
|
return new Vector(1, 1);
|
|
}
|
|
|
|
getAvailableVariants() {
|
|
return [
|
|
defaultBuildingVariant,
|
|
enumLogicGateVariants.not,
|
|
enumLogicGateVariants.xor,
|
|
enumLogicGateVariants.or,
|
|
enumLogicGateVariants.transistor,
|
|
];
|
|
}
|
|
|
|
getRenderPins() {
|
|
// We already have it included
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {Entity} entity
|
|
* @param {number} rotationVariant
|
|
*/
|
|
updateVariants(entity, rotationVariant, variant) {
|
|
const gateType = enumVariantToGate[variant];
|
|
entity.components.LogicGate.type = gateType;
|
|
|
|
const pinComp = entity.components.WiredPins;
|
|
|
|
switch (gateType) {
|
|
case enumLogicGateType.and:
|
|
case enumLogicGateType.xor:
|
|
case enumLogicGateType.or: {
|
|
pinComp.setSlots([
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.top,
|
|
type: enumPinSlotType.logicalEjector,
|
|
},
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.left,
|
|
type: enumPinSlotType.logicalAcceptor,
|
|
},
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.right,
|
|
type: enumPinSlotType.logicalAcceptor,
|
|
},
|
|
]);
|
|
break;
|
|
}
|
|
case enumLogicGateType.transistor: {
|
|
pinComp.setSlots([
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.top,
|
|
type: enumPinSlotType.logicalEjector,
|
|
},
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.left,
|
|
type: enumPinSlotType.logicalAcceptor,
|
|
},
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.bottom,
|
|
type: enumPinSlotType.logicalAcceptor,
|
|
},
|
|
]);
|
|
break;
|
|
}
|
|
|
|
case enumLogicGateType.not: {
|
|
pinComp.setSlots([
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.top,
|
|
type: enumPinSlotType.logicalEjector,
|
|
},
|
|
{
|
|
pos: new Vector(0, 0),
|
|
direction: enumDirection.bottom,
|
|
type: enumPinSlotType.logicalAcceptor,
|
|
},
|
|
]);
|
|
break;
|
|
}
|
|
|
|
default:
|
|
assertAlways("unknown logic gate type: " + gateType);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates the entity at the given location
|
|
* @param {Entity} entity
|
|
*/
|
|
setupEntityComponents(entity) {
|
|
entity.addComponent(
|
|
new WiredPinsComponent({
|
|
slots: [],
|
|
})
|
|
);
|
|
|
|
entity.addComponent(new LogicGateComponent({}));
|
|
}
|
|
}
|