Add belt rendering (very slow for now)

This commit is contained in:
tobspr 2020-06-26 17:28:19 +02:00
parent a71c0b8039
commit e594b6a4a7
3 changed files with 44 additions and 20 deletions

View File

@ -58,6 +58,27 @@ export class BeltPath {
this.debug_checkIntegrity("constructor");
}
/**
* Returns whether this path can accept a new item
* @returns {boolean}
*/
canAcceptItem() {
return this.spacingToFirstItem >= globalConfig.itemSpacingOnBelts;
}
/**
* Tries to accept the item
* @param {BaseItem} item
*/
tryAcceptItem(item) {
if (this.spacingToFirstItem >= globalConfig.itemSpacingOnBelts) {
this.items.unshift([this.spacingToFirstItem, item]);
this.spacingToFirstItem = 0;
return true;
}
return false;
}
/**
* Helper to throw an error on mismatch
* @param {string} change
@ -174,7 +195,7 @@ export class BeltPath {
for (let i = 0; i < this.items.length; ++i) {
const item = this.items[i];
if (item[_nextDistance] < 0 || item[_nextDistance] > this.totalLength) {
if (item[_nextDistance] < 0 || item[_nextDistance] > this.totalLength + 0.02) {
return fail(
"Item has invalid offset to next item: ",
item[_nextDistance],
@ -777,21 +798,6 @@ export class BeltPath {
*/
update() {
this.debug_checkIntegrity("pre-update");
const firstBeltItems = this.initialBeltComponent.sortedItems;
const transferItemAndProgress = firstBeltItems[0];
// Check if the first belt took a new item
if (transferItemAndProgress) {
const transferItem = transferItemAndProgress[_item];
if (this.spacingToFirstItem >= globalConfig.itemSpacingOnBelts) {
// Can take new item
firstBeltItems.splice(0, 1);
this.items.unshift([this.spacingToFirstItem, transferItem]);
this.spacingToFirstItem = 0;
}
}
// Divide by item spacing on belts since we use throughput and not speed
let beltSpeed =
@ -930,4 +936,20 @@ export class BeltPath {
parameters.context.fillStyle = "purple";
parameters.context.fillRect(firstItemIndicator.x - 3, firstItemIndicator.y - 1, 6, 2);
}
/**
* Draws the path
* @param {DrawParameters} parameters
*/
draw(parameters) {
let progress = this.spacingToFirstItem;
for (let i = 0; i < this.items.length; ++i) {
const nextDistanceAndItem = this.items[i];
const worldPos = this.computePositionFromProgress(progress).toWorldSpaceCenterOfTile();
if (parameters.visibleRect.containsCircle(worldPos.x, worldPos.y, 10)) {
nextDistanceAndItem[_item].draw(worldPos.x, worldPos.y, parameters);
}
progress += nextDistanceAndItem[_nextDistance];
}
}
}

View File

@ -227,7 +227,9 @@ export class BeltSystem extends GameSystemWithFilter {
}
draw(parameters) {
this.forEachMatchingEntityOnScreen(parameters, this.drawEntityItems.bind(this));
for (let i = 0; i < this.beltPaths.length; ++i) {
this.beltPaths[i].draw(parameters);
}
}
/**

View File

@ -239,9 +239,9 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
const beltComp = receiver.components.Belt;
if (beltComp) {
// Ayy, its a belt!
if (beltComp.canAcceptItem()) {
beltComp.takeItem(item);
const path = beltComp.assignedPath;
assert(path, "belt has no path");
if (path.tryAcceptItem(item)) {
return true;
}
}