aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Entities/Robot.hs
blob: 94e721d0b7796a7d8ff4bc8c561ec016d8f616dc (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
module Game.Entities.Robot (mkRobot) where

import Data.Bits (Bits (..))
import Game.Entities.Common
import Game.Entities.Const
import Game.Entities.Types
import Game.Sprites qualified as S

mkRobot :: S.SpriteSheet -> Int -> Int -> Dir -> Collision -> IsBlocked -> IO Entity
mkRobot sprites x y d playerCollision isBlocked = do
  s <- S.get sprites "robot"
  pure
    Entity
      { typ = TypeEnemy,
        x = x,
        y = y,
        delay = frameDelay,
        frame = 0,
        set = toSpriteSet d,
        jumping = False,
        gravity = gravityOff,
        dir = d,
        sprite = s,
        update = updateRobot playerCollision isBlocked,
        destroy = False,
        actions = [],
        dat = NoData
      }

updateRobot :: Collision -> IsBlocked -> Entity -> IO Entity
updateRobot touchedPlayer isBlocked e = do
  touched <- touchedPlayer e
  pure $ if touched then e {actions = [ActionHitPlayer]} else updateMovement $ updateFrame True e
  where
    updateMovement :: Entity -> Entity
    updateMovement ent
      | 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, 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, set = toSpriteSet DirLeft}
      | ent.dir == DirRight = ent {x = ent.x + 1}
      | otherwise = ent