2020-05-09 14:45:23 +00:00
|
|
|
import { types } from "../../savegame/serialization";
|
2020-05-16 20:45:40 +00:00
|
|
|
import { BaseItem } from "../base_item";
|
2020-08-14 11:09:10 +00:00
|
|
|
import { Component } from "../component";
|
|
|
|
import { typeItemSingleton } from "../item_resolver";
|
2020-05-16 20:45:40 +00:00
|
|
|
|
2020-05-17 13:32:19 +00:00
|
|
|
const chainBufferSize = 3;
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
export class MinerComponent extends Component {
|
|
|
|
static getId() {
|
|
|
|
return "Miner";
|
|
|
|
}
|
|
|
|
|
|
|
|
static getSchema() {
|
2020-06-17 19:36:53 +00:00
|
|
|
// cachedMinedItem is not serialized.
|
2020-05-09 14:45:23 +00:00
|
|
|
return {
|
|
|
|
lastMiningTime: types.ufloat,
|
2020-08-14 11:09:10 +00:00
|
|
|
itemChainBuffer: types.array(typeItemSingleton),
|
2020-05-09 14:45:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-27 12:30:59 +00:00
|
|
|
duplicateWithoutContents() {
|
|
|
|
return new MinerComponent({
|
|
|
|
chainable: this.chainable,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:36:53 +00:00
|
|
|
constructor({ chainable = false }) {
|
2020-05-09 14:45:23 +00:00
|
|
|
super();
|
|
|
|
this.lastMiningTime = 0;
|
2020-05-16 20:45:40 +00:00
|
|
|
this.chainable = chainable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores items from other miners which were chained to this
|
|
|
|
* miner.
|
|
|
|
* @type {Array<BaseItem>}
|
|
|
|
*/
|
|
|
|
this.itemChainBuffer = [];
|
2020-06-16 20:02:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {BaseItem}
|
|
|
|
*/
|
2020-06-17 19:36:53 +00:00
|
|
|
this.cachedMinedItem = null;
|
2020-05-16 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {BaseItem} item
|
|
|
|
*/
|
|
|
|
tryAcceptChainedItem(item) {
|
|
|
|
if (this.itemChainBuffer.length > chainBufferSize) {
|
|
|
|
// Well, this one is full
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.itemChainBuffer.push(item);
|
|
|
|
return true;
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
}
|