about summary refs log tree commit diff
path: root/src/Xanthous/App.hs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2020-01-05T17·55-0500
committerGriffin Smith <root@gws.fyi>2020-01-05T17·55-0500
commit0f79a06733c30ddca4bc0746ddc99e1626775fff (patch)
tree7f9bffb1740a531ac11c3130020bb86723bde5a8 /src/Xanthous/App.hs
parent6b0bab0e85266ce66836c4584f8cc83b307a3af5 (diff)
Add staircases, and moving between levels
Currently we just pick randomly between the cave and dungeon level
generators. There's a lot of bugs here, but it's *sorta* working, so I'm
leaving it as is.
Diffstat (limited to 'src/Xanthous/App.hs')
-rw-r--r--src/Xanthous/App.hs60
1 files changed, 55 insertions, 5 deletions
diff --git a/src/Xanthous/App.hs b/src/Xanthous/App.hs
index 1db75bb58565..2fd821af1cf7 100644
--- a/src/Xanthous/App.hs
+++ b/src/Xanthous/App.hs
@@ -14,6 +14,7 @@ import           Control.Monad.Random (MonadRandom)
 import           Control.Monad.State.Class (modify)
 import           Data.Aeson (object, ToJSON)
 import qualified Data.Aeson as A
+import           Data.List.NonEmpty (NonEmpty(..))
 import qualified Data.Vector as V
 import           System.Exit
 import           System.Directory (doesFileExist)
@@ -30,6 +31,8 @@ import           Xanthous.Data
                  )
 import           Xanthous.Data.EntityMap (EntityMap)
 import qualified Xanthous.Data.EntityMap as EntityMap
+import           Xanthous.Data.Levels (prevLevel, nextLevel)
+import qualified Xanthous.Data.Levels as Levels
 import           Xanthous.Game
 import           Xanthous.Game.State
 import           Xanthous.Game.Draw (drawGame)
@@ -37,6 +40,7 @@ import           Xanthous.Game.Prompt
 import           Xanthous.Monad
 import           Xanthous.Resource (Name, Panel(..))
 import qualified Xanthous.Messages as Messages
+import           Xanthous.Random
 import           Xanthous.Util (removeVectorIndex)
 import           Xanthous.Util.Inflection (toSentence)
 --------------------------------------------------------------------------------
@@ -47,13 +51,14 @@ import qualified Xanthous.Entities.Item as Item
 import           Xanthous.Entities.Creature (Creature)
 import qualified Xanthous.Entities.Creature as Creature
 import           Xanthous.Entities.Environment
-                 (Door, open, locked, GroundMessage(..))
+                 (Door, open, locked, GroundMessage(..), Staircase(..))
 import           Xanthous.Entities.RawTypes
                  ( edible, eatMessage, hitpointsHealed
                  , attackMessage
                  )
 import           Xanthous.Generators
 import qualified Xanthous.Generators.CaveAutomata as CaveAutomata
+import qualified Xanthous.Generators.Dungeon as Dungeon
 --------------------------------------------------------------------------------
 
 type App = Brick.App GameState () Name
@@ -87,10 +92,7 @@ startEvent = do
 
 initLevel :: AppM ()
 initLevel = do
-  level <-
-    generateLevel SCaveAutomata CaveAutomata.defaultParams
-    $ Dimensions 80 80
-
+  level <- genLevel 0
   entities <>= levelToEntityMap level
   characterPosition .= level ^. levelCharacterPosition
 
@@ -273,6 +275,40 @@ handleCommand Save = do
         writeFile (unpack filename) $ toStrict src
         exitSuccess
 
+handleCommand GoUp = do
+  charPos <- use characterPosition
+  hasStairs <- uses (entities . EntityMap.atPosition charPos)
+              $ elem (SomeEntity UpStaircase)
+  if hasStairs
+  then uses levels prevLevel >>= \case
+    Just levs' -> levels .= levs'
+    Nothing ->
+      -- TODO in nethack, this leaves the game. Maybe something similar here?
+      say_ ["cant", "goUp"]
+  else say_ ["cant", "goUp"]
+
+  continue
+
+handleCommand GoDown = do
+  charPos <- use characterPosition
+  hasStairs <- uses (entities . EntityMap.atPosition charPos)
+              $ elem (SomeEntity DownStaircase)
+
+  if hasStairs
+  then do
+    levs <- use levels
+    let newLevelNum = Levels.pos levs + 1
+    levs' <- nextLevel (levelToEntityMap <$> genLevel newLevelNum) levs
+    cEID <- use characterEntityID
+    pCharacter <- use $ entities . at cEID
+    entities . at cEID .= Nothing
+    levels .= levs'
+    entities . at cEID .= pCharacter
+  else say_ ["cant", "goDown"]
+
+  continue
+
+--
 
 handleCommand ToggleRevealAll = do
   val <- debugState . allRevealed <%= not
@@ -551,3 +587,17 @@ showPanel panel = do
   prompt_ @'Continue ["generic", "continue"] Uncancellable
     . const
     $ activePanel .= Nothing
+
+--------------------------------------------------------------------------------
+
+genLevel
+  :: Int -- ^ level number
+  -> AppM Level
+genLevel _num = do
+  let dims = Dimensions 80 80
+  generator <- choose $ CaveAutomata :| [Dungeon]
+  level <- case generator of
+    CaveAutomata -> generateLevel SCaveAutomata CaveAutomata.defaultParams dims
+    Dungeon -> generateLevel SDungeon Dungeon.defaultParams dims
+  characterPosition .= level ^. levelCharacterPosition
+  pure $!! level