Merge branch 'origin/daytime-emulation'

This commit is contained in:
Robert Bendun 2021-03-28 22:58:27 +02:00
commit da5bcc30c7
3 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,7 @@
export const UnitsInGroupCount = 6
export const RowsOfGroupsCount = 6
export const ColumnsOfGroupsCount = 3
export const StorageCenterLocation = { x: 7, y: 8, w: UnitsInGroupCount, h: 1 }
export const UnitsCount = ((ColumnsOfGroupsCount * RowsOfGroupsCount) * UnitsInGroupCount
- StorageCenterLocation.w * StorageCenterLocation.h)

63
src/modules/shop.js Normal file
View 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
View 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
}