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 }