chore: Introduction to agent resetting

This commit is contained in:
Marcin Czerniak 2021-06-20 00:25:32 +02:00
parent a87d4ecb2f
commit 84ef4a5290
3 changed files with 29 additions and 5 deletions

View File

@ -9,3 +9,4 @@ const UnitsCount = ((ColumnsOfGroupsCount * RowsOfGroupsCount) * UnitsInGroupCou
const AgentMoveSpeed = 60 /* 1 - slowest, 89 - fastest */ const AgentMoveSpeed = 60 /* 1 - slowest, 89 - fastest */
const AgentRotationSpeed = 60 /* 1 - slowest, 89 - fastest */ const AgentRotationSpeed = 60 /* 1 - slowest, 89 - fastest */
const AgentWaitTime = 500 /* in miliseconds */ const AgentWaitTime = 500 /* in miliseconds */
const AgentStartPosition = { x: 6, y: 0 }

View File

@ -57,4 +57,13 @@ class Agent extends AgentController {
this.ownedProductAmount = amount; this.ownedProductAmount = amount;
this.ownedProduct = product; this.ownedProduct = product;
} }
/**
* Cancel all agent actions, teleport to start position
*/
reset() {
this.cancelAllActions();
this.position = AgentStartPosition;
}
} }

View File

@ -16,7 +16,7 @@ class AgentController {
this.currentAction = this.actions.STATIONARY; this.currentAction = this.actions.STATIONARY;
// Pozycja i rotacja // Pozycja i rotacja
this.position = { x: 6, y: 0 }; this.position = AgentStartPosition;
this.rotation = { z: 0 }; this.rotation = { z: 0 };
this.update(); this.update();
} }
@ -95,6 +95,8 @@ class AgentController {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const cycle = () => { const cycle = () => {
if (this.currentAction == this.actions.STATIONARY) return resolve();
let { speed } = this; let { speed } = this;
speed *= (actionCost - 1) * 4 + 1; speed *= (actionCost - 1) * 4 + 1;
@ -259,13 +261,17 @@ class AgentController {
} }
findPathTo(dx, dy, sx, sy) { findPathTo(dx, dy, sx, sy) {
if (Grid.instance.getGrid()[dx][dy] == GRID_FIELD_TYPE.SHELF || Grid.instance.getGrid()[dx][dy] == GRID_FIELD_TYPE.STORAGE) { const position = { x: sx, y: sy };
const rotation = this.rotation.z;
if (Grid.instance.getGrid()[dx][dy] == GRID_FIELD_TYPE.SHELF ||
Grid.instance.getGrid()[dx][dy] == GRID_FIELD_TYPE.STORAGE) {
if (Grid.instance.getGrid()[dx][dy - 1] == GRID_FIELD_TYPE.PATH) { if (Grid.instance.getGrid()[dx][dy - 1] == GRID_FIELD_TYPE.PATH) {
return Pathfinding.search({ position: { x: sx, y: sy }, rotation: this.rotation.z }, { position: { x: dx, y: dy - 1 }, rotation: 180 }); return Pathfinding.search({ position, rotation }, { position: { x: dx, y: dy - 1 }, rotation: 180 });
} }
return Pathfinding.search({ position: { x: sx, y: sy }, rotation: this.rotation.z }, { position: { x: dx, y: dy + 1 }, rotation: 0 }); return Pathfinding.search({ position, rotation }, { position: { x: dx, y: dy + 1 }, rotation: 0 });
} }
return Pathfinding.search({ position: { x: sx, y: sy }, rotation: this.rotation.z }, { position: { x: dx, y: dy }, rotation: this.rotation.z }); return Pathfinding.search({ position, rotation }, { position: { x: dx, y: dy }, rotation });
} }
clearCanvas() { clearCanvas() {
@ -288,4 +294,12 @@ class AgentController {
this.ctx.closePath(); this.ctx.closePath();
this.ctx.stroke(); this.ctx.stroke();
} }
cancelAllActions() {
this.ownedProduct = Product.REGISTRY.empty;
this.ownedProductAmount = 0;
this.currentPath = [];
this.currentAction = this.actions.STATIONARY;
this.currentPathIndex = 0;
}
} }