aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Map.hs
blob: 9596b14a1ab454b2bf112266c8771f4fc16b8b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
module Game.Map
  ( Map,
    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
import Text.JSON.Types

data Tileset = Tileset
  { cols :: Int,
    width :: Int,
    height :: Int,
    firstGid :: Int
  }
  deriving (Show)

data Layer
  = TileLayer
      {name :: String, tiles :: [Int]}
  | ObjectLayer
      {name :: String, objects :: [MapObject]}
  deriving (Show)

-- | The object types in the map.
data MapObject = PlayerEntity Int Int deriving (Show)

data JsonMapData = JsonMapData
  { width :: Int,
    height :: Int,
    tileset :: Tileset,
    layers :: [Layer]
  }
  deriving (Show)

instance JSON Tileset where
  showJSON = undefined
  readJSON (JSArray [JSObject obj]) = do
    Tileset
      <$> valFromObj "columns" obj
      <*> valFromObj "tilewidth" obj
      <*> valFromObj "tileheight" obj
      <*> valFromObj "firstgid" obj
  readJSON _ = mzero

instance JSON Layer where
  showJSON = undefined
  readJSON (JSObject obj) =
    case get_field obj "type" of
      Just "tilelayer" ->
        TileLayer <$> valFromObj "name" obj <*> valFromObj "data" obj
      Just "objectgroup" ->
        ObjectLayer <$> valFromObj "name" obj <*> valFromObj "objects" obj
      _ -> Error "unsupported layer type"
  readJSON _ = mzero

instance JSON MapObject where
  showJSON = undefined
  readJSON (JSObject obj) =
    case get_field obj "name" of
      Just "Player" ->
        PlayerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj
      Just (JSString (JSONString s)) -> Error $ "unsupported entity " ++ show s
      e -> Error $ "unsupported entity in " ++ show e
  readJSON _ = mzero

instance JSON JsonMapData where
  showJSON = undefined
  readJSON (JSObject obj) =
    JsonMapData
      <$> valFromObj "width" obj
      <*> valFromObj "height" obj
      <*> valFromObj "tilesets" obj
      <*> valFromObj "layers" obj
  readJSON _ = mzero

data MapData = MapData
  { width :: Int,
    height :: Int,
    tileset :: Tileset,
    tileLayers :: [[Int]],
    blocked :: [Int],
    objects :: [MapObject]
  }
  deriving (Show)

data Map = Map MapData SDL.Texture

-- | Loads a map from a JSON file.
load :: String -> SDL.Texture -> IO Map
load filename tex = do
  d <- readFile filename
  case decode d :: Result JsonMapData of
    Ok s -> do
      let tileLayers = mapMaybe (filterTileLayer (\l -> l.name /= "Blocked")) s.layers
          blockedLayers = mapMaybe (filterTileLayer (\l -> l.name == "Blocked")) s.layers
      blockedLayer <- case blockedLayers of
        [l] -> pure l
        xs -> error $ "A single \"Blocked\" layer expected, " ++ show (length xs) ++ " found"
      pure $
        Map
          MapData
            { width = s.width,
              height = s.height,
              tileset = s.tileset,
              tileLayers = tileLayers,
              blocked = blockedLayer,
              objects = concat $ mapMaybe filterObjectLayer s.layers
            }
          tex
    Error e -> error e
  where
    filterTileLayer :: (Layer -> Bool) -> Layer -> Maybe [Int]
    filterTileLayer f l = case l of
      TileLayer _ tiles -> if f l then Just tiles else Nothing
      _ -> Nothing
    filterObjectLayer :: Layer -> Maybe [MapObject]
    filterObjectLayer l = case l of
      ObjectLayer _ objs -> Just objs
      _ -> Nothing

-- | Check for collision detection vs "Blocked" TileLayer.
--   x and y in pixels.
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)

-- | Renders a map.
render :: SDL.Renderer -> Map -> IO ()
render renderer (Map mapData tex) = do
  mapM_
    ( \layer ->
        mapM_
          ( \(x, y) ->
              renderTile x y $ layer !! (x + (y * mapData.width))
          )
          index
    )
    mapData.tileLayers
  where
    index = [(x, y) | x <- [0 .. mapData.width - 1], y <- [0 .. mapData.height - 1]]

    columns = mapData.tileset.cols
    firstgid = mapData.tileset.firstGid
    tileWidth = mapData.tileset.width
    tileHeight = mapData.tileset.height

    renderTile :: Int -> Int -> Int -> IO ()
    renderTile x y tile
      | tile < firstgid = pure ()
      | otherwise = do
          let tx = (tile - firstgid) `rem` columns
              ty = (tile - firstgid) `div` columns
              src = U.rect (tx * tileWidth) (ty * tileHeight) tileWidth tileHeight
              dst = U.rect (x * tileWidth) (y * tileHeight) tileWidth tileHeight
          SDL.copy renderer tex (Just src) (Just dst)