#4 Conditions
This commit is contained in:
		
							parent
							
								
									9addf1ed07
								
							
						
					
					
						commit
						72b460788d
					
				
					 3 changed files with 34 additions and 16 deletions
				
			
		|  | @ -45,4 +45,10 @@ assetsFolder = "assets/" | |||
| 
 | ||||
| -- Location of the level folder containing all levels | ||||
| levelFolder :: FilePath | ||||
| levelFolder = "levels/" | ||||
| levelFolder = "levels/" | ||||
| 
 | ||||
| ------------------------- Game configuration ------------------------- | ||||
| 
 | ||||
| -- How many items can a player keep in their inventory? | ||||
| inventorySize :: Int | ||||
| inventorySize = 5 | ||||
|  | @ -5,8 +5,8 @@ where | |||
| import GHC.IO (unsafePerformIO) | ||||
| import System.Directory (getDirectoryContents) | ||||
| import RPGEngine.Input.Core (ListSelector(..)) | ||||
| import RPGEngine.Data (Action(..), Level (..), Physical (..), Direction (..), Entity (..), Game (..), Item (..), Player (..), State (..), X, Y, Layout, Condition (InventoryFull, InventoryContains, Not, AlwaysFalse)) | ||||
| import RPGEngine.Config (levelFolder) | ||||
| import RPGEngine.Data (Action(..), Level (..), Physical (..), Direction (..), Entity (..), Game (..), Item (..), Player (..), State (..), X, Y, Layout, Condition (InventoryFull, InventoryContains, Not, AlwaysFalse), ItemId) | ||||
| import RPGEngine.Config (levelFolder, inventorySize) | ||||
| import Data.Foldable (find) | ||||
| 
 | ||||
| ------------------------------ Exported ------------------------------ | ||||
|  | @ -53,21 +53,33 @@ getActions (Left item)    = itemActions item | |||
| getActions (Right entity) = entityActions entity | ||||
| 
 | ||||
| getActionText :: Action -> String | ||||
| getActionText Leave            = "Leave" | ||||
| getActionText (RetrieveItem _) = "Pick up" | ||||
| getActionText (UseItem _)      = "Use item" | ||||
| getActionText Leave                = "Leave" | ||||
| getActionText (RetrieveItem _)     = "Pick up" | ||||
| getActionText (UseItem _)          = "Use item" | ||||
| getActionText (IncreasePlayerHp _) = "Take a healing potion" | ||||
| getActionText (DecreaseHp _ used)  = "Attack using " ++ used | ||||
| getActionText _ = "ERROR" | ||||
| 
 | ||||
| -- TODO Check conditions | ||||
| -- Filter based on the conditions, keep only the actions of which the | ||||
| -- conditions are met. | ||||
| filterActions :: [([Condition], Action)] -> [Action] | ||||
| filterActions [] = [] | ||||
| filterActions ((conditions, action):others) = action:filterActions others | ||||
| -- Should receive a Playing state | ||||
| filterActions :: State -> [([Condition], Action)] -> [Action] | ||||
| filterActions _ [] = [] | ||||
| filterActions s (entry:others) = met entry $ filterActions  s others | ||||
|     where met (conditions, action) l | all (meetsCondition s) conditions = action:l | ||||
|                                      | otherwise = l | ||||
| 
 | ||||
| -- Check if a condition is met or not. | ||||
| meetsCondition :: Condition -> Bool | ||||
| meetsCondition InventoryFull          = False -- TODO | ||||
| meetsCondition (InventoryContains id) = True -- TODO | ||||
| meetsCondition (Not condition)        = not $ meetsCondition condition | ||||
| meetsCondition AlwaysFalse            = False | ||||
| meetsCondition :: State -> Condition -> Bool | ||||
| meetsCondition s InventoryFull          = isInventoryFull $ player s | ||||
| meetsCondition s (InventoryContains id) = inventoryContains id $ player s | ||||
| meetsCondition s (Not condition)        = not $ meetsCondition s condition | ||||
| meetsCondition _ AlwaysFalse            = False | ||||
| 
 | ||||
| -- Check if the inventory of the player is full. | ||||
| isInventoryFull :: Player -> Bool | ||||
| isInventoryFull p = inventorySize <= length (inventory p) | ||||
| 
 | ||||
| -- Check if the inventory of the player contains an item. | ||||
| inventoryContains :: ItemId -> Player -> Bool | ||||
| inventoryContains id p = any ((== id) . itemId) $ inventory p | ||||
|  | @ -104,7 +104,7 @@ checkForInteraction g = g{ state = Error "something went wrong while checking fo | |||
| interact :: Game -> Game | ||||
| interact g@Game{ state = s@Playing{ level = level, player = player } } = g{ state = newState } | ||||
|     where newState   = ActionSelection actionList selector continue | ||||
|           actionList = filterActions $ getActions $ fromJust $ hasAt pos level | ||||
|           actionList = filterActions s $ getActions $ fromJust $ hasAt pos level | ||||
|           selector   = ListSelector 0 False | ||||
|           pos        = position player | ||||
|           continue   = s | ||||
|  |  | |||
		Reference in a new issue