Skip to content

Commit

Permalink
Add items to open spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hendrix committed Jun 20, 2015
1 parent c887d17 commit a7b1a3f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
43 changes: 38 additions & 5 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ type alias Game =

type Item = Item

type alias Contents =
{ player : Maybe Player
, items : List Item
}

type Cell
= Open { player : Maybe Player
}
= Open Contents
| Barrier {}

type alias Dir =
Expand All @@ -39,8 +43,12 @@ randomizeLocationsWithin size numLocations =
in
generate locationGenerator (initialSeed (round Now.loadTime)) |> fst

defaultCell : Cell
defaultCell =
Open {player = Nothing, items = []}

defaultGameMap : Int -> GameMap
defaultGameMap size = Matrix.initialize size (\_ -> Open {player = Nothing})
defaultGameMap size = Matrix.initialize size (always defaultCell)

generateMap : GameMap -> List (GameMap -> GameMap) -> GameMap
generateMap startingMap mapTransformers =
Expand All @@ -55,18 +63,29 @@ defaultGame =
g = generateMap (defaultGameMap size)
[ insertPlayer p startLoc
, setBarriers (randomizeLocationsWithin size 11)
, addItems (randomizeLocationsWithin size 30)
]
in
{ gameMap = g
, player = p
}

--doGame : Location -> Location -> List Location ->
addPlayer : Player -> Cell -> Cell
addPlayer p c =
case c of
Open cell -> Open {cell | player <- Just p}
otherwise -> c

removePlayer : Cell -> Cell
removePlayer c =
case c of
Open cell -> Open {cell | player <- Nothing}
otherwise -> c

insertPlayer : Player -> Location -> GameMap -> GameMap
insertPlayer p here gm =
Matrix.mapWithLocation (
\location cell -> if location == here then Open {player = Just p} else cell
\location cell -> if location == here then addPlayer p cell else cell
) gm

setBarriers : List Location -> GameMap -> GameMap
Expand All @@ -77,6 +96,20 @@ setBarriers barrierLocations gm =
| otherwise -> cell
) gm

addItems : List Location -> GameMap -> GameMap
addItems itemLocations gm =
let
numItems = (\location -> filter (\otherLoc -> otherLoc == location) itemLocations |> length)
in
Matrix.mapWithLocation (
\location cell -> if | location `member` itemLocations ->
case cell of
Open c -> Open { c | items <- (repeat (numItems location) Item) }
otherwise -> cell
| otherwise -> cell

) gm

currentPlayerLocation : GameMap -> Maybe Location
currentPlayerLocation gameMap =
let
Expand Down
15 changes: 2 additions & 13 deletions src/Rogue/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,10 @@ updateBoard : Player -> GameMap -> Location -> GameMap
updateBoard p gameMap newPlayerLoc =
mapWithLocation (
\(rowNum,colNum) cell ->
if | (rowNum, colNum) == newPlayerLoc -> insertPerson p cell
| otherwise -> clearCell cell
if | (rowNum, colNum) == newPlayerLoc -> addPlayer p cell
| otherwise -> removePlayer cell
) gameMap

insertPerson : Player -> Cell -> Cell
insertPerson p c =
case c of
Open _ -> Open {player = Just p}
otherwise -> c

clearCell : Cell -> Cell
clearCell c =
case c of
Open _ -> Open {player = Nothing}
otherwise -> c

movePlayerToLocation : GameMap -> Maybe Location -> Maybe Location -> Maybe Location
movePlayerToLocation gameMap fromLoc toLoc =
Expand Down
21 changes: 15 additions & 6 deletions src/Rogue/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import String exposing (join)
import Text
import Matrix exposing (..)

import List
import Rogue.Model exposing (..)

view : Game -> Element
Expand All @@ -22,7 +23,7 @@ viewGameMap gameMap =
viewCell : Cell -> Element
viewCell c =
case c of
Open {player} -> open player
Open contents -> open contents
Barrier _ -> barrier

txt str =
Expand All @@ -40,11 +41,19 @@ barrier =
|> (\sq -> collage 16 16 [sq])
|> standardize

open : Maybe a -> Element
open p =
case p of
Just _ -> person
Nothing -> unoccupied
open : Contents -> Element
open {player,items} =
let
itemCount = List.length items
in
case player of
Just _ -> person
Nothing ->
(if itemCount == 0 then "." else (itemCount |> toString))
|> txt
|> standardize



unoccupied =
txt "."
Expand Down

0 comments on commit a7b1a3f

Please sign in to comment.