aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-02-19 20:43:38 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-02-19 20:43:38 +0000
commit93aedcb24f6646ebcbca105267dc07f369fc6391 (patch)
treeaf2508f0f5145e06272292c3a330522749bcdf7f /src
parent3748dc2551de1704fa1a20df6bd5f4d9917cda3f (diff)
downloadspace-plat-hs-93aedcb24f6646ebcbca105267dc07f369fc6391.tar.gz
space-plat-hs-93aedcb24f6646ebcbca105267dc07f369fc6391.zip
A bit cleaner
Diffstat (limited to 'src')
-rw-r--r--src/Game/Entities.hs20
1 files changed, 12 insertions, 8 deletions
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