1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Implement saving and restoring belt paths

This commit is contained in:
tobspr
2020-06-26 18:24:02 +02:00
parent 9a6029279d
commit 42c569d91f
10 changed files with 181 additions and 294 deletions

View File

@@ -1,12 +1,9 @@
import { Component } from "../component";
import { Math_cos, Math_PI, Math_sin } from "../../core/builtins";
import { enumDirection, Vector } from "../../core/vector";
import { types } from "../../savegame/serialization";
import { gItemRegistry } from "../../core/global_registries";
import { BaseItem } from "../base_item";
import { Vector, enumDirection } from "../../core/vector";
import { Math_PI, Math_sin, Math_cos } from "../../core/builtins";
import { globalConfig } from "../../core/config";
import { Entity } from "../entity";
import { BeltPath } from "../belt_path";
import { Component } from "../component";
import { Entity } from "../entity";
export const curvedBeltLength = /* Math_PI / 4 */ 0.78;
@@ -19,7 +16,6 @@ export class BeltComponent extends Component {
// The followUpCache field is not serialized.
return {
direction: types.string,
sortedItems: types.array(types.pair(types.float, types.obj(gItemRegistry))),
};
}
@@ -37,9 +33,6 @@ export class BeltComponent extends Component {
this.direction = direction;
/** @type {Array<[number, BaseItem]>} */
this.sortedItems = [];
/** @type {Entity} */
this.followUpCache = null;
@@ -85,46 +78,4 @@ export class BeltComponent extends Component {
return new Vector(0, 0);
}
}
/**
* Returns if the belt can currently accept an item from the given direction
*/
canAcceptItem() {
const firstItem = this.sortedItems[0];
if (!firstItem) {
return true;
}
return firstItem[0] > globalConfig.itemSpacingOnBelts;
}
/**
* Pushes a new item to the belt
* @param {BaseItem} item
*/
takeItem(item, leftoverProgress = 0.0) {
if (G_IS_DEV) {
assert(
this.sortedItems.length === 0 ||
leftoverProgress <= this.sortedItems[0][0] - globalConfig.itemSpacingOnBelts + 0.001,
"Invalid leftover: " +
leftoverProgress +
" items are " +
this.sortedItems.map(item => item[0])
);
assert(leftoverProgress < 1.0, "Invalid leftover: " + leftoverProgress);
}
this.sortedItems.unshift([leftoverProgress, item]);
}
/**
* Returns how much space there is to the first item
*/
getDistanceToFirstItemCenter() {
const firstItem = this.sortedItems[0];
if (!firstItem) {
return 1;
}
return firstItem[0];
}
}