diff --git a/elm-package.json b/elm-package.json index df7a3df..26aa577 100644 --- a/elm-package.json +++ b/elm-package.json @@ -5,13 +5,14 @@ "license": "BSD3", "source-directories": [ ".", - "./src" + "./src" ], "exposed-modules": [], "dependencies": { "elm-lang/animation-frame": "1.0.1 <= v < 2.0.0", "elm-lang/core": "5.1.1 <= v < 6.0.0", "elm-lang/html": "2.0.0 <= v < 3.0.0", + "elm-lang/keyboard": "1.0.1 <= v < 2.0.0", "elm-lang/svg": "2.0.0 <= v < 3.0.0", "folkertdev/svg-path-dsl": "2.0.0 <= v < 3.0.0" }, diff --git a/src/Subscriptions.elm b/src/Subscriptions.elm index 96b8d81..ef1a012 100644 --- a/src/Subscriptions.elm +++ b/src/Subscriptions.elm @@ -1,7 +1,8 @@ module Subscriptions exposing (..) import AnimationFrame exposing (times, diffs) -import Types exposing (Model, Msg(Tick, Tock)) +import Types exposing (Model, Msg(Tick, Tock, Press)) +import Keyboard exposing (downs) subscriptions : Model -> Sub Msg @@ -9,4 +10,5 @@ subscriptions model = Sub.batch [ times Tick , diffs Tock + , downs Press ] diff --git a/src/Types.elm b/src/Types.elm index 12306e1..1bb89bb 100644 --- a/src/Types.elm +++ b/src/Types.elm @@ -1,6 +1,7 @@ module Types exposing (..) import Time exposing (Time) +import Keyboard exposing (KeyCode) type Direction @@ -10,6 +11,14 @@ type Direction | Right +type KeyAction + = MoveUp + | MoveDown + | MoveLeft + | MoveRight + | NoKeyAction + + type alias Model = { time : Time , player : PlayerModel @@ -19,6 +28,7 @@ type alias Model = type Msg = Tick Time | Tock Time + | Press KeyCode type alias PlayerModel = diff --git a/src/Updates/Player.elm b/src/Updates/Player.elm index 447c640..aa674dd 100644 --- a/src/Updates/Player.elm +++ b/src/Updates/Player.elm @@ -1,6 +1,12 @@ module Updates.Player exposing (update) -import Types exposing (Direction(Up, Down, Left, Right), PlayerModel, Msg(Tick, Tock)) +import Types + exposing + ( Direction(Up, Down, Left, Right) + , PlayerModel + , Msg(Tick, Tock, Press) + , KeyAction(MoveUp, MoveDown, MoveLeft, MoveRight, NoKeyAction) + ) import AnimationHelpers exposing (movePosition, distanceFromSpeed) import GLOBALS exposing (player_move_speed) @@ -33,3 +39,39 @@ update msg model = ( { model | location = movePosition model.location direction distance } , Cmd.none ) + + Press keyCode -> + let + keyMap = + [ ( MoveUp, [ 38 ] ) + , ( MoveDown, [ 40 ] ) + , ( MoveLeft, [ 37 ] ) + , ( MoveRight, [ 39 ] ) + ] + + direction = + List.foldl + (\( key, codes ) found -> + if List.member keyCode codes then + key + else + found + ) + NoKeyAction + keyMap + in + case direction of + MoveUp -> + ( { model | direction = Up }, Cmd.none ) + + MoveDown -> + ( { model | direction = Down }, Cmd.none ) + + MoveLeft -> + ( { model | direction = Left }, Cmd.none ) + + MoveRight -> + ( { model | direction = Right }, Cmd.none ) + + NoKeyAction -> + ( model, Cmd.none ) diff --git a/src/Updates/Time.elm b/src/Updates/Time.elm index ef07cda..242bdc8 100644 --- a/src/Updates/Time.elm +++ b/src/Updates/Time.elm @@ -1,7 +1,7 @@ module Updates.Time exposing (update) import Time exposing (Time) -import Types exposing (Msg(Tick, Tock)) +import Types exposing (Msg(Tick, Tock, Press)) update : Msg -> Time -> ( Time, Cmd Msg ) @@ -12,3 +12,6 @@ update msg model = Tock timeDiff -> ( model, Cmd.none ) + + _ -> + ( model, Cmd.none ) diff --git a/tests/elm-package.json b/tests/elm-package.json index 512fc5d..8728be2 100644 --- a/tests/elm-package.json +++ b/tests/elm-package.json @@ -13,6 +13,7 @@ "elm-community/json-extra": "2.3.0 <= v < 3.0.0", "elm-lang/core": "5.0.0 <= v < 6.0.0", "elm-lang/html": "2.0.0 <= v < 3.0.0", + "elm-lang/keyboard": "1.0.1 <= v < 2.0.0", "folkertdev/svg-path-dsl": "2.0.0 <= v < 3.0.0", "mgold/elm-random-pcg": "5.0.0 <= v < 6.0.0", "rtfeldman/node-test-runner": "3.0.0 <= v < 4.0.0"