aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-01 20:34:22 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-01 20:34:22 +0000
commit6f9e98cc5cefa9b35f139d3715339d676eff18ed (patch)
treeed752753a5e9fa684e29d3ab7b882183d241ef31 /src
parent16039ab6f25b5c10421d9d0bd96a3d97a0d2d5c9 (diff)
downloadspace-plat-hs-6f9e98cc5cefa9b35f139d3715339d676eff18ed.tar.gz
space-plat-hs-6f9e98cc5cefa9b35f139d3715339d676eff18ed.zip
Removed another unncessary IORef
Diffstat (limited to 'src')
-rw-r--r--src/Game.hs87
1 files changed, 45 insertions, 42 deletions
diff --git a/src/Game.hs b/src/Game.hs
index 2d8c23a..41c8178 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -42,12 +42,12 @@ data Env = Env
canvas :: SDL.Texture,
fullscreen :: Bool,
renderRect :: SDL.Rectangle CInt,
- controls :: IORef C.Controls,
map :: M.Map,
sprites :: S.SpriteSheet,
font :: BF.BitmapFont,
- entities :: IORef E.Entities,
+ entities :: E.Entities,
hud :: H.Hud,
+ controls :: IORef C.Controls,
state :: IORef GS.State
}
@@ -74,7 +74,6 @@ main = do
tsTexture <- SDL.Image.loadTexture renderer "data/tiles.png"
ssTexture <- SDL.Image.loadTexture renderer "data/sprites.png"
bfTexture <- SDL.Image.loadTexture renderer "data/font.png"
- controls <- newIORef =<< C.init
map' <- M.load "data/map.json" tsTexture
sprites <- S.load "data/sprites.json" ssTexture
font <- BF.load "data/font.json" bfTexture
@@ -88,8 +87,9 @@ main = do
hitDelay = 0,
gameOverDelay = 0
}
+ controls <- newIORef =<< C.init
hud <- H.mkHud sprites state
- entities <- newIORef =<< E.mkEntities sprites map' controls state
+ entities <- E.mkEntities sprites map' controls state
gameLoop
Env
{ window = window,
@@ -97,12 +97,12 @@ main = do
canvas = canvas,
fullscreen = False,
renderRect = defaultRenderRect,
- controls = controls,
map = map',
sprites = sprites,
font = font,
entities = entities,
hud = hud,
+ controls = controls,
state = state
}
SDL.destroyWindow window
@@ -141,6 +141,7 @@ gameLoop e = do
renderRect = env.renderRect
controls = env.controls
stateRef = env.state
+ entities = env.entities
-- ESC or close the window to quit
let quit = fromMaybe False (U.isPressed SDL.KeycodeEscape events) || SDL.QuitEvent `elem` events
@@ -153,8 +154,9 @@ gameLoop e = do
state <- readIORef stateRef
when (state.gameOverDelay > 1) $ stateRef $= state {GS.gameOverDelay = state.gameOverDelay - 1}
- when (state.gameOverDelay /= 1) playLoop
- when (state.gameOverDelay == 1) gameOverLoop
+ when (state.gameOverDelay == 1) $ gameOverLoop env
+
+ updatedEntities <- if state.gameOverDelay /= 1 then playLoop env else pure entities
SDL.rendererRenderTarget renderer $= Nothing
SDL.clear renderer
@@ -162,38 +164,39 @@ gameLoop e = do
SDL.present renderer
- gameLoop env
- where
- playLoop :: IO ()
- playLoop = do
- let renderer = e.renderer
- map' = e.map
- entities = e.entities
- hud = e.hud
-
- updated <- E.updateAll =<< readIORef entities
- entities $= updated
-
- -- to update the map viewport
- let (px, py) = E.playerPosition updated
-
- -- render map and entities
-
- -- set the SDL viewport
- viewport <- M.viewport renderer map' px py (fromIntegral gameWidth) (fromIntegral gameHeight - H.height) (Just (0, H.height))
- M.render renderer map' viewport
- E.renderVisible renderer updated viewport
-
- -- reset viewport to draw the HUD
- SDL.rendererViewport renderer $= Nothing
- H.render renderer hud
-
- gameOverLoop :: IO ()
- gameOverLoop = do
- let renderer = e.renderer
- sprites = e.sprites
- hud = e.hud
-
- H.render renderer hud
- title <- S.get sprites "game-over"
- S.render renderer title 112 80 0 0
+ gameLoop env {entities = updatedEntities}
+
+playLoop :: Env -> IO E.Entities
+playLoop e = do
+ let renderer = e.renderer
+ map' = e.map
+ entities = e.entities
+ hud = e.hud
+
+ updated <- E.updateAll entities
+
+ -- to update the map viewport
+ let (px, py) = E.playerPosition updated
+
+ -- render map and entities
+
+ -- set the SDL viewport
+ viewport <- M.viewport renderer map' px py (fromIntegral gameWidth) (fromIntegral gameHeight - H.height) (Just (0, H.height))
+ M.render renderer map' viewport
+ E.renderVisible renderer updated viewport
+
+ -- reset viewport to draw the HUD
+ SDL.rendererViewport renderer $= Nothing
+ H.render renderer hud
+
+ pure updated
+
+gameOverLoop :: Env -> IO ()
+gameOverLoop e = do
+ let renderer = e.renderer
+ sprites = e.sprites
+ hud = e.hud
+
+ H.render renderer hud
+ title <- S.get sprites "game-over"
+ S.render renderer title 112 80 0 0