2020-05-09 16:45:23 +02:00
|
|
|
import { Component } from "../component";
|
2020-05-17 10:07:20 +02:00
|
|
|
import { Vector, enumDirection, enumInvertedDirections } from "../../core/vector";
|
2020-05-09 16:45:23 +02:00
|
|
|
import { BaseItem } from "../base_item";
|
|
|
|
import { ShapeItem } from "../items/shape_item";
|
|
|
|
import { ColorItem } from "../items/color_item";
|
2020-05-14 21:54:11 +02:00
|
|
|
import { types } from "../../savegame/serialization";
|
2020-05-09 16:45:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @enum {string?}
|
|
|
|
*/
|
|
|
|
export const enumItemAcceptorItemFilter = {
|
|
|
|
shape: "shape",
|
|
|
|
color: "color",
|
|
|
|
none: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @typedef {{
|
|
|
|
* pos: Vector,
|
|
|
|
* directions: enumDirection[],
|
|
|
|
* filter?: enumItemAcceptorItemFilter
|
|
|
|
* }} ItemAcceptorSlot */
|
|
|
|
|
|
|
|
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(enumItemAcceptorItemFilter)),
|
|
|
|
})
|
|
|
|
),
|
2020-05-17 10:07:20 +02:00
|
|
|
animated: types.bool,
|
|
|
|
beltUnderlays: types.array(
|
|
|
|
types.structured({
|
|
|
|
pos: types.vector,
|
|
|
|
direction: types.enum(enumDirection),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
|
|
|
|
// We don't actually need to store the animations
|
|
|
|
// itemConsumptionAnimations: types.array(
|
|
|
|
// types.structured({
|
|
|
|
// item: types.obj(gItemRegistry),
|
|
|
|
// slotIndex: types.uint,
|
|
|
|
// animProgress: types.float,
|
|
|
|
// direction: types.enum(enumDirection),
|
|
|
|
// })
|
|
|
|
// ),
|
2020-05-09 16:45:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} param0
|
|
|
|
* @param {Array<{pos: Vector, directions: enumDirection[], filter?: enumItemAcceptorItemFilter}>} 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?: enumItemAcceptorItemFilter}>} slots
|
|
|
|
*/
|
|
|
|
setSlots(slots) {
|
|
|
|
/** @type {Array<{pos: Vector, directions: enumDirection[], filter?: enumItemAcceptorItemFilter}>} */
|
|
|
|
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 enumItemAcceptorItemFilter
|
|
|
|
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];
|
|
|
|
switch (slot.filter) {
|
|
|
|
case enumItemAcceptorItemFilter.shape: {
|
|
|
|
return item instanceof ShapeItem;
|
|
|
|
}
|
|
|
|
case enumItemAcceptorItemFilter.color: {
|
|
|
|
return item instanceof ColorItem;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {{
|
|
|
|
* slot: ItemAcceptorSlot,
|
|
|
|
* index: number,
|
|
|
|
* acceptedDirection: enumDirection
|
|
|
|
* }|null}
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|