#4 Setup interaction

This commit is contained in:
Tibo De Peuter 2022-12-23 09:42:34 +01:00
parent ef784c2dbc
commit 9addf1ed07
13 changed files with 223 additions and 33 deletions

View file

@ -5,8 +5,9 @@ where
import GHC.IO (unsafePerformIO)
import System.Directory (getDirectoryContents)
import RPGEngine.Input.Core (ListSelector(..))
import RPGEngine.Data (Level (..), Physical (..), Direction (..), Entity (..), Game (..), Item (..), Player (..), State (..), X, Y, Layout)
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 Data.Foldable (find)
------------------------------ Exported ------------------------------
@ -25,6 +26,17 @@ findAt pos lvl@Level{ index = index } = try
try | not (null matches) = head matches
| otherwise = Void
hasAt :: (X, Y) -> Level -> Maybe (Either Item Entity)
hasAt pos level = match firstItem firstEntity
where match :: Maybe Item -> Maybe Entity -> Maybe (Either Item Entity)
match (Just a) _ = Just $ Left a
match _ (Just a) = Just $ Right a
match _ _ = Nothing
firstEntity = find ((== pos) . getECoord) $ entities level
getECoord e = (entityX e, entityY e)
firstItem = find ((== pos) . getICoord) $ items level
getICoord i = (itemX i, itemY i)
directionOffsets :: Direction -> (X, Y)
directionOffsets North = ( 0, 1)
directionOffsets East = ( 1, 0)
@ -33,4 +45,29 @@ directionOffsets West = (-1, 0)
directionOffsets Stay = ( 0, 0)
getLevelList :: [FilePath]
getLevelList = drop 2 $ unsafePerformIO $ getDirectoryContents levelFolder
getLevelList = drop 2 $ unsafePerformIO $ getDirectoryContents levelFolder
-- Get the actions of either an entity or an item
getActions :: Either Item Entity -> [([Condition], Action)]
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 _ = "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
-- 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