From 1c8a22d24b14d258a9103f7b83d627359eae0da8 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 21 Mar 2023 21:02:45 +0000 Subject: Player transition into the stage --- data/sprites.json | 3 ++- game.cabal | 1 + src/Game/Entities.hs | 6 +++++- src/Game/Entities/Entry.hs | 31 +++++++++++++++++++++++++++++++ src/Game/Entities/Types.hs | 8 +++++++- src/Game/State.hs | 6 +++--- 6 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 src/Game/Entities/Entry.hs diff --git a/data/sprites.json b/data/sprites.json index 182ef35..4e76eab 100644 --- a/data/sprites.json +++ b/data/sprites.json @@ -106,7 +106,8 @@ "sets": [ [0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 10, 10, 10, 10, 10, 10], - [11, 11, 5, 4, 3, 2, 1, 0] + [11, 11, 5, 4, 3, 2, 1, 0], + [0, 1, 2, 3, 4, 5, 11, 11] ] } } diff --git a/game.cabal b/game.cabal index f3955c4..c54bf5c 100644 --- a/game.cabal +++ b/game.cabal @@ -26,6 +26,7 @@ library Game.Entities.Const Game.Entities.Common Game.Entities.Player + Game.Entities.Entry Game.Entities.Exit Game.Entities.Effect Game.Entities.Pickup diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index 8a939f4..b43ab0e 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -19,6 +19,7 @@ import qualified Game.Controller as C import Game.Entities.Common import Game.Entities.Const import Game.Entities.Effect +import Game.Entities.Entry import Game.Entities.Exit import Game.Entities.Pickup import Game.Entities.Player @@ -37,8 +38,10 @@ mkEntities sprites m controls = do _ -> error "No player entity in map" playerRef <- newIORef player entities <- traverse (toEntity playerRef) $ sort $ filter (not . M.isPlayer) (M.objects m) + -- the effect of the player "warping" into the level + entryEffect <- mkEntry sprites player.x player.y -- the entities list has always player first - pure $ Entities sprites playerRef (player : entities) + pure $ Entities sprites playerRef (player : (entryEffect : entities)) where toEntity :: IORef Entity -> M.Object -> IO Entity toEntity playerRef (M.SlimeEntity x y) = mkSlime sprites x y (collision playerRef 16) (M.isBlocked m) @@ -105,6 +108,7 @@ updateAll es state = do -- the player is not in the action, changing the type disables collision detection ((head ents) {typ = TypeEffect} : tail ents) t + ActionEntryDone -> processActions s {GS.levelCompleted = GS.ExitOff} ents t ActionExitDone -> processActions s {GS.levelCompleted = GS.ExitDone} ents t processActions s ents [] = pure (s, ents) diff --git a/src/Game/Entities/Entry.hs b/src/Game/Entities/Entry.hs new file mode 100644 index 0000000..5c17146 --- /dev/null +++ b/src/Game/Entities/Entry.hs @@ -0,0 +1,31 @@ +module Game.Entities.Entry (mkEntry) where + +import Game.Entities.Common +import Game.Entities.Const +import Game.Entities.Types +import qualified Game.Sprites as S + +mkEntry :: S.SpriteSheet -> Int -> Int -> IO Entity +mkEntry sprites x y = do + s <- S.get sprites "exit" + pure + Entity + { typ = TypePickup, + x = x, + y = y, + delay = frameDelay, + frame = 0, + jumping = False, + gravity = gravityOff, + dir = Dying, + sprite = s, + update = pure . updateEntry, + destroy = False, + actions = [] + } + +updateEntry :: Entity -> Entity +updateEntry e + | 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 = [ActionEntryDone]} diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs index 4ece6c5..e8b29b0 100644 --- a/src/Game/Entities/Types.hs +++ b/src/Game/Entities/Types.hs @@ -29,7 +29,13 @@ 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 | ActionExitStarted | ActionExitDone +data Action + = ActionAddEffect Int Int EffectName + | ActionAddBattery Int Int + | ActionHitPlayer + | ActionExitStarted + | ActionEntryDone + | ActionExitDone data Entity = Entity { typ :: Type, diff --git a/src/Game/State.hs b/src/Game/State.hs index 4f46c01..083e6d4 100644 --- a/src/Game/State.hs +++ b/src/Game/State.hs @@ -6,7 +6,7 @@ import qualified Game.Map as M maxLives :: Int maxLives = 4 -data ExitState = ExitOff | ExitStarted | ExitDone deriving (Eq) +data ExitState = ExitOff | ExitEntry | ExitStarted | ExitDone deriving (Eq) data State = State { batteries :: Int, @@ -33,7 +33,7 @@ initialState m = exit = False, -- doesn't matter where lastBattery = (0, 0), - levelCompleted = ExitOff, + levelCompleted = ExitEntry, currentLevel = 0 } @@ -47,5 +47,5 @@ levelState s m = exit = False, -- doesn't matter where lastBattery = (0, 0), - levelCompleted = ExitOff + levelCompleted = ExitEntry } -- cgit v1.2.3