aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-04-25 22:18:54 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-04-25 22:18:54 +0100
commit1cd60a951b2c2f83b927a6b6961cbd3206bf5316 (patch)
tree59861ba4125ed15ff8711ccd55e4b5a24fd3235c
parenta5ac165b1e9adbdf459ebc9df42edc024389e9b8 (diff)
downloadspace-plat-hs-1cd60a951b2c2f83b927a6b6961cbd3206bf5316.tar.gz
space-plat-hs-1cd60a951b2c2f83b927a6b6961cbd3206bf5316.zip
Added "tracker" enemy
-rw-r--r--data/map1.json8
-rw-r--r--data/sprites.json14
-rw-r--r--data/sprites.pngbin23326 -> 24220 bytes
-rw-r--r--game.cabal1
-rw-r--r--src/Game/Entities.hs2
-rw-r--r--src/Game/Entities/Common.hs13
-rw-r--r--src/Game/Entities/Tracker.hs26
-rw-r--r--src/Game/Map.hs5
8 files changed, 65 insertions, 4 deletions
diff --git a/data/map1.json b/data/map1.json
index ee95e6c..d68295c 100644
--- a/data/map1.json
+++ b/data/map1.json
@@ -52,8 +52,8 @@
"type":"",
"visible":true,
"width":16,
- "x":176,
- "y":88
+ "x":368,
+ "y":112
},
{
"height":16,
@@ -256,12 +256,12 @@
{
"height":24,
"id":38,
- "name":"Runner",
+ "name":"Tracker-l",
"rotation":0,
"type":"",
"visible":true,
"width":16,
- "x":136,
+ "x":496,
"y":136
}],
"opacity":1,
diff --git a/data/sprites.json b/data/sprites.json
index fac70b5..eaa86d1 100644
--- a/data/sprites.json
+++ b/data/sprites.json
@@ -134,6 +134,20 @@
[5, 4, 5, 3]
]
},
+ "tracker": {
+ "frames": [
+ { "x": 0, "y": 240, "width": 16, "height": 24 },
+ { "x": 16, "y": 240, "width": 16, "height": 24 },
+ { "x": 32, "y": 240, "width": 16, "height": 24 },
+ { "x": 48, "y": 240, "width": 16, "height": 24 },
+ { "x": 64, "y": 240, "width": 16, "height": 24 },
+ { "x": 80, "y": 240, "width": 16, "height": 24 }
+ ],
+ "sets": [
+ [0, 1, 0, 2],
+ [5, 4, 5, 3]
+ ]
+ },
"exit": {
"frames": [
{ "x": 0, "y": 160, "width": 16, "height": 24 },
diff --git a/data/sprites.png b/data/sprites.png
index 59ad50a..70445db 100644
--- a/data/sprites.png
+++ b/data/sprites.png
Binary files differ
diff --git a/game.cabal b/game.cabal
index f5b92dd..9b358a0 100644
--- a/game.cabal
+++ b/game.cabal
@@ -35,6 +35,7 @@ library
Game.Entities.Shooter
Game.Entities.Blast
Game.Entities.Runner
+ Game.Entities.Tracker
Game.Controller
Game.Utils
Game.Toaster
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index a0c596d..3cb2510 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -28,6 +28,7 @@ import Game.Entities.Robot
import Game.Entities.Runner
import Game.Entities.Shooter
import Game.Entities.Slime
+import Game.Entities.Tracker
import Game.Entities.Types
import qualified Game.Map as M
import qualified Game.Sprites as S
@@ -51,6 +52,7 @@ mkEntities sprites m controls = do
toEntity playerRef (M.RobotEntity x y d) = mkRobot sprites x y d (collision playerRef 24) (M.isBlocked m)
toEntity playerRef (M.ShooterEntity x y d) = mkShooter sprites x y d (collision playerRef 24) (inLine playerRef 24) (M.isBlocked m) (collision playerRef 8)
toEntity playerRef (M.RunnerEntity x y d) = mkRunner sprites x y d (collision playerRef 24) (M.isBlocked m)
+ toEntity playerRef (M.TrackerEntity x y d) = mkTracker sprites x y d (collision playerRef 24) (facingLower playerRef) (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/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
diff --git a/src/Game/Map.hs b/src/Game/Map.hs
index 6c01061..2a85dbe 100644
--- a/src/Game/Map.hs
+++ b/src/Game/Map.hs
@@ -45,6 +45,7 @@ data Object
| RobotEntity Int Int Dir
| ShooterEntity Int Int Dir
| RunnerEntity Int Int Dir
+ | TrackerEntity Int Int Dir
deriving (Show, Eq, Ord)
data JsonMapData = JsonMapData
@@ -92,6 +93,8 @@ instance JSON Object where
ShooterEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight
Just "Runner" ->
RunnerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight
+ Just "Tracker" ->
+ TrackerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirRight
Just "Slime-l" ->
SlimeEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft
Just "Robot-l" ->
@@ -100,6 +103,8 @@ instance JSON Object where
ShooterEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft
Just "Runner-l" ->
RunnerEntity <$> valFromObj "x" obj <*> valFromObj "y" obj <*> pure DirLeft
+ Just "Tracker-l" ->
+ TrackerEntity <$> 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