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 | |
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')
-rw-r--r-- | src/Game.hs | 4 | ||||
-rw-r--r-- | src/Game/Entities.hs | 15 | ||||
-rw-r--r-- | src/Game/Map.hs | 28 |
3 files changed, 21 insertions, 26 deletions
diff --git a/src/Game.hs b/src/Game.hs index dcb5eab..c267453 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -69,9 +69,7 @@ main = do controls <- newIORef =<< C.init map' <- M.load "data/map.json" tsTexture sprites <- S.load "data/sprites.json" ssTexture - entities <- newIORef $ E.mkEntities sprites - spawned <- M.mkEntities map' sprites controls - entities $~ flip E.add spawned + entities <- newIORef =<< E.mkEntities sprites map' controls runReaderT gameLoop Env diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index 7d29e41..36d25c1 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -1,8 +1,9 @@ -module Game.Entities (Entities, Entity, mkEntities, mkPlayer, mkEffect, add, updateAll, render) where +module Game.Entities (Entities, Entity, mkEntities, updateAll, render) where import Data.Foldable (traverse_) import Data.IORef import qualified Game.Controller as C +import qualified Game.Map as M import qualified Game.Sprites as S import qualified SDL @@ -56,11 +57,13 @@ data Entity = Entity spawns :: [Spawn] } -mkEntities :: S.SpriteSheet -> Entities -mkEntities sprites = Entities sprites [] - -add :: Entities -> [Entity] -> Entities -add (Entities sprites entities) es = Entities sprites (entities ++ es) +mkEntities :: S.SpriteSheet -> M.Map -> IORef C.Controls -> IO Entities +mkEntities sprites m controls = do + entities <- traverse toEntity $ M.objects m + pure $ Entities sprites entities + where + toEntity :: M.Object -> IO Entity + toEntity (M.PlayerEntity x y) = mkPlayer sprites x y controls (M.isBlocked m) processSpawn :: S.SpriteSheet -> Spawn -> IO Entity processSpawn sprites (DustEffectSpawn x y) = mkEffect sprites x y "dust" 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 () |