-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Hendrix and Sean Brady
committed
Jun 19, 2015
1 parent
ce1c9f6
commit 2d2fb5a
Showing
3 changed files
with
114 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,50 @@ | ||
module Rogue.Update where | ||
|
||
import Rogue.Model exposing (..) | ||
import Array | ||
import Maybe | ||
import Maybe exposing (andThen) | ||
|
||
update : Input -> Game -> Game | ||
update i game = | ||
{ game | gameMap <- updateGameMap i game.gameMap } | ||
{ game | gameMap <- updateGameMap i game.player game.gameMap } | ||
|
||
updateGameMap : Input -> GameMap -> GameMap | ||
updateGameMap {dir} ({board,start,currentPlayerLocation} as gameMap) = | ||
updateGameMap : Input -> Player -> GameMap -> GameMap | ||
updateGameMap {dir} p gameMap = | ||
let | ||
newLocation = translate currentPlayerLocation dir | ||
newCell = cellAt newLocation board | ||
openness = Maybe.withDefault False (Maybe.map isOpen newCell) | ||
maybeCurLocation = currentPlayerLocation gameMap | ||
maybeTranslatedLocation = Maybe.map (translate dir) (maybeCurLocation) | ||
maybeNewLocation = movePlayerToLocation gameMap maybeCurLocation maybeTranslatedLocation | ||
maybeNewGameMap = Maybe.map (updateBoard p gameMap) maybeNewLocation | ||
in | ||
if | within board newLocation && openness -> { gameMap | currentPlayerLocation <- newLocation } | ||
| otherwise -> gameMap | ||
Maybe.withDefault gameMap maybeNewGameMap | ||
|
||
updateBoard : Player -> GameMap -> Location -> GameMap | ||
updateBoard p gameMap newPlayerLoc = | ||
Array.indexedMap ( | ||
\rowNum row -> Array.indexedMap ( | ||
\colNum cell -> | ||
if | (rowNum, colNum) == newPlayerLoc -> insertPerson p cell | ||
| otherwise -> clearCell cell | ||
) row | ||
) 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 = | ||
toLoc `andThen` (cellAt gameMap) `andThen` (\cell -> | ||
case cell of | ||
Open _ -> toLoc | ||
otherwise -> fromLoc | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters