about summary refs log tree commit diff
path: root/src/Xanthous/Game/Lenses.hs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-10-16T02·54-0400
committerGriffin Smith <root@gws.fyi>2019-10-16T02·54-0400
commit4882350f5d7e54a6ae5c8760f2510273dae19c60 (patch)
treeb8b61ec39abf51d2599be2dead82504f67f6fbe0 /src/Xanthous/Game/Lenses.hs
parent8a4220df830adb6f1616ca02dd06902474fd25df (diff)
Don't walk gormlaks into walls
Because of the way lines are drawn, a specific configuration of
positioning for gormlaks would have them decide they desperately wanted
to walk *inside* a wall, which they would then both fail to do but also
always collide with whenever they tried to go anywhere else.
Diffstat (limited to 'src/Xanthous/Game/Lenses.hs')
-rw-r--r--src/Xanthous/Game/Lenses.hs27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/Xanthous/Game/Lenses.hs b/src/Xanthous/Game/Lenses.hs
index f49477a2db..77314a9aea 100644
--- a/src/Xanthous/Game/Lenses.hs
+++ b/src/Xanthous/Game/Lenses.hs
@@ -10,6 +10,7 @@ module Xanthous.Game.Lenses
 
     -- * Collisions
   , Collision(..)
+  , entityCollision
   , collisionAt
   ) where
 --------------------------------------------------------------------------------
@@ -93,13 +94,21 @@ data Collision
   deriving stock (Show, Eq, Ord, Generic)
   deriving anyclass (NFData)
 
+entityCollision
+  :: ( MonoFoldable (f SomeEntity)
+    , Foldable f
+    , Element (f SomeEntity) ~ SomeEntity
+    , AsEmpty (f SomeEntity)
+    )
+  => f SomeEntity
+  -> Maybe Collision
+entityCollision Empty = Nothing
+entityCollision ents
+  | any (entityIs @Creature) ents = pure Combat
+  | all (entityIs @Item) ents = Nothing
+  | doors@(_ : _) <- ents ^.. folded . _SomeEntity @Door
+  , all (view open) doors = Nothing
+  | otherwise = pure Stop
+
 collisionAt :: MonadState GameState m => Position -> m (Maybe Collision)
-collisionAt pos = do
-  ents <- use $ entities . EntityMap.atPosition pos
-  pure $
-    if | null ents -> Nothing
-       | any (entityIs @Creature) ents -> pure Combat
-       | all (entityIs @Item) ents -> Nothing
-       | doors@(_ : _) <- ents ^.. folded . _SomeEntity @Door
-       , all (view open) doors -> Nothing
-       | otherwise -> pure Stop
+collisionAt pos = uses (entities . EntityMap.atPosition pos) entityCollision