aboutsummaryrefslogtreecommitdiff
path: root/src/Update/Update.elm
blob: 97cf6e34f7a4a0acdc536b8ad7558aa81e7f2c77 (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
module Update.Update
  ( update
  ) where

import List
import Keyboard exposing (KeyCode)
import Char exposing (fromCode, toCode)
import Maybe
import Set
import Set exposing (Set)

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)

import Utils.Geometry exposing (..)
import Utils.Physics exposing (getNewPosAndSpeed)

import Update.CloudUpdate exposing (cloudUpdate)

import Input exposing (Input)

update : Input -> Game -> Game
update input game =
  let hostilePoints = game.cloud.points (otherConfig game.player.config)
  in  if(playerPointsCollision game.time game.player hostilePoints)
        then
          { game
          | time <- 0
          , currentScore <- 0
          , cloud <- initCloud
          , rounds <- (Round game.time game.currentScore) :: game.rounds
          }
        else
          let newPlayer = playerStep input.delta game.boardSize input.dir (newKeyCode game.keysDown input.inputKeysDown) game.player
              (newCloud, addScore, newSeed) = cloudUpdate game.time game.boardSize game.seed newPlayer game.cloud
          in
              { game
              | time <- game.time + input.delta
              , keysDown <- input.inputKeysDown
              , currentScore <- game.currentScore + addScore
              , player <- newPlayer
              , cloud <- newCloud
              , seed <- newSeed
              }

playerStep : Float -> Vec2 -> Vec2 -> (KeyCode -> Bool) -> Player -> Player
playerStep dt boardSize dir newKey player =
  let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed)
      newConfig = if (newKey 69) then otherConfig player.config else player.config
  in  { pos = inBoard boardSize playerSize pos
      , speed = speed
      , config = newConfig
      }

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