module RPGEngine.Input.Level ( putCoords , findFirst , whatIsAt ) where import RPGEngine.Data (Level (..), Y, X, Physical(..)) -- Map all Physicals onto coordinates putCoords :: Level -> [(X, Y, Physical)] putCoords l@Level{ layout = lay } = concatMap (\(a, bs) -> map (\(b, c) -> (b, a, c)) bs) numberedList where numberedStrips = zip [0::Int .. ] lay numberedList = map (\(x, strip) -> (x, zip [0::Int ..] strip)) numberedStrips -- Find first position of a Physical -- Graceful exit by giving Nothing if there is nothing found. findFirst :: Level -> Physical -> Maybe (X, Y) findFirst l@Level{ coordlayout = lay } physical = try where matches = filter (\(x, y, v) -> v == physical) lay try | not (null matches) = Just $ (\(x, y, _) -> (x, y)) $ head matches | otherwise = Nothing -- What is located at a given position in the level? whatIsAt :: (X, Y) -> Level -> Physical whatIsAt pos lvl@Level{ coordlayout = lay } = try where matches = map (\(_, _, v) -> v) $ filter (\(x, y, v) -> (x, y) == pos) lay try | not (null matches) = head matches | otherwise = Void