From e066422b950a06873efb6a884b47155c867b7acf Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Fri, 24 Feb 2023 22:51:42 +0000 Subject: Cleaned passing the collision detection function to the entities --- src/Game/Entities.hs | 6 +++--- src/Game/Entities/Pickup.hs | 7 +++---- src/Game/Entities/Robot.hs | 7 +++---- src/Game/Entities/Slime.hs | 7 +++---- 4 files changed, 12 insertions(+), 15 deletions(-) (limited to 'src/Game') diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index 291f10e..d265ff5 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -30,9 +30,9 @@ mkEntities sprites m controls stateRef = do pure $ Entities sprites playerRef stateRef (player : entities) where toEntity :: IORef Entity -> M.Object -> IO Entity - toEntity playerRef (M.SlimeEntity x y) = mkSlime sprites x y playerRef (M.isBlocked m) (hitPlayer stateRef) - toEntity playerRef (M.RobotEntity x y) = mkRobot sprites x y playerRef (M.isBlocked m) (hitPlayer stateRef) - toEntity playerRef (M.BatteryEntity x y) = mkBattery sprites x y playerRef (collectedBattery stateRef) + toEntity playerRef (M.SlimeEntity x y) = mkSlime sprites x y (collision playerRef 16) (M.isBlocked m) (hitPlayer stateRef) + toEntity playerRef (M.RobotEntity x y) = mkRobot sprites x y (collision playerRef 24) (M.isBlocked m) (hitPlayer stateRef) + toEntity playerRef (M.BatteryEntity x y) = mkBattery sprites x y (collision playerRef 16) (collectedBattery stateRef) toEntity _ (M.PlayerEntity _ _) = error "Player already processed" processSpawn :: S.SpriteSheet -> Spawn -> IO Entity diff --git a/src/Game/Entities/Pickup.hs b/src/Game/Entities/Pickup.hs index 8d8beb8..4aa7326 100644 --- a/src/Game/Entities/Pickup.hs +++ b/src/Game/Entities/Pickup.hs @@ -1,13 +1,12 @@ module Game.Entities.Pickup (mkBattery) where -import Data.IORef import Game.Entities.Common import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S -mkBattery :: S.SpriteSheet -> Int -> Int -> IORef Entity -> IO () -> IO Entity -mkBattery sprites x y playerRef collectedBattery' = do +mkBattery :: S.SpriteSheet -> Int -> Int -> Collision -> IO () -> IO Entity +mkBattery sprites x y playerCollision collectedBattery' = do s <- S.get sprites "battery" pure Entity @@ -20,7 +19,7 @@ mkBattery sprites x y playerRef collectedBattery' = do gravity = gravityOff, dir = DirRight, sprite = s, - update = updateBattery (collision playerRef 16) collectedBattery', + update = updateBattery playerCollision collectedBattery', destroy = False, spawns = [] } diff --git a/src/Game/Entities/Robot.hs b/src/Game/Entities/Robot.hs index 64c9d48..39b63aa 100644 --- a/src/Game/Entities/Robot.hs +++ b/src/Game/Entities/Robot.hs @@ -1,14 +1,13 @@ module Game.Entities.Robot (mkRobot) where import Data.Bits (Bits (..)) -import Data.IORef import Game.Entities.Common import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S -mkRobot :: S.SpriteSheet -> Int -> Int -> IORef Entity -> IsBlocked -> IO () -> IO Entity -mkRobot sprites x y playerRef isBlocked hitPlayer' = do +mkRobot :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO () -> IO Entity +mkRobot sprites x y playerCollision isBlocked hitPlayer' = do s <- S.get sprites "robot" pure Entity @@ -21,7 +20,7 @@ mkRobot sprites x y playerRef isBlocked hitPlayer' = do gravity = gravityOff, dir = DirRight, sprite = s, - update = updateRobot (collision playerRef 24) isBlocked hitPlayer', + update = updateRobot playerCollision isBlocked hitPlayer', destroy = False, spawns = [] } diff --git a/src/Game/Entities/Slime.hs b/src/Game/Entities/Slime.hs index d87f735..5ca6fe7 100644 --- a/src/Game/Entities/Slime.hs +++ b/src/Game/Entities/Slime.hs @@ -1,14 +1,13 @@ module Game.Entities.Slime (mkSlime) where import Data.Bits (Bits (..)) -import Data.IORef import Game.Entities.Common import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S -mkSlime :: S.SpriteSheet -> Int -> Int -> IORef Entity -> IsBlocked -> IO () -> IO Entity -mkSlime sprites x y playerRef isBlocked hitPlayer' = do +mkSlime :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO () -> IO Entity +mkSlime sprites x y playerCollision isBlocked hitPlayer' = do s <- S.get sprites "slime" pure Entity @@ -21,7 +20,7 @@ mkSlime sprites x y playerRef isBlocked hitPlayer' = do gravity = gravityOff, dir = DirRight, sprite = s, - update = updateSlime (collision playerRef 16) isBlocked hitPlayer', + update = updateSlime playerCollision isBlocked hitPlayer', destroy = False, spawns = [] } -- cgit v1.2.3