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/storage.js

97 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-05-20 13:51:06 +00:00
import { types } from "../../savegame/serialization";
2020-08-15 16:39:08 +00:00
import { BaseItem } from "../base_item";
import { Component } from "../component";
import { typeItemSingleton } from "../item_resolver";
import { ColorItem } from "../items/color_item";
import { ShapeItem } from "../items/shape_item";
2020-05-20 13:51:06 +00:00
export class StorageComponent extends Component {
static getId() {
return "Storage";
}
static getSchema() {
return {
storedCount: types.uint,
storedItem: types.nullable(typeItemSingleton),
2020-05-20 13:51:06 +00:00
};
}
2020-05-27 12:30:59 +00:00
duplicateWithoutContents() {
return new StorageComponent({ maximumStorage: this.maximumStorage });
}
2020-05-20 13:51:06 +00:00
/**
* @param {object} param0
* @param {number=} param0.maximumStorage How much this storage can hold
*/
constructor({ maximumStorage = 1e20 }) {
super();
this.maximumStorage = maximumStorage;
/**
* Currently stored item
* @type {BaseItem}
*/
this.storedItem = null;
/**
* How many of this item we have stored
*/
this.storedCount = 0;
/**
* We compute an opacity to make sure it doesn't flicker
*/
this.overlayOpacity = 0;
}
/**
* Returns whether this storage can accept the item
* @param {BaseItem} item
*/
canAcceptItem(item) {
if (this.storedCount >= this.maximumStorage) {
return false;
}
if (!this.storedItem || this.storedCount === 0) {
return true;
}
const itemType = item.getItemType();
// Check type matches
if (itemType !== this.storedItem.getItemType()) {
return false;
2020-05-20 13:51:06 +00:00
}
2020-08-15 16:39:08 +00:00
if (itemType === "color") {
return /** @type {ColorItem} */ (this.storedItem).color === /** @type {ColorItem} */ (item).color;
}
2020-08-15 16:39:08 +00:00
if (itemType === "shape") {
2020-05-20 13:51:06 +00:00
return (
/** @type {ShapeItem} */ (this.storedItem).definition.getHash() ===
/** @type {ShapeItem} */ (item).definition.getHash()
2020-05-20 13:51:06 +00:00
);
}
return false;
}
/**
* Returns whether the storage is full
* @returns {boolean}
*/
getIsFull() {
return this.storedCount >= this.maximumStorage;
}
2020-05-20 13:51:06 +00:00
/**
* @param {BaseItem} item
*/
takeItem(item) {
this.storedItem = item;
this.storedCount++;
}
}