Skip to content

Commit

Permalink
Add items to open slots, player picks them up
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hendrix committed Jun 20, 2015
1 parent 3126f56 commit c3148e6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
42 changes: 30 additions & 12 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import Now

type alias GameMap = Matrix Cell

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

type alias Game =
{ gameMap : GameMap
Expand All @@ -36,6 +38,9 @@ type alias Input =
{ dir : Dir
}

newPlayer : Player
newPlayer = {inventory = []}

randomizeLocationsWithin : Int -> Int -> List Location
randomizeLocationsWithin size numLocations =
let
Expand All @@ -57,7 +62,7 @@ generateMap startingMap mapTransformers =
defaultGame : Game
defaultGame =
let
p = Player
p = newPlayer
startLoc = (0,0)
size = 10
g = generateMap (defaultGameMap size)
Expand All @@ -70,17 +75,32 @@ defaultGame =
, player = p
}

addPlayer : Player -> Cell -> Cell
addPlayer p c =
updateContents : (Contents -> Contents) -> Cell -> Cell
updateContents f c =
case c of
Open cell -> Open {cell | player <- Just p}
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 c =
case c of
Open cell -> Open {cell | player <- Nothing}
otherwise -> c
removePlayer =
updateContents (
\contents -> {contents | player <- Nothing}
)

insertPlayer : Player -> Location -> GameMap -> GameMap
insertPlayer p here gm =
Expand All @@ -103,9 +123,7 @@ addItems itemLocations gm =
in
Matrix.mapWithLocation (
\location cell -> if | location `member` itemLocations ->
case cell of
Open c -> Open { c | items <- (repeat (numItems location) Item) }
otherwise -> cell
updateContents (\c -> { c | items <- (repeat (numItems location) Item) }) cell
| otherwise -> cell

) gm
Expand Down
6 changes: 3 additions & 3 deletions test/RogueTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tests = suite "A Test Suite"
test_update_moves_currentPlayerLocation : Test
test_update_moves_currentPlayerLocation =
let
player = Player
player = newPlayer
game_map = generateMap (defaultGameMap 2) [ insertPlayer player (0,0) ]
down_right = Input {x=1,y=-1}
in
Expand All @@ -30,7 +30,7 @@ test_update_moves_currentPlayerLocation =
test_updateGameMap_does_not_move_outside_of_bounds : Test
test_updateGameMap_does_not_move_outside_of_bounds =
let
p = Player
p = newPlayer
game_map = defaultGameMap 2
gameMapAtTopLeft = generateMap game_map [ insertPlayer p (0,0) ]
left = Input { x =-1, y = 0 }
Expand All @@ -57,7 +57,7 @@ test_updateGameMap_does_not_move_outside_of_bounds =
test_updateGameMap_does_not_move_into_barriers : Test
test_updateGameMap_does_not_move_into_barriers =
let
p = Player
p = newPlayer
game_map = generateMap (defaultGameMap 2) [ insertPlayer p (0,0), setBarriers [(0,1)] ]
right = Input { x =1, y = 0 }
in
Expand Down

0 comments on commit c3148e6

Please sign in to comment.