Update.elm 2.26 KB
Newer Older
1 2 3
module Update.Update
  ( update
  ) where
Joris Guyonvarch's avatar
Joris Guyonvarch committed
4

5
import List
Joris's avatar
Joris committed
6
import Char exposing (fromCode, toCode, KeyCode)
7
import Maybe
8 9
import Set
import Set exposing (Set)
10

11 12 13 14 15 16
import Model.Player exposing (..)
import Model.Vec2 exposing (..)
import Model.Config exposing (otherConfig)
import Model.Cloud exposing (..)
import Model.Game exposing (..)
import Model.Round exposing (Round)
17

18 19
import Utils.Geometry exposing (..)
import Utils.Physics exposing (getNewPosAndSpeed)
20

21
import Update.CloudUpdate exposing (cloudUpdate)
22

23
import Input exposing (Input)
24

25
update : Input -> Game -> Game
26 27
update input game =
  let hostilePoints = game.cloud.points (otherConfig game.player.config)
28
  in  if(playerPointsCollision game.elapsedTime game.player (getPlayerSize game.currentScore) hostilePoints)
29 30
        then
          { game
31
          | elapsedTime = 0
Joris's avatar
Joris committed
32 33
          , currentScore = 0
          , cloud = initCloud
34
          , rounds = (Round game.elapsedTime game.currentScore) :: game.rounds
35 36
          }
        else
37 38
          let newPlayer = playerStep input.delta game.boardSize input.dir (newKeyCode game.keysDown input.inputKeysDown) game.player (getPlayerSize game.currentScore)
              (newCloud, addScore, newSeed) = cloudUpdate game.elapsedTime game.boardSize game.seed newPlayer (getPlayerSize game.currentScore) game.cloud game.currentScore
39 40
          in
              { game
41
              | elapsedTime = game.elapsedTime + input.delta
Joris's avatar
Joris committed
42 43 44 45 46
              , keysDown = input.inputKeysDown
              , currentScore = game.currentScore + addScore
              , player = newPlayer
              , cloud = newCloud
              , seed = newSeed
47
              }
Joris Guyonvarch's avatar
Joris Guyonvarch committed
48

49 50
playerStep : Float -> Vec2 -> Vec2 -> (KeyCode -> Bool) -> Player -> Float -> Player
playerStep dt boardSize dir newKey player playerSize =
Joris Guyonvarch's avatar
Joris Guyonvarch committed
51
  let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed)
52
      newConfig = if (newKey 69) then otherConfig player.config else player.config
53
  in  { pos = inBoard boardSize playerSize pos
Joris Guyonvarch's avatar
Joris Guyonvarch committed
54
      , speed = speed
55
      , config = newConfig
Joris Guyonvarch's avatar
Joris Guyonvarch committed
56
      }
57

58
newKeyCode : Set KeyCode -> Set KeyCode -> KeyCode -> Bool
59
newKeyCode lastKeyCodes newKeyCodes keyCode =
60
  let contains = not << List.isEmpty << List.filter (\kc -> kc == keyCode) << Set.toList
61
  in  not (contains lastKeyCodes) && (contains newKeyCodes)