aboutsummaryrefslogtreecommitdiff
path: root/src/Update.elm
blob: 8aea2140158783f5c7295a570f0ee5d3bbf86c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
      }