about summary refs log tree commit diff
path: root/src/Xanthous
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2020-01-25T16·18-0500
committerGriffin Smith <root@gws.fyi>2020-01-25T16·38-0500
commit9256c976edec462af26f33317df6171045e68aa5 (patch)
treecedf0e97a8a6a40f0f083e164ad88daad57dc3e7 /src/Xanthous
parent2fc4fcfee95ad34a9272414c4fd214b10007539f (diff)
Factor out an "entitiesAtCharacter" lens
Factor an "entitiesAtCharacter" lens from the one-two step of getting
the character position, then getting the entities at that position.
Diffstat (limited to 'src/Xanthous')
-rw-r--r--src/Xanthous/App.hs11
-rw-r--r--src/Xanthous/Game.hs1
-rw-r--r--src/Xanthous/Game/Lenses.hs9
3 files changed, 13 insertions, 8 deletions
diff --git a/src/Xanthous/App.hs b/src/Xanthous/App.hs
index 202f38e8685b..1c2fbf86f3b8 100644
--- a/src/Xanthous/App.hs
+++ b/src/Xanthous/App.hs
@@ -167,8 +167,7 @@ handleCommand Drop = do
   selectItemFromInventory_ ["drop", "menu"] Cancellable id
     (say_ ["drop", "nothing"])
     $ \(MenuResult item) -> do
-      charPos <- use characterPosition
-      entities . EntityMap.atPosition charPos %= (SomeEntity item <|)
+      entitiesAtCharacter %= (SomeEntity item <|)
       say ["drop", "dropped"] $ object [ "item" A..= item ]
   continue
 
@@ -277,9 +276,7 @@ handleCommand Save = do
         exitSuccess
 
 handleCommand GoUp = do
-  charPos <- use characterPosition
-  hasStairs <- uses (entities . EntityMap.atPosition charPos)
-              $ elem (SomeEntity UpStaircase)
+  hasStairs <- uses entitiesAtCharacter $ elem (SomeEntity UpStaircase)
   if hasStairs
   then uses levels prevLevel >>= \case
     Just levs' -> levels .= levs'
@@ -291,9 +288,7 @@ handleCommand GoUp = do
   continue
 
 handleCommand GoDown = do
-  charPos <- use characterPosition
-  hasStairs <- uses (entities . EntityMap.atPosition charPos)
-              $ elem (SomeEntity DownStaircase)
+  hasStairs <- uses entitiesAtCharacter $ elem (SomeEntity DownStaircase)
 
   if hasStairs
   then do
diff --git a/src/Xanthous/Game.hs b/src/Xanthous/Game.hs
index a8d096f02fc0..4ca668891971 100644
--- a/src/Xanthous/Game.hs
+++ b/src/Xanthous/Game.hs
@@ -16,6 +16,7 @@ module Xanthous.Game
   , characterPosition
   , updateCharacterVision
   , characterVisiblePositions
+  , entitiesAtCharacter
 
     -- * Messages
   , MessageHistory(..)
diff --git a/src/Xanthous/Game/Lenses.hs b/src/Xanthous/Game/Lenses.hs
index 8f6053a5ecc6..dc886f65c698 100644
--- a/src/Xanthous/Game/Lenses.hs
+++ b/src/Xanthous/Game/Lenses.hs
@@ -10,6 +10,7 @@ module Xanthous.Game.Lenses
   , characterVisiblePositions
   , getInitialState
   , initialStateFromSeed
+  , entitiesAtCharacter
 
     -- * Collisions
   , Collision(..)
@@ -28,6 +29,7 @@ import           Xanthous.Data
 import           Xanthous.Data.Levels
 import qualified Xanthous.Data.EntityMap as EntityMap
 import           Xanthous.Data.EntityMap.Graphics (visiblePositions)
+import           Xanthous.Data.VectorBag
 import           Xanthous.Entities.Character (Character, mkCharacter)
 import           {-# SOURCE #-} Xanthous.Entities.Entities ()
 --------------------------------------------------------------------------------
@@ -113,3 +115,10 @@ entitiesCollision = join . maximumMay . fmap entityCollision
 
 collisionAt :: MonadState GameState m => Position -> m (Maybe Collision)
 collisionAt p = uses (entities . EntityMap.atPosition p) entitiesCollision
+
+entitiesAtCharacter :: Lens' GameState (VectorBag SomeEntity)
+entitiesAtCharacter = lens getter setter
+  where
+    getter gs = gs ^. entities . EntityMap.atPosition (gs ^. characterPosition)
+    setter gs ents = gs
+      & entities . EntityMap.atPosition (gs ^. characterPosition) .~ ents