diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-03-21 21:28:25 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-03-21 21:28:25 +0000 |
commit | 11d2ed7f15a86dd465bc3456e3478e9ff03bf5d2 (patch) | |
tree | f5e6df771eb4396df13fe3cdc2d98721917a2d68 /src | |
parent | 1c8a22d24b14d258a9103f7b83d627359eae0da8 (diff) | |
download | space-plat-hs-11d2ed7f15a86dd465bc3456e3478e9ff03bf5d2.tar.gz space-plat-hs-11d2ed7f15a86dd465bc3456e3478e9ff03bf5d2.zip |
Introduce a set property
Stopped abusing the direction property!
Diffstat (limited to 'src')
-rw-r--r-- | src/Game/Entities.hs | 6 | ||||
-rw-r--r-- | src/Game/Entities/Common.hs | 4 | ||||
-rw-r--r-- | src/Game/Entities/Effect.hs | 1 | ||||
-rw-r--r-- | src/Game/Entities/Entry.hs | 6 | ||||
-rw-r--r-- | src/Game/Entities/Exit.hs | 8 | ||||
-rw-r--r-- | src/Game/Entities/Pickup.hs | 1 | ||||
-rw-r--r-- | src/Game/Entities/Player.hs | 12 | ||||
-rw-r--r-- | src/Game/Entities/Robot.hs | 5 | ||||
-rw-r--r-- | src/Game/Entities/Slime.hs | 1 | ||||
-rw-r--r-- | src/Game/Entities/Types.hs | 3 |
10 files changed, 30 insertions, 17 deletions
diff --git a/src/Game/Entities.hs b/src/Game/Entities.hs index b43ab0e..2f647d9 100644 --- a/src/Game/Entities.hs +++ b/src/Game/Entities.hs @@ -95,7 +95,7 @@ updateAll es state = do then ( s {GS.lives = 0, GS.gameOverDelay = gameOverDelay}, -- the player is not in the action, changing then type disables collision detection - (head ents) {typ = TypeEffect, dir = Dying, gravity = gravityUp, frame = 0} : tail ents + (head ents) {typ = TypeEffect, set = dyingSet, gravity = gravityUp, frame = 0} : tail ents ) else ( s {GS.lives = s.lives - 1, GS.hitDelay = hitDelay}, @@ -158,6 +158,4 @@ render renderer es state = do renderOne :: Entity -> IO () renderOne e = - S.render renderer e.sprite e.x e.y set e.frame - where - set = toSpriteSet e.dir + S.render renderer e.sprite e.x e.y e.set e.frame diff --git a/src/Game/Entities/Common.hs b/src/Game/Entities/Common.hs index 69786e5..82e790e 100644 --- a/src/Game/Entities/Common.hs +++ b/src/Game/Entities/Common.hs @@ -16,12 +16,10 @@ import qualified Game.Sprites as S toSpriteSet :: Dir -> Int toSpriteSet DirRight = 0 toSpriteSet DirLeft = 1 --- not really a direction -toSpriteSet Dying = 2 -- | Return the number of frames available on the entity's sprite for current direction. frameLimit :: Entity -> Int -frameLimit e = S.frameCount e.sprite (toSpriteSet e.dir) +frameLimit e = S.frameCount e.sprite e.set -- | Collision detection of player vs entity. -- diff --git a/src/Game/Entities/Effect.hs b/src/Game/Entities/Effect.hs index 7a2b341..f8b668a 100644 --- a/src/Game/Entities/Effect.hs +++ b/src/Game/Entities/Effect.hs @@ -15,6 +15,7 @@ mkEffect sprites x y name = do y = y, delay = frameDelay, frame = 0, + set = 0, jumping = False, gravity = gravityOff, dir = DirRight, diff --git a/src/Game/Entities/Entry.hs b/src/Game/Entities/Entry.hs index 5c17146..28b1c4e 100644 --- a/src/Game/Entities/Entry.hs +++ b/src/Game/Entities/Entry.hs @@ -5,6 +5,9 @@ import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S +openingDoorSet :: Int +openingDoorSet = 2 + mkEntry :: S.SpriteSheet -> Int -> Int -> IO Entity mkEntry sprites x y = do s <- S.get sprites "exit" @@ -15,9 +18,10 @@ mkEntry sprites x y = do y = y, delay = frameDelay, frame = 0, + set = openingDoorSet, jumping = False, gravity = gravityOff, - dir = Dying, + dir = DirRight, sprite = s, update = pure . updateEntry, destroy = False, diff --git a/src/Game/Entities/Exit.hs b/src/Game/Entities/Exit.hs index b87c115..206bfaa 100644 --- a/src/Game/Entities/Exit.hs +++ b/src/Game/Entities/Exit.hs @@ -5,6 +5,9 @@ import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S +closingDoorSet :: Int +closingDoorSet = 1 + mkExit :: S.SpriteSheet -> Int -> Int -> Collision -> IO Entity mkExit sprites x y playerCollision = do s <- S.get sprites "exit" @@ -15,6 +18,7 @@ mkExit sprites x y playerCollision = do y = y, delay = frameDelay, frame = 0, + set = 0, jumping = False, gravity = gravityOff, dir = DirRight, @@ -27,9 +31,9 @@ mkExit sprites x y playerCollision = do updateExit :: Collision -> Entity -> IO Entity updateExit touchedPlayer e -- right is the set 0, left set 1 - | e.dir == DirRight = do + | e.set /= closingDoorSet = do touched <- if e.frame < fullyOpen then pure False else touchedPlayer e - pure $ if touched then e {dir = DirLeft, frame = 0, actions = [ActionExitStarted]} else updateLoop + pure $ if touched then e {set = closingDoorSet, frame = 0, actions = [ActionExitStarted]} else updateLoop | otherwise = pure updateOnce where fullyOpen :: Int diff --git a/src/Game/Entities/Pickup.hs b/src/Game/Entities/Pickup.hs index a933efc..68d3852 100644 --- a/src/Game/Entities/Pickup.hs +++ b/src/Game/Entities/Pickup.hs @@ -15,6 +15,7 @@ mkBattery sprites x y playerCollision = do y = y, delay = frameDelay, frame = 0, + set = 0, jumping = False, gravity = gravityOff, dir = DirRight, diff --git a/src/Game/Entities/Player.hs b/src/Game/Entities/Player.hs index a69799b..18625b6 100644 --- a/src/Game/Entities/Player.hs +++ b/src/Game/Entities/Player.hs @@ -1,4 +1,4 @@ -module Game.Entities.Player (mkPlayer) where +module Game.Entities.Player (mkPlayer, dyingSet) where import Data.IORef import qualified Game.Controller as C @@ -7,6 +7,9 @@ import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S +dyingSet :: Int +dyingSet = 2 + mkPlayer :: S.SpriteSheet -> Int -> Int -> IORef C.Controls -> IsBlocked -> IO Entity mkPlayer sprites x y controls isBlocked = do s <- S.get sprites "player" @@ -17,6 +20,7 @@ mkPlayer sprites x y controls isBlocked = do y = y, delay = 0, frame = 0, + set = toSpriteSet DirRight, jumping = False, gravity = gravityOff, dir = DirRight, @@ -31,8 +35,8 @@ updateHorizontal isBlocked left right e -- prevent pressing both directions (keyboard) | left && right = e -- change direction first - | left && e.dir /= DirLeft = e {dir = DirLeft, delay = 0} - | right && e.dir /= DirRight = e {dir = DirRight, delay = 0} + | left && e.dir /= DirLeft = e {dir = DirLeft, set = toSpriteSet DirLeft, delay = 0} + | right && e.dir /= DirRight = e {dir = DirRight, set = toSpriteSet DirRight, delay = 0} | left && isGoingDown = if isBlocked (e.x - 1) (e.y + 23) then e else e {x = e.x - 1} | left && not isGoingDown = if isBlocked (e.x - 1) (e.y + 23) && isBlocked (e.x - 1) (e.y + 17) then e else e {x = e.x - 1} | right && isGoingDown = if isBlocked (e.x + 17) (e.y + 23) then e else e {x = e.x + 1} @@ -59,7 +63,7 @@ updateVertical isBlocked jump down e updatePlayer :: IORef C.Controls -> IsBlocked -> Entity -> IO Entity updatePlayer controls isBlocked e - | e.dir /= Dying = do + | e.set /= dyingSet = do ctl <- readIORef controls pure $ updateGravity isBlocked $ diff --git a/src/Game/Entities/Robot.hs b/src/Game/Entities/Robot.hs index dd3404a..44fc310 100644 --- a/src/Game/Entities/Robot.hs +++ b/src/Game/Entities/Robot.hs @@ -16,6 +16,7 @@ mkRobot sprites x y playerCollision isBlocked = do y = y, delay = frameDelay, frame = 0, + set = toSpriteSet DirRight, jumping = False, gravity = gravityOff, dir = DirRight, @@ -36,10 +37,10 @@ updateRobot touchedPlayer isBlocked e = do | 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 = DirRight, set = toSpriteSet 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 = DirLeft, set = toSpriteSet 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 1f50927..c102712 100644 --- a/src/Game/Entities/Slime.hs +++ b/src/Game/Entities/Slime.hs @@ -16,6 +16,7 @@ mkSlime sprites x y playerCollision isBlocked = do y = y, delay = frameDelay, frame = 0, + set = 0, jumping = False, gravity = gravityOff, dir = DirRight, diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs index e8b29b0..34bddd3 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 | Dying deriving (Eq) +data Dir = DirRight | DirLeft deriving (Eq) data Type = TypePlayer | TypePickup | TypeEffect | TypeEnemy deriving (Eq) @@ -43,6 +43,7 @@ data Entity = Entity y :: Int, delay :: Int, frame :: Int, + set :: Int, jumping :: Bool, gravity :: Int, dir :: Dir, |