diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-02-21 12:40:31 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-02-21 12:40:31 +0000 |
commit | 198498b3c604d55d3b54fef34c62f215ba8a2525 (patch) | |
tree | f3d7725657608cafdfc4888313f3631c14857032 | |
parent | fd74ead510f4ca326e36450fa23dee0d681c296b (diff) | |
download | space-plat-hs-198498b3c604d55d3b54fef34c62f215ba8a2525.tar.gz space-plat-hs-198498b3c604d55d3b54fef34c62f215ba8a2525.zip |
New Robot enemy.
-rw-r--r-- | data/map.json | 43 | ||||
-rw-r--r-- | data/sprites.json | 14 | ||||
-rw-r--r-- | data/sprites.png | bin | 10894 -> 13384 bytes | |||
-rw-r--r-- | data/tiles.png | bin | 7277 -> 7275 bytes | |||
-rw-r--r-- | game.cabal | 1 | ||||
-rw-r--r-- | src/Game/Entities.hs | 2 | ||||
-rw-r--r-- | src/Game/Entities/Common.hs | 6 | ||||
-rw-r--r-- | src/Game/Entities/Pickup.hs | 2 | ||||
-rw-r--r-- | src/Game/Entities/Robot.hs | 46 | ||||
-rw-r--r-- | src/Game/Entities/Slime.hs | 2 | ||||
-rw-r--r-- | src/Game/Map.hs | 3 |
11 files changed, 109 insertions, 10 deletions
diff --git a/data/map.json b/data/map.json index 4cc427d..015b82c 100644 --- a/data/map.json +++ b/data/map.json @@ -74,7 +74,7 @@ "type":"", "visible":true, "width":16, - "x":112, + "x":240, "y":96 }, { @@ -89,15 +89,48 @@ "y":144 }, { - "height":16, + "height":24, "id":16, - "name":"Slime", + "name":"Robot", "rotation":0, "type":"", "visible":true, "width":16, "x":192, - "y":24 + "y":16 + }, + { + "height":24, + "id":24, + "name":"Robot", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":232, + "y":64 + }, + { + "height":24, + "id":26, + "name":"Robot", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":144, + "y":88 + }, + { + "height":24, + "id":25, + "name":"Robot", + "rotation":0, + "type":"", + "visible":true, + "width":16, + "x":32, + "y":40 }, { "height":16, @@ -194,7 +227,7 @@ "y":0 }], "nextlayerid":6, - "nextobjectid":23, + "nextobjectid":27, "orientation":"orthogonal", "renderorder":"right-down", "tiledversion":"1.7.2", diff --git a/data/sprites.json b/data/sprites.json index 3ce716b..844a796 100644 --- a/data/sprites.json +++ b/data/sprites.json @@ -54,5 +54,19 @@ [0, 1], [0, 1] ] + }, + "robot": { + "frames": [ + { "x": 0, "y": 72, "width": 16, "height": 24 }, + { "x": 16, "y": 72, "width": 16, "height": 24 }, + { "x": 32, "y": 72, "width": 16, "height": 24 }, + { "x": 48, "y": 72, "width": 16, "height": 24 }, + { "x": 64, "y": 72, "width": 16, "height": 24 }, + { "x": 80, "y": 72, "width": 16, "height": 24 } + ], + "sets": [ + [0, 1, 0, 2], + [5, 4, 5, 3] + ] } } diff --git a/data/sprites.png b/data/sprites.png Binary files differindex 646ff14..ee25de5 100644 --- a/data/sprites.png +++ b/data/sprites.png diff --git a/data/tiles.png b/data/tiles.png Binary files differindex d145e6c..451df8d 100644 --- a/data/tiles.png +++ b/data/tiles.png @@ -28,6 +28,7 @@ library Game.Entities.Effect Game.Entities.Pickup Game.Entities.Slime + Game.Entities.Robot Game.Controller Game.Utils build-depends: diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index 1977678..291f10e 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -11,6 +11,7 @@ import Game.Entities.Const import Game.Entities.Effect import Game.Entities.Pickup import Game.Entities.Player +import Game.Entities.Robot import Game.Entities.Slime import Game.Entities.Types import qualified Game.Map as M @@ -30,6 +31,7 @@ mkEntities sprites m controls stateRef = do 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 _ (M.PlayerEntity _ _) = error "Player already processed" diff --git a/src/Game/Entities/Common.hs b/src/Game/Entities/Common.hs index f72c33a..2a0c6f1 100644 --- a/src/Game/Entities/Common.hs +++ b/src/Game/Entities/Common.hs @@ -28,13 +28,13 @@ frameLimit e = S.frameCount e.sprite (toSpriteSet e.dir) -- | Collision detection of player vs entity. -- -- The player's head won't register, this is necessary to avoid hitting things on a platform above when jumping. -collision :: IORef Entity -> Collision -collision playerRef other = do +collision :: IORef Entity -> Int -> Collision +collision playerRef otherHeight other = do player <- readIORef playerRef pure $ player.x + 4 < other.x + 12 && other.x + 4 < player.x + 12 - && player.y + 12 < other.y + 16 + && player.y + otherHeight - 4 < other.y + otherHeight && other.y + 4 < player.y + 24 -- | Update game state to reflect that the player was hit by an enemy. diff --git a/src/Game/Entities/Pickup.hs b/src/Game/Entities/Pickup.hs index d0f80f6..8d8beb8 100644 --- a/src/Game/Entities/Pickup.hs +++ b/src/Game/Entities/Pickup.hs @@ -20,7 +20,7 @@ mkBattery sprites x y playerRef collectedBattery' = do gravity = gravityOff, dir = DirRight, sprite = s, - update = updateBattery (collision playerRef) collectedBattery', + update = updateBattery (collision playerRef 16) collectedBattery', destroy = False, spawns = [] } diff --git a/src/Game/Entities/Robot.hs b/src/Game/Entities/Robot.hs new file mode 100644 index 0000000..64c9d48 --- /dev/null +++ b/src/Game/Entities/Robot.hs @@ -0,0 +1,46 @@ +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 + s <- S.get sprites "robot" + pure + Entity + { typ = TypeEnemy, + x = x, + y = y, + delay = frameDelay, + frame = 0, + jumping = False, + gravity = gravityOff, + dir = DirRight, + sprite = s, + update = updateRobot (collision playerRef 24) isBlocked hitPlayer', + destroy = False, + spawns = [] + } + +updateRobot :: Collision -> IsBlocked -> IO () -> Entity -> IO Entity +updateRobot touchedPlayer isBlocked hitPlayer' e = do + touched <- touchedPlayer e + let updated = updateFrame True e + if touched then fmap (const e) hitPlayer' else pure $ updateMovement updated + where + updateMovement :: Entity -> Entity + updateMovement ent + | testBit ent.delay 1 = ent + | ent.dir == DirLeft + && (isBlocked (ent.x - 1) (ent.y + 17) || isBlocked (ent.x - 1) (ent.y + 17) || not (isBlocked (ent.x - 1) (ent.y + 24))) = + ent {dir = DirRight} + | ent.dir == DirLeft = ent {x = ent.x - 1} + | ent.dir == DirRight + && (isBlocked (ent.x + 16) (ent.y + 17) || isBlocked (ent.x + 16) (ent.y + 17) || not (isBlocked (ent.x + 16) (ent.y + 24))) = + ent {dir = DirLeft} + | ent.dir == DirRight = ent {x = ent.x + 1} + | otherwise = ent diff --git a/src/Game/Entities/Slime.hs b/src/Game/Entities/Slime.hs index 91e756f..d87f735 100644 --- a/src/Game/Entities/Slime.hs +++ b/src/Game/Entities/Slime.hs @@ -21,7 +21,7 @@ mkSlime sprites x y playerRef isBlocked hitPlayer' = do gravity = gravityOff, dir = DirRight, sprite = s, - update = updateSlime (collision playerRef) isBlocked hitPlayer', + update = updateSlime (collision playerRef 16) isBlocked hitPlayer', destroy = False, spawns = [] } diff --git a/src/Game/Map.hs b/src/Game/Map.hs index 95e5618..47dc31c 100644 --- a/src/Game/Map.hs +++ b/src/Game/Map.hs @@ -37,6 +37,7 @@ data Object = PlayerEntity Int Int | BatteryEntity Int Int | SlimeEntity Int Int + | RobotEntity Int Int deriving (Show, Eq, Ord) data JsonMapData = JsonMapData @@ -78,6 +79,8 @@ instance JSON Object where BatteryEntity <$> valFromObj "x" obj <*> valFromObj "y" obj Just "Slime" -> SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj + Just "Robot" -> + RobotEntity <$> valFromObj "x" obj <*> valFromObj "y" obj Just (JSString (JSONString s)) -> Error $ "unsupported entity " ++ show s e -> Error $ "unsupported entity in " ++ show e readJSON _ = mzero |