aboutsummaryrefslogtreecommitdiff
path: root/src/CloudStep.elm
blob: 65609cb587ff577b62b69faf3e85637833ce9fbe (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
module CloudStep where

import Vec2 (..)
import Geometry (..)
import Player (..)
import Board (boardSize, boardDiagonal)
import Point (..)
import RandomValues (..)
import Physics (getMove)
import Cloud (..)
import Config (..)

cloudStep : Float -> RandomValues -> Player -> Cloud -> (Cloud, Int)
cloudStep time randomValues player {points, spawn, lastSpawn} =
  let pointsToCatch = presentPoints time (points player.config)
      presentAndNotCaughtPoints = filter (not . (playerPointCollision time player)) pointsToCatch
      addScore = (length pointsToCatch) - (length presentAndNotCaughtPoints)
      presentOtherPoints = presentPoints time (points (otherConfig player.config))
      newCloud =
        if time > lastSpawn + spawn then
          let newPoint1 = newPoint time randomValues.point1
              newPoint2 = newPoint time randomValues.point2
          in
            { points config =
                if(config == player.config) then
                  newPoint1 :: presentAndNotCaughtPoints
                else
                  newPoint2 :: presentOtherPoints
            , spawn = spawn - sqrt(spawn) / 50
            , lastSpawn = time
            }
        else
          { points config =
              if(config == player.config) then
                presentAndNotCaughtPoints
              else
                presentOtherPoints
          , spawn = spawn
          , lastSpawn = lastSpawn
          }
  in  (newCloud, addScore)

presentPoints : Float -> [Point] -> [Point]
presentPoints time points =
  let isPresent point = (distance (pointMove point time) originVec) < pointAwayDist
  in  filter isPresent points

newPoint : Float -> PointRandomValues -> Point
newPoint time pointRandomValues =
  { initTime = time
  , initPos = pointInitPos pointRandomValues.angle
  , initDest = pointDestination pointRandomValues.x pointRandomValues.y
  , move initTime initPos initDest time =
      let delta = time - initTime
          move = getMove (pointSpeed delta) (initDest `sub` initPos)
      in  initPos `add` move
  }

pointInitPos : Float -> Vec2
pointInitPos randomAngle =
  let angle = randomAngle * (degrees 360)
      dist = boardDiagonal * 3 / 5
  in  polarToCartesian angle dist

pointDestination : Float -> Float -> Vec2
pointDestination randomX randomY =
  randomBoardPosition (randomX, randomY) (1, 1)

randomBoardPosition : (Float, Float) -> (Float, Float) -> Vec2
randomBoardPosition (randomX, randomY) (percentX, percentY) =
  let width = boardSize.x * percentX
      height = boardSize.y * percentY
  in  { x = width * randomX - width / 2
      , y = height * randomY - height / 2
      }