aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-02-08 07:16:43 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-02-08 07:16:43 +0000
commitffc274d310b14eb663d018a00a64b78ba45d0fc5 (patch)
treef081426939e181d7fe81bc0fe1e1ef7a4f8c87cc /src
parent6c4ea0fd4831e887103fafe6587e1c6a73914833 (diff)
downloadspace-plat-hs-ffc274d310b14eb663d018a00a64b78ba45d0fc5.tar.gz
space-plat-hs-ffc274d310b14eb663d018a00a64b78ba45d0fc5.zip
Use NoFieldSelectors
Diffstat (limited to 'src')
-rw-r--r--src/Game.hs52
-rw-r--r--src/Game/Entities.hs20
2 files changed, 42 insertions, 30 deletions
diff --git a/src/Game.hs b/src/Game.hs
index 48a8e21..463ef27 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -31,15 +31,15 @@ version :: String
version = "0.1.0"
data Env = Env
- { _window :: SDL.Window,
- _renderer :: SDL.Renderer,
- _canvas :: SDL.Texture,
- _fullscreen :: IORef Bool,
- _renderRect :: IORef (SDL.Rectangle CInt),
- _controls :: IORef C.Controls,
- _map :: M.Map,
- _sprites :: S.SpriteSheet,
- _entities :: IORef E.Entities
+ { window :: SDL.Window,
+ renderer :: SDL.Renderer,
+ canvas :: SDL.Texture,
+ fullscreen :: IORef Bool,
+ renderRect :: IORef (SDL.Rectangle CInt),
+ controls :: IORef C.Controls,
+ map :: M.Map,
+ sprites :: S.SpriteSheet,
+ entities :: IORef E.Entities
}
defaultRenderRect :: SDL.Rectangle CInt
@@ -72,17 +72,29 @@ main = do
entities <- newIORef $ E.mkEntities sprites
player <- E.mkPlayer sprites 32 104 controls (M.isBlocked map')
entities $~ E.append player
- runReaderT gameLoop (Env window renderer canvas fullscreen renderRect controls map' sprites entities)
+ runReaderT
+ gameLoop
+ Env
+ { window = window,
+ renderer = renderer,
+ canvas = canvas,
+ fullscreen = fullscreen,
+ renderRect = renderRect,
+ controls = controls,
+ map = map',
+ sprites = sprites,
+ entities = entities
+ }
SDL.destroyWindow window
SDL.quit
toggleFullscreen :: ReaderT Env IO ()
toggleFullscreen = do
env <- ask
- let fullscreen = env._fullscreen
- renderRect = env._renderRect
- renderer = env._renderer
- window = env._window
+ let fullscreen = env.fullscreen
+ renderRect = env.renderRect
+ renderer = env.renderer
+ window = env.window
fullscreen $~ not
fs <- SDL.get fullscreen
let mode = if fs then SDL.FullscreenDesktop else SDL.Windowed
@@ -104,12 +116,12 @@ toggleFullscreen = do
gameLoop :: ReaderT Env IO ()
gameLoop = do
env <- ask
- let renderer = env._renderer
- canvas = env._canvas
- renderRect = env._renderRect
- controls = env._controls
- map' = env._map
- entities = env._entities
+ let renderer = env.renderer
+ canvas = env.canvas
+ renderRect = env.renderRect
+ controls = env.controls
+ map' = env.map
+ entities = env.entities
events <- map SDL.eventPayload <$> SDL.pollEvents
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index e8267db..96cc4fa 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -63,13 +63,13 @@ append :: Entity -> Entities -> Entities
append e (Entities sprites entities) = Entities sprites (entities ++ [e])
processSpawn :: S.SpriteSheet -> Spawn -> IO Entity
-processSpawn sprites (DustEffectSpawn x' y') = mkEffect sprites x' y' "dust"
+processSpawn sprites (DustEffectSpawn x y) = mkEffect sprites x y "dust"
updateAll :: Entities -> IO Entities
updateAll (Entities sprites entities) = do
updated <- traverse (\e -> e.update e) entities
new <- traverse (processSpawn sprites) (concatMap (\e -> e.spawns) updated)
- pure $ Entities sprites $ map (\e -> e {spawns = []}) (filter (not . destroy) updated) ++ new
+ pure $ Entities sprites $ map (\e -> e {spawns = []}) (filter (\e -> not e.destroy) updated) ++ new
render :: SDL.Renderer -> Entities -> IO ()
render renderer (Entities _ entities) = do
@@ -82,13 +82,13 @@ render renderer (Entities _ entities) = do
set = toSpriteSet e.dir
mkEffect :: S.SpriteSheet -> Int -> Int -> String -> IO Entity
-mkEffect sprites x' y' name = do
+mkEffect sprites x y name = do
s <- S.get sprites name
pure $
Entity
{ typ = TypeEffect,
- x = x',
- y = y',
+ x = x,
+ y = y,
delay = frameDelay,
frame = 0,
frameLimit = 3,
@@ -109,13 +109,13 @@ updateEffect e
| otherwise = e {destroy = True}
mkPlayer :: S.SpriteSheet -> Int -> Int -> IORef C.Controls -> (Int -> Int -> Bool) -> IO Entity
-mkPlayer sprites x' y' controls isBlocked' = do
+mkPlayer sprites x y controls isBlocked' = do
s <- S.get sprites "player"
pure $
Entity
{ typ = TypePlayer,
- x = x',
- y = y',
+ x = x,
+ y = y,
delay = 0,
frame = 0,
frameLimit = 3,
@@ -140,7 +140,7 @@ updateFrame updated e
updateHorizontal :: Bool -> Bool -> Entity -> Entity
updateHorizontal left right e
- -- prevent pressing both directions (ky'board)
+ -- prevent pressing both directions (kyboard)
| left && right = e
-- change direction first
| left && e.dir /= DirLeft = e {dir = DirLeft, delay = 0}
@@ -199,5 +199,5 @@ updatePlayer controls e = do
updateGravity $
updateVertical ctl.a ctl.down $
updateHorizontal ctl.left ctl.right $
- -- left or right, but not both (ky'board)
+ -- left or right, but not both (keyboard)
updateFrame ((ctl.left || ctl.right) && (ctl.left /= ctl.right)) e