52 lines
1.5 KiB
Haskell
52 lines
1.5 KiB
Haskell
module Dodge.Item.Location where
|
|
|
|
import Control.Lens
|
|
import Data.Maybe
|
|
import Dodge.Data.World
|
|
import qualified IntMapHelp as IM
|
|
|
|
setHeldItemLoc :: Creature -> World -> World
|
|
setHeldItemLoc cr = fst . getHeldItemLoc cr
|
|
|
|
getHeldItemLoc :: Creature -> World -> (World, Int)
|
|
getHeldItemLoc cr w = case maybeitid of
|
|
Nothing ->
|
|
( w & cWorld . creatures . ix cid . crInv . ix j . itID .~ newitid
|
|
& cWorld . itemLocations %~ IM.insert newitid (InInv cid j)
|
|
, itid
|
|
)
|
|
_ -> (w, itid)
|
|
where
|
|
cid = _crID cr
|
|
j = crSel cr
|
|
newitid = IM.newKey $ _itemLocations (_cWorld w)
|
|
maybeitid = cr ^? crInv . ix j . itID
|
|
itid = fromMaybe newitid maybeitid
|
|
|
|
getItem :: Int -> World -> Maybe Item
|
|
getItem itid w = do
|
|
itpos <- w ^? cWorld . itemLocations . ix itid
|
|
case itpos of
|
|
OnFloor flitid -> w ^? cWorld . floorItems . ix flitid . flIt
|
|
InInv cid invid -> w ^? cWorld . creatures . ix cid . crInv . ix invid
|
|
OnTurret mcid -> w ^? cWorld . machines . ix mcid . mcType . _McTurret . tuWeapon
|
|
InVoid -> Nothing
|
|
|
|
pointerToItemLocation ::
|
|
Applicative f =>
|
|
ItemLocation ->
|
|
(Item -> f Item) ->
|
|
World ->
|
|
f World
|
|
pointerToItemLocation (InInv cid invid) = cWorld . creatures . ix cid . crInv . ix invid
|
|
pointerToItemLocation (OnFloor flid) = cWorld . floorItems . ix flid . flIt
|
|
pointerToItemLocation _ = const pure
|
|
|
|
pointerToItem ::
|
|
Applicative f =>
|
|
Item ->
|
|
(Item -> f Item) ->
|
|
World ->
|
|
f World
|
|
pointerToItem = pointerToItemLocation . _itLocation
|