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

188 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-05-09 14:45:23 +00:00
import { Vector, enumDirection, enumDirectionToVector } from "../../core/vector";
import { BaseItem } from "../base_item";
import { Component } from "../component";
2020-05-14 19:54:11 +00:00
import { types } from "../../savegame/serialization";
import { gItemRegistry } from "../../core/global_registries";
2020-05-09 14:45:23 +00:00
/**
* @typedef {{
* pos: Vector,
* direction: enumDirection,
* item: BaseItem,
* progress: number?
* }} ItemEjectorSlot
*/
export class ItemEjectorComponent extends Component {
static getId() {
return "ItemEjector";
}
static getSchema() {
return {
2020-05-14 19:54:11 +00:00
instantEject: types.bool,
slots: types.array(
types.structured({
pos: types.vector,
direction: types.enum(enumDirection),
item: types.nullable(types.obj(gItemRegistry)),
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-06-01 09:45:59 +00:00
instantEject: this.instantEject,
2020-05-27 12:30:59 +00:00
});
}
2020-05-09 14:45:23 +00:00
/**
*
* @param {object} param0
2020-05-16 22:21:33 +00:00
* @param {Array<{pos: Vector, direction: enumDirection}>=} param0.slots The slots to eject on
2020-05-09 14:45:23 +00:00
* @param {boolean=} param0.instantEject If the ejection is instant
*/
2020-05-14 19:54:11 +00:00
constructor({ slots = [], instantEject = false }) {
2020-05-09 14:45:23 +00:00
super();
// How long items take to eject
this.instantEject = instantEject;
this.setSlots(slots);
}
/**
* @param {Array<{pos: Vector, direction: enumDirection}>} slots The slots to eject on
*/
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,
});
}
}
/**
* Returns the amount of slots
*/
getNumSlots() {
return this.slots.length;
}
/**
* Returns where this slot ejects to
* @param {number} index
* @returns {Vector}
*/
getSlotTargetLocalTile(index) {
const slot = this.slots[index];
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) {
for (let i = 0; i < this.slots.length; ++i) {
if (this.getSlotTargetLocalTile(i).equals(tile)) {
return true;
}
}
return false;
}
/**
* Returns if slot # is currently ejecting
* @param {number} slotIndex
* @returns {boolean}
*/
isSlotEjecting(slotIndex) {
assert(slotIndex >= 0 && slotIndex < this.slots.length, "Invalid ejector slot: " + slotIndex);
return !!this.slots[slotIndex].item;
}
/**
* 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() {
for (let i = 0; i < this.slots.length; ++i) {
if (this.canEjectOnSlot(i)) {
return i;
}
}
return null;
}
/**
* Returns if any slot is ejecting
* @returns {boolean}
*/
isAnySlotEjecting() {
for (let i = 0; i < this.slots.length; ++i) {
if (this.slots[i].item) {
return true;
}
}
return false;
}
/**
* Returns if any slot is free
* @returns {boolean}
*/
hasAnySlotFree() {
for (let i = 0; i < this.slots.length; ++i) {
if (this.canEjectOnSlot(i)) {
return true;
}
}
return false;
}
/**
* 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;
this.slots[slotIndex].progress = this.instantEject ? 1 : 0;
return true;
}
}