Skip to content

Commit

Permalink
Listen to ESCAPE key to deselect fighter
Browse files Browse the repository at this point in the history
  • Loading branch information
mwunsch committed Oct 11, 2016
1 parent ec250a1 commit ae8a705
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions elm-package.json
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"elm-lang/core": "4.0.5 <= v < 5.0.0",
"elm-lang/html": "1.1.0 <= v < 2.0.0",
"elm-lang/keyboard": "1.0.0 <= v < 2.0.0",
"elm-lang/mouse": "1.0.0 <= v < 2.0.0",
"elm-lang/svg": "1.1.0 <= v < 2.0.0",
"elm-lang/window": "1.0.0 <= v < 2.0.0"
Expand Down
26 changes: 24 additions & 2 deletions src/Main.elm
Expand Up @@ -4,6 +4,7 @@ import Html exposing (Html)
import Html.App as App
import Html.Events exposing (on, onClick)
import Json.Decode as Json exposing ((:=))
import Keyboard
import List exposing (map)
import Maybe exposing (andThen)
import Model exposing (Model)
Expand Down Expand Up @@ -62,6 +63,7 @@ type Msg
= Select Model
| Click Mouse.Position
| Hover Mouse.Position
| KeyPress Keyboard.KeyCode
| Resize Int
| NoOp

Expand Down Expand Up @@ -120,6 +122,15 @@ update msg game =
Nothing ->
( game, Cmd.none )

KeyPress key ->
case key of
-- ESCAPE
27 ->
( { game | playerSelection = Nothing }, Cmd.none )

_ ->
( game, Cmd.none )

Resize w ->
( { game
| windowWidth = w
Expand All @@ -139,8 +150,19 @@ update msg game =
subscriptions : GameState -> Sub Msg
subscriptions game =
Sub.batch
[ Mouse.moves Hover
, Window.resizes (\size -> Resize size.width)
[ Window.resizes (\size -> Resize size.width)
, case game.playerSelection of
Just _ ->
Mouse.moves Hover

Nothing ->
Sub.none
, case game.playerSelection of
Just _ ->
Keyboard.presses KeyPress

Nothing ->
Sub.none
]


Expand Down
2 changes: 1 addition & 1 deletion src/Model.elm
Expand Up @@ -175,7 +175,7 @@ view model msg =
[ fontSize "1"
, fontFamily "monospace"
, textAnchor "middle"
, fill (if model.selected then "white" else "black")
, fill <| if model.selected then "white" else "black"
, x
(model.position |> posX |> toString)
, y
Expand Down

0 comments on commit ae8a705

Please sign in to comment.