Partially working change to selected items

This commit is contained in:
2023-02-07 12:07:13 +00:00
parent 5f7d662454
commit c354949ca9
27 changed files with 386 additions and 288 deletions
+32 -22
View File
@@ -5,57 +5,67 @@ module Dodge.Reloading (
tryStartLoading,
) where
import Dodge.Data.Item.Use.Consumption.LoadAction
import Control.Lens
import Dodge.Data.Creature
import Data.Maybe
crCancelReloading :: Creature -> Creature
crCancelReloading cr =
cr
& crInv . ix (crSel cr) . itUse . heldConsumption . laProgress %~ const Nothing
& crInvSel . iselAction .~ NoInvSelAction
& updateProgress
& crInvSel . isel . iselAction .~ NoInvSelAction
where
updateProgress = case crSel cr of
SelItem i _ -> crInv . ix i . itUse . heldConsumption . laProgress %~ const Nothing
_ -> id
stepReloading :: Creature -> Creature
stepReloading cr = case cr ^?! crInvSel . iselAction of
ReloadAction x la
stepReloading cr = case cr ^?! crInvSel . isel of
SelItem i (ReloadAction x la)
| x > 0 ->
cr & crInvSel . iselAction . actionProgress -~ 1
cr & crInvSel . isel . iselAction . actionProgress -~ 1
| otherwise ->
cr
& crInv . ix (crSel cr) . itUse . heldConsumption %~ doLoadAction la
& crInv . ix (crSel cr) . itUse . heldConsumption %~ rotateActionProgress
& crInv . ix i . itUse . heldConsumption %~ doLoadAction la
& crInv . ix i . 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
tryNextLoadAction cr = case cr ^? crInvSel . isel of
Just (SelItem i _) -> case cr ^? crInv . ix i . itUse . heldConsumption . laProgress . _Just . ix 0 of
Nothing -> cr & crInvSel . isel . iselAction .~ NoInvSelAction
Just la ->
cr & crInvSel . isel . iselAction . actionProgress .~ _actionTime la
& crInvSel . isel . iselAction . reloadAction .~ la
_ -> cr
crToggleReloading :: Creature -> Creature
crToggleReloading cr = case cr ^? crInvSel . iselAction of
Just ReloadAction{} -> cr & crInvSel . iselAction .~ NoInvSelAction
crToggleReloading cr = case cr ^? crInvSel . isel . iselAction of
Just ReloadAction{} -> cr & crInvSel . isel . iselAction .~ NoInvSelAction
_ -> cr & tryStartLoading
tryStartLoading :: Creature -> Creature
tryStartLoading cr = case cr ^? crInv . ix (crSel cr) . itUse . heldConsumption of
Just hc -> startLoading hc cr
Nothing -> cr
tryStartLoading cr = fromMaybe cr $ do
i <- cr ^? crInvSel . isel . ispItem
hc <- cr ^? crInv . ix i . itUse . heldConsumption
return $ startLoading hc cr
startLoading :: HeldConsumption -> Creature -> Creature
startLoading ic cr = case ic ^? laProgress . _Just . ix 0 of
Just la -> cr & startLoadingStep la
Nothing -> case ic ^? laCycle of
Nothing -> cr
Just [] -> error ("item has empty load cycle" ++ show (cr ^?! crInv . ix (crSel cr) . itType))
Just [] -> error ("item has empty load cycle")
Just _ | _laLoaded ic >= _laMax ic -> cr
Just (la : las) ->
cr & startLoadingStep la
& crInv . ix (crSel cr) . itUse . heldConsumption . laProgress ?~ (la : las)
Just (la : las) -> fromMaybe (error "item loading error") $ do
i <- cr ^? crInvSel . isel . ispItem
return $ cr & startLoadingStep la
& crInv . ix i . itUse . heldConsumption . laProgress ?~ (la : las)
startLoadingStep :: LoadAction -> Creature -> Creature
startLoadingStep la cr = cr & crInvSel . iselAction .~ ReloadAction (_actionTime la) la
startLoadingStep la cr = cr & crInvSel . isel . iselAction .~ ReloadAction (_actionTime la) la
rotateActionProgress :: HeldConsumption -> HeldConsumption
rotateActionProgress ic = case ic ^? laProgress . _Just of