From 449dc21f1b48648c5e4abf1a6cace21176fa2226 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 26 Mar 2023 08:49:35 +0100 Subject: Support entities facing left Perhaps it would be better to support "custom properties" in tiled, but this is OK for now. --- data/map1.json | 4 ++-- src/Game/Entities.hs | 4 ++-- src/Game/Entities/Robot.hs | 8 ++++---- src/Game/Entities/Slime.hs | 6 +++--- src/Game/Entities/Types.hs | 2 +- src/Game/Map.hs | 13 +++++++++---- 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/data/map1.json b/data/map1.json index 0973e6a..c1f6030 100644 --- a/data/map1.json +++ b/data/map1.json @@ -168,7 +168,7 @@ { "height":24, "id":26, - "name":"Robot", + "name":"Robot-l", "rotation":0, "type":"", "visible":true, @@ -201,7 +201,7 @@ { "height":16, "id":20, - "name":"Slime", + "name":"Slime-l", "rotation":0, "type":"", "visible":true, diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index 2f647d9..6506a60 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -44,8 +44,8 @@ mkEntities sprites m controls = do 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) - toEntity playerRef (M.RobotEntity x y) = mkRobot sprites x y (collision playerRef 24) (M.isBlocked m) + toEntity playerRef (M.SlimeEntity x y d) = mkSlime sprites x y d (collision playerRef 16) (M.isBlocked m) + toEntity playerRef (M.RobotEntity x y d) = mkRobot sprites x y d (collision playerRef 24) (M.isBlocked m) toEntity playerRef (M.BatteryEntity x y) = mkBattery sprites x y (collision playerRef 16) toEntity _ (M.PlayerEntity _ _) = error "Player already processed" diff --git a/src/Game/Entities/Robot.hs b/src/Game/Entities/Robot.hs index 44fc310..664079a 100644 --- a/src/Game/Entities/Robot.hs +++ b/src/Game/Entities/Robot.hs @@ -6,8 +6,8 @@ import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S -mkRobot :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO Entity -mkRobot sprites x y playerCollision isBlocked = do +mkRobot :: S.SpriteSheet -> Int -> Int -> Dir -> Collision -> IsBlocked -> IO Entity +mkRobot sprites x y d playerCollision isBlocked = do s <- S.get sprites "robot" pure Entity @@ -16,10 +16,10 @@ mkRobot sprites x y playerCollision isBlocked = do y = y, delay = frameDelay, frame = 0, - set = toSpriteSet DirRight, + set = toSpriteSet d, jumping = False, gravity = gravityOff, - dir = DirRight, + dir = d, sprite = s, update = updateRobot playerCollision isBlocked, destroy = False, diff --git a/src/Game/Entities/Slime.hs b/src/Game/Entities/Slime.hs index c102712..7607fb7 100644 --- a/src/Game/Entities/Slime.hs +++ b/src/Game/Entities/Slime.hs @@ -6,8 +6,8 @@ import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S -mkSlime :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO Entity -mkSlime sprites x y playerCollision isBlocked = do +mkSlime :: S.SpriteSheet -> Int -> Int -> Dir -> Collision -> IsBlocked -> IO Entity +mkSlime sprites x y d playerCollision isBlocked = do s <- S.get sprites "slime" pure Entity @@ -19,7 +19,7 @@ mkSlime sprites x y playerCollision isBlocked = do set = 0, jumping = False, gravity = gravityOff, - dir = DirRight, + dir = d, sprite = s, update = updateSlime playerCollision isBlocked, destroy = False, diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs index 34bddd3..05b419e 100644 --- a/src/Game/Entities/Types.hs +++ b/src/Game/Entities/Types.hs @@ -12,7 +12,7 @@ where import Data.IORef import qualified Game.Sprites as S -data Dir = DirRight | DirLeft deriving (Eq) +data Dir = DirRight | DirLeft deriving (Eq, Show, Ord) data Type = TypePlayer | TypePickup | TypeEffect | TypeEnemy deriving (Eq) diff --git a/src/Game/Map.hs b/src/Game/Map.hs index 3fb72f9..ea20b17 100644 --- a/src/Game/Map.hs +++ b/src/Game/Map.hs @@ -14,6 +14,7 @@ where import Control.Monad import Data.Maybe (fromMaybe, mapMaybe) +import Game.Entities.Types (Dir (..)) import qualified Game.Utils as U import SDL (($=)) import qualified SDL @@ -39,8 +40,8 @@ data Layer data Object = PlayerEntity Int Int | BatteryEntity Int Int - | SlimeEntity Int Int - | RobotEntity Int Int + | SlimeEntity Int Int Dir + | RobotEntity Int Int Dir deriving (Show, Eq, Ord) data JsonMapData = JsonMapData @@ -81,9 +82,13 @@ instance JSON Object where Just "Battery" -> BatteryEntity <$> valFromObj "x" obj <*> valFromObj "y" obj Just "Slime" -> - SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj + SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight Just "Robot" -> - RobotEntity <$> valFromObj "x" obj <*> valFromObj "y" obj + RobotEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight + Just "Slime-l" -> + SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft + Just "Robot-l" -> + RobotEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft Just (JSString (JSONString s)) -> Error $ "unsupported entity " ++ show s e -> Error $ "unsupported entity in " ++ show e readJSON _ = mzero -- cgit v1.2.3