From 22e4ec45adf1c9a69813e0feb86c8d821f7657e5 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Tue, 16 Mar 2021 23:54:39 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Szukanie=20s=C4=85siednich=20p=C3=B3=C5=82e?= =?UTF-8?q?k=20wg=20pozycji=20agenta.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/configuration.js | 6 ++++ src/modules/shop.js | 63 ++++++++++++++++++++++++++++++++++++ src/modules/utilities.js | 23 +++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/modules/configuration.js create mode 100644 src/modules/shop.js create mode 100644 src/modules/utilities.js diff --git a/src/modules/configuration.js b/src/modules/configuration.js new file mode 100644 index 0000000..12461f6 --- /dev/null +++ b/src/modules/configuration.js @@ -0,0 +1,6 @@ +export const UnitsInGroupCount = 6 +export const RowsOfGroupsCount = 6 +export const ColumnsOfGroupsCount = 3 +export const StorageCenterLocation = { x: 8, y: 8, w: UnitsInGroupCount, h: 1 } +// -1 by zarezerwować przestrzeń dla magazynu +export const UnitsCount = (ColumnsOfGroupsCount * RowsOfGroupsCount - 1) * UnitsInGroupCount diff --git a/src/modules/shop.js b/src/modules/shop.js new file mode 100644 index 0000000..a761e5d --- /dev/null +++ b/src/modules/shop.js @@ -0,0 +1,63 @@ +import { fromTable } from './utilities' +import { RowsOfGroupsCount, StorageCenterLocation, UnitsCount } from './configuration' + +/** + * Zwraca koordynaty półek przy danym kafelku ścieżki + * @param {number} gridX + * @param {number} gridY + * @returns {{x:number;y:number}[]} Koordynaty sąsiednich półek do kafelka + */ +export function nearbyStorageUnitsCoords(gridX, gridY) { + function outsideOfStorageCenter(v) { + const { x, y, w, h } = StorageCenterLocation + return v.x <= x || v.x >= x + w || v.y <= y || v.y >= y + h + } + return (gridX % UnitsInGroupCount == 0 + ? (gridY % 3 == 1 + ? /* skrzyżowanie */ [[-1, -1], [-1, 1], [1, -1], [1,1]] + : /* korytarz góra-dół */ [[-1, 0], [1, 0]]) + : /* korytarz lewo-prawo */ [[0, -1], [0, 1]]) + .map(([xoff, yoff]) => ({ x: gridX + xoff, y: yGrid + yoff })) + .filter(outsideOfStorageCenter) +} + +/** + * Zwraca indeksy półek przy danym kafelku ścieżki + * @param {number} gridX + * @param {number} gridY + * @returns {{x:number;y:number}[]} Indeksy sąsiednich półek do kafelka + */ +export function nearbyStorageUnitsIndexes(gridX, gridY) { + return nearbyStorageUnitsCoords(gridX, gridY) + .map(({ x, y }) => x * UnitsCount + y * UnitsCount * RowsOfGroupsCount) +} + +class Shop { + constructor() { + // pełna lista wszystkich możliwych produktów dostępnych w sklepie, + // w formacie [{ name: '', category: '', ... }, { name: '', category: '', ...}, ...] + // kolejność przedmiotów jest niezmienna, przez co można przechowywać ich indeksy zamiast referencji + this.products = fromTable( + ['name', 'category', 'intrest', '', 'icon'], + /* 0 */ ['Lodówka', 'AGD', 0.4, '', ''] + ) + + this.productPlacement = [...Array(UnitsCount)].map(_ => ({ + productId: null, + count: 0, + })) + } + + /** + * Zwraca listę produktów w okolicy danego kafelka ścieżki + * @param {number} gridX + * @param {number} gridY + */ + nearbyProducts(gridX, gridY) { + return nearbyStorageUnitsIndexes(gridX, gridY) + .map(i => this.productPlacement[i]) + } +} + +const shop = new Shop() +export default shop diff --git a/src/modules/utilities.js b/src/modules/utilities.js new file mode 100644 index 0000000..2203003 --- /dev/null +++ b/src/modules/utilities.js @@ -0,0 +1,23 @@ +/** + * Converts table like data to array of objects, where keys are from header + * and values are at the same offset as header for that field + * @param {string[]} header + * @param {...any[]} rows + * @returns + */ +export function fromTable(header, ...rows) { + function tupleToRecord(names, array) { + return names.reduce((p, v, i) => ({ ...p, [v]: array[i] }), {}) + } + return rows.reduce((p, row) => [...p, tupleToRecord(header, row)], []) +} + +/** + * returns v truncated to range [lo, hi] + * @param {number} v Value possibly out of range + * @param {number} lo Minimum return value + * @param {number} hi Maximum return value + * */ +export function clamp(v, lo, hi) { + return v <= lo ? lo : v >= hi ? hi : v +} From 40d39db313083477da9c393c4611198e2d00ab4a Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Wed, 17 Mar 2021 00:06:28 +0100 Subject: [PATCH 2/2] Zgeneralizowany i zbugfixowany magazyn --- src/modules/configuration.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/modules/configuration.js b/src/modules/configuration.js index 12461f6..406dc4d 100644 --- a/src/modules/configuration.js +++ b/src/modules/configuration.js @@ -1,6 +1,7 @@ export const UnitsInGroupCount = 6 export const RowsOfGroupsCount = 6 export const ColumnsOfGroupsCount = 3 -export const StorageCenterLocation = { x: 8, y: 8, w: UnitsInGroupCount, h: 1 } -// -1 by zarezerwować przestrzeń dla magazynu -export const UnitsCount = (ColumnsOfGroupsCount * RowsOfGroupsCount - 1) * UnitsInGroupCount +export const StorageCenterLocation = { x: 7, y: 8, w: UnitsInGroupCount, h: 1 } + +export const UnitsCount = ((ColumnsOfGroupsCount * RowsOfGroupsCount) * UnitsInGroupCount + - StorageCenterLocation.w * StorageCenterLocation.h)