Improve item location code

This commit is contained in:
2024-09-22 14:29:02 +01:00
parent 236f7c7750
commit 4f6431968c
25 changed files with 218 additions and 186 deletions
+6 -4
View File
@@ -15,7 +15,8 @@ getItem itid w = do
itpos <- w ^? cWorld . lWorld . itemLocations . ix itid
case itpos of
OnFloor flitid -> w ^? cWorld . lWorld . floorItems . ix flitid . flIt
InInv cid invid -> w ^? cWorld . lWorld . creatures . ix cid . crInv . ix invid
InInv {_ilCrID = cid, _ilInvID = invid}
-> w ^? cWorld . lWorld . creatures . ix cid . crInv . ix invid
OnTurret mcid -> w ^? cWorld . lWorld . machines . ix mcid . mcType . _McTurret . tuWeapon
InVoid -> Nothing
@@ -25,19 +26,20 @@ pointerToItemLocation ::
(Item -> f Item) ->
World ->
f World
pointerToItemLocation (InInv cid invid) = cWorld . lWorld . creatures . ix cid . crInv . ix invid
pointerToItemLocation InInv {_ilCrID = cid, _ilInvID = invid}
= cWorld . lWorld . creatures . ix cid . crInv . ix invid
pointerToItemLocation (OnFloor flid) = cWorld . lWorld . floorItems . ix flid . flIt
pointerToItemLocation _ = const pure
pointerYourSelectedItem :: Applicative a => (Item -> a Item) -> World -> a World
pointerYourSelectedItem f w = fromMaybe (pure w) $ do
itinvid <- w ^? cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . inInventory . imSelectedItem
Just $ pointerToItemLocation (InInv 0 itinvid) f w
Just $ pointerToItemLocation (InInv 0 itinvid True True) f w
pointerYourRootItem :: Applicative a => (Item -> a Item) -> World -> a World
pointerYourRootItem f w = fromMaybe (pure w) $ do
itinvid <- w ^? cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . inInventory . imRootItem
Just $ pointerToItemLocation (InInv 0 itinvid) f w
Just $ pointerToItemLocation (InInv 0 itinvid True True) f w
pointerToItem ::
Applicative f =>