This commit is contained in:
Robert Bendun 2021-04-26 19:53:54 +02:00
commit 8cd815418f
5 changed files with 18 additions and 16 deletions

View File

@ -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 */

View File

@ -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;
}

View File

@ -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 = [];

View File

@ -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;
}));

View File

@ -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 {