aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Entities/Pickup.hs
blob: 8d8beb887f8c17fe66e43062bee367203c25a167 (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
module Game.Entities.Pickup (mkBattery) where

import Data.IORef
import Game.Entities.Common
import Game.Entities.Const
import Game.Entities.Types
import qualified Game.Sprites as S

mkBattery :: S.SpriteSheet -> Int -> Int -> IORef Entity -> IO () -> IO Entity
mkBattery sprites x y playerRef collectedBattery' = do
  s <- S.get sprites "battery"
  pure
    Entity
      { typ = TypePickup,
        x = x,
        y = y,
        delay = frameDelay,
        frame = 0,
        jumping = False,
        gravity = gravityOff,
        dir = DirRight,
        sprite = s,
        update = updateBattery (collision playerRef 16) collectedBattery',
        destroy = False,
        spawns = []
      }

updateBattery :: Collision -> IO () -> Entity -> IO Entity
updateBattery touchedPlayer collectedBattery' e = do
  touched <- touchedPlayer e
  if touched then e {destroy = True} <$ collectedBattery' else pure updateBatteryFrame
  where
    updateBatteryFrame :: Entity
    updateBatteryFrame
      | 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 = 0}