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

@ -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"
},

View File

@ -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
]

View File

@ -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 =

View File

@ -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 )

View File

@ -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 )

View File

@ -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"