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

Custom wire processor + building variants fixes

This commit is contained in:
Exund 2020-09-12 22:11:25 +02:00
parent d887c8dcd8
commit bf2d5f333c
4 changed files with 282 additions and 5 deletions

View File

@ -1,9 +1,13 @@
//Mod imports
import { MetaModBuilding } from "./mod_building"; import { MetaModBuilding } from "./mod_building";
import { ModComponent } from "./mod_component"; import { ModComponent } from "./mod_component";
import { ModItem } from "./mod_item"; import { ModItem } from "./mod_item";
import { ModProcessor } from "./mod_processor"; import { ModProcessor } from "./mod_processor";
import { ModWireProcessor } from "./mod_wireprocessor";
import { ModSystem, ModSystemWithFilter } from "./mod_system"; import { ModSystem, ModSystemWithFilter } from "./mod_system";
import { keyCodeOf } from "./mod_utils"; import { keyCodeOf } from "./mod_utils";
//Game imports
import { gComponentRegistry, gItemRegistry, gMetaBuildingRegistry } from "../core/global_registries"; import { gComponentRegistry, gItemRegistry, gMetaBuildingRegistry } from "../core/global_registries";
import { GameSystemManager } from "../game/game_system_manager"; import { GameSystemManager } from "../game/game_system_manager";
import { GameCore } from "../game/core"; import { GameCore } from "../game/core";
@ -19,6 +23,7 @@ export { MetaModBuilding } from "./mod_building";
export { ModComponent } from "./mod_component"; export { ModComponent } from "./mod_component";
export { ModItem } from "./mod_item"; export { ModItem } from "./mod_item";
export { ModProcessor } from "./mod_processor"; export { ModProcessor } from "./mod_processor";
export { ModWireProcessor } from "./mod_wireprocessor";
export { ModSystem, ModSystemWithFilter } from "./mod_system"; export { ModSystem, ModSystemWithFilter } from "./mod_system";
/** /**
@ -28,6 +33,7 @@ export { ModSystem, ModSystemWithFilter } from "./mod_system";
* @property {Array<typeof ModComponent>=} components * @property {Array<typeof ModComponent>=} components
* @property {Array<typeof ModItem>=} items * @property {Array<typeof ModItem>=} items
* @property {Array<typeof ModProcessor>=} processors * @property {Array<typeof ModProcessor>=} processors
* @property {Array<typeof ModWireProcessor>=} wireProcessors
* @property {Array<typeof ModSystem | typeof ModSystemWithFilter>=} systems * @property {Array<typeof ModSystem | typeof ModSystemWithFilter>=} systems
* @property {Array<ShapeData>=} shapes * @property {Array<ShapeData>=} shapes
*/ */
@ -46,6 +52,9 @@ export const ModSystems = [];
/** @type {Object.<string, typeof ModProcessor>} */ /** @type {Object.<string, typeof ModProcessor>} */
export const ModProcessors = {}; export const ModProcessors = {};
/** @type {Object.<string, typeof ModWireProcessor>} */
export const ModWireProcessors = {};
/** @type {Array<typeof ModItem>} */ /** @type {Array<typeof ModItem>} */
export const ModItems = []; export const ModItems = [];
@ -202,6 +211,14 @@ export async function initMods() {
} }
} }
if (mod.wireProcessors) {
mod_infos += `${mod.wireProcessors.length} wire processors, `;
for (const wireProcessor of mod.wireProcessors) {
const type = wireProcessor.getType();
ModWireProcessors[type] = wireProcessor;
}
}
if (mod.items) { if (mod.items) {
mod_infos += `${mod.items.length} items, `; mod_infos += `${mod.items.length} items, `;
for (const item of mod.items) { for (const item of mod.items) {
@ -219,7 +236,7 @@ export async function initMods() {
registerBuildingVariant(base_id, building); registerBuildingVariant(base_id, building);
for (const variant of building.getVariants()) { for (const variant of building.getVariants()) {
registerBuildingVariant(`${base_id}-${variant}`, building); registerBuildingVariant(`${base_id}-${variant}`, building, variant);
} }
supportedBuildings.push(building); supportedBuildings.push(building);

View File

@ -0,0 +1,24 @@
import { BaseItem } from "../game/base_item";
import { LogicGateSystem } from "../game/systems/logic_gate";
/**
* Custom wire processor (logic gate/virtual processor)
*/
export class ModWireProcessor {
/**
* @returns {String}
*/
static getType() {
return this.prototype.constructor.name;
}
/**
* @param {Array<BaseItem|null>} parameters
* @param {LogicGateSystem} system
* @returns {Array<BaseItem>|BaseItem}
*/
static compute(system, parameters) {
abstract;
return [];
}
}

View File

@ -7,6 +7,13 @@ import { ShapeDefinition } from "../../../game/shape_definition";
import { ItemProcessorComponent } from "../../../game/components/item_processor"; import { ItemProcessorComponent } from "../../../game/components/item_processor";
import { ItemEjectorComponent } from "../../../game/components/item_ejector"; import { ItemEjectorComponent } from "../../../game/components/item_ejector";
import { ItemAcceptorComponent } from "../../../game/components/item_acceptor"; import { ItemAcceptorComponent } from "../../../game/components/item_acceptor";
import { ModWireProcessor } from "../../mod_wireprocessor";
import { BaseItem } from "../../../game/base_item";
import { LogicGateSystem } from "../../../game/systems/logic_gate";
import { BOOL_FALSE_SINGLETON, BOOL_TRUE_SINGLETON, isTruthyItem } from "../../../game/items/boolean_item";
import { enumPinSlotType, WiredPinsComponent } from "../../../game/components/wired_pins";
import { LogicGateComponent } from "../../../game/components/logic_gate";
import { defaultBuildingVariant } from "../../../game/meta_building";
class MetaTestBuilding extends GeoZ.MetaModBuilding { class MetaTestBuilding extends GeoZ.MetaModBuilding {
static getId() { static getId() {
@ -46,7 +53,8 @@ class MetaTestBuilding extends GeoZ.MetaModBuilding {
*/ */
getSpriteMetas() { getSpriteMetas() {
const normal = { const normal = {
url: "https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/wires/boolean_false.png", url:
"https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/wires/boolean_false.png",
width: 64, width: 64,
height: 64, height: 64,
}; };
@ -92,9 +100,168 @@ class MetaTestBuilding extends GeoZ.MetaModBuilding {
} }
} }
class MetaInvertedGatesBuilding extends GeoZ.MetaModBuilding {
static getId() {
return "NANDGate";
}
static getKeybinding() {
return "0";
}
static getTranslations() {
return {
variants: {
default: {
name: "NAND Gate",
description: "Test GeoZ building for custom wire processor",
},
NORGate: {
name: "NOR Gate",
description: "Test GeoZ building for custom wire processor",
},
XNORGate: {
name: "XNOR Gate",
description: "Test GeoZ building for custom wire processor",
},
},
keybinding: "NAND Gate",
};
}
static getVariants() {
return ["NORGate", "XNORGate"];
}
constructor() {
super("NANDGate");
}
getSilhouetteColor() {
return "#89dc60";
}
getDimensions() {
return new Vector(1, 1);
}
getAvailableVariants() {
return [...super.getAvailableVariants(null), ...MetaInvertedGatesBuilding.getVariants()];
}
/**
* @returns {Layer}
*/
getLayer() {
return "wires";
}
/**
* @returns {import("../../mod_building").BuildingSpriteMetas}
*/
getSpriteMetas() {
return {
default: [
{
normal: {
url:
"https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/buildings/logic_gate.png",
width: 192,
height: 192,
},
blueprint: {
url:
"https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/blueprints/logic_gate.png",
width: 192,
height: 192,
},
},
],
NORGate: [
{
normal: {
url:
"https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/buildings/logic_gate-or.png",
width: 192,
height: 192,
},
blueprint: {
url:
"https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/blueprints/logic_gate-or.png",
width: 192,
height: 192,
},
},
],
XNORGate: [
{
normal: {
url:
"https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/buildings/logic_gate-xor.png",
width: 192,
height: 192,
},
blueprint: {
url:
"https://raw.githubusercontent.com/Exund/shapez.io/master/res_raw/sprites/blueprints/logic_gate-xor.png",
width: 192,
height: 192,
},
},
],
};
}
/**
* @param {Entity} entity
* @param {string} variant
*/
updateVariants(entity, rotationVariant, variant) {
entity.components.LogicGate.type = enumInvertedGatesVariants[variant];
}
/**
* Creates the entity at the given location
* @param {Entity} entity
*/
setupEntityComponents(entity) {
entity.addComponent(
new WiredPinsComponent({
slots: [
{
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,
},
],
})
);
entity.addComponent(new LogicGateComponent({ type: NANDGate.getType() }));
}
}
const enumInvertedGatesVariants = {
[defaultBuildingVariant]: "NANDGate",
};
for (const v of MetaInvertedGatesBuilding.getVariants()) {
enumInvertedGatesVariants[v] = v;
}
class SquareConverter extends ModProcessor { class SquareConverter extends ModProcessor {
/** /**
* @returns {Number} * @returns {Number}
*/ */
static getBaseSpeed() { static getBaseSpeed() {
return 0.5; return 0.5;
@ -108,14 +275,77 @@ class SquareConverter extends ModProcessor {
static process({ outItems }) { static process({ outItems }) {
outItems.push({ item: new ShapeItem(ShapeDefinition.fromShortKey("SuSuSuSu")) }); outItems.push({ item: new ShapeItem(ShapeDefinition.fromShortKey("SuSuSuSu")) });
return true; return true;
} }
}
class NANDGate extends ModWireProcessor {
/**
* @param {Array<BaseItem|null>} parameters
* @param {LogicGateSystem} system
* @returns {Array<BaseItem>|BaseItem}
*/
static compute(system, parameters) {
assert(parameters.length === 2, "bad parameter count for NAND");
return isTruthyItem(parameters[0]) && isTruthyItem(parameters[1])
? BOOL_FALSE_SINGLETON
: BOOL_TRUE_SINGLETON;
}
}
class NORGate extends ModWireProcessor {
/**
* @param {Array<BaseItem|null>} parameters
* @param {LogicGateSystem} system
* @returns {Array<BaseItem>|BaseItem}
*/
static compute(system, parameters) {
assert(parameters.length === 2, "bad parameter count for NOR");
return isTruthyItem(parameters[0]) || isTruthyItem(parameters[1])
? BOOL_FALSE_SINGLETON
: BOOL_TRUE_SINGLETON;
}
}
class XNORGate extends ModWireProcessor {
/**
* @param {Array<BaseItem|null>} parameters
* @param {LogicGateSystem} system
* @returns {Array<BaseItem>|BaseItem}
*/
static compute(system, parameters) {
assert(parameters.length === 2, "bad parameter count for XNOR");
return isTruthyItem(parameters[0]) !== isTruthyItem(parameters[1])
? BOOL_FALSE_SINGLETON
: BOOL_TRUE_SINGLETON;
}
}
class VirtualStacker extends ModWireProcessor {
/**
* @param {Array<BaseItem|null>} parameters
* @param {LogicGateSystem} system
* @returns {Array<BaseItem>|BaseItem}
*/
static compute(system, parameters) {
const item1 = parameters[0];
const item2 = parameters[0];
if (!item1 || !item2 || item1.getItemType() !== "shape" || item2.getItemType() !== "shape") {
return null;
}
const definition1 = /** @type {ShapeItem} */ (item1).definition;
const definition2 = /** @type {ShapeItem} */ (item2).definition;
const result = system.root.shapeDefinitionMgr.shapeActionStack(definition1, definition2);
return system.root.shapeDefinitionMgr.getShapeItemFromDefinition(result);
}
} }
/**@type {GeoZ.Mod}*/ /**@type {GeoZ.Mod}*/
const test = { const test = {
name: "test", name: "test",
buildings: [MetaTestBuilding], buildings: [MetaTestBuilding, MetaInvertedGatesBuilding],
processors: [SquareConverter], processors: [SquareConverter],
wireProcessors: [NANDGate, NORGate, XNORGate, VirtualStacker],
shapes: [ shapes: [
{ {
id: "leaf", id: "leaf",

View File

@ -25,6 +25,12 @@ export class LogicGateSystem extends GameSystemWithFilter {
[enumLogicGateType.unstacker]: this.compute_UNSTACK.bind(this), [enumLogicGateType.unstacker]: this.compute_UNSTACK.bind(this),
[enumLogicGateType.shapecompare]: this.compute_SHAPECOMPARE.bind(this), [enumLogicGateType.shapecompare]: this.compute_SHAPECOMPARE.bind(this),
}; };
const { ModWireProcessors } = require("../../GeoZ/main");
for (const type in ModWireProcessors) {
//@ts-ignore
this.boundOperations[type] = ModWireProcessors[type].compute.bind(null, this);
}
} }
update() { update() {