From 04c2839de5b860ff0efe957e2be9a21dbdfa329a Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 9 Feb 2023 23:43:14 +0000 Subject: Make entities in the map module This needs to be reviewed. --- src/Game.hs | 9 +-------- src/Game/Map.hs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index 8cdd45f..dcb5eab 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -45,13 +45,6 @@ data Env = Env defaultRenderRect :: SDL.Rectangle CInt defaultRenderRect = SDL.Rectangle (SDL.P $ V2 0 0) (V2 windowWidth windowHeight) -spawn :: M.Map -> S.SpriteSheet -> IORef C.Controls -> IO [E.Entity] -spawn m sprites controls = - traverse toEntity $ M.objects m - where - toEntity :: M.MapObject -> IO E.Entity - toEntity (M.PlayerEntity x y) = E.mkPlayer sprites x y controls (M.isBlocked m) - main :: IO () main = do SDL.initialize [SDL.InitVideo, SDL.InitGameController] @@ -77,7 +70,7 @@ main = do map' <- M.load "data/map.json" tsTexture sprites <- S.load "data/sprites.json" ssTexture entities <- newIORef $ E.mkEntities sprites - spawned <- spawn map' sprites controls + spawned <- M.mkEntities map' sprites controls entities $~ flip E.add spawned runReaderT gameLoop diff --git a/src/Game/Map.hs b/src/Game/Map.hs index b4d848e..9596b14 100644 --- a/src/Game/Map.hs +++ b/src/Game/Map.hs @@ -1,15 +1,18 @@ module Game.Map ( Map, - MapObject (..), - 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 @@ -133,9 +136,12 @@ 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 --- | Returns the objects in the map. -objects :: Map -> [MapObject] -objects (Map md _) = md.objects +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 () -- cgit v1.2.3