aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-02-09 23:43:14 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-02-09 23:43:14 +0000
commit04c2839de5b860ff0efe957e2be9a21dbdfa329a (patch)
treef179999988414fc8fff62b0e50d9d15d0e813f1b
parent6e48bd38850f6326fe435ff87af3ca14e6707379 (diff)
downloadspace-plat-hs-04c2839de5b860ff0efe957e2be9a21dbdfa329a.tar.gz
space-plat-hs-04c2839de5b860ff0efe957e2be9a21dbdfa329a.zip
Make entities in the map module
This needs to be reviewed.
-rw-r--r--src/Game.hs9
-rw-r--r--src/Game/Map.hs16
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 ()