#4 Conditions

This commit is contained in:
Tibo De Peuter 2022-12-23 10:03:53 +01:00
parent 9addf1ed07
commit 72b460788d
3 changed files with 34 additions and 16 deletions

View file

@ -45,4 +45,10 @@ assetsFolder = "assets/"
-- Location of the level folder containing all levels -- Location of the level folder containing all levels
levelFolder :: FilePath levelFolder :: FilePath
levelFolder = "levels/" levelFolder = "levels/"
------------------------- Game configuration -------------------------
-- How many items can a player keep in their inventory?
inventorySize :: Int
inventorySize = 5

View file

@ -5,8 +5,8 @@ where
import GHC.IO (unsafePerformIO) import GHC.IO (unsafePerformIO)
import System.Directory (getDirectoryContents) import System.Directory (getDirectoryContents)
import RPGEngine.Input.Core (ListSelector(..)) 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.Data (Action(..), Level (..), Physical (..), Direction (..), Entity (..), Game (..), Item (..), Player (..), State (..), X, Y, Layout, Condition (InventoryFull, InventoryContains, Not, AlwaysFalse), ItemId)
import RPGEngine.Config (levelFolder) import RPGEngine.Config (levelFolder, inventorySize)
import Data.Foldable (find) import Data.Foldable (find)
------------------------------ Exported ------------------------------ ------------------------------ Exported ------------------------------
@ -53,21 +53,33 @@ getActions (Left item) = itemActions item
getActions (Right entity) = entityActions entity getActions (Right entity) = entityActions entity
getActionText :: Action -> String getActionText :: Action -> String
getActionText Leave = "Leave" getActionText Leave = "Leave"
getActionText (RetrieveItem _) = "Pick up" getActionText (RetrieveItem _) = "Pick up"
getActionText (UseItem _) = "Use item" getActionText (UseItem _) = "Use item"
getActionText (IncreasePlayerHp _) = "Take a healing potion"
getActionText (DecreaseHp _ used) = "Attack using " ++ used
getActionText _ = "ERROR" getActionText _ = "ERROR"
-- TODO Check conditions
-- Filter based on the conditions, keep only the actions of which the -- Filter based on the conditions, keep only the actions of which the
-- conditions are met. -- conditions are met.
filterActions :: [([Condition], Action)] -> [Action] -- Should receive a Playing state
filterActions [] = [] filterActions :: State -> [([Condition], Action)] -> [Action]
filterActions ((conditions, action):others) = action:filterActions others 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. -- Check if a condition is met or not.
meetsCondition :: Condition -> Bool meetsCondition :: State -> Condition -> Bool
meetsCondition InventoryFull = False -- TODO meetsCondition s InventoryFull = isInventoryFull $ player s
meetsCondition (InventoryContains id) = True -- TODO meetsCondition s (InventoryContains id) = inventoryContains id $ player s
meetsCondition (Not condition) = not $ meetsCondition condition meetsCondition s (Not condition) = not $ meetsCondition s condition
meetsCondition AlwaysFalse = False 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

View file

@ -104,7 +104,7 @@ checkForInteraction g = g{ state = Error "something went wrong while checking fo
interact :: Game -> Game interact :: Game -> Game
interact g@Game{ state = s@Playing{ level = level, player = player } } = g{ state = newState } interact g@Game{ state = s@Playing{ level = level, player = player } } = g{ state = newState }
where newState = ActionSelection actionList selector continue 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 selector = ListSelector 0 False
pos = position player pos = position player
continue = s continue = s