Work on inventory management
This commit is contained in:
+32
-21
@@ -5,14 +5,17 @@ module Dodge.Inventory
|
||||
, updateCloseObjects
|
||||
, closeObjScrollDir
|
||||
, swapInvDir
|
||||
, dirInvPos
|
||||
, changeInvPos
|
||||
, changeInvCloseObjectsPos
|
||||
, crNumFreeSlots
|
||||
, crInvSize
|
||||
)
|
||||
where
|
||||
import Dodge.Data
|
||||
import Dodge.Base
|
||||
import Dodge.Base.Collide
|
||||
import Geometry
|
||||
import FoldableHelp
|
||||
--import FoldableHelp
|
||||
import Padding
|
||||
import qualified IntMapHelp as IM
|
||||
|
||||
@@ -25,28 +28,26 @@ import Control.Lens
|
||||
-- if so return Just the next slot to be used
|
||||
checkInvSlotsYou :: Item -> World -> Maybe Int
|
||||
checkInvSlotsYou it w
|
||||
| _crInvCapacity ycr >= curinvsize + _itInvSize it
|
||||
| crNumFreeSlots ycr >= _itInvSize it
|
||||
= Just $ maybe 0 ((+1) . fst) $ IM.lookupMax inv
|
||||
| otherwise = Nothing
|
||||
where
|
||||
ycr = you w
|
||||
inv = _crInv ycr
|
||||
curinvsize = sum $ fmap _itInvSize inv
|
||||
-- curinvsize = invSize 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
|
||||
crNumFreeSlots :: Creature -> Int
|
||||
crNumFreeSlots cr = _crInvCapacity cr - invSize (_crInv cr)
|
||||
|
||||
itNotFull :: Item -> Bool
|
||||
itNotFull it = case it ^? itMaxStack of
|
||||
Just themax -> themax > _itAmount it
|
||||
Nothing -> False
|
||||
crInvSize :: Creature -> Int
|
||||
crInvSize = invSize . _crInv
|
||||
|
||||
rmInvItem
|
||||
:: Int -- ^ Creature id
|
||||
invSize :: IM.IntMap Item -> Int
|
||||
invSize = sum . fmap _itInvSize
|
||||
|
||||
rmInvItem :: Int -- ^ Creature id
|
||||
-> Int -- ^ Inventory position
|
||||
-> World
|
||||
-> World
|
||||
-> World -> World
|
||||
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
|
||||
@@ -56,8 +57,8 @@ rmInvItem cid invid w = case w ^? creatures . ix cid . crInv . ix invid . itAmou
|
||||
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
|
||||
g x | x > invid || Just x == maxk = max 0 $ x - 1
|
||||
| otherwise = x
|
||||
{- Delete a creature's selected item, the item will no longer exist. -}
|
||||
rmSelectedInvItem
|
||||
:: Int -- ^ Creature id
|
||||
@@ -91,7 +92,9 @@ closeObjScrollDir x
|
||||
| otherwise = id
|
||||
|
||||
swapInvDir :: Int -> World -> World
|
||||
swapInvDir k w = w & creatures . ix (_yourID w) %~ updatecreature
|
||||
swapInvDir k w
|
||||
| n == 0 = w
|
||||
| otherwise = w & creatures . ix (_yourID w) %~ updatecreature
|
||||
where
|
||||
updatecreature = ( crInv %~ IM.swapKeys (i `mod` n) swapi )
|
||||
. (crLeftInvSel . _Just %~ updateLeftInvSel)
|
||||
@@ -102,8 +105,16 @@ swapInvDir k w = w & creatures . ix (_yourID w) %~ updatecreature
|
||||
i = _crInvSel $ you w
|
||||
n = length $ IM.keys $ _crInv $ _creatures w IM.! _yourID w
|
||||
|
||||
dirInvPos :: Int -> World -> World
|
||||
dirInvPos i w = w
|
||||
& creatures . ix (_yourID w) . crInvSel %~ (`mod` n) . subtract i
|
||||
changeInvPos :: Int -> World -> World
|
||||
changeInvPos i w
|
||||
| n == 0 = w
|
||||
| otherwise = w & creatures . ix (_yourID w) . crInvSel %~ (`mod` n) . subtract i
|
||||
where
|
||||
n = length $ IM.keys $ _crInv $ _creatures w IM.! _yourID w
|
||||
|
||||
changeInvCloseObjectsPos :: Int -> World -> World
|
||||
changeInvCloseObjectsPos i w
|
||||
| n == 0 = w
|
||||
| otherwise = w & creatures . ix (_yourID w) . crInvSel %~ (`mod` n) . subtract i
|
||||
where
|
||||
n = (length $ IM.keys $ _crInv $ _creatures w IM.! _yourID w) + length (_closeObjects w)
|
||||
|
||||
Reference in New Issue
Block a user