1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00

Minor PR adjustments

This commit is contained in:
tobspr 2021-08-04 12:49:01 +02:00
parent e4f02abeb3
commit d8b34ca438
2 changed files with 53 additions and 40 deletions

View File

@ -73,6 +73,12 @@ export class ItemProcessorComponent extends Component {
// Type of processing requirement
this.processingRequirement = processingRequirement;
/**
* Our current inputs
* @type {Map<number, BaseItem>}
*/
this.inputSlots = new Map();
this.clear();
}
@ -82,11 +88,7 @@ export class ItemProcessorComponent extends Component {
// sure the outputs always match
this.nextOutputSlot = 0;
/**
* Our current inputs
* @type {Array<BaseItem?>}
*/
this.inputSlots = [];
this.inputSlots.clear();
/**
* Current input count
@ -121,15 +123,16 @@ export class ItemProcessorComponent extends Component {
this.type === enumItemProcessorTypes.goal
) {
// Hub has special logic .. not really nice but efficient.
this.inputSlots.push(item);
this.inputSlots.set(this.inputCount, item);
this.inputCount++;
return true;
}
// Check that we only take one item per slot
if (this.inputSlots[sourceSlot]) return false;
this.inputSlots[sourceSlot] = item;
if (this.inputSlots.has(sourceSlot)) {
return false;
}
this.inputSlots.set(sourceSlot, item);
this.inputCount++;
return true;
}

View File

@ -32,7 +32,8 @@ const MAX_QUEUED_CHARGES = 2;
* Type of a processor implementation
* @typedef {{
* entity: Entity,
* items: Array<BaseItem?>,
* items: Map<number, BaseItem>,
* inputCount: number,
* outItems: Array<ProducedItem>
* }} ProcessorImplementationPayload
*/
@ -197,11 +198,11 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
const pinsComp = entity.components.WiredPins;
// First slot is the shape, so if it's not there we can't do anything
if (!processorComp.inputSlots[0]) {
const shapeItem = /** @type {ShapeItem} */ (processorComp.inputSlots.get(0));
if (!shapeItem) {
return false;
}
const shapeItem = /** @type {ShapeItem} */ (processorComp.inputSlots[0]);
const slotStatus = [];
// Check which slots are enabled
@ -226,7 +227,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
// Check if all colors of the enabled slots are there
for (let i = 0; i < slotStatus.length; ++i) {
if (slotStatus[i] && !processorComp.inputSlots[1 + i]) {
if (slotStatus[i] && !processorComp.inputSlots.get(1 + i)) {
// A slot which is enabled wasn't enabled. Make sure if there is anything on the quadrant,
// it is not possible to paint, but if there is nothing we can ignore it
for (let j = 0; j < 4; ++j) {
@ -268,6 +269,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
entity,
items,
outItems,
inputCount: processorComp.inputCount,
});
// Track produced items
@ -290,7 +292,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
remainingTime: timeToProcess,
});
processorComp.inputSlots.length = 0;
processorComp.inputSlots.clear();
processorComp.inputCount = 0;
}
@ -307,10 +309,14 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
const nextSlot = processorComp.nextOutputSlot++ % availableSlots;
for (let i = 0; i < payload.items.length; ++i) {
if (!payload.items[i]) continue;
// Hardcoded to 2, to avoid accessing the length
for (let i = 0; i < 2; ++i) {
const item = payload.items.get(i);
if (!item) {
continue;
}
payload.outItems.push({
item: payload.items[i],
item,
preferredSlot: (nextSlot + i) % availableSlots,
doNotTrack: true,
});
@ -322,7 +328,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_CUTTER(payload) {
const inputItem = /** @type {ShapeItem} */ (payload.items[0]);
const inputItem = /** @type {ShapeItem} */ (payload.items.get(0));
assert(inputItem instanceof ShapeItem, "Input for cut is not a shape");
const inputDefinition = inputItem.definition;
@ -343,7 +349,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_CUTTER_QUAD(payload) {
const inputItem = /** @type {ShapeItem} */ (payload.items[0]);
const inputItem = /** @type {ShapeItem} */ (payload.items.get(0));
assert(inputItem instanceof ShapeItem, "Input for cut is not a shape");
const inputDefinition = inputItem.definition;
@ -364,7 +370,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_ROTATER(payload) {
const inputItem = /** @type {ShapeItem} */ (payload.items[0]);
const inputItem = /** @type {ShapeItem} */ (payload.items.get(0));
assert(inputItem instanceof ShapeItem, "Input for rotation is not a shape");
const inputDefinition = inputItem.definition;
@ -378,7 +384,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_ROTATER_CCW(payload) {
const inputItem = /** @type {ShapeItem} */ (payload.items[0]);
const inputItem = /** @type {ShapeItem} */ (payload.items.get(0));
assert(inputItem instanceof ShapeItem, "Input for rotation is not a shape");
const inputDefinition = inputItem.definition;
@ -392,7 +398,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_ROTATER_180(payload) {
const inputItem = /** @type {ShapeItem} */ (payload.items[0]);
const inputItem = /** @type {ShapeItem} */ (payload.items.get(0));
assert(inputItem instanceof ShapeItem, "Input for rotation is not a shape");
const inputDefinition = inputItem.definition;
@ -406,8 +412,8 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_STACKER(payload) {
const lowerItem = /** @type {ShapeItem} */ (payload.items[0]);
const upperItem = /** @type {ShapeItem} */ (payload.items[1]);
const lowerItem = /** @type {ShapeItem} */ (payload.items.get(0));
const upperItem = /** @type {ShapeItem} */ (payload.items.get(1));
assert(lowerItem instanceof ShapeItem, "Input for lower stack is not a shape");
assert(upperItem instanceof ShapeItem, "Input for upper stack is not a shape");
@ -433,8 +439,8 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
*/
process_MIXER(payload) {
// Find both colors and combine them
const item1 = /** @type {ColorItem} */ (payload.items[0]);
const item2 = /** @type {ColorItem} */ (payload.items[1]);
const item1 = /** @type {ColorItem} */ (payload.items.get(0));
const item2 = /** @type {ColorItem} */ (payload.items.get(1));
assert(item1 instanceof ColorItem, "Input for color mixer is not a color");
assert(item2 instanceof ColorItem, "Input for color mixer is not a color");
@ -456,8 +462,8 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_PAINTER(payload) {
const shapeItem = /** @type {ShapeItem} */ (payload.items[0]);
const colorItem = /** @type {ColorItem} */ (payload.items[1]);
const shapeItem = /** @type {ShapeItem} */ (payload.items.get(0));
const colorItem = /** @type {ColorItem} */ (payload.items.get(1));
const colorizedDefinition = this.root.shapeDefinitionMgr.shapeActionPaintWith(
shapeItem.definition,
@ -473,9 +479,9 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_PAINTER_DOUBLE(payload) {
const shapeItem1 = /** @type {ShapeItem} */ (payload.items[0]);
const shapeItem2 = /** @type {ShapeItem} */ (payload.items[1]);
const colorItem = /** @type {ColorItem} */ (payload.items[2]);
const shapeItem1 = /** @type {ShapeItem} */ (payload.items.get(0));
const shapeItem2 = /** @type {ShapeItem} */ (payload.items.get(1));
const colorItem = /** @type {ColorItem} */ (payload.items.get(2));
assert(shapeItem1 instanceof ShapeItem, "Input for painter is not a shape");
assert(shapeItem2 instanceof ShapeItem, "Input for painter is not a shape");
@ -503,14 +509,15 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
* @param {ProcessorImplementationPayload} payload
*/
process_PAINTER_QUAD(payload) {
const shapeItem = /** @type {ShapeItem} */ (payload.items[0]);
const shapeItem = /** @type {ShapeItem} */ (payload.items.get(0));
assert(shapeItem instanceof ShapeItem, "Input for painter is not a shape");
/** @type {Array<enumColors>} */
const colors = [null, null, null, null];
for (let i = 0; i < 4; ++i) {
if (payload.items[i + 1]) {
colors[i] = /** @type {ColorItem} */ (payload.items[i + 1]).color;
const colorItem = /** @type {ColorItem} */ (payload.items.get(i + 1));
if (colorItem) {
colors[i] = colorItem.color;
}
}
@ -529,7 +536,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
*/
process_READER(payload) {
// Pass through the item
const item = payload.items[0];
const item = payload.items.get(0);
payload.outItems.push({
item,
doNotTrack: true,
@ -548,9 +555,12 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
const hubComponent = payload.entity.components.Hub;
assert(hubComponent, "Hub item processor has no hub component");
for (let i = 0; i < payload.items.length; ++i) {
if (!payload.items[i]) continue;
const item = /** @type {ShapeItem} */ (payload.items[i]);
// Hardcoded
for (let i = 0; i < payload.inputCount; ++i) {
const item = /** @type {ShapeItem} */ (payload.items.get(i));
if (!item) {
continue;
}
this.root.hubGoals.handleDefinitionDelivered(item.definition);
}
}
@ -560,7 +570,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
*/
process_GOAL(payload) {
const goalComp = payload.entity.components.GoalAcceptor;
const item = payload.items[0];
const item = payload.items.get(0);
const now = this.root.time.now();
if (goalComp.item && !item.equals(goalComp.item)) {
@ -574,7 +584,7 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
if (this.root.gameMode.getIsEditor()) {
// while playing in editor, assign the item
goalComp.item = payload.items[0];
goalComp.item = item;
}
goalComp.lastDelivery = {