1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-02-16 21:09:22 +00:00
tobspr_shapez.io/src/js/game/components/item_ejector.js

162 lines
4.2 KiB
JavaScript
Raw Normal View History

import { enumDirection, enumDirectionToVector, Vector } from "../../core/vector";
import { types } from "../../savegame/serialization";
2020-05-09 14:45:23 +00:00
import { BaseItem } from "../base_item";
import { BeltPath } from "../belt_path";
2020-05-09 14:45:23 +00:00
import { Component } from "../component";
import { Entity } from "../entity";
import { typeItemSingleton } from "../item_resolver";
2020-05-09 14:45:23 +00:00
/**
* @typedef {{
* pos: Vector,
* direction: enumDirection,
* item: BaseItem,
* progress: number?,
* cachedDestSlot?: import("./item_acceptor").ItemAcceptorLocatedSlot,
2020-08-10 13:02:14 +00:00
* cachedBeltPath?: BeltPath,
* cachedTargetEntity?: Entity
2020-05-09 14:45:23 +00:00
* }} ItemEjectorSlot
*/
export class ItemEjectorComponent extends Component {
static getId() {
return "ItemEjector";
}
static getSchema() {
2020-08-10 18:32:45 +00:00
// The cachedDestSlot, cachedTargetEntity fields are not serialized.
2020-05-09 14:45:23 +00:00
return {
2020-05-14 19:54:11 +00:00
slots: types.array(
types.structured({
item: types.nullable(typeItemSingleton),
progress: types.float,
2020-05-14 19:54:11 +00:00
})
),
2020-05-09 14:45:23 +00:00
};
}
2020-05-27 12:30:59 +00:00
duplicateWithoutContents() {
const slotsCopy = [];
for (let i = 0; i < this.slots.length; ++i) {
const slot = this.slots[i];
slotsCopy.push({
pos: slot.pos.copy(),
direction: slot.direction,
});
}
return new ItemEjectorComponent({
slots: slotsCopy,
});
}
2020-05-09 14:45:23 +00:00
/**
*
* @param {object} param0
* @param {Array<{pos: Vector, direction: enumDirection }>=} param0.slots The slots to eject on
2020-05-09 14:45:23 +00:00
*/
2020-08-10 18:32:45 +00:00
constructor({ slots = [] }) {
2020-05-09 14:45:23 +00:00
super();
this.setSlots(slots);
/**
* Whether this ejector slot is enabled
*/
this.enabled = true;
2020-05-09 14:45:23 +00:00
}
/**
* @param {Array<{pos: Vector, direction: enumDirection }>} slots The slots to eject on
2020-05-09 14:45:23 +00:00
*/
setSlots(slots) {
/** @type {Array<ItemEjectorSlot>} */
this.slots = [];
for (let i = 0; i < slots.length; ++i) {
const slot = slots[i];
this.slots.push({
pos: slot.pos,
direction: slot.direction,
item: null,
progress: 0,
cachedDestSlot: null,
cachedTargetEntity: null,
2020-05-09 14:45:23 +00:00
});
}
}
/**
* Returns where this slot ejects to
* @param {ItemEjectorSlot} slot
2020-05-09 14:45:23 +00:00
* @returns {Vector}
*/
getSlotTargetLocalTile(slot) {
2020-05-09 14:45:23 +00:00
const directionVector = enumDirectionToVector[slot.direction];
return slot.pos.add(directionVector);
}
/**
* Returns whether any slot ejects to the given local tile
* @param {Vector} tile
*/
anySlotEjectsToLocalTile(tile) {
2020-05-09 14:45:23 +00:00
for (let i = 0; i < this.slots.length; ++i) {
if (this.getSlotTargetLocalTile(this.slots[i]).equals(tile)) {
2020-05-09 14:45:23 +00:00
return true;
}
}
return false;
}
/**
* Returns if we can eject on a given slot
* @param {number} slotIndex
* @returns {boolean}
*/
canEjectOnSlot(slotIndex) {
assert(slotIndex >= 0 && slotIndex < this.slots.length, "Invalid ejector slot: " + slotIndex);
return !this.slots[slotIndex].item;
}
/**
* Returns the first free slot on this ejector or null if there is none
* @returns {number?}
*/
getFirstFreeSlot() {
2020-05-09 14:45:23 +00:00
for (let i = 0; i < this.slots.length; ++i) {
if (this.canEjectOnSlot(i)) {
2020-05-09 14:45:23 +00:00
return i;
}
}
return null;
}
/**
* Tries to eject a given item
* @param {number} slotIndex
* @param {BaseItem} item
* @returns {boolean}
*/
tryEject(slotIndex, item) {
if (!this.canEjectOnSlot(slotIndex)) {
return false;
}
this.slots[slotIndex].item = item;
2020-08-10 18:32:45 +00:00
this.slots[slotIndex].progress = 0;
2020-05-09 14:45:23 +00:00
return true;
}
2020-06-26 11:57:07 +00:00
/**
* Clears the given slot and returns the item it had
* @param {number} slotIndex
* @returns {BaseItem|null}
*/
takeSlotItem(slotIndex) {
const slot = this.slots[slotIndex];
const item = slot.item;
slot.item = null;
slot.progress = 0.0;
return item;
}
2020-05-09 14:45:23 +00:00
}