aboutsummaryrefslogtreecommitdiff
path: root/src/Game/Entities
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-04-19 23:16:22 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-04-19 23:19:37 +0100
commit9ffa3f57067598acfbe5196a4452b225ab295b83 (patch)
treed893d9eba5a51f5a831e287bf1724e1f93a48026 /src/Game/Entities
parent7a1d4eb4481fa54753673e67532039da2b9c0b2f (diff)
downloadspace-plat-hs-9ffa3f57067598acfbe5196a4452b225ab295b83.tar.gz
space-plat-hs-9ffa3f57067598acfbe5196a4452b225ab295b83.zip
Added "runner" enemy
Diffstat (limited to 'src/Game/Entities')
-rw-r--r--src/Game/Entities/Common.hs6
-rw-r--r--src/Game/Entities/Runner.hs86
-rw-r--r--src/Game/Entities/Types.hs3
3 files changed, 94 insertions, 1 deletions
diff --git a/src/Game/Entities/Common.hs b/src/Game/Entities/Common.hs
index 661fe07..eb84400 100644
--- a/src/Game/Entities/Common.hs
+++ b/src/Game/Entities/Common.hs
@@ -5,6 +5,7 @@ module Game.Entities.Common
inLine,
updateFrame,
updateGravity,
+ turn,
)
where
@@ -13,6 +14,11 @@ import Game.Entities.Const
import Game.Entities.Types
import qualified Game.Sprites as S
+-- | Return the opposite direction.
+turn :: Dir -> Dir
+turn DirRight = DirLeft
+turn DirLeft = DirRight
+
-- | Convert direction into a sprite set.
toSpriteSet :: Dir -> Int
toSpriteSet DirRight = 0
diff --git a/src/Game/Entities/Runner.hs b/src/Game/Entities/Runner.hs
new file mode 100644
index 0000000..7d969a5
--- /dev/null
+++ b/src/Game/Entities/Runner.hs
@@ -0,0 +1,86 @@
+module Game.Entities.Runner (mkRunner) where
+
+import Data.Bits (Bits (..))
+import Game.Entities.Common
+import Game.Entities.Const
+import Game.Entities.Types
+import qualified Game.Sprites as S
+import System.Random (randomRIO)
+
+mkRunner :: S.SpriteSheet -> Int -> Int -> Dir -> Collision -> IsBlocked -> IO Entity
+mkRunner sprites x y d playerCollision isBlocked = do
+ s <- S.get sprites "runner"
+ pure
+ Entity
+ { typ = TypeEnemy,
+ x = x,
+ y = y,
+ delay = frameDelay,
+ frame = 0,
+ set = toSpriteSet d,
+ jumping = False,
+ gravity = gravityOff,
+ dir = d,
+ sprite = s,
+ update = updateRunner playerCollision isBlocked,
+ destroy = False,
+ actions = [],
+ dat = RunnerData 0
+ }
+
+updateRunner :: Collision -> IsBlocked -> Entity -> IO Entity
+updateRunner touchedPlayer isBlocked e = do
+ touched <- touchedPlayer e
+ if touched
+ then pure e {actions = [ActionHitPlayer]}
+ else do
+ let gravityUpdated = updateGravity isBlocked e
+ if gravityUpdated.gravity /= gravityOff then pure gravityUpdated else updateMovement $ updateFrame True gravityUpdated
+ where
+ updateMovement :: Entity -> IO Entity
+ updateMovement ent
+ | testBit ent.delay 1 = pure ent
+ | ent.dir == DirLeft
+ && (isBlocked (ent.x - 1) (ent.y + 17) || isBlocked (ent.x - 1) (ent.y + 17)) =
+ tryJumpingOrTurn ent
+ | ent.dir == DirLeft = pure ent {x = ent.x - 1}
+ | ent.dir == DirRight
+ && (isBlocked (ent.x + 16) (ent.y + 17) || isBlocked (ent.x + 16) (ent.y + 17)) =
+ tryJumpingOrTurn ent
+ | ent.dir == DirRight = pure ent {x = ent.x + 1}
+ | otherwise = pure ent
+
+ randomWallCount :: [Int]
+ randomWallCount = [0, 0, 0, 2, 2, 3]
+
+ tryJumpingOrTurn :: Entity -> IO Entity
+ tryJumpingOrTurn ent
+ | ent.dat.wallCount > 2 && not (isBlocked (ent.x + 8) (ent.y + 24 + 8)) = do
+ r <- randomRIO (0, length randomWallCount - 1) :: IO Int
+ pure
+ ent
+ { dir = turn ent.dir,
+ set = toSpriteSet $ turn ent.dir,
+ y = e.y + 1,
+ gravity = gravityDown,
+ frame = jumpFrame,
+ dat = RunnerData $ randomWallCount !! r
+ }
+ | ent.dat.wallCount > 1 && not (isBlocked (ent.x + 8) (ent.y - 1)) && isBlocked (ent.x + 8) ent.y = do
+ r <- randomRIO (0, length randomWallCount - 1) :: IO Int
+ pure
+ ent
+ { dir = turn ent.dir,
+ set = toSpriteSet $ turn ent.dir,
+ gravity = gravityUp,
+ frame = jumpFrame,
+ actions = [ActionAddEffect ent.x (ent.y + 8) "dust"],
+ dat = RunnerData $ randomWallCount !! r
+ }
+ | otherwise =
+ pure
+ ent
+ { dir = turn ent.dir,
+ set = toSpriteSet $ turn ent.dir,
+ dat = RunnerData (ent.dat.wallCount + 1)
+ }
diff --git a/src/Game/Entities/Types.hs b/src/Game/Entities/Types.hs
index 05726a8..71a1718 100644
--- a/src/Game/Entities/Types.hs
+++ b/src/Game/Entities/Types.hs
@@ -17,7 +17,7 @@ data Dir = DirRight | DirLeft deriving (Eq, Show, Ord)
data Type = TypePlayer | TypePickup | TypeEffect | TypeEnemy deriving (Eq)
-data EntityData = NoData | ShooterData {coolDown :: Int}
+data EntityData = NoData | ShooterData {coolDown :: Int} | RunnerData {wallCount :: Int}
type Collision = Entity -> IO Bool
@@ -48,6 +48,7 @@ data Entity = Entity
delay :: Int,
frame :: Int,
set :: Int,
+ -- FIXME: this should be player data
jumping :: Bool,
gravity :: Int,
dir :: Dir,