From 9b2a65f64ddddeeb60d54b3f4b4f8fadbb71786e Mon Sep 17 00:00:00 2001 From: John Shaver Date: Tue, 27 Jun 2017 15:25:06 -0700 Subject: [PATCH] Added simple movement to model updates. --- src/AnimationHelpers.elm | 26 ++++++++++++++++++++++++++ src/GLOBALS.elm | 4 ++-- src/Updates/Player.elm | 31 +++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/AnimationHelpers.elm b/src/AnimationHelpers.elm index 7c4d65b..a8dd938 100644 --- a/src/AnimationHelpers.elm +++ b/src/AnimationHelpers.elm @@ -1,6 +1,7 @@ module AnimationHelpers exposing (..) import Types exposing (Range) +import Svg.Path exposing (Point) calculateAnimation : Int -> Int -> Range -> Float @@ -13,3 +14,28 @@ calculateAnimation current cycle range = end - start in (toFloat (current % (cycle + 1))) / (toFloat (cycle)) * rangeDiff + start + + +movePosition : Point -> Float -> Float -> Point +movePosition position direction distance = + let + rads = + degrees direction + + x = + (Tuple.first position) + (cos rads * distance) + + y = + (Tuple.second position) + (sin rads * distance) + in + ( x, y ) + + +distanceFromSpeed : Float -> Float -> Float +distanceFromSpeed pixPerSec millis = + pixPerSec * millis / 1000 + + +fmod : Float -> Float -> Float +fmod lh rh = + lh - (toFloat (floor (lh / rh)) * rh) diff --git a/src/GLOBALS.elm b/src/GLOBALS.elm index a54b603..6f53390 100644 --- a/src/GLOBALS.elm +++ b/src/GLOBALS.elm @@ -1,8 +1,8 @@ module GLOBALS exposing (..) -move_speed = - 60 +player_move_speed = + 120 character_radius = diff --git a/src/Updates/Player.elm b/src/Updates/Player.elm index 3b0c74f..447c640 100644 --- a/src/Updates/Player.elm +++ b/src/Updates/Player.elm @@ -1,8 +1,35 @@ module Updates.Player exposing (update) -import Types exposing (PlayerModel, Msg) +import Types exposing (Direction(Up, Down, Left, Right), PlayerModel, Msg(Tick, Tock)) +import AnimationHelpers exposing (movePosition, distanceFromSpeed) +import GLOBALS exposing (player_move_speed) update : Msg -> PlayerModel -> ( PlayerModel, Cmd Msg ) update msg model = - ( model, Cmd.none ) + case msg of + Tick newTime -> + ( model, Cmd.none ) + + Tock timeDiff -> + let + direction = + case model.direction of + Right -> + 0 + + Up -> + 90 + + Left -> + 180 + + Down -> + 270 + + distance = + distanceFromSpeed player_move_speed timeDiff + in + ( { model | location = movePosition model.location direction distance } + , Cmd.none + )