Added auto wrapping of location.

This commit is contained in:
John Shaver 2017-06-29 10:50:54 -07:00
parent d07e68a16f
commit 292ee99eab
5 changed files with 26 additions and 10 deletions

View File

@ -31,6 +31,18 @@ movePosition position direction distance =
( x, y ) ( x, y )
wrapLocation : Point -> Point -> Point
wrapLocation maxLocation location =
let
( x, y ) =
location
( maxX, maxY ) =
maxLocation
in
( (fmod x maxX), (fmod y maxY) )
distanceFromSpeed : Float -> Float -> Float distanceFromSpeed : Float -> Float -> Float
distanceFromSpeed pixPerSec millis = distanceFromSpeed pixPerSec millis =
pixPerSec * millis / 1000 pixPerSec * millis / 1000

View File

@ -9,9 +9,9 @@ character_radius =
20 20
playAreaSize = play_area_size =
( 800, 880 ) ( 800, 880 )
backgroundColor = background_color =
"black" "black"

View File

@ -7,8 +7,8 @@ import Types
, Msg(Tick, Tock, Press) , Msg(Tick, Tock, Press)
, KeyAction(MoveUp, MoveDown, MoveLeft, MoveRight, NoKeyAction) , KeyAction(MoveUp, MoveDown, MoveLeft, MoveRight, NoKeyAction)
) )
import AnimationHelpers exposing (movePosition, distanceFromSpeed) import AnimationHelpers exposing (movePosition, distanceFromSpeed, wrapLocation)
import GLOBALS exposing (player_move_speed) import GLOBALS exposing (player_move_speed, play_area_size)
update : Msg -> PlayerModel -> ( PlayerModel, Cmd Msg ) update : Msg -> PlayerModel -> ( PlayerModel, Cmd Msg )
@ -36,8 +36,12 @@ update msg model =
distance = distance =
distanceFromSpeed player_move_speed timeDiff distanceFromSpeed player_move_speed timeDiff
newLocation =
movePosition model.location direction distance
|> wrapLocation play_area_size
in in
( { model | location = movePosition model.location direction distance } ( { model | location = newLocation }
, Cmd.none , Cmd.none
) )
else else

View File

@ -1,7 +1,7 @@
module Views.Body exposing (..) module Views.Body exposing (..)
import Html exposing (Html, h1, div, text) import Html exposing (Html, h1, div, text)
import GLOBALS exposing (playAreaSize) import GLOBALS exposing (play_area_size)
import Types exposing (Model, Range) import Types exposing (Model, Range)
import Views.PlayArea import Views.PlayArea
@ -10,7 +10,7 @@ view : Model -> Html msg
view model = view model =
let let
( width, height ) = ( width, height ) =
playAreaSize play_area_size
in in
div [] div []
[ h1 [] [ text "ElmMan!" ] [ h1 [] [ text "ElmMan!" ]

View File

@ -1,6 +1,6 @@
module Views.Map exposing (..) module Views.Map exposing (..)
import GLOBALS exposing (backgroundColor, playAreaSize) import GLOBALS exposing (background_color, play_area_size)
import Svg exposing (Svg, g, path, rect) import Svg exposing (Svg, g, path, rect)
import Svg.Attributes exposing (d, fill, x, y, width, height) import Svg.Attributes exposing (d, fill, x, y, width, height)
import Svg.Path exposing (pathToString, lineTo, subpath, startAt, closed) import Svg.Path exposing (pathToString, lineTo, subpath, startAt, closed)
@ -10,11 +10,11 @@ render : Svg msg
render = render =
let let
( areaWidth, areaHeight ) = ( areaWidth, areaHeight ) =
playAreaSize play_area_size
in in
g [] g []
[ rect [ rect
[ fill backgroundColor [ fill background_color
, width (toString areaWidth) , width (toString areaWidth)
, height (toString areaHeight) , height (toString areaHeight)
, x (toString 0) , x (toString 0)