Refactor reloading to use list of required steps

This commit is contained in:
2022-06-20 19:06:02 +01:00
parent a85cf4d1fb
commit 37eb69c661
16 changed files with 79 additions and 118 deletions
+20 -46
View File
@@ -11,41 +11,12 @@ import Dodge.Data
import Control.Lens
import Data.Maybe
---- TODO clean this up, rethink datatypes
--icTryStartReloading :: Item -> Maybe Item
--icTryStartReloading it = do
-- am <- it ^? itConsumption
-- amloaded <- am ^? laLoaded
-- ammax <- am ^? laMax
-- rstate <- am ^? laReloadState
-- if amloaded < ammax && rstate == Nothing'
-- then Just $ it
-- & itConsumption . laReloadState .~ Just' (_laReloadTime am)
-- & if _laReloadType am == ActiveClear
-- then itConsumption . laLoaded .~ 0
-- else id
-- else Nothing
crStartReloading :: Creature -> World -> World
crStartReloading cr = creatures . ix (_crID cr) . crStance . posture .~ Reloading
crStopReloading :: Creature -> Creature
crStopReloading cr = cr & crStance . posture .~ AtEase
startInsertAmmo :: ItemConsumption -> ItemConsumption
startInsertAmmo ic = ic & laLoadType . loadProgress .~ Insert intime inamount
where
intime = _insertTime (_laLoadType ic)
inamount = maybe id min (ic ^? laLoadType . loadAmount) (_laMax ic - _laLoaded ic)
startEjectAmmo :: ItemConsumption -> ItemConsumption
startEjectAmmo ic = ic & laLoadType . loadProgress .~ Eject (_ejectTime $ _laLoadType ic)
loadAmmo :: Int -> ItemConsumption -> ItemConsumption
loadAmmo inamount ic = ic
& laLoaded +~ inamount
& laLoadType . loadProgress .~ NoTransfer
stepReloading :: Creature -> Creature
stepReloading cr = case _posture (_crStance cr) of
Reloading -> fromMaybe (cr & crStance . posture .~ AtEase)
@@ -53,23 +24,26 @@ stepReloading cr = case _posture (_crStance cr) of
_ -> cr
stepReloading' :: ItemConsumption -> Maybe ItemConsumption
stepReloading' ic = do
ltype <- ic ^? laLoadType
lprog <- ltype ^? loadProgress
case lprog of
Eject x
| x > 0 -> Just $ ic & laLoadType . loadProgress . ejectProgress -~ 1
| otherwise -> Just $ ic & startInsertAmmo
Insert x inamount
| x > 0 -> Just $ ic & laLoadType . loadProgress . insertProgress -~ 1
| otherwise -> Just $ ic & loadAmmo inamount
NoTransfer
| _laLoaded ic >= _laMax ic
-> Nothing
| otherwise -> case ltype of
ActiveEject {} -> Just $ ic & startEjectAmmo
ActivePartial {} -> Just $ ic & startInsertAmmo
_ -> Nothing
stepReloading' ic = case _laProgress ic of
FinishedLoading
| _laLoaded ic >= _laMax ic -> Nothing
| otherwise -> Just $ ic & laProgress
.~ InProgress Nothing (map toLoadProgress (_laCycle ic))
InProgress _ [] -> Just $ ic & laProgress .~ FinishedLoading
InProgress _ (a:_)
| _actionProgress a > 0 -> Just $ ic & laProgress . futureActions . ix 0 . actionProgress -~ 1
| otherwise -> Just $ ic & doLoadAction (_actionType a)
& laProgress . futureActions %~ tail
& laProgress . lastAction ?~ (_actionType a)
toLoadProgress :: LoadAction -> LoadActionProgress
toLoadProgress la = LoadActionProgress (_actionTime la) la
doLoadAction :: LoadAction -> ItemConsumption -> ItemConsumption
doLoadAction la ic = case la of
Eject {} -> ic & laLoaded .~ 0
Insert {_insertMax = mx} -> ic & laLoaded +~ maybe id min mx (_laMax ic - _laLoaded ic)
Prime {} -> ic
--{- | Start reloading if clip is empty. -}
--crAutoReload :: Creature -> Creature