1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/js/game/systems/logic_gate.js

130 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-08-13 17:23:00 +00:00
import { LogicGateComponent, enumLogicGateType } from "../components/logic_gate";
import { GameSystemWithFilter } from "../game_system_with_filter";
2020-08-15 16:39:08 +00:00
import { BaseItem } from "../base_item";
2020-08-13 17:23:00 +00:00
import { enumPinSlotType } from "../components/wired_pins";
2020-08-18 03:22:29 +00:00
import { BOOL_TRUE_SINGLETON, BOOL_FALSE_SINGLETON, BooleanItem, isBooleanItem } from "../items/boolean_item";
2020-08-13 17:23:00 +00:00
export class LogicGateSystem extends GameSystemWithFilter {
constructor(root) {
super(root, [LogicGateComponent]);
this.boundOperations = {
[enumLogicGateType.and]: this.compute_AND.bind(this),
2020-08-13 17:33:35 +00:00
[enumLogicGateType.not]: this.compute_NOT.bind(this),
[enumLogicGateType.xor]: this.compute_XOR.bind(this),
[enumLogicGateType.or]: this.compute_OR.bind(this),
2020-08-13 18:47:33 +00:00
[enumLogicGateType.transistor]: this.compute_IF.bind(this),
2020-08-13 17:23:00 +00:00
};
}
update() {
2020-08-18 03:22:29 +00:00
this.allEntities.forEach(entity => {
2020-08-13 17:23:00 +00:00
const logicComp = entity.components.LogicGate;
const slotComp = entity.components.WiredPins;
const slotValues = [];
for (let i = 0; i < slotComp.slots.length; ++i) {
const slot = slotComp.slots[i];
if (slot.type !== enumPinSlotType.logicalAcceptor) {
continue;
}
if (slot.linkedNetwork) {
slotValues.push(slot.linkedNetwork.currentValue);
} else {
slotValues.push(null);
}
}
const result = this.boundOperations[logicComp.type](slotValues);
// @TODO: For now we hardcode the value to always be slot 0
assert(
slotValues.length === slotComp.slots.length - 1,
"Bad slot config, should have N acceptor slots and 1 ejector"
);
assert(slotComp.slots[0].type === enumPinSlotType.logicalEjector, "Slot 0 should be ejector");
slotComp.slots[0].value = result;
2020-08-18 03:22:29 +00:00
});
2020-08-13 17:23:00 +00:00
}
/**
* @param {Array<BaseItem|null>} parameters
2020-08-18 03:22:29 +00:00
* @returns {BooleanItem}
2020-08-13 17:23:00 +00:00
*/
compute_AND(parameters) {
assert(parameters.length === 2, "bad parameter count for AND");
2020-08-18 03:22:29 +00:00
const [param1, param2] = parameters;
2020-08-13 17:23:00 +00:00
2020-08-18 03:22:29 +00:00
if (!isBooleanItem(param1) || !isBooleanItem(param2)) {
2020-08-13 17:23:00 +00:00
return BOOL_FALSE_SINGLETON;
}
2020-08-18 03:22:29 +00:00
return param1.value && param2.value ? BOOL_TRUE_SINGLETON : BOOL_FALSE_SINGLETON;
2020-08-13 17:23:00 +00:00
}
2020-08-13 17:33:35 +00:00
/**
* @param {Array<BaseItem|null>} parameters
2020-08-18 03:22:29 +00:00
* @returns {BooleanItem}
2020-08-13 17:33:35 +00:00
*/
compute_NOT(parameters) {
2020-08-18 03:22:29 +00:00
const [item] = parameters;
2020-08-13 17:33:35 +00:00
if (!item) {
return BOOL_TRUE_SINGLETON;
2020-08-13 17:33:35 +00:00
}
2020-08-18 03:22:29 +00:00
if (!isBooleanItem(item)) {
2020-08-13 17:33:35 +00:00
return BOOL_FALSE_SINGLETON;
}
2020-08-18 03:22:29 +00:00
return item.value ? BOOL_FALSE_SINGLETON : BOOL_TRUE_SINGLETON;
2020-08-13 17:33:35 +00:00
}
/**
* @param {Array<BaseItem|null>} parameters
2020-08-18 03:22:29 +00:00
* @returns {BooleanItem}
*/
compute_XOR(parameters) {
assert(parameters.length === 2, "bad parameter count for XOR");
2020-08-18 03:22:29 +00:00
const [param1, param2] = parameters;
2020-08-18 03:22:29 +00:00
if (!isBooleanItem(param1) || !isBooleanItem(param2)) {
return BOOL_FALSE_SINGLETON;
}
2020-08-18 03:22:29 +00:00
return param1.value ^ param2.value ? BOOL_TRUE_SINGLETON : BOOL_FALSE_SINGLETON;
}
/**
* @param {Array<BaseItem|null>} parameters
2020-08-18 03:22:29 +00:00
* @returns {BooleanItem}
*/
compute_OR(parameters) {
assert(parameters.length === 2, "bad parameter count for OR");
2020-08-18 03:22:29 +00:00
const [param1, param2] = parameters;
if (!isBooleanItem(param1) || !isBooleanItem(param2)) {
return BOOL_FALSE_SINGLETON;
}
2020-08-18 03:22:29 +00:00
return param1.value || param2.value ? BOOL_TRUE_SINGLETON : BOOL_FALSE_SINGLETON;
}
2020-08-13 18:47:33 +00:00
/**
* @param {Array<BaseItem|null>} parameters
2020-08-18 03:22:29 +00:00
* @returns {BaseItem|null}
2020-08-13 18:47:33 +00:00
*/
compute_IF(parameters) {
assert(parameters.length === 2, "bad parameter count for IF");
2020-08-18 03:22:29 +00:00
const [flag, item] = parameters;
2020-08-13 18:47:33 +00:00
2020-08-18 03:22:29 +00:00
return isBooleanItem(flag) && flag.value ? item : null;
2020-08-13 18:47:33 +00:00
}
2020-08-13 17:23:00 +00:00
}