Szukanie sąsiednich półek wg pozycji agenta.
This commit is contained in:
parent
3388743f4a
commit
22e4ec45ad
6
src/modules/configuration.js
Normal file
6
src/modules/configuration.js
Normal file
@ -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
|
63
src/modules/shop.js
Normal file
63
src/modules/shop.js
Normal file
@ -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, '', '<path-to-icon>']
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
23
src/modules/utilities.js
Normal file
23
src/modules/utilities.js
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user