Work on pickup and putdown of inventory items

This commit is contained in:
2021-11-30 16:44:33 +00:00
parent 27b1a6da9e
commit 3754627e0d
10 changed files with 42 additions and 60 deletions
+20 -23
View File
@@ -1,7 +1,6 @@
module Dodge.Inventory
( checkInvSlotsYou
, rmSelectedInvItem
, addItem
, rmInvItem
, updateCloseObjects
, closeObjScrollDir
@@ -22,32 +21,22 @@ import Data.List
--import System.Random
import Control.Lens
-- | Finds first available slot where a new item may be placed in your inventory
-- | checks whether or not an item will fit in your inventory
-- if so return Just the next slot to be used
checkInvSlotsYou :: Item -> World -> Maybe Int
checkInvSlotsYou it w = checkInvSlotsH it invListSelFirst
checkInvSlotsYou it w
| _crInvCapacity ycr >= curinvsize + _itInvSize it
= Just $ maybe 0 ((+1) . fst) $ IM.lookupMax inv
| otherwise = Nothing
where
youCr = you w
youSel = _crInvSel youCr
invList = _crInv youCr
invListSelFirst = (youSel , invList IM.! youSel) : IM.toList invList
checkInvSlotsH :: Item -> [(Int,Item)] -> Maybe Int
checkInvSlotsH it = fmap fst . find cond
where
cond (_,NoItem) = True
cond (_,it' ) = itNotFull it' && _itName it == _itName it'
ycr = you w
inv = _crInv ycr
curinvsize = sum $ fmap _itInvSize inv
---- | Finds first available slot where a new item may be placed in the inventory
--checkInvSlots :: Item -> IM.IntMap Item -> Maybe Int
--checkInvSlots it = checkInvSlotsH it . IM.toList
addItem :: Item -> Item -> Item
addItem it NoItem = it
addItem _ it' = it' & itAmount +~ 1
--numInventorySlots :: Int
--numInventorySlots = 9
itNotFull :: Item -> Bool
itNotFull it = case it ^? itMaxStack of
Just themax -> themax > _itAmount it
@@ -58,9 +47,17 @@ rmInvItem
-> Int -- ^ Inventory position
-> World
-> World
rmInvItem cid itid w = case w ^? creatures . ix cid . crInv . ix itid . itAmount of
Just x | x > 1 -> w & creatures . ix cid . crInv . ix itid . itAmount %~ subtract 1
_ -> w & creatures . ix cid . crInv . ix itid .~ NoItem
rmInvItem cid invid w = case w ^? creatures . ix cid . crInv . ix invid . itAmount of
Just x | x > 1 -> w & creatures . ix cid . crInv . ix invid . itAmount %~ subtract 1
_ -> w & creatures . ix cid . crInv %~ f
& creatures . ix cid . crInvSel %~ g
& creatures . ix cid . crLeftInvSel . _Just %~ g
where
maxk = fmap fst $ IM.lookupMax $ _crInv $ _creatures w IM.! cid
f inv = let (xs,ys) = IM.split invid inv
in xs `IM.union` IM.mapKeys (subtract 1) ys
g x | x > invid || Just x == maxk = x - 1
| otherwise = x
{- Delete a creature's selected item, the item will no longer exist. -}
rmSelectedInvItem
:: Int -- ^ Creature id