Skip to content

Commit

Permalink
Add time/clock signal
Browse files Browse the repository at this point in the history
  • Loading branch information
chendrix committed Jul 3, 2015
1 parent 9b5a404 commit 0811c10
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
21 changes: 14 additions & 7 deletions src/Rogue.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Numpad
import Rogue.Model exposing (..)
import Rogue.Update exposing (..)
import Rogue.View exposing (..)
import Signal exposing ((<~), (~), Signal)
import Signal exposing (..)
import Time exposing (..)

-- SIGNALS

Expand All @@ -19,9 +20,15 @@ gameState =

input : Signal Input
input =
Input <~
Signal.mergeMany
[ Keyboard.wasd
, Keyboard.arrows
, Numpad.numpad
]
mergeMany
[ (\d -> Input { dir = d }) <~
Signal.mergeMany
[ Keyboard.wasd
, Keyboard.arrows
, Numpad.numpad
]
, TimeDelta <~ delta
]

delta : Signal Time
delta = Signal.map (\t -> t / 20) (fps 30)
7 changes: 2 additions & 5 deletions src/Rogue/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import List exposing (..)
import Maybe exposing (..)
import Random exposing (..)
import Matrix exposing (..)
import Time exposing (..)

import Now

type alias GameMap = Matrix Cell

type alias HitPoints = Int
type alias HitPoints = Float
type alias Player =
{ inventory : List Item
, hp : HitPoints
Expand All @@ -36,10 +37,6 @@ type alias Dir =
, y : Int
}

type alias Input =
{ dir : Dir
}

newPlayer : Player
newPlayer = {inventory = [], hp = 100}

Expand Down
39 changes: 27 additions & 12 deletions src/Rogue/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@ import Matrix exposing (..)
import Maybe
import Maybe exposing (andThen)
import List exposing (append)
import Time exposing (..)

type Input
= Input { dir : Dir
}
| TimeDelta Time

update : Input -> Game -> Game
update {dir} ({gameMap,player,playerLocation} as game) =
let
translatedLocation = (translate dir playerLocation)
newLocation = movePlayerToLocation gameMap playerLocation translatedLocation
update input ({gameMap,player,playerLocation} as game) =
case input of
Input {dir} ->
let
translatedLocation = (translate dir playerLocation)
newLocation = movePlayerToLocation gameMap playerLocation translatedLocation

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

newMap = updateGameMap newLocation gameMap
in
{ game | gameMap <- newMap
, player <- newPlayer
, playerLocation <- newLocation
}
newMap = updateGameMap newLocation gameMap
in
{ game | gameMap <- newMap
, player <- newPlayer
, playerLocation <- newLocation
}
TimeDelta delta ->
let
newPlayer = { player | hp <- player.hp - (delta / 100) }
in
{ game | player <- newPlayer }

updatePlayer : Cell -> Player -> Player
updatePlayer cell player =
Expand Down
2 changes: 1 addition & 1 deletion src/Rogue/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ viewPlayer : Player -> Element
viewPlayer {inventory, hp} =
flow down
[ txt (String.join "" ["Item Count: ", List.length inventory |> toString ])
, txt (String.join "" ["Current HP: ", hp |> toString ])
, txt (String.join "" ["Current HP: ", floor hp |> toString ])
]


Expand Down

0 comments on commit 0811c10

Please sign in to comment.