From b108b2ed656573da7bd78e7ad5704648716a98fc Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Fri, 23 Dec 2022 10:21:56 +0100 Subject: [PATCH 1/2] #4 Pick up items --- lib/RPGEngine/Data/Level.hs | 9 +++++++++ lib/RPGEngine/Input/ActionSelection.hs | 18 ++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/RPGEngine/Data/Level.hs b/lib/RPGEngine/Data/Level.hs index 7265e25..e3bf9f2 100644 --- a/lib/RPGEngine/Data/Level.hs +++ b/lib/RPGEngine/Data/Level.hs @@ -37,6 +37,15 @@ hasAt pos level = match firstItem firstEntity firstItem = find ((== pos) . getICoord) $ items level 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 North = ( 0, 1) directionOffsets East = ( 1, 0) diff --git a/lib/RPGEngine/Input/ActionSelection.hs b/lib/RPGEngine/Input/ActionSelection.hs index 99e6cfe..d454df9 100644 --- a/lib/RPGEngine/Input/ActionSelection.hs +++ b/lib/RPGEngine/Input/ActionSelection.hs @@ -4,10 +4,11 @@ module RPGEngine.Input.ActionSelection 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.Interact ( SpecialKey(..), KeyState(..) ) +import RPGEngine.Data.Level (getWithId) ------------------------------ Exported ------------------------------ @@ -22,9 +23,10 @@ handleInputActionSelection = composeInputHandlers [ ---------------------------------------------------------------------- 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 } - selectedAction = Leave + selectedAction = list !! index + index = selection selector selectAction g = g -- TODO Lift this code from LevelSelection @@ -52,8 +54,16 @@ execute (IncreasePlayerHp iid) s = increasePlayerHp iid s execute _ s = s -- Pick up the item with itemId and put it in the players inventory +-- Should receive a Playing 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 useItem :: ItemId -> State -> State -- TODO From f2844138366b2ca433ca5fdeeadce9b515c84e72 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Fri, 23 Dec 2022 10:50:42 +0100 Subject: [PATCH 2/2] #4 Increase playerhp --- lib/RPGEngine/Input/ActionSelection.hs | 37 +++++++++++++++++++------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/RPGEngine/Input/ActionSelection.hs b/lib/RPGEngine/Input/ActionSelection.hs index d454df9..0a34015 100644 --- a/lib/RPGEngine/Input/ActionSelection.hs +++ b/lib/RPGEngine/Input/ActionSelection.hs @@ -4,11 +4,12 @@ module RPGEngine.Input.ActionSelection import RPGEngine.Input.Core (InputHandler, handleKey, composeInputHandlers, ListSelector (selection)) -import RPGEngine.Data (Game (..), State (..), Direction (..), Action (..), ItemId, EntityId, Level (..), Player (inventory)) +import RPGEngine.Data (Game (..), State (..), Direction (..), Action (..), ItemId, EntityId, Level (..), Player (inventory, playerHp, Player), Item (..), HP) import Graphics.Gloss.Interface.IO.Game (Key(SpecialKey), SpecialKey (KeyUp, KeyDown)) import Graphics.Gloss.Interface.IO.Interact ( SpecialKey(..), KeyState(..) ) import RPGEngine.Data.Level (getWithId) +import Data.Foldable (find) ------------------------------ Exported ------------------------------ @@ -50,7 +51,8 @@ execute :: Action -> State -> State execute (RetrieveItem id ) s = pickUpItem id s execute (UseItem id ) s = useItem id s execute (DecreaseHp eid iid) s = decreaseHp eid iid s -execute (IncreasePlayerHp iid) s = increasePlayerHp iid s +execute (IncreasePlayerHp iid) s = healedPlayer + where healedPlayer = s{ player = increasePlayerHp iid (player s)} execute _ s = s -- Pick up the item with itemId and put it in the players inventory @@ -58,14 +60,14 @@ execute _ s = s pickUpItem :: ItemId -> State -> State 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 } + 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 useItem :: ItemId -> State -> State -- TODO useItem _ s = s -- TODO @@ -79,8 +81,23 @@ decreaseHp _ _ s = s -- TODO Break item if durability below zero -- Heal a bit -increasePlayerHp :: ItemId -> State -> State -increasePlayerHp _ s = s --- TODO Increase playerHp --- TODO Decrease durability of item --- TODO Remove item if durability below zero \ No newline at end of file +-- Should receive a Player +increasePlayerHp :: ItemId -> Player -> Player +increasePlayerHp id p@Player{ playerHp = hp, inventory = inventory} = newPlayer + where newPlayer = p{ playerHp = newHp, inventory = newInventory newItem } + (Just usedItem) = find ((== id) . itemId) inventory + newItem = decreaseDurability usedItem + newInventory (Just item) = item:filteredInventory + newInventory _ = filteredInventory + filteredInventory =filter (/= usedItem) inventory + newHp = changeHealth hp (itemValue usedItem) + +decreaseDurability :: Item -> Maybe Item +decreaseDurability item@Item{ useTimes = Nothing } = Just item -- Infinite uses, never breaks +decreaseDurability item@Item{ useTimes = Just val } | 0 < val - 1 = Just item{ useTimes = Just (val - 1) } + | otherwise = Nothing -- Broken + +-- Change given health by a given amount +changeHealth :: HP -> HP -> HP +changeHealth (Just health) (Just difference) = Just (health + difference) +changeHealth health _ = health \ No newline at end of file