1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/js/game/components/item_acceptor.js

226 lines
6.8 KiB
JavaScript

import { directions, invertedDirectionMap } from "../../core/vector";
import { types } from "../../savegame/serialization";
import { BaseItem, itemTypes } from "../base_item";
import { Component } from "../component";
import { layers } from "../root";
/**
* @typedef {import("../../core/vector").Direction} Direction
* @typedef {import("../../core/vector").Vector} Vector
* @typedef {import("../root").Layer} Layer
* @typedef {import ("../base_item").ItemType} ItemType
*
* @typedef {{
* pos: Vector,
* directions: Direction[],
* layer: Layer,
* filter?: ItemType
* }} ItemAcceptorSlot
*
* Contains information about a slot plus its location
* @typedef {{
* slot: ItemAcceptorSlot,
* index: number,
* acceptedDirection: Direction
* }} ItemAcceptorLocatedSlot
*
* @typedef {{
* pos: Vector,
* directions: Direction[],
* layer?: Layer,
* filter?: ItemType
* }} ItemAcceptorSlotConfig
**/
export class ItemAcceptorComponent extends Component {
static getId() {
return "ItemAcceptor";
}
static getSchema() {
return {
slots: types.array(
types.structured({
pos: types.vector,
directions: types.array(types.enum(directions)),
filter: types.nullable(types.enum(itemTypes)),
// TODO: MIGRATE
layer: types.enum(layers),
})
),
animated: types.bool,
beltUnderlays: types.array(
types.structured({
pos: types.vector,
direction: types.enum(directions),
// TODO: MIGRATE
layer: types.enum(layers),
})
),
};
}
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,
layer: slot.layer,
});
}
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,
layer: underlay.layer,
});
}
return new ItemAcceptorComponent({
slots: slotsCopy,
beltUnderlays: beltUnderlaysCopy,
animated: this.animated,
});
}
/**
*
* @param {object} param0
* @param {Array<ItemAcceptorSlotConfig>} param0.slots The slots from which we accept items
* @param {boolean=} param0.animated Whether to animate item consumption
* @param {Array<{pos: Vector, direction: Direction, layer: Layer}>=} param0.beltUnderlays Where to render belt underlays
*/
constructor({ slots = [], beltUnderlays = [], animated = true }) {
super();
this.animated = animated;
/**
* Fixes belt animations
* @type {Array<{ item: BaseItem, slotIndex: number, animProgress: number, direction: Direction }>}
*/
this.itemConsumptionAnimations = [];
/* Which belt underlays to render */
this.beltUnderlays = beltUnderlays;
this.setSlots(slots);
}
/**
*
* @param {Array<ItemAcceptorSlotConfig>} slots
*/
setSlots(slots) {
/** @type {Array<ItemAcceptorSlot>} */
this.slots = [];
for (let i = 0; i < slots.length; ++i) {
const slot = slots[i];
this.slots.push({
pos: slot.pos,
directions: slot.directions,
layer: slot.layer || "regular",
// Which type of item to accept (shape | color | all) @see ItemType
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];
return this.filterMatches(slot.filter, item);
}
/**
* Returns if the given filter matches
* @param {ItemType|null} filter
* @param {BaseItem} item
*/
filterMatches(filter, item) {
if (!filter) {
return true;
}
const itemType = item.getItemType();
if (filter === "genericEnergy") {
return itemType === "positiveEnergy" || itemType === "negativeEnergy";
}
return itemType === filter;
}
/**
* Called when an item has been accepted so that
* @param {number} slotIndex
* @param {Direction} direction
* @param {BaseItem} item
*/
onItemAccepted(slotIndex, direction, item) {
if (this.animated) {
this.itemConsumptionAnimations.push({
item,
slotIndex,
direction,
animProgress: 0.0,
});
}
}
/**
* Tries to find a slot which accepts the current item
* @param {Vector} targetLocalTile
* @param {Direction} fromLocalDirection
* @param {Layer} layer
* @returns {ItemAcceptorLocatedSlot|null}
*/
findMatchingSlot(targetLocalTile, fromLocalDirection, layer) {
// 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 = invertedDirectionMap[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;
}
// Make sure the layer matches
if (slot.layer !== layer) {
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;
}
}