From 15b4f0e6a73987f9afbc46f46862b5120029e715 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sat, 16 May 2020 18:57:07 -0400 Subject: Stop auto-moving if there's an enemy nearby If at any point during an auto-move there's an enemy in the character's line of sight, cancel the autocommand and send a message --- src/Xanthous/App/Autocommands.hs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src/Xanthous/App/Autocommands.hs') diff --git a/src/Xanthous/App/Autocommands.hs b/src/Xanthous/App/Autocommands.hs index e8d94ce741fd..35b92bba7289 100644 --- a/src/Xanthous/App/Autocommands.hs +++ b/src/Xanthous/App/Autocommands.hs @@ -4,16 +4,24 @@ module Xanthous.App.Autocommands , autoStep ) where -------------------------------------------------------------------------------- -import Xanthous.Prelude +import Xanthous.Prelude -------------------------------------------------------------------------------- -import Control.Concurrent (threadDelay) +import Control.Concurrent (threadDelay) +import qualified Data.Aeson as A +import Data.Aeson (object) +import Data.List.NonEmpty (nonEmpty) +import qualified Data.List.NonEmpty as NE +import Control.Monad.State (gets) -------------------------------------------------------------------------------- -import Xanthous.App.Common -import Xanthous.App.Time -import Xanthous.Data -import Xanthous.Data.App -import Xanthous.Entities.Character (speed) -import Xanthous.Game.State +import Xanthous.App.Common +import Xanthous.App.Time +import Xanthous.Data +import Xanthous.Data.App +import Xanthous.Entities.Character (speed) +import Xanthous.Entities.Creature (Creature, creatureType) +import Xanthous.Entities.RawTypes (hostile) +import Xanthous.Game.State +import Xanthous.Game.Lenses (characterVisibleEntities) -------------------------------------------------------------------------------- autoStep :: Autocommand -> AppM () @@ -24,7 +32,20 @@ autoStep (AutoMove dir) = do characterPosition .= newPos stepGameBy =<< uses (character . speed) (|*| 1) describeEntitiesAt newPos + maybeVisibleEnemies <- nonEmpty <$> enemiesInSight + for_ maybeVisibleEnemies $ \visibleEnemies -> do + say ["autoMove", "enemyInSight"] + $ object [ "firstEntity" A..= NE.head visibleEnemies ] + cancelAutocommand Just _ -> cancelAutocommand + where + enemiesInSight :: AppM [Creature] + enemiesInSight = do + ents <- gets characterVisibleEntities + pure $ ents + ^.. folded + . _SomeEntity @Creature + . filtered (view $ creatureType . hostile) -------------------------------------------------------------------------------- -- cgit 1.4.1