#4 Attack
This commit is contained in:
parent
f284413836
commit
11eb00ea0b
6 changed files with 59 additions and 21 deletions
|
@ -4,11 +4,11 @@ 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, playerHp, Player), Item (..), HP)
|
||||
import RPGEngine.Data (Game (..), State (..), Direction (..), Action (..), ItemId, EntityId, Level (..), Player (inventory, playerHp, Player), Item (..), HP, Entity (..))
|
||||
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 RPGEngine.Data.Level (getWithId, itemFromInventory)
|
||||
import Data.Foldable (find)
|
||||
|
||||
------------------------------ Exported ------------------------------
|
||||
|
@ -72,13 +72,30 @@ useItem :: ItemId -> State -> State -- TODO
|
|||
useItem _ s = s -- TODO
|
||||
|
||||
-- Attack an entity using an item
|
||||
-- Should receive a Playing state
|
||||
decreaseHp :: EntityId -> ItemId -> State -> State
|
||||
decreaseHp _ _ s = s
|
||||
-- TODO DecreaseHp of monster
|
||||
-- TODO Check if monster is dead
|
||||
-- TODO Entity attack player
|
||||
-- TODO Decrease durability of item
|
||||
-- TODO Break item if durability below zero
|
||||
decreaseHp eid iid s@Playing{ level = level, player = player } = newState
|
||||
where newState = s{ level = newLevel, player = newPlayer }
|
||||
-- Change player
|
||||
(Just usingItem) = find ((== iid) . itemId) (inventory player)
|
||||
usedItem = decreaseDurability usingItem
|
||||
newInventory = filter (/= usingItem) $ inventory player
|
||||
newPlayer = player{ inventory = putItemBack usedItem newInventory, playerHp = newHp }
|
||||
putItemBack Nothing inv = inv
|
||||
putItemBack (Just item) inv = item:inv
|
||||
newHp = changeHealth (playerHp player) damageGetAmount -- Damage dealt by entity
|
||||
damageDealAmount = itemValue usingItem
|
||||
-- Change entity
|
||||
(Just (Right attackedEntity)) = getWithId eid level
|
||||
newLevel = level{ entities = putEntityBack dealtWithEntity newEntities }
|
||||
newEntities = filter ((/= eid) . entityId) $ entities level
|
||||
dealtWithEntity = decreaseHealth attackedEntity damageDealAmount
|
||||
putEntityBack Nothing list = list
|
||||
putEntityBack (Just ent) list = ent:list
|
||||
damageGetAmount = inverse (entityValue attackedEntity)
|
||||
inverse (Just val) = Just (-val)
|
||||
inverse Nothing = Nothing
|
||||
decreaseHp _ _ _ = Error "something went wrong while attacking"
|
||||
|
||||
-- Heal a bit
|
||||
-- Should receive a Player
|
||||
|
@ -97,6 +114,12 @@ decreaseDurability item@Item{ useTimes = Nothing } = Just item -- Infinite uses
|
|||
decreaseDurability item@Item{ useTimes = Just val } | 0 < val - 1 = Just item{ useTimes = Just (val - 1) }
|
||||
| otherwise = Nothing -- Broken
|
||||
|
||||
decreaseHealth :: Entity -> Maybe Int -> Maybe Entity
|
||||
decreaseHealth entity@Entity{ entityHp = Nothing } _ = Just entity
|
||||
decreaseHealth entity@Entity{ entityHp = Just val } (Just i) | 0 < val - i = Just entity{ entityHp = Just (val - i) }
|
||||
| otherwise = Nothing
|
||||
decreaseHealth entity _ = Just entity
|
||||
|
||||
-- Change given health by a given amount
|
||||
changeHealth :: HP -> HP -> HP
|
||||
changeHealth (Just health) (Just difference) = Just (health + difference)
|
||||
|
|
Reference in a new issue