diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-02-21 12:40:31 +0000 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-02-21 12:40:31 +0000 |
commit | 198498b3c604d55d3b54fef34c62f215ba8a2525 (patch) | |
tree | f3d7725657608cafdfc4888313f3631c14857032 /src/Game/Entities/Robot.hs | |
parent | fd74ead510f4ca326e36450fa23dee0d681c296b (diff) | |
download | space-plat-hs-198498b3c604d55d3b54fef34c62f215ba8a2525.tar.gz space-plat-hs-198498b3c604d55d3b54fef34c62f215ba8a2525.zip |
New Robot enemy.
Diffstat (limited to 'src/Game/Entities/Robot.hs')
-rw-r--r-- | src/Game/Entities/Robot.hs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Game/Entities/Robot.hs b/src/Game/Entities/Robot.hs new file mode 100644 index 0000000..64c9d48 --- /dev/null +++ b/src/Game/Entities/Robot.hs @@ -0,0 +1,46 @@ +module Game.Entities.Robot (mkRobot) where + +import Data.Bits (Bits (..)) +import Data.IORef +import Game.Entities.Common +import Game.Entities.Const +import Game.Entities.Types +import qualified Game.Sprites as S + +mkRobot :: S.SpriteSheet -> Int -> Int -> IORef Entity -> IsBlocked -> IO () -> IO Entity +mkRobot sprites x y playerRef isBlocked hitPlayer' = do + s <- S.get sprites "robot" + pure + Entity + { typ = TypeEnemy, + x = x, + y = y, + delay = frameDelay, + frame = 0, + jumping = False, + gravity = gravityOff, + dir = DirRight, + sprite = s, + update = updateRobot (collision playerRef 24) isBlocked hitPlayer', + destroy = False, + spawns = [] + } + +updateRobot :: Collision -> IsBlocked -> IO () -> Entity -> IO Entity +updateRobot touchedPlayer isBlocked hitPlayer' e = do + touched <- touchedPlayer e + let updated = updateFrame True e + if touched then fmap (const e) hitPlayer' else pure $ 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} + | 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 == DirRight = ent {x = ent.x + 1} + | otherwise = ent |