From 72b460788da4fc38dd5b200ec610ef5e9684d76a Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Fri, 23 Dec 2022 10:03:53 +0100 Subject: [PATCH] #4 Conditions --- lib/RPGEngine/Config.hs | 8 ++++++- lib/RPGEngine/Data/Level.hs | 40 ++++++++++++++++++++++------------ lib/RPGEngine/Input/Playing.hs | 2 +- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/RPGEngine/Config.hs b/lib/RPGEngine/Config.hs index 66af492..49d0cc7 100644 --- a/lib/RPGEngine/Config.hs +++ b/lib/RPGEngine/Config.hs @@ -45,4 +45,10 @@ assetsFolder = "assets/" -- Location of the level folder containing all levels levelFolder :: FilePath -levelFolder = "levels/" \ No newline at end of file +levelFolder = "levels/" + +------------------------- Game configuration ------------------------- + +-- How many items can a player keep in their inventory? +inventorySize :: Int +inventorySize = 5 \ No newline at end of file diff --git a/lib/RPGEngine/Data/Level.hs b/lib/RPGEngine/Data/Level.hs index f415844..7265e25 100644 --- a/lib/RPGEngine/Data/Level.hs +++ b/lib/RPGEngine/Data/Level.hs @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/lib/RPGEngine/Input/Playing.hs b/lib/RPGEngine/Input/Playing.hs index f2c3f1a..02e70d6 100644 --- a/lib/RPGEngine/Input/Playing.hs +++ b/lib/RPGEngine/Input/Playing.hs @@ -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