@jeremyckahn/farmhand
Version:
A farming game
36 lines (30 loc) • 1.05 kB
JavaScript
import { itemsMap } from '../../data/maps.js'
import { areHuggingMachinesInInventory } from '../../utils/index.js'
import { HUGGING_MACHINE_ITEM_ID } from '../../constants.js'
import { addItemToInventory } from './addItemToInventory.js'
import { decrementItemFromInventory } from './decrementItemFromInventory.js'
import { modifyCow } from './modifyCow.js'
/**
* @param {farmhand.state} state
* @param {farmhand.cow} cow
* @param {boolean} doUseHuggingMachine
* @returns {farmhand.state}
*/
export const changeCowAutomaticHugState = (state, cow, doUseHuggingMachine) => {
if (doUseHuggingMachine) {
if (
cow.isUsingHuggingMachine ||
!areHuggingMachinesInInventory(state.inventory)
) {
return state
}
}
state = modifyCow(state, cow.id, updatedCow => ({
...updatedCow,
isUsingHuggingMachine: doUseHuggingMachine,
}))
state = doUseHuggingMachine
? decrementItemFromInventory(state, HUGGING_MACHINE_ITEM_ID)
: addItemToInventory(state, itemsMap[HUGGING_MACHINE_ITEM_ID])
return state
}