From 8714c3befcf3f9923cf72e8d992ba6d963c0e6e7 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 4 Sep 2016 15:52:17 +0200 Subject: Upgrade to elm 0.17.1 --- src/Update.elm | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/Update.elm (limited to 'src/Update.elm') diff --git a/src/Update.elm b/src/Update.elm new file mode 100644 index 0000000..8aea214 --- /dev/null +++ b/src/Update.elm @@ -0,0 +1,79 @@ +module Update exposing + ( update + ) + +import List +import Char exposing (fromCode, toCode, KeyCode) +import Maybe +import Set +import Set exposing (Set) +import Time exposing (Time) +import Keyboard.Extra as Keyboard + +import Msg exposing (Msg(..)) +import Model.Player exposing (..) +import Model.Vec2 exposing (..) +import Model.Config exposing (otherConfig) +import Model.Cloud exposing (..) +import Model exposing (..) +import Model.Round exposing (Round) + +import Utils.Geometry exposing (..) +import Utils.Physics exposing (getNewPosAndSpeed) + +import Update.CloudUpdate exposing (cloudUpdate) + +update : Msg -> Model -> (Model, Cmd Msg) +update msg model = + case msg of + NoOp -> + (model, Cmd.none) + + Time time -> + (updateTime time model, Cmd.none) + + Keyboard keyboardMsg -> + let (keyboard, keyboardCmd) = Keyboard.update keyboardMsg model.keyboard + in ( { model | keyboard = keyboard } + , Cmd.map Keyboard keyboardCmd + ) + + Transform -> + ({ model | transform = True }, Cmd.none) + +updateTime : Time -> Model -> Model +updateTime time model = + let delta = time - model.time + dir = case Keyboard.arrows model.keyboard of {x, y} -> {x = toFloat x, y = toFloat y} + hostilePoints = model.cloud.points (otherConfig model.player.config) + in if(playerPointsCollision model.elapsedTime model.player (getPlayerSize model.currentScore) hostilePoints) + then + { model + | time = time + , elapsedTime = 0 + , currentScore = 0 + , cloud = initCloud + , rounds = (Round model.elapsedTime model.currentScore) :: model.rounds + } + else + let newPlayer = updatePlayer delta model.boardSize dir (Debug.log "transform" model.transform) model.player (getPlayerSize model.currentScore) + (newCloud, addScore, newSeed) = cloudUpdate model.elapsedTime model.boardSize model.seed newPlayer (getPlayerSize model.currentScore) model.cloud model.currentScore + in + { model + | time = time + , elapsedTime = model.elapsedTime + delta + , currentScore = model.currentScore + addScore + , player = newPlayer + , cloud = newCloud + , seed = newSeed + , transform = False + } + +updatePlayer : Float -> Vec2 -> Vec2 -> Bool -> Player -> Float -> Player +updatePlayer dt boardSize dir transform player playerSize = + let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed) + newConfig = if transform then otherConfig player.config else player.config + in { pos = inBoard boardSize playerSize pos + , speed = speed + , config = newConfig + } -- cgit v1.2.3