aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-03-05 11:42:56 +0000
committerJuan J. Martinez <jjm@usebox.net>2023-03-09 12:21:05 +0000
commitbeecc1e2af08f81f5cb5d2ebb93aaef59b20fcd5 (patch)
treef0d1947e0369fdf59a1f626f953c778884873cc0
parentee50719de81145b5859d957da722cf183e0c8c60 (diff)
downloadspace-plat-hs-beecc1e2af08f81f5cb5d2ebb93aaef59b20fcd5.tar.gz
space-plat-hs-beecc1e2af08f81f5cb5d2ebb93aaef59b20fcd5.zip
Exit stage WIP
-rw-r--r--data/map.json66
-rw-r--r--data/sprites.json19
-rw-r--r--data/sprites.pngbin16332 -> 17866 bytes
-rw-r--r--game.cabal1
-rw-r--r--src/Game.hs17
-rw-r--r--src/Game/Entities.hs20
-rw-r--r--src/Game/Entities/Exit.hs31
-rw-r--r--src/Game/State.hs3
8 files changed, 84 insertions, 73 deletions
diff --git a/data/map.json b/data/map.json
index 117732f..0973e6a 100644
--- a/data/map.json
+++ b/data/map.json
@@ -57,17 +57,6 @@
},
{
"height":16,
- "id":3,
- "name":"Battery",
- "rotation":0,
- "type":"",
- "visible":true,
- "width":16,
- "x":160,
- "y":120
- },
- {
- "height":16,
"id":15,
"name":"Slime",
"rotation":0,
@@ -222,61 +211,6 @@
},
{
"height":16,
- "id":5,
- "name":"Battery",
- "rotation":0,
- "type":"",
- "visible":true,
- "width":16,
- "x":176,
- "y":120
- },
- {
- "height":16,
- "id":6,
- "name":"Battery",
- "rotation":0,
- "type":"",
- "visible":true,
- "width":16,
- "x":608,
- "y":120
- },
- {
- "height":16,
- "id":7,
- "name":"Battery",
- "rotation":0,
- "type":"",
- "visible":true,
- "width":16,
- "x":288,
- "y":144
- },
- {
- "height":16,
- "id":8,
- "name":"Battery",
- "rotation":0,
- "type":"",
- "visible":true,
- "width":16,
- "x":320,
- "y":72
- },
- {
- "height":16,
- "id":9,
- "name":"Battery",
- "rotation":0,
- "type":"",
- "visible":true,
- "width":16,
- "x":592,
- "y":24
- },
- {
- "height":16,
"id":10,
"name":"Battery",
"rotation":0,
diff --git a/data/sprites.json b/data/sprites.json
index 9132500..2ee7663 100644
--- a/data/sprites.json
+++ b/data/sprites.json
@@ -87,5 +87,24 @@
[0, 1, 0, 2],
[5, 4, 5, 3]
]
+ },
+ "exit": {
+ "frames": [
+ { "x": 0, "y": 160, "width": 16, "height": 24 },
+ { "x": 16, "y": 160, "width": 16, "height": 24 },
+ { "x": 32, "y": 160, "width": 16, "height": 24 },
+ { "x": 48, "y": 160, "width": 16, "height": 24 },
+ { "x": 64, "y": 160, "width": 16, "height": 24 },
+ { "x": 80, "y": 160, "width": 16, "height": 24 },
+ { "x": 0, "y": 184, "width": 16, "height": 24 },
+ { "x": 16, "y": 184, "width": 16, "height": 24 },
+ { "x": 32, "y": 184, "width": 16, "height": 24 },
+ { "x": 48, "y": 184, "width": 16, "height": 24 },
+ { "x": 64, "y": 184, "width": 16, "height": 24 }
+ ],
+ "sets": [
+ [0, 1, 2, 3, 4, 5, 5,
+ 6, 7, 8, 9, 10, 10, 10, 10, 10, 10, 10]
+ ]
}
}
diff --git a/data/sprites.png b/data/sprites.png
index 2de2c9c..b048f4c 100644
--- a/data/sprites.png
+++ b/data/sprites.png
Binary files differ
diff --git a/game.cabal b/game.cabal
index 9852415..f3955c4 100644
--- a/game.cabal
+++ b/game.cabal
@@ -26,6 +26,7 @@ library
Game.Entities.Const
Game.Entities.Common
Game.Entities.Player
+ Game.Entities.Exit
Game.Entities.Effect
Game.Entities.Pickup
Game.Entities.Slime
diff --git a/src/Game.hs b/src/Game.hs
index 52ba6a7..850d6ed 100644
--- a/src/Game.hs
+++ b/src/Game.hs
@@ -62,7 +62,8 @@ initialState m =
lives = maxLives,
totalLives = maxLives,
hitDelay = hitDelay,
- gameOverDelay = 0
+ gameOverDelay = 0,
+ exit = False
}
main :: IO ()
@@ -155,7 +156,10 @@ gameLoop e = do
SDL.rendererRenderTarget renderer $= Just canvas
SDL.clear renderer
- updatedEnv <- if state.gameOverDelay /= 1 then playLoop (updateState env) else gameOverLoop env
+ updatedEnv <-
+ if state.gameOverDelay /= 1
+ then playLoop =<< updateState env
+ else gameOverLoop env
SDL.rendererRenderTarget renderer $= Nothing
SDL.clear renderer
@@ -166,10 +170,13 @@ gameLoop e = do
gameLoop updatedEnv
where
-- update state counters
- updateState :: Env -> Env
+ updateState :: Env -> IO Env
updateState env
- | state.gameOverDelay > 1 = env {state = state {GS.gameOverDelay = state.gameOverDelay - 1}}
- | otherwise = env
+ | state.gameOverDelay > 1 = pure env {state = state {GS.gameOverDelay = state.gameOverDelay - 1}}
+ | state.batteries == state.totalBatteries && not state.exit = do
+ es <- E.addExit env.entities
+ pure env {entities = es, state = state {GS.exit = True}}
+ | otherwise = pure env
where
state = env.state
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs
index 2810fcf..2cfdd76 100644
--- a/src/Game/Entities.hs
+++ b/src/Game/Entities.hs
@@ -1,4 +1,14 @@
-module Game.Entities (Entities, Entity, mkEntities, updateAll, render, renderVisible, playerPosition) where
+module Game.Entities
+ ( Entities,
+ Entity,
+ mkEntities,
+ addExit,
+ updateAll,
+ render,
+ renderVisible,
+ playerPosition,
+ )
+where
import Control.Monad
import Data.Bits (Bits (..))
@@ -9,6 +19,7 @@ import qualified Game.Controller as C
import Game.Entities.Common
import Game.Entities.Const
import Game.Entities.Effect
+import Game.Entities.Exit
import Game.Entities.Pickup
import Game.Entities.Player
import Game.Entities.Robot
@@ -42,6 +53,13 @@ playerPosition (Entities _ _ entities) =
where
player = head entities
+addExit :: Entities -> IO Entities
+addExit es = do
+ exit <- mkExit es.sprites x y (collision es.player 24)
+ pure es {entities = es.entities ++ [exit]}
+ where
+ (x, y) = playerPosition es
+
updateAll :: Entities -> GS.State -> IO (Entities, GS.State)
updateAll es state = do
-- update the player first (including the reference)
diff --git a/src/Game/Entities/Exit.hs b/src/Game/Entities/Exit.hs
new file mode 100644
index 0000000..075e1f4
--- /dev/null
+++ b/src/Game/Entities/Exit.hs
@@ -0,0 +1,31 @@
+module Game.Entities.Exit (mkExit) where
+
+import Game.Entities.Common
+import Game.Entities.Const
+import Game.Entities.Types
+import qualified Game.Sprites as S
+
+mkExit :: S.SpriteSheet -> Int -> Int -> Collision -> IO Entity
+mkExit sprites x y playerCollision = do
+ s <- S.get sprites "exit"
+ pure
+ Entity
+ { typ = TypePickup,
+ x = x,
+ y = y,
+ delay = frameDelay,
+ frame = 0,
+ jumping = False,
+ gravity = gravityOff,
+ dir = DirRight,
+ sprite = s,
+ update = pure . updateExit playerCollision,
+ destroy = False,
+ actions = []
+ }
+
+updateExit :: Collision -> Entity -> Entity
+updateExit _ e
+ | e.delay > 0 = e {delay = e.delay - 1}
+ | e.frame + 1 < frameLimit e = e {delay = frameDelay, frame = e.frame + 1}
+ | otherwise = e {delay = frameDelay, frame = 7}
diff --git a/src/Game/State.hs b/src/Game/State.hs
index f8b5721..8987a2e 100644
--- a/src/Game/State.hs
+++ b/src/Game/State.hs
@@ -6,5 +6,6 @@ data State = State
lives :: Int,
totalLives :: Int,
hitDelay :: Int,
- gameOverDelay :: Int
+ gameOverDelay :: Int,
+ exit :: Bool
}