The arrow keys work... kind of...xD

This commit is contained in:
John Shaver 2017-06-28 09:09:20 -07:00
parent 80b92c7444
commit 36260c9dc9
6 changed files with 63 additions and 4 deletions

View file

@ -12,6 +12,7 @@
"elm-lang/animation-frame": "1.0.1 <= v < 2.0.0", "elm-lang/animation-frame": "1.0.1 <= v < 2.0.0",
"elm-lang/core": "5.1.1 <= v < 6.0.0", "elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.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", "elm-lang/svg": "2.0.0 <= v < 3.0.0",
"folkertdev/svg-path-dsl": "2.0.0 <= v < 3.0.0" "folkertdev/svg-path-dsl": "2.0.0 <= v < 3.0.0"
}, },

View file

@ -1,7 +1,8 @@
module Subscriptions exposing (..) module Subscriptions exposing (..)
import AnimationFrame exposing (times, diffs) 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 subscriptions : Model -> Sub Msg
@ -9,4 +10,5 @@ subscriptions model =
Sub.batch Sub.batch
[ times Tick [ times Tick
, diffs Tock , diffs Tock
, downs Press
] ]

View file

@ -1,6 +1,7 @@
module Types exposing (..) module Types exposing (..)
import Time exposing (Time) import Time exposing (Time)
import Keyboard exposing (KeyCode)
type Direction type Direction
@ -10,6 +11,14 @@ type Direction
| Right | Right
type KeyAction
= MoveUp
| MoveDown
| MoveLeft
| MoveRight
| NoKeyAction
type alias Model = type alias Model =
{ time : Time { time : Time
, player : PlayerModel , player : PlayerModel
@ -19,6 +28,7 @@ type alias Model =
type Msg type Msg
= Tick Time = Tick Time
| Tock Time | Tock Time
| Press KeyCode
type alias PlayerModel = type alias PlayerModel =

View file

@ -1,6 +1,12 @@
module Updates.Player exposing (update) 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 AnimationHelpers exposing (movePosition, distanceFromSpeed)
import GLOBALS exposing (player_move_speed) import GLOBALS exposing (player_move_speed)
@ -33,3 +39,39 @@ update msg model =
( { model | location = movePosition model.location direction distance } ( { model | location = movePosition model.location direction distance }
, Cmd.none , 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 )

View file

@ -1,7 +1,7 @@
module Updates.Time exposing (update) module Updates.Time exposing (update)
import Time exposing (Time) import Time exposing (Time)
import Types exposing (Msg(Tick, Tock)) import Types exposing (Msg(Tick, Tock, Press))
update : Msg -> Time -> ( Time, Cmd Msg ) update : Msg -> Time -> ( Time, Cmd Msg )
@ -12,3 +12,6 @@ update msg model =
Tock timeDiff -> Tock timeDiff ->
( model, Cmd.none ) ( model, Cmd.none )
_ ->
( model, Cmd.none )

View file

@ -13,6 +13,7 @@
"elm-community/json-extra": "2.3.0 <= v < 3.0.0", "elm-community/json-extra": "2.3.0 <= v < 3.0.0",
"elm-lang/core": "5.0.0 <= v < 6.0.0", "elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.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", "folkertdev/svg-path-dsl": "2.0.0 <= v < 3.0.0",
"mgold/elm-random-pcg": "5.0.0 <= v < 6.0.0", "mgold/elm-random-pcg": "5.0.0 <= v < 6.0.0",
"rtfeldman/node-test-runner": "3.0.0 <= v < 4.0.0" "rtfeldman/node-test-runner": "3.0.0 <= v < 4.0.0"