diff options
author | Joris Guyonvarch | 2014-10-03 16:18:24 +0200 |
---|---|---|
committer | Joris Guyonvarch | 2014-10-03 16:18:24 +0200 |
commit | a1a3dbadffaaab290a6b6bb4b61df787d3b8ffdd (patch) | |
tree | 039b6b171d9dbdc2c10101a3a5664125a4eddcbf /src | |
parent | b3975b15a16fb22739f2d633caaeed0cb1c39ab7 (diff) |
Adding a target to catch
Diffstat (limited to 'src')
-rw-r--r-- | src/Display.elm | 45 | ||||
-rw-r--r-- | src/Enemy.elm | 2 | ||||
-rw-r--r-- | src/EnemyState.elm | 10 | ||||
-rw-r--r-- | src/Game.elm | 26 | ||||
-rw-r--r-- | src/Input.elm | 6 | ||||
-rw-r--r-- | src/Main.elm | 2 | ||||
-rw-r--r-- | src/RandomValues.elm | 2 | ||||
-rw-r--r-- | src/Step.elm | 32 | ||||
-rw-r--r-- | src/Target.elm | 24 | ||||
-rwxr-xr-x | src/generate (renamed from src/genGame) | 0 |
10 files changed, 103 insertions, 46 deletions
diff --git a/src/Display.elm b/src/Display.elm index cb9fb86..b586129 100644 --- a/src/Display.elm +++ b/src/Display.elm @@ -5,22 +5,24 @@ import Player (..) import Game (Game) import Enemy (..) import Board (boardSize) +import Target (..) display : Game -> Element -display {time, player, enemyState, bestTime} = +display {time, player, target, enemyState, bestScore} = let enemyForms = map (enemyForm time) enemyState.enemies forms = boardForms ++ playerForms player + ++ targetForms target.pos ++ enemyForms - ++ bestTimeForms bestTime - ++ timeForms time + ++ scoreForms target.score + ++ bestScoreForms bestScore in collage (truncate boardSize.x) (truncate boardSize.y) forms boardForms : [Form] boardForms = [filled boardColor (rect boardSize.x boardSize.y)] boardColor : Color -boardColor = rgb 34 122 34 +boardColor = rgb 17 17 17 playerForms : Player -> [Form] playerForms player = [circleForm player.pos playerSize playerColor] @@ -28,6 +30,12 @@ playerForms player = [circleForm player.pos playerSize playerColor] playerColor : Color playerColor = rgb 224 224 224 +targetForms : Vec2 -> [Form] +targetForms pos = [circleForm pos targetSize targetColor] + +targetColor : Color +targetColor = rgb 34 85 34 + enemyForm : Float -> Enemy -> Form enemyForm time enemy = let pos = enemyMove enemy time @@ -39,17 +47,25 @@ enemyColor = rgb 170 0 0 circleForm : Vec2 -> Float -> Color -> Form circleForm pos size color = let outline = circle size - |> filled black - inside = circle (size - 2) + |> filled outlineColor + inside = circle (size - 1) |> filled color in group [outline, inside] |> move (pos.x, pos.y) -bestTimeForms : Float -> [Form] -bestTimeForms bestTime = - if(bestTime > 0) then - let seconds = truncate (bestTime / 1000) - text = "Record: " ++ (show seconds) +outlineColor : Color +outlineColor = rgb 34 34 34 + +scoreForms : Int -> [Form] +scoreForms score = + let text = (show score) + scorePos = { x = 0.0, y = boardSize.y / 2 - 30 } + in [textForm text scorePos centered] + +bestScoreForms : Int -> [Form] +bestScoreForms bestScore = + if(bestScore > 0) then + let text = "Record: " ++ (show bestScore) pos = { x = boardSize.x / 2 - 65 , y = -boardSize.y / 2 + 30 @@ -57,13 +73,6 @@ bestTimeForms bestTime = in [textForm text pos rightAligned] else [] -timeForms : Float -> [Form] -timeForms time = - let seconds = truncate (time / 1000) - text = (show seconds) - pos = { x = 0.0, y = boardSize.y / 2 - 30 } - in [textForm text pos centered] - textForm : String -> Vec2 -> (Text -> Element) -> Form textForm content pos alignment = let textElement = toText content diff --git a/src/Enemy.elm b/src/Enemy.elm index 2c80f0a..0f986aa 100644 --- a/src/Enemy.elm +++ b/src/Enemy.elm @@ -15,7 +15,7 @@ enemyMove enemy time = enemy.move enemy.initTime enemy.initPos enemy.initDest time enemySize : Float -enemySize = 8 +enemySize = 5 enemySpeed : Float -> Float enemySpeed dt = dt / 25 diff --git a/src/EnemyState.elm b/src/EnemyState.elm index 81766bf..fed2981 100644 --- a/src/EnemyState.elm +++ b/src/EnemyState.elm @@ -18,12 +18,12 @@ initEnemyState = , lastSpawn = -spawn } -playerEnemyCollision : Float -> Player -> Enemy -> Bool -playerEnemyCollision time player enemy = - let enemyPos = enemyMove enemy time - in (distance enemyPos player.pos) < enemySize + playerSize - playerEnemiesCollision : Float -> Player -> [Enemy] -> Bool playerEnemiesCollision time player enemies = let collision = playerEnemyCollision time player in length (filter collision enemies) > 0 + +playerEnemyCollision : Float -> Player -> Enemy -> Bool +playerEnemyCollision time player enemy = + let enemyPos = enemyMove enemy time + in (distance enemyPos player.pos) < enemySize + playerSize diff --git a/src/Game.elm b/src/Game.elm index c86af26..8de0c40 100644 --- a/src/Game.elm +++ b/src/Game.elm @@ -3,23 +3,27 @@ module Game where import Player (..) import Enemy (..) import EnemyState (..) +import Target(..) import Vec2 (Vec2) type Game = { time : Float , player : Player + , target : Target , enemyState : EnemyState - , bestTime : Float + , bestScore : Int } -initialGame : Vec2 -> Float -> Game -initialGame playerPos bestTime = +initialGame : Vec2 -> Int -> Game +initialGame playerPos bestScore = let initPlayer = - { pos = playerPos - , speed = { x = 0, y = 0 } - } - in { time = 0 - , player = initPlayer - , enemyState = initEnemyState - , bestTime = bestTime - } + { pos = playerPos + , speed = { x = 0, y = 0 } + } + in + { time = 0 + , player = initPlayer + , target = initTarget + , enemyState = initEnemyState + , bestScore = bestScore + } diff --git a/src/Input.elm b/src/Input.elm index d8614b0..69e7503 100644 --- a/src/Input.elm +++ b/src/Input.elm @@ -16,7 +16,7 @@ getInput : Signal Input getInput = let dtSignal = delta dirSignal = lift recordIntToVec2 Keyboard.arrows - randomFloatsSignal = Random.floatList (lift (\_ -> 3) dtSignal) + randomFloatsSignal = Random.floatList (lift (\_ -> 5) dtSignal) randomValuesSignal = lift floatsToRandomValues randomFloatsSignal in sampleOn dtSignal <| Input <~ dirSignal ~ dtSignal @@ -32,8 +32,10 @@ recordIntToVec2 {x, y} = } floatsToRandomValues : [Float] -> RandomValues -floatsToRandomValues [enemyAngle, enemyX, enemyY] = +floatsToRandomValues [enemyAngle, enemyX, enemyY, targetX, targetY] = { enemyAngle = enemyAngle , enemyX = enemyX , enemyY = enemyY + , targetX = targetX + , targetY = targetY } diff --git a/src/Main.elm b/src/Main.elm index 267bb8c..c51ddc8 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -7,4 +7,4 @@ import Input (getInput) import Vec2 (originVec) main : Signal Element -main = lift display (foldp step (initialGame originVec 0) getInput) +main = lift display (foldp step (initialGame { x = -50, y = 0 } 0) getInput) diff --git a/src/RandomValues.elm b/src/RandomValues.elm index 4638037..3d389f3 100644 --- a/src/RandomValues.elm +++ b/src/RandomValues.elm @@ -4,4 +4,6 @@ type RandomValues = { enemyAngle : Float , enemyX : Float , enemyY : Float + , targetX : Float + , targetY : Float } diff --git a/src/Step.elm b/src/Step.elm index 7894e45..b88740a 100644 --- a/src/Step.elm +++ b/src/Step.elm @@ -11,20 +11,23 @@ import Physics (getNewPosAndSpeed, getMove) import Board (boardSize, boardDiagonal) import Geometry (..) import RandomValues (RandomValues) +import Target(..) step : Input -> Game -> Game -step {dir, delta, randomValues} {time, player, enemyState, bestTime} = +step {dir, delta, randomValues} {time, player, target, enemyState, bestScore} = if(playerEnemiesCollision time player enemyState.enemies) then - let newBestTime = if(time > bestTime) then time else bestTime - in initialGame player.pos newBestTime + let newBestScore = if(target.score > bestScore) then target.score else bestScore + in initialGame player.pos newBestScore else let newTime = time + delta newPlayer = playerStep delta dir player + newTarget = targetStep player randomValues target newEnemyState = enemyStep time randomValues enemyState in { time = newTime , player = newPlayer + , target = newTarget , enemyState = newEnemyState - , bestTime = bestTime + , bestScore = bestScore } playerStep : Float -> Vec2 -> Player -> Player @@ -34,6 +37,15 @@ playerStep dt dir player = , speed = speed } +targetStep : Player -> RandomValues -> Target -> Target +targetStep player randomValues target = + if(targetCollision player target) then + { score = target.score + 1 + , pos = randomBoardPosition (randomValues.targetX, randomValues.targetY) (0.8, 0.8) + } + else + target + enemyStep : Float -> RandomValues -> EnemyState -> EnemyState enemyStep time randomValues {enemies, spawn, lastSpawn} = let isPresent enemy = (distance (enemyMove enemy time) originVec) < enemyAwayDist @@ -65,8 +77,12 @@ enemyInitPos randomValues = enemyDestination : RandomValues -> Vec2 enemyDestination randomValues = - let destWidth = boardSize.x - destHeight = boardSize.y - in { x = destWidth * randomValues.enemyX - destWidth / 2 - , y = destHeight * randomValues.enemyY - destHeight / 2 + randomBoardPosition (randomValues.enemyX, randomValues.enemyY) (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 } diff --git a/src/Target.elm b/src/Target.elm new file mode 100644 index 0000000..92ab4c2 --- /dev/null +++ b/src/Target.elm @@ -0,0 +1,24 @@ +module Target where + +import Board (boardSize) +import Geometry (distance) +import Vec2 (Vec2) +import Player (..) + +type Target = + { score : Int + , pos : Vec2 + } + +targetSize : Float +targetSize = 20 + +targetCollision : Player -> Target -> Bool +targetCollision player target = + (distance player.pos target.pos) < playerSize + targetSize + +initTarget : Target +initTarget = + { score = 0 + , pos = { x = 50, y = 0 } + } diff --git a/src/genGame b/src/generate index 0b71849..0b71849 100755 --- a/src/genGame +++ b/src/generate |