mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-06-13 13:04:03 +00:00
changed filter system to be a component
This commit is contained in:
parent
7612c75f56
commit
b0dd075e5f
@ -8,10 +8,10 @@ import { clamp, epsilonCompare, round4Digits } from "../core/utils";
|
||||
import { enumDirection, enumDirectionToVector, enumInvertedDirections, Vector } from "../core/vector";
|
||||
import { BasicSerializableObject, types } from "../savegame/serialization";
|
||||
import { BaseItem } from "./base_item";
|
||||
import { inputFilterId } from "./components/input_filter";
|
||||
import { Entity } from "./entity";
|
||||
import { typeItemSingleton } from "./item_resolver";
|
||||
import { GameRoot } from "./root";
|
||||
import { MOD_ITEM_FILTERS } from "./systems/item_ejector";
|
||||
|
||||
const logger = createLogger("belt_path");
|
||||
|
||||
@ -364,13 +364,10 @@ export class BeltPath extends BasicSerializableObject {
|
||||
}
|
||||
|
||||
return function (item) {
|
||||
// it could be a custom mod case
|
||||
// let's check if it is any of them
|
||||
for (let id in MOD_ITEM_FILTERS) {
|
||||
if (entity.components[id]) {
|
||||
let handler = MOD_ITEM_FILTERS[id];
|
||||
if (handler(item, entity, matchingSlotIndex)) return true;
|
||||
}
|
||||
const inputFilterComp = entity.components[inputFilterId];
|
||||
if (inputFilterComp) {
|
||||
// it's a custom filter
|
||||
if (inputFilterComp.canAcceptItem(item, entity, matchingSlotIndex)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
19
src/js/game/components/input_filter.js
Normal file
19
src/js/game/components/input_filter.js
Normal file
@ -0,0 +1,19 @@
|
||||
import { BaseItem } from "../base_item";
|
||||
import { Component } from "../component";
|
||||
import { Entity } from "../entity";
|
||||
|
||||
export const inputFilterId = "InputFilter";
|
||||
|
||||
export class InputFilterComponent extends Component {
|
||||
/**
|
||||
* @param {(item: BaseItem, entity: Entity, slot: number) => boolean} canAcceptItem
|
||||
*/
|
||||
constructor(canAcceptItem = () => true) {
|
||||
super();
|
||||
this.canAcceptItem = canAcceptItem;
|
||||
}
|
||||
|
||||
static getId() {
|
||||
return inputFilterId;
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import { StaleAreaDetector } from "../../core/stale_area_detector";
|
||||
import { enumDirection, enumDirectionToVector } from "../../core/vector";
|
||||
import { BaseItem } from "../base_item";
|
||||
import { BeltComponent } from "../components/belt";
|
||||
import { inputFilterId } from "../components/input_filter";
|
||||
import { ItemAcceptorComponent } from "../components/item_acceptor";
|
||||
import { ItemEjectorComponent } from "../components/item_ejector";
|
||||
import { Entity } from "../entity";
|
||||
@ -14,12 +15,6 @@ import { MapChunkView } from "../map_chunk_view";
|
||||
|
||||
const logger = createLogger("systems/ejector");
|
||||
|
||||
/**
|
||||
* Key is component id, value is function to handle whether or not item can enter.
|
||||
* @type {Object<string, (item: BaseItem, reciever: Entity, slot: number) => boolean>}
|
||||
*/
|
||||
export const MOD_ITEM_FILTERS = {};
|
||||
|
||||
export class ItemEjectorSystem extends GameSystemWithFilter {
|
||||
constructor(root) {
|
||||
super(root, [ItemEjectorComponent]);
|
||||
@ -300,13 +295,10 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
||||
}
|
||||
}
|
||||
|
||||
// it could be a custom mod case
|
||||
// let's check if it is any of them
|
||||
for (let id in MOD_ITEM_FILTERS) {
|
||||
if (receiver.components[id]) {
|
||||
let handler = MOD_ITEM_FILTERS[id];
|
||||
if (handler(item, receiver, slotIndex)) return true;
|
||||
}
|
||||
const inputFilterComp = receiver.components[inputFilterId];
|
||||
if (inputFilterComp) {
|
||||
// it's a custom filter
|
||||
if (inputFilterComp.canAcceptItem(item, receiver, slotIndex)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
Loading…
Reference in New Issue
Block a user