From cfc5b71c86a4927fa8d2294fce5e8b8264b4a3ca Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 10 Feb 2023 12:50:40 +0000 Subject: Moved the entity spawn to Entity module --- src/Game/Entities.hs | 15 +++++++++------ src/Game/Map.hs | 28 +++++++++++----------------- 2 files changed, 20 insertions(+), 23 deletions(-) (limited to 'src/Game') 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 () -- cgit v1.2.3