diff --git a/src/main.js b/src/main.js index 32cd266..b253951 100644 --- a/src/main.js +++ b/src/main.js @@ -8,6 +8,4 @@ window.addEventListener('DOMContentLoaded', () => document.fonts.ready.then(() = const grid = new Grid(gridCanvas); const agent = new Agent(agentCanvas); const products = new Products(productsCanvas); - - window.products = products; })); \ No newline at end of file diff --git a/src/modules/agent.js b/src/modules/agent.js index 75aa379..5db31e0 100644 --- a/src/modules/agent.js +++ b/src/modules/agent.js @@ -3,19 +3,14 @@ class Agent extends AgentController { super(canvas); const cycle = async () => { - - // await this.takeFromStore(Product.RANDOM_FROM_REGISTRY(), Math.floor(Math.random() * 40) + 10); - // const randomPlace = Grid.instance.randomPlace(); - // await this.deliver(randomPlace.x, randomPlace.y); - const jobRequest = window.products.getJobRequest(); + const jobRequest = Products.instance.getJobRequest(); if (jobRequest) { - console.log(jobRequest); const requiredAmount = 50 - jobRequest.amount; await this.takeFromStore(jobRequest.product, requiredAmount); await this.deliver(jobRequest.x, jobRequest.y); } - await waitFor(2000); + await waitFor(AgentWaitTime); cycle(); }; @@ -39,7 +34,8 @@ class Agent extends AgentController { */ async deliver(x, y) { await this.moveTo(x, y); - window.products.exchange(x, y, this.ownedProduct, 50); + await waitFor(AgentWaitTime); + Products.instance.exchange(x, y, this.ownedProduct, 50); this.ownedProductAmount = 0; this.ownedProduct = Product.REGISTRY.empty; } @@ -50,7 +46,7 @@ class Agent extends AgentController { */ async takeFromStore(product, amount) { await this.moveTo(9, 8); - await waitFor(1000); + await waitFor(AgentWaitTime); this.ownedProductAmount = amount; this.ownedProduct = product; } diff --git a/src/modules/agentController.js b/src/modules/agentController.js index d9dd7d6..fea335a 100644 --- a/src/modules/agentController.js +++ b/src/modules/agentController.js @@ -18,8 +18,8 @@ class AgentController { // Pozycja i poruszanie siÄ™ this.position = { x: 6, y: 0 }; this.rotation = { z: 0 }; - this.speed = 30; - this.rotationSpeed = 2; + this.speed = 90 - AgentMoveSpeed; + this.rotationSpeed = 90 / ( 90 - AgentRotationSpeed); this.update(); } @@ -64,6 +64,8 @@ class AgentController { } else { cycle(); } + } else { + resolve(); } } @@ -156,7 +158,7 @@ class AgentController { this.rotation = { z: clampRotation(oldZRotation + (this.rotationSpeed * sign)) }; - if (Math.abs(oldZRotation - newZRotation) < 1) { + if (Math.abs(oldZRotation - newZRotation) < 5) { resolve(); this.rotation = { z: clampRotation(newZRotation) } } else { diff --git a/src/modules/configuration.js b/src/modules/configuration.js index 2163e2f..fbec95d 100644 --- a/src/modules/configuration.js +++ b/src/modules/configuration.js @@ -5,3 +5,7 @@ const StorageCenterLocation = { x: 7, y: 8, w: UnitsInGroupCount, h: 1 } const UnitsCount = ((ColumnsOfGroupsCount * RowsOfGroupsCount) * UnitsInGroupCount - StorageCenterLocation.w * StorageCenterLocation.h) + +const AgentMoveSpeed = 20 /* 1 - slowest, 89 - fastest */ +const AgentRotationSpeed = 20 /* 1 - slowest, 89 - fastest */ +const AgentWaitTime = 500 /* in miliseconds */ diff --git a/src/modules/pathfinding.js b/src/modules/pathfinding.js index 62ac5a8..823b491 100644 --- a/src/modules/pathfinding.js +++ b/src/modules/pathfinding.js @@ -16,7 +16,6 @@ class Pathfinding { return Pathfinding.graphSearch({ state }, { state: expectedState }) } - static graphSearch(node, expectedNode, fringe = [], explored = []) { fringe.push(node); @@ -34,13 +33,16 @@ class Pathfinding { explored.push(elem); for (const successor of this.successor(elem)) { - if (explored.every(n => !Pathfinding.reached(n.state, successor.state))) { + if (!Pathfinding.isNodeInArr(successor, explored) && !Pathfinding.isNodeInArr(successor, fringe)) { fringe.push(successor); } } } } + static isNodeInArr(node, arr) { + return !arr.every(n => !Pathfinding.reached(n.state, node.state)) + } static successor(node) { const list = [];