#4 Decreasehp
This commit is contained in:
		
							parent
							
								
									11eb00ea0b
								
							
						
					
					
						commit
						c3f7e47703
					
				
					 2 changed files with 39 additions and 5 deletions
				
			
		|  | @ -68,8 +68,23 @@ pickUpItem id s@Playing{ level = level, player = player } = newState | |||
| pickUpItem _ _ = Error "Something went wrong while picking up an item" | ||||
| 
 | ||||
| -- Use an item | ||||
| useItem :: ItemId -> State -> State -- TODO | ||||
| useItem _ s = s -- TODO | ||||
| -- Should receive a Playing state | ||||
| useItem :: ItemId -> State -> State | ||||
| useItem iid s@Playing{ level = level, player = player} = newState | ||||
|     where newState = s{ level = newLevel, player = newPlayer } | ||||
|           -- Remove item from inventory if necessary | ||||
|           (Just usingItem) = find ((== iid) . itemId) $ inventory player | ||||
|           usedItem         = decreaseDurability usingItem | ||||
|           newInventory     = filter (/= usingItem) $ inventory player | ||||
|           newPlayer        = player{ inventory = putItemBack usedItem newInventory } | ||||
|           putItemBack Nothing inv     = inv | ||||
|           putItemBack (Just item) inv = item:inv | ||||
|           -- Remove entity if necessary | ||||
|           allEntities = entities level | ||||
|           entitiesWithUseItem = filter (any ((== UseItem iid) . snd) . entityActions) allEntities | ||||
|           attackedEntity = head entitiesWithUseItem | ||||
|           newLevel = level{ entities = filter (/= attackedEntity) $ entities level} | ||||
| useItem _ _ = Error "Something went wrong while using an item" | ||||
| 
 | ||||
| -- Attack an entity using an item | ||||
| -- Should receive a Playing state | ||||
|  | @ -77,7 +92,7 @@ decreaseHp :: EntityId -> ItemId -> State -> State | |||
| 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) | ||||
|           (Just usingItem) = find ((== iid) . itemId) $ inventory player | ||||
|           usedItem         = decreaseDurability usingItem | ||||
|           newInventory     = filter (/= usingItem) $ inventory player | ||||
|           newPlayer = player{ inventory = putItemBack usedItem newInventory, playerHp = newHp } | ||||
|  | @ -89,7 +104,7 @@ decreaseHp eid iid s@Playing{ level = level, player = player } = newState | |||
|           (Just (Right attackedEntity)) = getWithId eid level | ||||
|           newLevel = level{ entities = putEntityBack dealtWithEntity newEntities } | ||||
|           newEntities = filter ((/= eid) . entityId) $ entities level | ||||
|           dealtWithEntity = decreaseHealth attackedEntity damageDealAmount  | ||||
|           dealtWithEntity = decreaseHealth attackedEntity damageDealAmount | ||||
|           putEntityBack Nothing    list = list | ||||
|           putEntityBack (Just ent) list = ent:list | ||||
|           damageGetAmount = inverse (entityValue attackedEntity) | ||||
|  |  | |||
|  | @ -84,7 +84,7 @@ goToNextLevel s = s | |||
| 
 | ||||
| -- Move a player in a direction if possible. | ||||
| movePlayer :: Direction -> Game -> Game | ||||
| movePlayer dir g@Game{ state = s@Playing{ player = p@Player{ position = (x, y) }}} = newGame | ||||
| movePlayer dir g@Game{ state = s@Playing{ player = p@Player{ position = (x, y) }}} = tryForceInteraction newGame g | ||||
|     where newGame   = g{ state = newState } | ||||
|           newState  = s{ player   = newPlayer } | ||||
|           newPlayer = p{ position = newCoord  } | ||||
|  | @ -93,6 +93,25 @@ movePlayer dir g@Game{ state = s@Playing{ player = p@Player{ position = (x, y) } | |||
|           (xD, yD)  = directionOffsets dir | ||||
| movePlayer _ g = g{ state = Error "something went wrong while moving the player" } | ||||
| 
 | ||||
| -- TODO Clean this function | ||||
| -- Try to force an interaction. If there is an entity, you have to | ||||
| -- interact with it. If it is an item, the user should trigger this | ||||
| -- themselves. If forced, the player should not move to the new position. | ||||
| tryForceInteraction :: Game -> Game -> Game | ||||
| tryForceInteraction g@Game{ state = Playing { level = level, player = player }} fallBack@Game{ state = Playing{ player = firstPlayer }} = newGame triedInteraction | ||||
|     where newGame g@Game{ state = s@ActionSelection{ continue = c@Playing{ player = player}}} = g{ state = s{ continue = c{ player = playerWithRestorePos }}} | ||||
|           newGame g = g | ||||
|           playerWithRestorePos = (newPlayer triedInteraction){ position = position firstPlayer } | ||||
|           newPlayer Game{ state = ActionSelection{ continue = Playing{ player = player }}} = player | ||||
|           triedInteraction  | hasEntity (hasAt pos level) = interact g | ||||
|                             | otherwise = g | ||||
|           pos = position player | ||||
|           hasEntity (Just (Right entity)) = True | ||||
|           hasEntity _                     = False | ||||
| tryForceInteraction g _ = g{ state = Error "something went wrong while trying to force interaction"} | ||||
| 
 | ||||
| -- If there is an interaction at the current position, go to  | ||||
| -- actionSelection state. Otherwise just continue the game. | ||||
| checkForInteraction :: Game -> Game | ||||
| checkForInteraction g@Game{ state = Playing{ level = level, player = player }} = newGame | ||||
|     where newGame       | canInteract = interact g | ||||
|  |  | |||
		Reference in a new issue