Skip to content

Commit

Permalink
Rearchitected again, added player inventory viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
chendrix committed Jun 22, 2015
1 parent b7ed83d commit 10f185d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/Rogue.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ main =

gameState : Signal Game
gameState =
Signal.foldp update defaultGame input
Signal.foldp update newGame input

input : Signal Input
input =
Expand Down
125 changes: 51 additions & 74 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import Now

type alias GameMap = Matrix Cell

type alias Player =
type alias Player =
{ inventory : List Item
}

type alias Game =
{ gameMap : GameMap
, player : Player
, playerLocation : Location
}

type Item = Item

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

type Cell
type Cell
= Open Contents
| Barrier {}

Expand All @@ -43,14 +43,14 @@ newPlayer = {inventory = []}

randomizeLocationsWithin : Int -> Int -> List Location
randomizeLocationsWithin size numLocations =
let
let
locationGenerator = list numLocations (pair (int 0 size) (int 0 size))
in
generate locationGenerator (initialSeed (round Now.loadTime)) |> fst

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

defaultGameMap : Int -> GameMap
defaultGameMap size = Matrix.initialize size (always defaultCell)
Expand All @@ -59,94 +59,71 @@ generateMap : GameMap -> List (GameMap -> GameMap) -> GameMap
generateMap startingMap mapTransformers =
foldr (<|) startingMap mapTransformers

generateGame : Game -> List (Game -> Game) -> Game
generateGame =
foldr (<|)

defaultGame : Game
defaultGame =
let
p = newPlayer
startLoc = (0,0)
size = 10
g = generateMap (defaultGameMap size)
[ insertPlayer p startLoc
, setBarriers (randomizeLocationsWithin size 11)
, addItems (randomizeLocationsWithin size 30)
]
g = generateMap (defaultGameMap size) []
in
{ gameMap = g
, player = p
, playerLocation = startLoc
}

newGame : Game
newGame =
let
p = newPlayer
startLoc = (0,0)
size = 10
g = generateMap (defaultGameMap size) []
startingGame = { gameMap = g
, player = p
, playerLocation = startLoc
}
in
generateGame startingGame
[ setBarriers (randomizeLocationsWithin size 11)
, addItems (randomizeLocationsWithin size 30)
]

updateContents : (Contents -> Contents) -> Cell -> Cell
updateContents f c =
case c of
Open contents -> Open (f contents)
otherwise -> c

addPlayer : Player -> Cell -> Cell
addPlayer p c =
let
f : Contents -> Contents
f = (\contents ->
{ contents |
player <- Just
{ p | inventory <- (p.inventory `append` contents.items)
},
items <- []
}
)
in updateContents f c


removePlayer : Cell -> Cell
removePlayer =
updateContents (
\contents -> {contents | player <- Nothing}
)

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

setBarriers : List Location -> GameMap -> GameMap
setBarriers barrierLocations gm =
Matrix.mapWithLocation (
\location cell -> if | doesContainPlayer cell -> cell
| location `member` barrierLocations -> Barrier {}
| otherwise -> cell
) gm

addItems : List Location -> GameMap -> GameMap
addItems itemLocations gm =
setBarriers : List Location -> Game -> Game
setBarriers barrierLocations game =
let
numItems = (\location -> filter (\otherLoc -> otherLoc == location) itemLocations |> length)
in
Matrix.mapWithLocation (
\location cell -> if | location `member` itemLocations ->
updateContents (\c -> { c | items <- (repeat (numItems location) Item) }) cell
newMap = Matrix.mapWithLocation (
\location cell -> if | location == game.playerLocation -> cell
| location `member` barrierLocations -> Barrier {}
| otherwise -> cell
) game.gameMap
in
{ game | gameMap <- newMap }

) gm

currentPlayerLocation : GameMap -> Maybe Location
currentPlayerLocation gameMap =
addItems : List Location -> Game -> Game
addItems itemLocations game =
let
matrixOfLocAndHasPlayer =
mapWithLocation (
\loc cell -> (loc, doesContainPlayer cell)
) gameMap
numItems location = length <| filter ((==) location) itemLocations
newMap =
Matrix.mapWithLocation (
\location cell -> if | location `member` itemLocations ->
updateContents (\c -> { c | items <- (repeat (numItems location) Item) }) cell
| otherwise -> cell

) game.gameMap
in
matrixOfLocAndHasPlayer
|> flatten
|> List.filter snd
|> List.map fst
|> head

