#4 Pick up items
This commit is contained in:
parent
72b460788d
commit
b108b2ed65
2 changed files with 23 additions and 4 deletions
|
@ -37,6 +37,15 @@ hasAt pos level = match firstItem firstEntity
|
||||||
firstItem = find ((== pos) . getICoord) $ items level
|
firstItem = find ((== pos) . getICoord) $ items level
|
||||||
getICoord i = (itemX i, itemY i)
|
getICoord i = (itemX i, itemY i)
|
||||||
|
|
||||||
|
getWithId :: String -> Level -> Maybe (Either Item Entity)
|
||||||
|
getWithId id level = match firstItem firstEntity
|
||||||
|
where match :: Maybe Item -> Maybe Entity -> Maybe (Either Item Entity)
|
||||||
|
match (Just a) _ = Just $ Left a
|
||||||
|
match _ (Just a) = Just $ Right a
|
||||||
|
match _ _ = Nothing
|
||||||
|
firstEntity = find ((== id) . entityId) $ entities level
|
||||||
|
firstItem = find ((== id) . itemId) $ items level
|
||||||
|
|
||||||
directionOffsets :: Direction -> (X, Y)
|
directionOffsets :: Direction -> (X, Y)
|
||||||
directionOffsets North = ( 0, 1)
|
directionOffsets North = ( 0, 1)
|
||||||
directionOffsets East = ( 1, 0)
|
directionOffsets East = ( 1, 0)
|
||||||
|
|
|
@ -4,10 +4,11 @@ module RPGEngine.Input.ActionSelection
|
||||||
|
|
||||||
import RPGEngine.Input.Core (InputHandler, handleKey, composeInputHandlers, ListSelector (selection))
|
import RPGEngine.Input.Core (InputHandler, handleKey, composeInputHandlers, ListSelector (selection))
|
||||||
|
|
||||||
import RPGEngine.Data (Game (..), State (..), Direction (..), Action (..), ItemId, EntityId)
|
import RPGEngine.Data (Game (..), State (..), Direction (..), Action (..), ItemId, EntityId, Level (..), Player (inventory))
|
||||||
import Graphics.Gloss.Interface.IO.Game (Key(SpecialKey), SpecialKey (KeyUp, KeyDown))
|
import Graphics.Gloss.Interface.IO.Game (Key(SpecialKey), SpecialKey (KeyUp, KeyDown))
|
||||||
import Graphics.Gloss.Interface.IO.Interact
|
import Graphics.Gloss.Interface.IO.Interact
|
||||||
( SpecialKey(..), KeyState(..) )
|
( SpecialKey(..), KeyState(..) )
|
||||||
|
import RPGEngine.Data.Level (getWithId)
|
||||||
|
|
||||||
------------------------------ Exported ------------------------------
|
------------------------------ Exported ------------------------------
|
||||||
|
|
||||||
|
@ -22,9 +23,10 @@ handleInputActionSelection = composeInputHandlers [
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
|
|
||||||
selectAction :: Game -> Game
|
selectAction :: Game -> Game
|
||||||
selectAction game@Game{ state = ActionSelection list selection continue } = newGame
|
selectAction game@Game{ state = ActionSelection list selector continue } = newGame
|
||||||
where newGame = game{ state = execute selectedAction continue }
|
where newGame = game{ state = execute selectedAction continue }
|
||||||
selectedAction = Leave
|
selectedAction = list !! index
|
||||||
|
index = selection selector
|
||||||
selectAction g = g
|
selectAction g = g
|
||||||
|
|
||||||
-- TODO Lift this code from LevelSelection
|
-- TODO Lift this code from LevelSelection
|
||||||
|
@ -52,8 +54,16 @@ execute (IncreasePlayerHp iid) s = increasePlayerHp iid s
|
||||||
execute _ s = s
|
execute _ s = s
|
||||||
|
|
||||||
-- Pick up the item with itemId and put it in the players inventory
|
-- Pick up the item with itemId and put it in the players inventory
|
||||||
|
-- Should receive a Playing state
|
||||||
pickUpItem :: ItemId -> State -> State
|
pickUpItem :: ItemId -> State -> State
|
||||||
pickUpItem _ s = s -- TODO
|
pickUpItem id s@Playing{ level = level, player = player } = newState
|
||||||
|
where (Just (Left pickedUpItem)) = getWithId id level
|
||||||
|
newState = s{ level = newLevel, player = newPlayer }
|
||||||
|
newLevel = level{ items = filteredItems }
|
||||||
|
filteredItems = filter (/= pickedUpItem) $ items level
|
||||||
|
newPlayer = player{ inventory = newInventory }
|
||||||
|
newInventory = pickedUpItem:inventory player
|
||||||
|
pickUpItem _ _ = Error "Something went wrong while picking up an item"
|
||||||
|
|
||||||
-- Use an item on the player
|
-- Use an item on the player
|
||||||
useItem :: ItemId -> State -> State -- TODO
|
useItem :: ItemId -> State -> State -- TODO
|
||||||
|
|
Reference in a new issue