about summary refs log tree commit diff
path: root/src/Xanthous/Generators/LevelContents.hs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-09-19T17·56-0400
committerGriffin Smith <root@gws.fyi>2019-09-19T17·56-0400
commit62a2e05ef222dd69263b819a400a83f8910816f9 (patch)
treeb81ee35bcc1f6f20290e6347e5b6ceff8a9fff12 /src/Xanthous/Generators/LevelContents.hs
parent15895c69fe8f1415f45fe33f7b3d564f4239496e (diff)
Add items and inventory
Add a new "Item" entity, which pulls from the previously-existent
ItemType raw, and add a "PickUp" command which takes the (currently
*only*) item off the ground and puts it into the inventory.
Diffstat (limited to 'src/Xanthous/Generators/LevelContents.hs')
-rw-r--r--src/Xanthous/Generators/LevelContents.hs40
1 files changed, 32 insertions, 8 deletions
diff --git a/src/Xanthous/Generators/LevelContents.hs b/src/Xanthous/Generators/LevelContents.hs
index f8d9b8a2045a..9192674ba7a9 100644
--- a/src/Xanthous/Generators/LevelContents.hs
+++ b/src/Xanthous/Generators/LevelContents.hs
@@ -1,21 +1,45 @@
 --------------------------------------------------------------------------------
 module Xanthous.Generators.LevelContents
   ( chooseCharacterPosition
+  , randomItems
   ) where
 --------------------------------------------------------------------------------
-import Xanthous.Prelude
+import           Xanthous.Prelude
 --------------------------------------------------------------------------------
-import Control.Monad.Random
-import Data.Array.IArray (amap)
+import           Control.Monad.Random
+import           Data.Array.IArray (amap, bounds, rangeSize)
 --------------------------------------------------------------------------------
-import Xanthous.Generators.Util
-import Xanthous.Random
+import           Xanthous.Generators.Util
+import           Xanthous.Random
+import           Xanthous.Data (Position, positionFromPair)
+import           Xanthous.Data.EntityMap (EntityMap, _EntityMap)
+import           Xanthous.Entities.Item (Item(..))
+import           Xanthous.Entities.Raws
+import           Xanthous.Entities.RawTypes
+import qualified Xanthous.Entities.Item as Item
 --------------------------------------------------------------------------------
 
-chooseCharacterPosition :: MonadRandom m => Cells -> m (Word, Word)
-chooseCharacterPosition cells = choose $ impureNonNull candidates
+chooseCharacterPosition :: MonadRandom m => Cells -> m Position
+chooseCharacterPosition = randomPosition
+
+randomItems :: MonadRandom m => Cells -> m (EntityMap Item)
+randomItems cells = do
+  let len = rangeSize $ bounds cells
+  (numItems :: Int) <- floor . (* fromIntegral len)
+                     <$> getRandomR @_ @Float (0.0004, 0.001)
+  items <- for [0..numItems] $ const do
+    pos <- randomPosition cells
+    itemType <- fmap (fromMaybe (error "no item raws!"))
+               . choose . ChooseElement
+               $ rawsWithType @ItemType
+    let item = Item.newWithType itemType
+    pure (pos, item)
+  pure $ _EntityMap # items
+
+randomPosition :: MonadRandom m => Cells -> m Position
+randomPosition cells = fmap positionFromPair . choose $ impureNonNull candidates
   where
-    -- cells ends up with true = wall, we want true = can put a character here
+    -- cells ends up with true = wall, we want true = can put an item here
     placeableCells = amap not cells
 
     -- find the largest contiguous region of cells in the cave.