doesContainPlayer : Cell -> Bool
doesContainPlayer c =
case c of
Open {player} -> isJust player
otherwise -> False
{ game | gameMap <- newMap }


isJust : Maybe a -> Bool
isJust m =
Expand Down
55 changes: 36 additions & 19 deletions src/Rogue/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,51 @@ import Rogue.Model exposing (..)
import Matrix exposing (..)
import Maybe
import Maybe exposing (andThen)
import List exposing (append)

update : Input -> Game -> Game
update i game =
{ game | gameMap <- updateGameMap i game.player game.gameMap }

updateGameMap : Input -> Player -> GameMap -> GameMap
updateGameMap {dir} p gameMap =
update {dir} ({gameMap,player,playerLocation} as game) =
let
maybeCurLocation = currentPlayerLocation gameMap
maybeTranslatedLocation = Maybe.map (translate dir) (maybeCurLocation)
maybeNewLocation = movePlayerToLocation gameMap maybeCurLocation maybeTranslatedLocation
maybeNewGameMap = Maybe.map (updateBoard p gameMap) maybeNewLocation
translatedLocation = (translate dir playerLocation)
newLocation = movePlayerToLocation gameMap playerLocation translatedLocation

newCell = cellAt gameMap newLocation
newPlayer = Maybe.map (flip updatePlayer <| player) newCell |> Maybe.withDefault player

newMap = updateGameMap newLocation gameMap
in
Maybe.withDefault gameMap maybeNewGameMap
{ game | gameMap <- newMap
, player <- newPlayer
, playerLocation <- newLocation
}

updatePlayer : Cell -> Player -> Player
updatePlayer cell player =
case cell of
Open {items} -> {player | inventory <- player.inventory `append` items}
otherwise -> player

updateBoard : Player -> GameMap -> Location -> GameMap
updateBoard p gameMap newPlayerLoc =
updateGameMap : Location -> GameMap -> GameMap
updateGameMap newPlayerLoc gameMap =
mapWithLocation (
\(rowNum,colNum) cell ->
if | (rowNum, colNum) == newPlayerLoc -> addPlayer p cell
| otherwise -> removePlayer cell
) gameMap
\location cell ->
if | location == newPlayerLoc -> removeItems cell
| otherwise -> cell
) gameMap

removeItems : Cell -> Cell
removeItems =
updateContents (\contents -> {contents | items <- [] })

movePlayerToLocation : GameMap -> Maybe Location -> Maybe Location -> Maybe Location

movePlayerToLocation : GameMap -> Location -> Location -> Location
movePlayerToLocation gameMap fromLoc toLoc =
toLoc `andThen` (cellAt gameMap) `andThen` (\cell ->
let
cellChooser cell =
case cell of
Open _ -> toLoc
otherwise -> fromLoc
)
in
cellAt gameMap toLoc
|> Maybe.map cellChooser
|> Maybe.withDefault fromLoc
46 changes: 25 additions & 21 deletions src/Rogue/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@ import List
import Rogue.Model exposing (..)

view : Game -> Element
view g = viewGameMap g.gameMap
view g =
flow down
[ viewGameMap g.playerLocation g.gameMap
, viewPlayer g.player
]

viewGameMap : GameMap -> Element
viewGameMap gameMap =
Matrix.map viewCell gameMap
viewGameMap : Location -> GameMap -> Element
viewGameMap playerLocation gameMap =
Matrix.mapWithLocation (viewCell playerLocation) gameMap
|> Matrix.toList
|> List.map (flow right)
|> flow down

viewCell : Cell -> Element
viewCell c =
case c of
Open contents -> open contents
Barrier _ -> barrier
viewCell : Location -> Location -> Cell -> Element
viewCell curLocation cellLocation c =
if | curLocation == cellLocation -> person
| otherwise -> case c of
Open contents -> open contents
Barrier _ -> barrier

txt str =
viewPlayer : Player -> Element
viewPlayer {inventory} =
txt (String.join "" ["Item Count: ", List.length inventory |> toString ])

txt str =
Text.fromString str
|> Text.monospace
|> centered
Expand All @@ -42,19 +51,14 @@ barrier =
|> standardize

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

(if itemCount == 0 then "." else (itemCount |> toString))
|> txt
|> standardize


unoccupied =
txt "."
|> standardize
Expand Down

0 comments on commit 10f185d

Please sign in to comment.