From 1cd60a951b2c2f83b927a6b6961cbd3206bf5316 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Tue, 25 Apr 2023 22:18:54 +0100 Subject: Added "tracker" enemy --- src/Game/Entities/Common.hs | 13 +++++++++++++ src/Game/Entities/Tracker.hs | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/Game/Entities/Tracker.hs (limited to 'src/Game/Entities') diff --git a/src/Game/Entities/Common.hs b/src/Game/Entities/Common.hs index eb84400..8fa7fe3 100644 --- a/src/Game/Entities/Common.hs +++ b/src/Game/Entities/Common.hs @@ -3,6 +3,7 @@ module Game.Entities.Common frameLimit, collision, inLine, + facingLower, updateFrame, updateGravity, turn, @@ -54,6 +55,18 @@ inLine playerRef otherHeight other = do || (other.dir == DirRight && player.x > other.x) ) +-- | Check if the entity is facing the player and it is in a lower plarform. +facingLower :: IORef Entity -> Collision +facingLower playerRef other = do + player <- readIORef playerRef + pure $ + player.typ == TypePlayer + && player.gravity == gravityOff + && player.y < other.y + && ( (other.dir == DirLeft && player.x < other.x) + || (other.dir == DirRight && player.x > other.x) + ) + -- | Update frame animation for entities that have direction. updateFrame :: Bool -> Entity -> Entity updateFrame updated e diff --git a/src/Game/Entities/Tracker.hs b/src/Game/Entities/Tracker.hs new file mode 100644 index 0000000..ef7325f --- /dev/null +++ b/src/Game/Entities/Tracker.hs @@ -0,0 +1,26 @@ +module Game.Entities.Tracker (mkTracker) where + +import Game.Entities.Const +import Game.Entities.Runner +import Game.Entities.Types +import qualified Game.Sprites as S + +mkTracker :: S.SpriteSheet -> Int -> Int -> Dir -> Collision -> Collision -> IsBlocked -> IO Entity +mkTracker sprites x y d playerCollision facingPlayerLower isBlocked = do + s <- S.get sprites "tracker" + runner <- mkRunner sprites x y d playerCollision isBlocked + pure runner {sprite = s, update = updateTracker runner.update facingPlayerLower isBlocked} + +updateTracker :: (Entity -> IO Entity) -> Collision -> IsBlocked -> Entity -> IO Entity +updateTracker updateRunner facingPlayerLower isBlocked ent = do + e <- updateRunner ent + facing <- facingPlayerLower e + pure $ + if facing && e.gravity == gravityOff && not (isBlocked (ent.x + 8) (ent.y - 1)) && isBlocked (ent.x + 8) ent.y + then + e + { gravity = gravityUp, + frame = jumpFrame, + actions = [ActionAddEffect ent.x (ent.y + 8) "dust"] + } + else e -- cgit v1.2.3