module Game.Entities.Robot (mkRobot) where import Data.Bits (Bits (..)) import Game.Entities.Common import Game.Entities.Const import Game.Entities.Types import qualified Game.Sprites as S mkRobot :: S.SpriteSheet -> Int -> Int -> Collision -> IsBlocked -> IO Entity mkRobot sprites x y playerCollision isBlocked = do s <- S.get sprites "robot" pure Entity { typ = TypeEnemy, x = x, y = y, delay = frameDelay, frame = 0, set = toSpriteSet DirRight, jumping = False, gravity = gravityOff, dir = DirRight, sprite = s, update = updateRobot playerCollision isBlocked, destroy = False, actions = [] } updateRobot :: Collision -> IsBlocked -> Entity -> IO Entity updateRobot touchedPlayer isBlocked e = do touched <- touchedPlayer e let updated = updateFrame True e pure $ if touched then updated {actions = [ActionHitPlayer]} else updateMovement updated 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