shapez/src/js/game/components/item_acceptor.js

178 lines
5.7 KiB
JavaScript
Raw Normal View History

import { enumDirection, enumInvertedDirections, Vector } from "../../core/vector";
2020-05-14 21:54:11 +02:00
import { types } from "../../savegame/serialization";
import { BaseItem, enumItemType } from "../base_item";
import { Component } from "../component";
2020-05-09 16:45:23 +02:00
/** @typedef {{
* pos: Vector,
* directions: enumDirection[],
* filter?: enumItemType
2020-05-09 16:45:23 +02:00
* }} ItemAcceptorSlot */
/**
* Contains information about a slot plus its location
* @typedef {{
* slot: ItemAcceptorSlot,
* index: number,
* acceptedDirection: enumDirection
* }} ItemAcceptorLocatedSlot */
2020-05-09 16:45:23 +02:00
export class ItemAcceptorComponent extends Component {
static getId() {
return "ItemAcceptor";
}
static getSchema() {
return {
2020-05-14 21:54:11 +02:00
slots: types.array(
types.structured({
pos: types.vector,
directions: types.array(types.enum(enumDirection)),
filter: types.nullable(types.enum(enumItemType)),
2020-05-14 21:54:11 +02:00
})
),
2020-05-17 10:07:20 +02:00
animated: types.bool,
beltUnderlays: types.array(
types.structured({
pos: types.vector,
direction: types.enum(enumDirection),
})
),
2020-05-09 16:45:23 +02:00
};
}
2020-05-27 14:30:59 +02:00
duplicateWithoutContents() {
const slotsCopy = [];
for (let i = 0; i < this.slots.length; ++i) {
const slot = this.slots[i];
slotsCopy.push({
pos: slot.pos.copy(),
directions: slot.directions.slice(),
filter: slot.filter,
2020-05-27 14:30:59 +02:00
});
}
const beltUnderlaysCopy = [];
for (let i = 0; i < this.beltUnderlays.length; ++i) {
const underlay = this.beltUnderlays[i];
beltUnderlaysCopy.push({
pos: underlay.pos.copy(),
direction: underlay.direction,
});
}
return new ItemAcceptorComponent({
slots: slotsCopy,
beltUnderlays: beltUnderlaysCopy,
animated: this.animated,
});
}
2020-05-09 16:45:23 +02:00
/**
*
* @param {object} param0
* @param {Array<{pos: Vector, directions: enumDirection[], filter?: enumItemType}>} param0.slots The slots from which we accept items
2020-05-17 10:07:20 +02:00
* @param {boolean=} param0.animated Whether to animate item consumption
* @param {Array<{pos: Vector, direction: enumDirection}>=} param0.beltUnderlays Where to render belt underlays
2020-05-09 16:45:23 +02:00
*/
2020-05-17 10:07:20 +02:00
constructor({ slots = [], beltUnderlays = [], animated = true }) {
2020-05-09 16:45:23 +02:00
super();
2020-05-17 10:07:20 +02:00
this.animated = animated;
/**
* Fixes belt animations
* @type {Array<{ item: BaseItem, slotIndex: number, animProgress: number, direction: enumDirection}>}
*/
this.itemConsumptionAnimations = [];
/* Which belt underlays to render */
this.beltUnderlays = beltUnderlays;
2020-05-09 16:45:23 +02:00
this.setSlots(slots);
}
/**
*
* @param {Array<{pos: Vector, directions: enumDirection[], filter?: enumItemType}>} slots
2020-05-09 16:45:23 +02:00
*/
setSlots(slots) {
/** @type {Array<{pos: Vector, directions: enumDirection[], filter?: enumItemType}>} */
2020-05-09 16:45:23 +02:00
this.slots = [];
for (let i = 0; i < slots.length; ++i) {
const slot = slots[i];
this.slots.push({
pos: slot.pos,
directions: slot.directions,
// Which type of item to accept (shape | color | all) @see enumItemType
2020-05-09 16:45:23 +02:00
filter: slot.filter,
});
}
}
/**
* Returns if this acceptor can accept a new item at slot N
* @param {number} slotIndex
* @param {BaseItem=} item
*/
canAcceptItem(slotIndex, item) {
const slot = this.slots[slotIndex];
2020-06-27 10:44:23 +02:00
return !slot.filter || slot.filter === item.getItemType();
2020-05-09 16:45:23 +02:00
}
2020-05-17 10:07:20 +02:00
/**
* Called when an item has been accepted so that
* @param {number} slotIndex
* @param {enumDirection} direction
* @param {BaseItem} item
*/
onItemAccepted(slotIndex, direction, item) {
if (this.animated) {
this.itemConsumptionAnimations.push({
item,
slotIndex,
direction,
animProgress: 0.0,
});
}
}
2020-05-09 16:45:23 +02:00
/**
* Tries to find a slot which accepts the current item
* @param {Vector} targetLocalTile
* @param {enumDirection} fromLocalDirection
* @returns {ItemAcceptorLocatedSlot|null}
2020-05-09 16:45:23 +02:00
*/
findMatchingSlot(targetLocalTile, fromLocalDirection) {
// We need to invert our direction since the acceptor specifies *from* which direction
// it accepts items, but the ejector specifies *into* which direction it ejects items.
// E.g.: Ejector ejects into "right" direction but acceptor accepts from "left" direction.
const desiredDirection = enumInvertedDirections[fromLocalDirection];
// Go over all slots and try to find a target slot
for (let slotIndex = 0; slotIndex < this.slots.length; ++slotIndex) {
const slot = this.slots[slotIndex];
// Make sure the acceptor slot is on the right position
if (!slot.pos.equals(targetLocalTile)) {
continue;
}
// Check if the acceptor slot accepts items from our direction
for (let i = 0; i < slot.directions.length; ++i) {
// const localDirection = targetStaticComp.localDirectionToWorld(slot.directions[l]);
if (desiredDirection === slot.directions[i]) {
return {
slot,
index: slotIndex,
acceptedDirection: desiredDirection,
};
}
}
}
return null;
}
}