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 +}