2020-06-24 20:23:10 +00:00
|
|
|
import { types } from "../../savegame/serialization";
|
|
|
|
|
import { BaseItem } from "../base_item";
|
|
|
|
|
import { Component } from "../component";
|
|
|
|
|
import { ShapeItem } from "../items/shape_item";
|
|
|
|
|
|
|
|
|
|
const maxQueueSize = 10;
|
|
|
|
|
|
|
|
|
|
export class EnergyGeneratorComponent extends Component {
|
|
|
|
|
static getId() {
|
|
|
|
|
return "EnergyGenerator";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getSchema() {
|
|
|
|
|
return {
|
|
|
|
|
requiredKey: types.string,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {object} param0
|
|
|
|
|
* @param {string} param0.requiredKey Which shape this generator needs, can be null if not computed yet
|
|
|
|
|
*/
|
|
|
|
|
constructor({ requiredKey }) {
|
|
|
|
|
super();
|
|
|
|
|
this.requiredKey = requiredKey;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stores how many items are ready to be converted to energy
|
|
|
|
|
* @type {number}
|
|
|
|
|
*/
|
|
|
|
|
this.itemsInQueue = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {BaseItem} item
|
|
|
|
|
*/
|
|
|
|
|
tryTakeItem(item) {
|
2020-06-27 08:38:11 +00:00
|
|
|
if (/** @type {ShapeItem} */ (item).definition.getHash() !== this.requiredKey) {
|
2020-06-24 20:23:10 +00:00
|
|
|
// Not our shape
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.itemsInQueue >= maxQueueSize) {
|
|
|
|
|
// Queue is full
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Take item and put it into the queue
|
|
|
|
|
++this.itemsInQueue;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|