Allow pausable reloads with discrete progess steps
This commit is contained in:
+41
-59
@@ -1,14 +1,13 @@
|
||||
module Dodge.Reloading
|
||||
( crCancelReloading
|
||||
, crStartReloading
|
||||
, crToggleReloading
|
||||
, stepReloading
|
||||
) where
|
||||
import Dodge.Data
|
||||
--import Dodge.Base.Arithmetic
|
||||
--import qualified Data.IntMap.Strict as IM
|
||||
import Dodge.Hammer
|
||||
|
||||
import Control.Lens
|
||||
import Data.Maybe
|
||||
--import Data.Maybe
|
||||
|
||||
crCancelReloading :: Creature -> Creature
|
||||
crCancelReloading cr = cr
|
||||
@@ -17,75 +16,58 @@ crCancelReloading cr = cr
|
||||
|
||||
stepReloading :: Creature -> Creature
|
||||
stepReloading cr = case cr ^?! crInvSel . iselAction of
|
||||
ReloadAction _ _ (HasHammer HammerUp) -> cr & crInvSel . iselAction .~ NoInvSelAction
|
||||
ReloadAction x la _
|
||||
| x > 0 -> cr & crInvSel . iselAction . actionProgress -~ 1
|
||||
| otherwise -> cr & completeLoadAction la
|
||||
& crInvSel . iselAction . actionHammer . hammerPosition %~ moveHammerUp
|
||||
| otherwise -> cr
|
||||
& crInvSel . iselAction . actionHammer . hammerPosition %~ moveHammerUp
|
||||
& crInv . ix (crSel cr) . itConsumption %~ doLoadAction la
|
||||
& crInv . ix (crSel cr) . itConsumption %~ rotateActionProgress
|
||||
& tryNextLoadAction
|
||||
_ -> cr
|
||||
|
||||
crStartReloading :: Creature -> World -> World
|
||||
crStartReloading cr w = fromMaybe w $ w
|
||||
& creatures . ix (_crID cr) %%~ tryStartLoading
|
||||
tryNextLoadAction :: Creature -> Creature
|
||||
tryNextLoadAction cr = case cr ^? crInv . ix (crSel cr) . itConsumption . laProgress . _Just . ix 0 of
|
||||
Nothing -> cr & crInvSel . iselAction .~ NoInvSelAction
|
||||
Just la -> cr & crInvSel . iselAction . actionProgress .~ _actionTime la
|
||||
& crInvSel . iselAction . reloadAction .~ la
|
||||
|
||||
tryStartLoading :: Creature -> Maybe Creature
|
||||
tryStartLoading cr = case cr ^? crInv . ix (crSel cr) . itConsumption . laProgress of
|
||||
Just Nothing -> Just . startLoadAction $ cr
|
||||
& crInv . ix (crSel cr) . itConsumption %~ setLoadCycle
|
||||
Just (Just (a:_)) -> Just . startNextLoadAction a $ cr
|
||||
Just (Just []) -> Nothing
|
||||
_ -> Nothing
|
||||
-- Just (Just _) -> Just . continueLoading $ cr
|
||||
-- Nothing -> Nothing
|
||||
|
||||
startLoadAction :: Creature -> Creature
|
||||
startLoadAction cr = continueLoading $ cr
|
||||
& crInvSel . iselAction .~ ReloadAction (_actionTime la) la NoHammer
|
||||
crToggleReloading :: Creature -> World -> World
|
||||
crToggleReloading cr w = case cr ^?! crInvSel . iselAction of
|
||||
ReloadAction {} -> w & creatures . ix (_crID cr) . crInvSel . iselAction .~ NoInvSelAction
|
||||
_ -> w & creatures . ix (_crID cr) %~ tryStartLoading
|
||||
|
||||
tryStartLoading :: Creature -> Creature
|
||||
tryStartLoading cr = case ic ^? laProgress . _Just . ix 0 of
|
||||
Just la -> cr & startLoadAction la
|
||||
Nothing -> case ic ^? laCycle of
|
||||
Nothing -> cr
|
||||
Just [] -> error ("item has empty load cycle" ++ show (cr ^?! crInv . ix (crSel cr) . itType))
|
||||
Just (la:las) -> cr & startLoadAction la
|
||||
& crInv . ix (crSel cr) . itConsumption . laProgress ?~ (la:las)
|
||||
where
|
||||
la = cr ^?! crInv . ix (crSel cr) . itConsumption . laProgress . _Just . ix 0
|
||||
ic = cr ^?! crInv . ix (crSel cr) . itConsumption
|
||||
|
||||
setLoadCycle :: ItemConsumption -> ItemConsumption
|
||||
setLoadCycle ic = ic & laProgress ?~ _laCycle ic
|
||||
startLoadAction :: LoadAction -> Creature -> Creature
|
||||
startLoadAction la cr = cr & crInvSel . iselAction .~ ReloadAction (_actionTime la) la NoHammer
|
||||
|
||||
startNextLoadAction :: LoadAction -> Creature -> Creature
|
||||
startNextLoadAction la cr = cr
|
||||
& crInvSel . iselAction .~ ReloadAction (_actionTime la) la NoHammer
|
||||
|
||||
continueLoading :: Creature -> Creature
|
||||
continueLoading cr = case cr ^? crInv . ix (crSel cr) . itConsumption . laProgress . _Just of
|
||||
Just (la:_) -> cr & crInvSel . iselAction . reloadAction .~ la
|
||||
& crInvSel . iselAction . actionProgress .~ _actionTime la
|
||||
Just [] -> case cr ^?! crInv . ix (crSel cr) . itConsumption of
|
||||
ic | _laLoaded ic < _laMax ic -> continueLoading $ cr
|
||||
& crInv . ix (crSel cr) . itConsumption . laProgress ?~ _laCycle ic
|
||||
_ -> cr & crInvSel . iselAction .~ NoInvSelAction
|
||||
& crInv . ix (crSel cr) . itConsumption . laProgress .~ Nothing
|
||||
_ -> cr & crInvSel . iselAction .~ NoInvSelAction
|
||||
& crInv . ix (crSel cr) . itConsumption . laProgress .~ Nothing
|
||||
|
||||
completeLoadAction :: LoadAction -> Creature -> Creature
|
||||
completeLoadAction la cr = continueLoading $ cr
|
||||
& crInv . ix (crSel cr) . itConsumption . laProgress . _Just %~ tail
|
||||
& crInv . ix (crSel cr) . itConsumption %~ doLoadAction la
|
||||
rotateActionProgress :: ItemConsumption -> ItemConsumption
|
||||
rotateActionProgress ic = case ic ^? laProgress . _Just of
|
||||
Just (_:la:las) -> ic & laProgress . _Just .~ (la:las)
|
||||
_ | _laLoaded ic < _laMax ic -> ic & laProgress ?~ _laCycle ic
|
||||
_ -> ic & laProgress .~ Nothing
|
||||
|
||||
|
||||
-- Just LoadFinished
|
||||
-- | _laLoaded ic >= _laMax ic -> Nothing
|
||||
-- | otherwise -> Just $ ic & laProgress
|
||||
-- .~ LoadUnderway (_actionTime (head (_laCycle ic))) (_laCycle ic)
|
||||
-- Just (LoadUnderway _ []) -> Just $ ic & laProgress .~ LoadFinished
|
||||
-- Just (LoadUnderway t (a:_))
|
||||
-- | t > 0 -> Just $ ic & laProgress . actionProgress -~ 1
|
||||
-- | otherwise -> Just $ ic & doLoadAction a
|
||||
-- & laProgress . futureActions %~ tail
|
||||
-- & laProgress . actionProgress .~ _actionTime a
|
||||
-- Just (LoadPaused (a:as)) -> Just $ ic & laProgress .~ LoadUnderway (_actionTime a) (a:as)
|
||||
-- Just (LoadPaused []) -> Just $ ic & laProgress .~ LoadFinished
|
||||
-- Nothing -> Nothing
|
||||
|
||||
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
|
||||
LoadEject {} -> ic & laLoaded .~ 0
|
||||
LoadInsert {} -> ic & laLoaded .~ _laMax ic
|
||||
LoadAdd {_insertMax = x}
|
||||
-> ic & laLoaded +~ min x (_laMax ic - _laLoaded ic)
|
||||
LoadPrime {} -> ic
|
||||
|
||||
--{- | Start reloading if clip is empty. -}
|
||||
--crAutoReload :: Creature -> Creature
|
||||
|
||||
Reference in New Issue
Block a user