From 4d007f6802246c6411a2838e68e957c2b4d56d3d Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Wed, 4 Mar 2015 23:27:59 +0100 Subject: Adapt the game to elm version 0.14.1 --- src/CloudStep.elm | 114 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 43 deletions(-) (limited to 'src/CloudStep.elm') 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) = -- cgit v1.2.3