diff --git a/src/Dodge/Reloading.hs b/src/Dodge/Reloading.hs new file mode 100644 index 000000000..9166e8bac --- /dev/null +++ b/src/Dodge/Reloading.hs @@ -0,0 +1,36 @@ +module Dodge.Reloading where +import Dodge.Data +import Dodge.Base +--import qualified Data.IntMap.Strict as IM + +import Control.Lens + +startReloadingWeapon :: Creature -> World -> Maybe World +startReloadingWeapon cr = (creatures . ix (_crID cr) . crInv . ix (_crInvSel cr) . itConsumption) + icTryStartReloading + +icTryStartReloading :: ItemConsumption -> Maybe ItemConsumption +icTryStartReloading am + | _ammoLoaded am < _ammoMax am && _reloadState am == Nothing' + = Just $ am & reloadState .~ Just' (_reloadTime am) + & if _reloadType am == ActiveClear then ammoLoaded .~ 0 else id + | otherwise = Nothing + +crStopReloading :: Creature -> Creature +crStopReloading cr = cr & crInv . ix (_crInvSel cr) . itConsumption . reloadState .~ Nothing' + +stepReloading :: Creature -> Creature +stepReloading cr = case cr ^? crInv . ix isel . itConsumption . reloadState . _Just' of + Just 0 -> cr & crInv . ix isel . itConsumption %~ doload + Just _ -> cr & crInv . ix isel . itConsumption . reloadState . _Just' %~ decreaseToZero + Nothing -> cr + where + isel = _crInvSel cr + doload itcon = case _reloadType itcon of + ActivePartial x + | x + _ammoLoaded itcon < _ammoMax itcon -> itcon & ammoLoaded %~ (+ x) + & reloadState .~ Just' (_reloadTime itcon) + | otherwise -> itcon & ammoLoaded .~ _ammoMax itcon + & reloadState .~ Nothing' + _ -> itcon & ammoLoaded .~ _ammoMax itcon + & reloadState .~ Nothing'