dev #25
					 1 changed files with 27 additions and 10 deletions
				
			
		|  | @ -4,11 +4,12 @@ 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, 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.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) | import RPGEngine.Data.Level (getWithId) | ||||||
|  | import Data.Foldable (find) | ||||||
| 
 | 
 | ||||||
| ------------------------------ Exported ------------------------------ | ------------------------------ Exported ------------------------------ | ||||||
| 
 | 
 | ||||||
|  | @ -50,7 +51,8 @@ execute :: Action -> State -> State | ||||||
| execute (RetrieveItem id     ) s = pickUpItem id s | execute (RetrieveItem id     ) s = pickUpItem id s | ||||||
| execute (UseItem      id     ) s = useItem id s | execute (UseItem      id     ) s = useItem id s | ||||||
| execute (DecreaseHp   eid iid) s = decreaseHp eid iid 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 | 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 | ||||||
|  | @ -65,7 +67,7 @@ pickUpItem id s@Playing{ level = level, player = player } = newState | ||||||
|           newInventory  = pickedUpItem:inventory player |           newInventory  = pickedUpItem:inventory player | ||||||
| pickUpItem _ _ = Error "Something went wrong while picking up an item" | 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 :: ItemId -> State -> State -- TODO | ||||||
| useItem _ s = s -- TODO | useItem _ s = s -- TODO | ||||||
| 
 | 
 | ||||||
|  | @ -79,8 +81,23 @@ decreaseHp _ _ s = s | ||||||
| -- TODO Break item if durability below zero | -- TODO Break item if durability below zero | ||||||
| 
 | 
 | ||||||
| -- Heal a bit | -- Heal a bit | ||||||
| increasePlayerHp :: ItemId -> State -> State | -- Should receive a Player | ||||||
| increasePlayerHp _ s = s | increasePlayerHp :: ItemId -> Player -> Player | ||||||
| -- TODO Increase playerHp | increasePlayerHp id p@Player{ playerHp = hp, inventory = inventory} = newPlayer | ||||||
| -- TODO Decrease durability of item |     where newPlayer = p{ playerHp = newHp, inventory = newInventory newItem } | ||||||
| -- TODO Remove item if durability below zero |           (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 | ||||||
		Reference in a new issue