mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Add storage building
This commit is contained in:
@@ -60,7 +60,7 @@ export class BeltComponent extends Component {
|
||||
/**
|
||||
* Returns if the belt can currently accept an item from the given direction
|
||||
*/
|
||||
canAcceptNewItem(leftoverProgress = 0.0) {
|
||||
canAcceptItem(leftoverProgress = 0.0) {
|
||||
const firstItem = this.sortedItems[0];
|
||||
if (!firstItem) {
|
||||
return true;
|
||||
@@ -73,7 +73,7 @@ export class BeltComponent extends Component {
|
||||
* Pushes a new item to the belt
|
||||
* @param {BaseItem} item
|
||||
*/
|
||||
takeNewItem(item, leftoverProgress = 0.0) {
|
||||
takeItem(item, leftoverProgress = 0.0) {
|
||||
if (G_IS_DEV) {
|
||||
assert(
|
||||
this.sortedItems.length === 0 ||
|
||||
|
||||
80
src/js/game/components/storage.js
Normal file
80
src/js/game/components/storage.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import { Component } from "../component";
|
||||
import { types } from "../../savegame/serialization";
|
||||
import { gItemRegistry } from "../../core/global_registries";
|
||||
import { BaseItem } from "../base_item";
|
||||
import { ColorItem } from "../items/color_item";
|
||||
import { ShapeItem } from "../items/shape_item";
|
||||
|
||||
export class StorageComponent extends Component {
|
||||
static getId() {
|
||||
return "Storage";
|
||||
}
|
||||
|
||||
static getSchema() {
|
||||
return {
|
||||
maximumStorage: types.uint,
|
||||
storedCount: types.uint,
|
||||
storedItem: types.nullable(types.obj(gItemRegistry)),
|
||||
overlayOpacity: types.ufloat,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
if (item instanceof ColorItem) {
|
||||
return this.storedItem instanceof ColorItem && this.storedItem.color === item.color;
|
||||
}
|
||||
|
||||
if (item instanceof ShapeItem) {
|
||||
return (
|
||||
this.storedItem instanceof ShapeItem &&
|
||||
this.storedItem.definition.getHash() === item.definition.getHash()
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {BaseItem} item
|
||||
*/
|
||||
takeItem(item) {
|
||||
this.storedItem = item;
|
||||
this.storedCount++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user