aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Entities/Exit.hs
blob: f5e01a85ef55e5915e299fa0b3ec6c6847ef8a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Game.Entities.Exit (mkExit) where

import Game.Entities.Common
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"
  pure
    Entity
      { typ = TypePickup,
        x = x,
        y = y,
        delay = frameDelay,
        frame = 0,
        set = 0,
        jumping = False,
        gravity = gravityOff,
        dir = DirRight,
        sprite = s,
        update = updateExit playerCollision,
        destroy = False,
        actions = [],
        dat = NoData
      }

updateExit :: Collision -> Entity -> IO Entity
updateExit touchedPlayer e
  -- right is the set 0, left set 1
  | e.set /= closingDoorSet = do
      touched <- if e.frame < fullyOpen then pure False else touchedPlayer e
      pure $ if touched then e {set = closingDoorSet, frame = 0, actions = [ActionExitStarted]} else updateLoop
  | otherwise = pure updateOnce
  where
    fullyOpen :: Int
    fullyOpen = 7

    updateLoop :: Entity
    updateLoop
      | 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 = fullyOpen}

    updateOnce :: Entity
    updateOnce
      | e.delay > 0 = e {delay = e.delay - 1}
      | e.frame + 1 < frameLimit e = e {delay = frameDelay, frame = e.frame + 1}
      | otherwise = e {destroy = True, actions = [ActionExitDone]}