Refactor, try to limit dependencies
This commit is contained in:
+41
-38
@@ -1,48 +1,52 @@
|
||||
module Dodge.Reloading
|
||||
( crCancelReloading
|
||||
, crToggleReloading
|
||||
, crHoldReloading
|
||||
, stepReloading
|
||||
, tryStartLoading
|
||||
) where
|
||||
import Dodge.Data
|
||||
import Dodge.Hammer
|
||||
module Dodge.Reloading (
|
||||
crCancelReloading,
|
||||
crToggleReloading,
|
||||
crHoldReloading,
|
||||
stepReloading,
|
||||
tryStartLoading,
|
||||
) where
|
||||
|
||||
import Control.Lens
|
||||
--import Data.Maybe
|
||||
import Dodge.Data.World
|
||||
import Dodge.Hammer
|
||||
|
||||
crCancelReloading :: Creature -> Creature
|
||||
crCancelReloading cr = cr
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption . laProgress %~ const Nothing
|
||||
& crInvSel . iselAction .~ NoInvSelAction
|
||||
crCancelReloading cr =
|
||||
cr
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption . laProgress %~ const Nothing
|
||||
& crInvSel . iselAction .~ NoInvSelAction
|
||||
|
||||
stepReloading :: Creature -> Creature
|
||||
stepReloading cr = case cr ^?! crInvSel . iselAction of
|
||||
ReloadAction _ _ (HasHammer HammerUp) -> cr & crInvSel . iselAction .~ NoInvSelAction
|
||||
ReloadAction _ _ (HasHammer HammerUp) -> cr & crInvSel . iselAction .~ NoInvSelAction
|
||||
ReloadAction x la _
|
||||
| x > 0 -> cr & crInvSel . iselAction . actionProgress -~ 1
|
||||
& crInvSel . iselAction . actionHammer . hammerPosition %~ moveHammerUp
|
||||
| otherwise -> cr
|
||||
& crInvSel . iselAction . actionHammer . hammerPosition %~ moveHammerUp
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption %~ doLoadAction la
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption %~ rotateActionProgress
|
||||
& tryNextLoadAction
|
||||
| x > 0 ->
|
||||
cr & crInvSel . iselAction . actionProgress -~ 1
|
||||
& crInvSel . iselAction . actionHammer . hammerPosition %~ moveHammerUp
|
||||
| otherwise ->
|
||||
cr
|
||||
& crInvSel . iselAction . actionHammer . hammerPosition %~ moveHammerUp
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption %~ doLoadAction la
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption %~ rotateActionProgress
|
||||
& tryNextLoadAction
|
||||
_ -> cr
|
||||
|
||||
tryNextLoadAction :: Creature -> Creature
|
||||
tryNextLoadAction cr = case cr ^? crInv . ix (crSel cr) . itUse . heldConsumption . laProgress . _Just . ix 0 of
|
||||
Nothing -> cr & crInvSel . iselAction .~ NoInvSelAction
|
||||
Just la -> cr & crInvSel . iselAction . actionProgress .~ _actionTime la
|
||||
& crInvSel . iselAction . reloadAction .~ la
|
||||
Just la ->
|
||||
cr & crInvSel . iselAction . actionProgress .~ _actionTime la
|
||||
& crInvSel . iselAction . reloadAction .~ la
|
||||
|
||||
crToggleReloading :: Creature -> World -> World
|
||||
crToggleReloading cr w = case cr ^?! crInvSel . iselAction of
|
||||
ReloadAction {} -> w & cWorld . creatures . ix (_crID cr) . crInvSel . iselAction .~ NoInvSelAction
|
||||
ReloadAction{} -> w & cWorld . creatures . ix (_crID cr) . crInvSel . iselAction .~ NoInvSelAction
|
||||
_ -> w & cWorld . creatures . ix (_crID cr) %~ tryStartLoading
|
||||
|
||||
crHoldReloading :: Creature -> World -> World
|
||||
crHoldReloading cr w = w
|
||||
& cWorld . creatures . ix (_crID cr) . crInvSel . iselAction . actionHammer .~ HasHammer HammerDown
|
||||
crHoldReloading cr w =
|
||||
w
|
||||
& cWorld . creatures . ix (_crID cr) . crInvSel . iselAction . actionHammer .~ HasHammer HammerDown
|
||||
|
||||
tryStartLoading :: Creature -> Creature
|
||||
tryStartLoading cr = case ic ^? laProgress . _Just . ix 0 of
|
||||
@@ -51,30 +55,29 @@ tryStartLoading cr = case ic ^? laProgress . _Just . ix 0 of
|
||||
Nothing -> cr
|
||||
Just [] -> error ("item has empty load cycle" ++ show (cr ^?! crInv . ix (crSel cr) . itType))
|
||||
Just _ | _laLoaded ic >= _laMax ic -> cr
|
||||
Just (la:las) -> cr & startLoadAction la
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption . laProgress ?~ (la:las)
|
||||
Just (la : las) ->
|
||||
cr & startLoadAction la
|
||||
& crInv . ix (crSel cr) . itUse . heldConsumption . laProgress ?~ (la : las)
|
||||
where
|
||||
ic = cr ^?! crInv . ix (crSel cr) . itUse . heldConsumption
|
||||
|
||||
startLoadAction :: LoadAction -> Creature -> Creature
|
||||
startLoadAction la cr = cr & crInvSel . iselAction .~ ReloadAction (_actionTime la) la NoHammer
|
||||
|
||||
|
||||
rotateActionProgress :: HeldConsumption -> HeldConsumption
|
||||
rotateActionProgress ic = case ic ^? laProgress . _Just of
|
||||
Just (_:la:las) -> ic & laProgress . _Just .~ (la:las)
|
||||
Just (_ : la : las) -> ic & laProgress . _Just .~ (la : las)
|
||||
_ | _laLoaded ic < _laMax ic -> ic & laProgress ?~ _laCycle ic
|
||||
_ -> ic & laProgress .~ Nothing
|
||||
|
||||
|
||||
doLoadAction :: LoadAction -> HeldConsumption -> HeldConsumption
|
||||
doLoadAction la ic = case la of
|
||||
LoadEject {} -> ic & laLoaded .~ 0 & laPrimed .~ False
|
||||
LoadInsert {} -> ic & laLoaded .~ _laMax ic
|
||||
LoadAdd {_insertMax = x}
|
||||
-> ic & laLoaded +~ min x (_laMax ic - _laLoaded ic)
|
||||
LoadPrime {} -> ic & laPrimed .~ True
|
||||
|
||||
LoadEject{} -> ic & laLoaded .~ 0 & laPrimed .~ False
|
||||
LoadInsert{} -> ic & laLoaded .~ _laMax ic
|
||||
LoadAdd{_insertMax = x} ->
|
||||
ic & laLoaded +~ min x (_laMax ic - _laLoaded ic)
|
||||
LoadPrime{} -> ic & laPrimed .~ True
|
||||
|
||||
--{- | Start reloading if clip is empty. -}
|
||||
--crAutoReload :: Creature -> Creature
|
||||
--crAutoReload cr = case cr ^? ptrItConsumption' . ammoLoaded of
|
||||
@@ -82,7 +85,7 @@ doLoadAction la ic = case la of
|
||||
-- -> cr & ptrItConsumption . reloadState .~ strictify reloadT
|
||||
-- & ptrItConsumption . ammoLoaded %~ (`fromMaybe` maxA)
|
||||
-- _ -> cr
|
||||
-- where
|
||||
-- where
|
||||
-- ptrItConsumption = crInv . ix (_crInvSel cr) . itConsumption
|
||||
-- ptrItConsumption' = crInv . ix (_crInvSel cr) . itConsumption
|
||||
-- reloadT = cr ^? ptrItConsumption' . reloadTime
|
||||
|
||||
Reference in New Issue
Block a user