aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-02-09 23:25:05 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-02-09 23:25:05 +0000
commit6e48bd38850f6326fe435ff87af3ca14e6707379 (patch)
tree6db157869c0df517e7b0629d0445bbf418f43a3b /src
parent84057b11507a1434a9f189bca2fdb71df294c572 (diff)
downloadspace-plat-hs-6e48bd38850f6326fe435ff87af3ca14e6707379.tar.gz
space-plat-hs-6e48bd38850f6326fe435ff87af3ca14e6707379.zip
Spawn objects
For now only the Player.
Diffstat (limited to 'src')
-rw-r--r--src/Game.hs11
-rw-r--r--src/Game/Entities.hs6
-rw-r--r--src/Game/Map.hs7
3 files changed, 19 insertions, 5 deletions
diff --git a/src/Game.hs b/src/Game.hs
index 463ef27..8cdd45f 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -45,6 +45,13 @@ 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]
@@ -70,8 +77,8 @@ main = do
map' <- M.load "data/map.json" tsTexture
sprites <- S.load "data/sprites.json" ssTexture
entities <- newIORef $ E.mkEntities sprites
- player <- E.mkPlayer sprites 32 104 controls (M.isBlocked map')
- entities $~ E.append player
+ spawned <- spawn map' sprites controls
+ entities $~ flip E.add spawned
runReaderT
gameLoop
Env
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index 96cc4fa..7d29e41 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -1,4 +1,4 @@
-module Game.Entities (Entities, mkEntities, mkPlayer, mkEffect, append, updateAll, render) where
+module Game.Entities (Entities, Entity, mkEntities, mkPlayer, mkEffect, add, updateAll, render) where
import Data.Foldable (traverse_)
import Data.IORef
@@ -59,8 +59,8 @@ data Entity = Entity
mkEntities :: S.SpriteSheet -> Entities
mkEntities sprites = Entities sprites []
-append :: Entity -> Entities -> Entities
-append e (Entities sprites entities) = Entities sprites (entities ++ [e])
+add :: Entities -> [Entity] -> Entities
+add (Entities sprites entities) es = Entities sprites (entities ++ es)
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 a1c7e84..b4d848e 100644
--- a/src/Game/Map.hs
+++ b/src/Game/Map.hs
@@ -1,5 +1,7 @@
module Game.Map
( Map,
+ MapObject (..),
+ objects,
load,
render,
isBlocked,
@@ -28,6 +30,7 @@ data Layer
{name :: String, objects :: [MapObject]}
deriving (Show)
+-- | The object types in the map.
data MapObject = PlayerEntity Int Int deriving (Show)
data JsonMapData = JsonMapData
@@ -130,6 +133,10 @@ 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
+
-- | Renders a map.
render :: SDL.Renderer -> Map -> IO ()
render renderer (Map mapData tex) = do