Add fields to determine an item's inventory position/whether it is held

This commit is contained in:
2022-04-11 13:32:06 +01:00
parent d46d315203
commit c686d9e111
14 changed files with 107 additions and 113 deletions
+5 -1
View File
@@ -217,7 +217,11 @@ youDropItem' w = case yourItem w ^? _Just . itCurseStatus of
cr = you w
{- | Copy an inventory item to the floor. -}
copyInvItemToFloor :: Creature -> Int -> World -> World
copyInvItemToFloor cr i = copyItemToFloor (_crPos cr) (_crInv cr IM.! i)
copyInvItemToFloor cr i = copyItemToFloor (_crPos cr)
(_crInv cr IM.! i
& itInvPos .~ Nothing
& itIsHeld .~ False
)
sizeSelf :: Float -> Creature -> World -> Maybe World
+1 -1
View File
@@ -45,7 +45,7 @@ basicCrPict col cr cfig w =
)
where
targetingPic = IM.elems $ IM.mapMaybeWithKey f $ _crInv cr
f invid it = fmap (\g -> g invid it cr w) (it ^? itTargeting . tgDraw)
f invid it = fmap (\g -> g invid it cr cfig w) (it ^? itTargeting . tgDraw)
drawCrEquipment :: Creature -> SPic
drawCrEquipment cr = uncurryV translateSPf (_crPos cr) (rotateSP (_crDir cr) $ drawEquipment cr)
+28 -12
View File
@@ -38,7 +38,12 @@ import System.Random
foldCr :: [Creature -> World -> World]
-> Creature -> World -> World
foldCr xs cr w = foldr ($ cr) w xs
--foldCr xs cr w = foldr ($ cr) w xs
foldCr xs cr w = foldr f w xs
where
f g w' = case w' ^? creatures . ix (_crID cr) of
Just cr' -> g cr' w'
Nothing -> w'
-- | this seems to work, but I am not sure about the ordering:
-- previously, the movement was updated before the ai in order to correctly set the oldpos.
@@ -48,12 +53,14 @@ foldCr xs cr w = foldr ($ cr) w xs
-- may effect whether the shield moves correctly
stateUpdate :: (Creature -> World -> World) -> Creature -> World -> World
stateUpdate f = foldCr
[ checkDeath
, doDamage
[ doDamage
, invSideEff
, movementSideEff
, internalUpdate
, f
, internalUpdate -- note that updateMovement needs to be evaluated at the correct moment
-- wrt any changes in position made by the creature: the
-- oldPos needs to be correctly set
, checkDeath
]
checkDeath :: Creature -> World -> World
@@ -121,14 +128,14 @@ movementSideEff cr w
(randDir,g) = randomR (-0.5,0.5) $ _randGen w
(randAng,_) = randomR (0,2*pi) $ _randGen w
heldItemUpdate :: Item -> Item
heldItemUpdate = invItemUpdate
heldItemUpdate :: Int -> Item -> Item
heldItemUpdate invid = (itIsHeld .~ True) . invItemUpdate invid
-- . (itUse %~ useupdate)
-- where
-- useupdate = id
invItemUpdate :: Item -> Item
invItemUpdate = itUse %~ useupdate
invItemUpdate :: Int -> Item -> Item
invItemUpdate invid = (itUse %~ useupdate) . (itIsHeld .~ False) . (itInvPos ?~ invid)
where
useupdate = (useHammer . hammerPosition %~ moveHammerUp)
. (useDelay . warmTime %~ decreaseToZero)
@@ -137,7 +144,7 @@ invItemUpdate = itUse %~ useupdate
invSideEff :: Creature -> World -> World
invSideEff cr w = weaponReloadSounds cr
. flip (IS.foldr useeq) (_crInvEquipped cr)
. doHeldItemTargeting cr
-- . doHeldItemTargeting cr
$ IM.foldrWithKey f w (_crInv cr)
where
useeq i = _eqUse (_itUse $ _crInv cr IM.! i) cr i
@@ -146,16 +153,25 @@ invSideEff cr w = weaponReloadSounds cr
Just g -> g (_itEffect it) cr i w' & doitemupdate
where
doitemupdate
| i == _crInvSel cr = itpointer %~ heldItemUpdate
| otherwise = itpointer %~ invItemUpdate
| i == _crInvSel cr = doHeldItemTargeting cr . (itpointer %~ heldItemUpdate i)
| otherwise = doNotHeldItemTargeting i cr . (itpointer %~ invItemUpdate i)
itpointer = creatures . ix (_crID cr) . crInv . ix i
doHeldItemTargeting :: Creature -> World -> World
doHeldItemTargeting cr w = case cr ^? crInv . ix (_crInvSel cr) . itTargeting of
Nothing -> w
Just NoTargeting -> w
Just t -> let (w',t') = _tgUpdate t (_crInv cr IM.! _crInvSel cr) cr w t
Just t -> let (w',t') = _tgHeldUpdate t (_crInv cr IM.! _crInvSel cr) cr w t
in w' & creatures . ix (_crID cr) . crInv . ix (_crInvSel cr) . itTargeting .~ t'
-- & testString .~ const ["Held"]
doNotHeldItemTargeting :: Int -> Creature -> World -> World
doNotHeldItemTargeting invid cr w = case cr ^? crInv . ix invid . itTargeting of
Nothing -> w
Just NoTargeting -> w
Just t -> let (w',t') = _tgNotHeldUpdate t (_crInv cr IM.! invid) cr w t
in w' & creatures . ix (_crID cr) . crInv . ix invid . itTargeting .~ t'
-- & testString .~ const ["Not"]
weaponReloadSounds :: Creature -> World -> World
weaponReloadSounds cr w = case cr ^? crInv . ix (_crInvSel cr) . itConsumption of