From 93aedcb24f6646ebcbca105267dc07f369fc6391 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 19 Feb 2023 20:43:38 +0000 Subject: A bit cleaner --- src/Game/Entities.hs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/Game/Entities.hs') diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index a74cedc..10ed1a8 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -1,5 +1,6 @@ module Game.Entities (Entities, Entity, mkEntities, updateAll, render) where +import Control.Monad (when) import Data.Bits (Bits (..)) import Data.Foldable (find, traverse_) import Data.IORef @@ -124,15 +125,11 @@ updateAll es = do updatedPlayer <- player.update player _ <- writeIORef es.player updatedPlayer state <- readIORef es.state + -- update hit delay if the player was hit + let playerWasHit = state.hitDelay > 0 + when playerWasHit (writeIORef es.state state {GS.hitDelay = state.hitDelay - 1}) -- then the other entities - updated <- - if state.hitDelay > 0 - then do - -- if the player was hit, update state and don't update the enemies - _ <- writeIORef es.state state {GS.hitDelay = state.hitDelay - 1} - (updatedPlayer :) <$> traverse (\e -> if notEnemy e then e.update e else pure e) others - else -- otherwise update all - (updatedPlayer :) <$> traverse (\e -> e.update e) others + updated <- (updatedPlayer :) <$> traverse (updateFilter playerWasHit) others -- collect new entities new <- traverse (processSpawn es.sprites) (concatMap (\e -> e.spawns) updated) -- clear spawns (new entities), filter out destroyed entities, and add the new ones @@ -141,6 +138,13 @@ updateAll es = do player = head es.entities others = tail es.entities + -- Update entities skipping enemies if the player was hit. + updateFilter :: Bool -> Entity -> IO Entity + updateFilter False e = e.update e + updateFilter True e + | notEnemy e = e.update e + | otherwise = pure e + notEnemy :: Entity -> Bool notEnemy ent = case ent.typ of TypeEnemy -> False -- cgit v1.2.3