diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-02-10 12:50:40 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-02-10 12:50:40 +0000 |
commit | cfc5b71c86a4927fa8d2294fce5e8b8264b4a3ca (patch) | |
tree | 091e64d3a936d28759f641627c96f5fd76bc595c /src/Game/Map.hs | |
parent | 04c2839de5b860ff0efe957e2be9a21dbdfa329a (diff) | |
download | space-plat-hs-cfc5b71c86a4927fa8d2294fce5e8b8264b4a3ca.tar.gz space-plat-hs-cfc5b71c86a4927fa8d2294fce5e8b8264b4a3ca.zip |
Moved the entity spawn to Entity module
Diffstat (limited to 'src/Game/Map.hs')
-rw-r--r-- | src/Game/Map.hs | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/src/Game/Map.hs b/src/Game/Map.hs index 9596b14..b79ba14 100644 --- a/src/Game/Map.hs +++ b/src/Game/Map.hs @@ -1,18 +1,15 @@ module Game.Map - ( Map, + ( Map (..), + Object (..), + objects, load, - mkEntities, render, isBlocked, ) where import Control.Monad -import Data.IORef import Data.Maybe (mapMaybe) -import qualified Game.Controller as C -import qualified Game.Entities as E -import qualified Game.Sprites as S import qualified Game.Utils as U import qualified SDL import Text.JSON @@ -30,11 +27,11 @@ data Layer = TileLayer {name :: String, tiles :: [Int]} | ObjectLayer - {name :: String, objects :: [MapObject]} + {name :: String, objects :: [Object]} deriving (Show) -- | The object types in the map. -data MapObject = PlayerEntity Int Int deriving (Show) +data Object = PlayerEntity Int Int deriving (Show) data JsonMapData = JsonMapData { width :: Int, @@ -65,7 +62,7 @@ instance JSON Layer where _ -> Error "unsupported layer type" readJSON _ = mzero -instance JSON MapObject where +instance JSON Object where showJSON = undefined readJSON (JSObject obj) = case get_field obj "name" of @@ -91,7 +88,7 @@ data MapData = MapData tileset :: Tileset, tileLayers :: [[Int]], blocked :: [Int], - objects :: [MapObject] + objects :: [Object] } deriving (Show) @@ -125,7 +122,7 @@ load filename tex = do filterTileLayer f l = case l of TileLayer _ tiles -> if f l then Just tiles else Nothing _ -> Nothing - filterObjectLayer :: Layer -> Maybe [MapObject] + filterObjectLayer :: Layer -> Maybe [Object] filterObjectLayer l = case l of ObjectLayer _ objs -> Just objs _ -> Nothing @@ -136,12 +133,9 @@ isBlocked :: Map -> Int -> Int -> Bool isBlocked (Map (MapData mapWidth _ ts _ blocked _) _) x y = blocked !! ((x `div` ts.width) + (y `div` ts.height) * mapWidth) >= ts.firstGid -mkEntities :: Map -> S.SpriteSheet -> IORef C.Controls -> IO [E.Entity] -mkEntities m@(Map md _) sprites controls = - traverse toEntity $ md.objects - where - toEntity :: MapObject -> IO E.Entity - toEntity (PlayerEntity x y) = E.mkPlayer sprites x y controls (isBlocked m) +-- | Return the objects in a map. +objects :: Map -> [Object] +objects (Map md _) = md.objects -- | Renders a map. render :: SDL.Renderer -> Map -> IO () |