From e594b6a4a7864750c30fcf80eeb8d0ee9887f57a Mon Sep 17 00:00:00 2001 From: tobspr Date: Fri, 26 Jun 2020 17:28:19 +0200 Subject: [PATCH] Add belt rendering (very slow for now) --- src/js/game/belt_path.js | 54 ++++++++++++++++++++--------- src/js/game/systems/belt.js | 4 ++- src/js/game/systems/item_ejector.js | 6 ++-- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/js/game/belt_path.js b/src/js/game/belt_path.js index 48d6a93e..0de39526 100644 --- a/src/js/game/belt_path.js +++ b/src/js/game/belt_path.js @@ -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]; + } + } } diff --git a/src/js/game/systems/belt.js b/src/js/game/systems/belt.js index 92de413b..04d66b2b 100644 --- a/src/js/game/systems/belt.js +++ b/src/js/game/systems/belt.js @@ -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); + } } /** diff --git a/src/js/game/systems/item_ejector.js b/src/js/game/systems/item_ejector.js index d6597ecd..b5da836a 100644 --- a/src/js/game/systems/item_ejector.js +++ b/src/js/game/systems/item_ejector.js @@ -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; } }