packages feed

venzone-1.0.0.0: src/Room.hs

{-# Language TemplateHaskell #-}

module Room where

import Plant
import Meeple.Operate
import Input

import Terminal.Game
import Lens.Micro.Platform

import Data.List as L


-- Everything non-modifiable (reader-ish) about a Screen.

----------
-- DATA --
----------

data Exit = Exit { eCardinal :: Cardinal,
                   eTitle    :: Title }
          deriving (Eq, Show)

-- Room structure
type Title = String
data Room = Room { _title         :: Title,
                   _plant         :: Plant,
                   _frozenMeeples :: [Meeple],
                        -- initial chars positions
                   _exits   :: [Exit] }
          deriving (Eq, Show)
makeLenses ''Room

defaultRoom :: Room
defaultRoom = Room "<default-room>" defaultPlant [] []

addMeeple :: Meeple -> Room -> Room
addMeeple m r = r & frozenMeeples %~ (m:)

---------------
-- FUNCTIONS --
---------------

cardinalTitle :: Room -> Cardinal -> Maybe Title
cardinalTitle r c =
        r ^? exits . each . filtered ((==c) . eCardinal) . to eTitle

-- use this instead of plantBoundaries, as it will fail with useful
-- info.
boundaries :: Room -> Coords
boundaries r
        | null rl ||
          null cl    = error $ "boundaries, empty plant in room " ++
                         show (r ^. title)
        | otherwise  = (maximum rl, maximum cl)
    where
          p        = r ^. plant
          (rl, cl) = unzip (map fst $ plantList p)

isOOBRoom :: Room -> Coords -> Bool
isOOBRoom ro (r, c) = let (rr, rc) = boundaries ro
                      in r < 1 || r > rr ||
                         c < 1 || c > rc

findSpawn :: Room -> Maybe Coords
findSpawn r = (^. position) <$> L.find isSpawn (r ^. frozenMeeples)
    where
          -- isQualcosa possono essere generalizzatr
          isSpawn (MSave _) = True
          isSpawn _         = False


----------
-- DRAW --
----------

drawRoom :: Room -> Plane
drawRoom r = r ^. plant . to drawPlant