@jeremyckahn/farmhand
Version:
A farming game
92 lines (84 loc) • 1.92 kB
text/typescript
import {
fieldMode,
itemType,
cropFamily as cropFamilyType,
grapeVariety,
} from '../enums.js'
import { getCropLifecycleDuration } from '../utils/getCropLifecycleDuration.js'
const { freeze } = Object
interface CropArgs extends Partial<farmhand.item> {
cropTimeline?: number[]
}
export const crop = ({
cropTimeline = [],
growsInto,
tier = 1,
isSeed = Boolean(growsInto),
cropLifecycleDuration = getCropLifecycleDuration({ cropTimeline }),
id = '',
name = '',
...rest
}: CropArgs): farmhand.item =>
freeze({
id,
name,
cropTimeline,
doesPriceFluctuate: true,
tier,
type: itemType.CROP,
value: 10 + cropLifecycleDuration * tier * (isSeed ? 1 : 3),
...(isSeed && {
enablesFieldMode: fieldMode.PLANT,
growsInto,
isPlantableCrop: true,
}),
...rest,
})
interface FromSeedConfig {
variantIdx?: number
canBeFermented?: boolean
}
export const fromSeed = (
{
cropTimeline,
cropType,
growsInto,
tier = 1,
highDemandSeasons,
lowDemandSeasons,
}: farmhand.item,
{ variantIdx = 0, canBeFermented = false }: FromSeedConfig = {}
): Partial<farmhand.item> => {
const variants = Array.isArray(growsInto) ? growsInto : [growsInto]
return {
id: variants[variantIdx] || '',
cropTimeline: cropTimeline || [],
cropType,
doesPriceFluctuate: true,
tier,
type: itemType.CROP,
highDemandSeasons,
lowDemandSeasons,
...(canBeFermented &&
cropTimeline && {
daysToFerment: getCropLifecycleDuration({ cropTimeline }) * tier,
}),
}
}
export const cropVariety = ({
imageId,
cropFamily,
variety,
...cropVarietyProperties
}: {
imageId: string
cropFamily: cropFamilyType
variety: grapeVariety
} & Partial<farmhand.item>): farmhand.cropVariety => {
return {
imageId,
cropFamily,
variety,
...crop({ ...cropVarietyProperties }),
}
}