From 897bfe0f72ddc80a020a421cbe987b54716e571e Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sat, 11 Mar 2023 23:17:21 +0000 Subject: Exit transition --- src/Game/Entities/Common.hs | 2 +- src/Game/Entities/Exit.hs | 19 ++++++++++++++----- src/Game/Entities/Types.hs | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) (limited to 'src/Game/Entities') diff --git a/src/Game/Entities/Common.hs b/src/Game/Entities/Common.hs index 2e447f5..69786e5 100644 --- a/src/Game/Entities/Common.hs +++ b/src/Game/Entities/Common.hs @@ -30,7 +30,7 @@ collision :: IORef Entity -> Int -> Collision collision playerRef otherHeight other = do player <- readIORef playerRef pure $ - player.dir /= Dying + player.typ == TypePlayer && player.x + 4 < other.x + 12 && other.x + 4 < player.x + 12 && player.y + otherHeight - 4 < other.y + otherHeight diff --git a/src/Game/Entities/Exit.hs b/src/Game/Entities/Exit.hs index 12f160d..b87c115 100644 --- a/src/Game/Entities/Exit.hs +++ b/src/Game/Entities/Exit.hs @@ -25,15 +25,24 @@ mkExit sprites x y playerCollision = do } updateExit :: Collision -> Entity -> IO Entity -updateExit touchedPlayer e = do - touched <- if e.frame < fullyOpen then pure False else touchedPlayer e - pure $ if touched then e {destroy = True, actions = [ActionExitLevel]} else update +updateExit touchedPlayer e + -- right is the set 0, left set 1 + | e.dir == DirRight = do + touched <- if e.frame < fullyOpen then pure False else touchedPlayer e + pure $ if touched then e {dir = DirLeft, frame = 0, actions = [ActionExitStarted]} else updateLoop + | otherwise = pure updateOnce where fullyOpen :: Int fullyOpen = 7 - update :: Entity - update + updateLoop :: Entity + updateLoop | e.delay > 0 = e {delay = e.delay - 1} | e.frame + 1 < frameLimit e = e {delay = frameDelay, frame = e.frame + 1} | otherwise = e {delay = frameDelay, frame = fullyOpen} + + updateOnce :: Entity + updateOnce + | e.delay > 0 = e {delay = e.delay - 1} + | e.frame + 1 < frameLimit e = e {delay = frameDelay, frame = e.frame + 1} + | otherwise = e {destroy = True, actions = [ActionExitDone]} diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs index 392a86f..4ece6c5 100644 --- a/src/Game/Entities/Types.hs +++ b/src/Game/Entities/Types.hs @@ -14,7 +14,7 @@ import qualified Game.Sprites as S data Dir = DirRight | DirLeft | Dying deriving (Eq) -data Type = TypePlayer | TypePickup | TypeEffect | TypeEnemy +data Type = TypePlayer | TypePickup | TypeEffect | TypeEnemy deriving (Eq) type Collision = Entity -> IO Bool @@ -29,7 +29,7 @@ data Entities = Entities -- | The effect name must match the sprite name in the spritesheet. type EffectName = String -data Action = ActionAddEffect Int Int EffectName | ActionAddBattery Int Int | ActionHitPlayer | ActionExitLevel +data Action = ActionAddEffect Int Int EffectName | ActionAddBattery Int Int | ActionHitPlayer | ActionExitStarted | ActionExitDone data Entity = Entity { typ :: Type, -- cgit v1.2.3