diff options
author | Joris Guyonvarch | 2015-03-04 23:27:59 +0100 |
---|---|---|
committer | Joris Guyonvarch | 2015-03-05 00:27:32 +0100 |
commit | 4d007f6802246c6411a2838e68e957c2b4d56d3d (patch) | |
tree | 70519a5a2d6825bf2b64f6a8950a003b2ff4f150 /src/CloudStep.elm | |
parent | d37a301ed39bac823e0f2223b8d229b417e128c7 (diff) |
Adapt the game to elm version 0.14.1
Diffstat (limited to 'src/CloudStep.elm')
-rw-r--r-- | src/CloudStep.elm | 114 |
1 files changed, 71 insertions, 43 deletions
diff --git a/src/CloudStep.elm b/src/CloudStep.elm index 65609cb..033c3c5 100644 --- a/src/CloudStep.elm +++ b/src/CloudStep.elm @@ -1,70 +1,98 @@ module CloudStep where +import List +import Random (..) + 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} = +cloudStep : Float -> Seed -> Player -> Cloud -> (Cloud, Int, Seed) +cloudStep time seed player {points, spawn, lastSpawn} = let pointsToCatch = presentPoints time (points player.config) - presentAndNotCaughtPoints = filter (not . (playerPointCollision time player)) pointsToCatch - addScore = (length pointsToCatch) - (length presentAndNotCaughtPoints) + presentAndNotCaughtPoints = List.filter (not << (playerPointCollision time player)) pointsToCatch + addScore = (List.length pointsToCatch) - (List.length presentAndNotCaughtPoints) presentOtherPoints = presentPoints time (points (otherConfig player.config)) - newCloud = + (newCloud, seed''') = if time > lastSpawn + spawn then - let newPoint1 = newPoint time randomValues.point1 - newPoint2 = newPoint time randomValues.point2 - in - { points config = + let (newPoint1, seed') = getNewPoint time seed + (newPoint2, seed'') = getNewPoint time seed' + in ( { points config = + if(config == player.config) + then + newPoint1 :: presentAndNotCaughtPoints + else + newPoint2 :: presentOtherPoints + , spawn = spawn - sqrt(spawn) / 50 + , lastSpawn = time + } + , seed'' + ) + else + ( { points config = if(config == player.config) then - newPoint1 :: presentAndNotCaughtPoints + presentAndNotCaughtPoints else - newPoint2 :: presentOtherPoints - , spawn = spawn - sqrt(spawn) / 50 - , lastSpawn = time + presentOtherPoints + , spawn = spawn + , lastSpawn = lastSpawn } - else - { points config = - if(config == player.config) then - presentAndNotCaughtPoints - else - presentOtherPoints - , spawn = spawn - , lastSpawn = lastSpawn - } - in (newCloud, addScore) + , seed + ) + in (newCloud, addScore, seed''') -presentPoints : Float -> [Point] -> [Point] +presentPoints : Float -> List Point -> List Point presentPoints time points = let isPresent point = (distance (pointMove point time) originVec) < pointAwayDist - in filter isPresent points + in List.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) +getNewPoint : Float -> Seed -> (Point, Seed) +getNewPoint time seed = + let (initPos, seed') = pointInitPos seed + (initDest, seed'') = pointDestination seed' + in ( { initTime = time + , initPos = initPos + , initDest = initDest + , move initTime initPos initDest time = + let delta = time - initTime + move = getMove (pointSpeed delta) (initDest `sub` initPos) + in initPos `add` move + } + , seed'' + ) + +pointInitPos : Seed -> (Vec2, Seed) +pointInitPos seed = + let (rand, seed') = generate floatGen seed + angle = rand * (degrees 360) dist = boardDiagonal * 3 / 5 - in polarToCartesian angle dist + in (polarToCartesian angle dist, seed') + +pointDestination : Seed -> (Vec2, Seed) +pointDestination seed = + let ([r1, r2, r3, r4], seed') = generateMany 4 floatGen seed + in ( randomBoardPosition (r1, r2) (r3, r4) + , seed' + ) + +generateMany : Int -> Generator a -> Seed -> (List a, Seed) +generateMany count gen seed = + if count == 0 + then + ([], seed) + else + let (rand, seed') = generate gen seed + (randList, seed'') = generateMany (count - 1) gen seed' + in (rand :: randList, seed'') -pointDestination : Float -> Float -> Vec2 -pointDestination randomX randomY = - randomBoardPosition (randomX, randomY) (1, 1) +floatGen : Generator Float +floatGen = float 0 1 randomBoardPosition : (Float, Float) -> (Float, Float) -> Vec2 randomBoardPosition (randomX, randomY) (percentX, percentY) = |