1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-16 19:51:50 +00:00
tobspr_shapez.io/src/js/game/systems/item_acceptor.js

157 lines
5.5 KiB
JavaScript
Raw Normal View History

import { globalConfig } from "../../core/config";
import { DrawParameters } from "../../core/draw_parameters";
import { enumDirectionToVector } from "../../core/vector";
2022-01-18 17:39:02 +00:00
import {
enumItemAcceptorTypes,
ItemAcceptorComponent,
InputCompletedArgs,
} from "../components/item_acceptor";
import { GameSystemWithFilter } from "../game_system_with_filter";
2022-01-18 17:39:02 +00:00
import { ShapeItem } from "../items/shape_item";
import { MapChunkView } from "../map_chunk_view";
export class ItemAcceptorSystem extends GameSystemWithFilter {
constructor(root) {
super(root, [ItemAcceptorComponent]);
2022-01-18 17:39:02 +00:00
/**
* @type {Object<enumItemAcceptorTypes, function(InputCompletedArgs) : string>}
*/
this.handlers = {
[enumItemAcceptorTypes.itemProcessor]: this.input_ITEMPROCESSOR,
[enumItemAcceptorTypes.hub]: this.input_HUB,
[enumItemAcceptorTypes.trash]: this.input_TRASH,
};
2020-09-19 08:34:46 +00:00
2022-01-18 17:39:02 +00:00
// Bind all handlers
for (const key in this.handlers) {
this.handlers[key] = this.handlers[key].bind(this);
}
2022-01-18 17:39:02 +00:00
}
2022-01-18 17:39:02 +00:00
update() {
// * 2 because its only a half tile - (same code as ejector)
const progressGrowth =
2 *
2022-01-18 17:39:02 +00:00
this.root.dynamicTickrate.deltaSeconds *
this.root.hubGoals.getBeltBaseSpeed() *
2022-01-18 17:39:02 +00:00
globalConfig.itemSpacingOnBelts;
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
2022-01-18 17:39:02 +00:00
const acceptorComp = entity.components.ItemAcceptor;
const inputs = acceptorComp.currentInputs;
inputs.forEach((values, index) => {
if (values.animProgress >= 1) return; // items which are inputted already
values.animProgress += progressGrowth;
if (values.animProgress < 1) return;
/** @type {function(InputCompletedArgs) : string} */
const handler = this.handlers[acceptorComp.type];
assert(handler, "No handler for acceptor type defined: " + acceptorComp.type);
// Call implementation
handler({
root: this.root,
entity,
item: values.item,
slotIndex: index,
extraProgress: values.animProgress - 1,
});
});
}
}
/**
* @param {DrawParameters} parameters
* @param {MapChunkView} chunk
*/
drawChunk(parameters, chunk) {
2020-09-19 08:34:46 +00:00
if (this.root.app.settings.getAllSettings().simplifiedBelts) {
// Disabled in potato mode
return;
}
const contents = chunk.containedEntitiesByLayer.regular;
for (let i = 0; i < contents.length; ++i) {
const entity = contents[i];
const acceptorComp = entity.components.ItemAcceptor;
if (!acceptorComp) {
continue;
}
const staticComp = entity.components.StaticMapEntity;
2022-01-18 17:39:02 +00:00
acceptorComp.currentInputs.forEach((values, index) => {
const { item, animProgress, direction } = values;
if (animProgress >= 1) return; // items which are inputted already
2022-01-18 17:39:02 +00:00
const slotData = acceptorComp.slots[index];
const realSlotPos = staticComp.localTileToWorld(slotData.pos);
if (!chunk.tileSpaceRectangle.containsPoint(realSlotPos.x, realSlotPos.y)) {
// Not within this chunk
2022-01-18 17:39:02 +00:00
return;
}
const fadeOutDirection = enumDirectionToVector[staticComp.localDirectionToWorld(direction)];
const finalTile = realSlotPos.subScalars(
fadeOutDirection.x * (animProgress / 2 - 0.5),
fadeOutDirection.y * (animProgress / 2 - 0.5)
);
item.drawItemCenteredClipped(
(finalTile.x + 0.5) * globalConfig.tileSize,
(finalTile.y + 0.5) * globalConfig.tileSize,
parameters,
globalConfig.defaultItemDiameter
);
2022-01-18 17:39:02 +00:00
});
}
}
2022-01-18 17:39:02 +00:00
/**
* @param {InputCompletedArgs} args
*/
input_ITEMPROCESSOR(args) {
const entity = args.entity;
const itemProcessorComp = entity.components.ItemProcessor;
assert(itemProcessorComp, "No item processor to input item to");
itemProcessorComp.inputs.set(args.slotIndex, {
item: args.item,
extraProgress: args.extraProgress,
}); // in the future the item processor will not need a list of items
}
//@SENSETODO this isn't set up like it should be yet
/**
* @param {InputCompletedArgs} args
*/
input_HUB(args) {
const item = /** @type {ShapeItem} */ (args.item);
assert(item instanceof ShapeItem, "Input for hub is not a shape");
this.root.hubGoals.handleDefinitionDelivered(item.definition);
const acceptorComp = args.entity.components.ItemAcceptor;
acceptorComp.currentInputs.delete(args.slotIndex);
}
/**
* @param {InputCompletedArgs} args
*/
input_TRASH(args) {
// just remove the item
const acceptorComp = args.entity.components.ItemAcceptor;
acceptorComp.currentInputs.delete(args.slotIndex);
}
//storage
//underground belt
}