aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Cloud.elm12
-rw-r--r--src/CloudStep.elm29
-rw-r--r--src/Config.elm9
-rw-r--r--src/Display.elm32
-rw-r--r--src/Game.elm5
-rw-r--r--src/Input.elm17
-rw-r--r--src/Player.elm4
-rw-r--r--src/Point.elm2
-rw-r--r--src/RandomValues.elm4
-rw-r--r--src/Step.elm44
10 files changed, 101 insertions, 57 deletions
diff --git a/src/Cloud.elm b/src/Cloud.elm
index 081862c..99a4949 100644
--- a/src/Cloud.elm
+++ b/src/Cloud.elm
@@ -2,20 +2,22 @@ module Cloud where
import Point (..)
import Player (..)
+import Config (..)
import Geometry (distance)
type Cloud =
- { greenPoints : [Point]
- , redPoints : [Point]
+ { points : Config -> [Point]
, spawn : Float
, lastSpawn : Float
}
initCloud : Cloud
initCloud =
- let spawn = 200
- in { greenPoints = []
- , redPoints = []
+ let spawn = 100
+ in { points config =
+ case config of
+ White -> []
+ Black -> []
, spawn = spawn
, lastSpawn = -spawn
}
diff --git a/src/CloudStep.elm b/src/CloudStep.elm
index e33d573..65609cb 100644
--- a/src/CloudStep.elm
+++ b/src/CloudStep.elm
@@ -8,26 +8,33 @@ import Point (..)
import RandomValues (..)
import Physics (getMove)
import Cloud (..)
+import Config (..)
cloudStep : Float -> RandomValues -> Player -> Cloud -> (Cloud, Int)
-cloudStep time {greenPoint, redPoint} player {greenPoints, redPoints, spawn, lastSpawn} =
- let insideGreenPoints = presentPoints time greenPoints
- insideNotCaughtGreenPoints = filter (not . (playerPointCollision time player)) insideGreenPoints
- addScore = (length insideGreenPoints) - (length insideNotCaughtGreenPoints)
- presentRedPoints = presentPoints time redPoints
+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 newGreenPoint = newPoint time greenPoint
- newRedPoint = newPoint time redPoint
+ let newPoint1 = newPoint time randomValues.point1
+ newPoint2 = newPoint time randomValues.point2
in
- { greenPoints = newGreenPoint :: insideNotCaughtGreenPoints
- , redPoints = newRedPoint :: presentRedPoints
+ { points config =
+ if(config == player.config) then
+ newPoint1 :: presentAndNotCaughtPoints
+ else
+ newPoint2 :: presentOtherPoints
, spawn = spawn - sqrt(spawn) / 50
, lastSpawn = time
}
else
- { greenPoints = insideNotCaughtGreenPoints
- , redPoints = presentRedPoints
+ { points config =
+ if(config == player.config) then
+ presentAndNotCaughtPoints
+ else
+ presentOtherPoints
, spawn = spawn
, lastSpawn = lastSpawn
}
diff --git a/src/Config.elm b/src/Config.elm
new file mode 100644
index 0000000..60f4cc3
--- /dev/null
+++ b/src/Config.elm
@@ -0,0 +1,9 @@
+module Config where
+
+data Config = White | Black
+
+otherConfig : Config -> Config
+otherConfig config =
+ case config of
+ White -> Black
+ Black -> White
diff --git a/src/Display.elm b/src/Display.elm
index aa4ed51..c52b9e3 100644
--- a/src/Display.elm
+++ b/src/Display.elm
@@ -5,15 +5,16 @@ import Player (..)
import Game (Game)
import Point (..)
import Board (boardSize)
+import Config (..)
display : Game -> Element
display {time, score, player, cloud, bestScore} =
- let greenPointForms = map (pointForm time greenPointColor) cloud.greenPoints
- redPointForms = map (pointForm time redPointColor) cloud.redPoints
+ let whitePointForms = map (pointForm time (configColor White)) (cloud.points White)
+ blackPointForms = map (pointForm time (configColor Black)) (cloud.points Black)
forms = boardForms
++ playerForms player
- ++ greenPointForms
- ++ redPointForms
+ ++ whitePointForms
+ ++ blackPointForms
++ scoreForms score
++ bestScoreForms bestScore
in collage (truncate boardSize.x) (truncate boardSize.y) forms
@@ -22,10 +23,12 @@ boardForms : [Form]
boardForms = [filled boardColor (rect boardSize.x boardSize.y)]
boardColor : Color
-boardColor = rgb 17 17 17
+boardColor = rgb 103 123 244
playerForms : Player -> [Form]
-playerForms player = [circleForm player.pos playerSize playerColor]
+playerForms player =
+ let playerColor = configColor player.config
+ in [circleForm player.pos playerSize playerColor]
playerColor : Color
playerColor = rgb 224 224 224
@@ -35,11 +38,11 @@ pointForm time color point =
let pos = pointMove point time
in circleForm pos pointSize color
-greenPointColor : Color
-greenPointColor = rgb 34 85 34
-
-redPointColor : Color
-redPointColor = rgb 170 0 0
+configColor : Config -> Color
+configColor config =
+ case config of
+ White -> rgb 240 240 240
+ Black -> rgb 14 17 33
circleForm : Vec2 -> Float -> Color -> Form
circleForm pos size color =
@@ -64,15 +67,16 @@ bestScoreForms bestScore =
if(bestScore > 0) then
let text = "Record: " ++ (show bestScore)
pos =
- { x = boardSize.x / 2 - 65
+ { x = -boardSize.x / 2 + 100
, y = -boardSize.y / 2 + 30
}
- in [textForm text pos rightAligned]
+ in [textForm text pos leftAligned]
else []
textForm : String -> Vec2 -> (Text -> Element) -> Form
textForm content pos alignment =
let textElement = toText content
+ |> Text.height 30
|> typeface ["calibri", "arial"]
|> Text.color textColor
|> bold
@@ -82,4 +86,4 @@ textForm content pos alignment =
|> move (pos.x, pos.y)
textColor : Color
-textColor = rgb 224 224 224
+textColor = rgb 14 17 33
diff --git a/src/Game.elm b/src/Game.elm
index 83d8baa..0a12db8 100644
--- a/src/Game.elm
+++ b/src/Game.elm
@@ -3,9 +3,12 @@ module Game where
import Player (..)
import Cloud (..)
import Vec2 (Vec2)
+import Config (..)
+import Keyboard (KeyCode)
type Game =
{ time : Float
+ , keysDown : [KeyCode]
, score : Int
, player : Player
, cloud : Cloud
@@ -17,9 +20,11 @@ initialGame playerPos bestScore =
let initPlayer =
{ pos = playerPos
, speed = { x = 0, y = 0 }
+ , config = White
}
in
{ time = 0
+ , keysDown = []
, score = 0
, player = initPlayer
, cloud = initCloud
diff --git a/src/Input.elm b/src/Input.elm
index 1015302..8ba43ec 100644
--- a/src/Input.elm
+++ b/src/Input.elm
@@ -1,13 +1,15 @@
module Input where
-import Keyboard
-import Random
+import Char (toCode)
import RandomValues (RandomValues)
+import Keyboard (KeyCode, keysDown, arrows)
+import Random
import Vec2 (Vec2)
type Input =
{ dir : Vec2
+ , inputKeysDown : [KeyCode]
, delta : Time
, randomValues : RandomValues
}
@@ -15,10 +17,11 @@ type Input =
getInput : Signal Input
getInput =
let dtSignal = delta
- dirSignal = lift recordIntToVec2 Keyboard.arrows
+ dirSignal = lift recordIntToVec2 arrows
randomFloatsSignal = Random.floatList (lift (\_ -> 6) dtSignal)
randomValuesSignal = lift floatsToRandomValues randomFloatsSignal
in sampleOn dtSignal <| Input <~ dirSignal
+ ~ keysDown
~ dtSignal
~ randomValuesSignal
@@ -33,16 +36,16 @@ recordIntToVec2 {x, y} =
floatsToRandomValues : [Float] -> RandomValues
floatsToRandomValues [angle1, x1, y1, angle2, x2, y2] =
- let greenPoint =
+ let point1 =
{ angle = angle1
, x = x1
, y = y1
}
- redPoint =
+ point2 =
{ angle = angle2
, x = x2
, y = y2
}
- in { greenPoint = greenPoint
- , redPoint = redPoint
+ in { point1 = point1
+ , point2 = point2
}
diff --git a/src/Player.elm b/src/Player.elm
index b64655e..56c4e97 100644
--- a/src/Player.elm
+++ b/src/Player.elm
@@ -1,14 +1,16 @@
module Player where
import Vec2 (..)
+import Config (Config)
type Player =
{ pos : Vec2
, speed : Vec2
+ , config : Config
}
playerSize : Float
playerSize = 10
playerSpeed : Float -> Float
-playerSpeed dt = dt / 500
+playerSpeed dt = dt / 400
diff --git a/src/Point.elm b/src/Point.elm
index 90c61c3..086f8a4 100644
--- a/src/Point.elm
+++ b/src/Point.elm
@@ -18,7 +18,7 @@ pointSize : Float
pointSize = 5
pointSpeed : Float -> Float
-pointSpeed dt = dt / 25
+pointSpeed dt = dt / 20
pointSpawnDist : Float
pointSpawnDist = boardDiagonal * 3 / 5
diff --git a/src/RandomValues.elm b/src/RandomValues.elm
index 5d45b15..5e40fc5 100644
--- a/src/RandomValues.elm
+++ b/src/RandomValues.elm
@@ -1,8 +1,8 @@
module RandomValues where
type RandomValues =
- { greenPoint : PointRandomValues
- , redPoint : PointRandomValues
+ { point1 : PointRandomValues
+ , point2 : PointRandomValues
}
type PointRandomValues =
diff --git a/src/Step.elm b/src/Step.elm
index 72d3ae1..08a5f49 100644
--- a/src/Step.elm
+++ b/src/Step.elm
@@ -11,26 +11,38 @@ import Input (Input)
import Physics (getNewPosAndSpeed)
import RandomValues (..)
import CloudStep (cloudStep)
+import Config (otherConfig)
+import Keyboard (KeyCode)
+import Char (fromCode, toCode)
step : Input -> Game -> Game
-step {dir, delta, randomValues} {time, score, player, cloud, bestScore} =
- if(playerPointsCollision time player cloud.redPoints) then
- let newBestScore = if(score > bestScore) then score else bestScore
- in initialGame player.pos newBestScore
- else
- let newTime = time + delta
- newPlayer = playerStep delta dir player
- (newCloud, addScore) = cloudStep time randomValues player cloud
- in { time = newTime
- , score = score + addScore
- , player = newPlayer
- , cloud = newCloud
- , bestScore = bestScore
- }
+step {dir, inputKeysDown, delta, randomValues} {time, keysDown, score, player, cloud, bestScore} =
+ let hostilePoints = cloud.points (otherConfig player.config)
+ in if(playerPointsCollision time player hostilePoints) then
+ let newBestScore = if(score > bestScore) then score else bestScore
+ in initialGame player.pos newBestScore
+ else
+ let newTime = time + delta
+ newPlayer = playerStep delta dir (newKeyCode keysDown inputKeysDown) player
+ (newCloud, addScore) = cloudStep time randomValues newPlayer cloud
+ in { time = newTime
+ , keysDown = inputKeysDown
+ , score = score + addScore
+ , player = newPlayer
+ , cloud = newCloud
+ , bestScore = bestScore
+ }
-playerStep : Float -> Vec2 -> Player -> Player
-playerStep dt dir player =
+playerStep : Float -> Vec2 -> (KeyCode -> Bool) -> Player -> Player
+playerStep dt dir newKey player =
let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed)
+ newConfig = if (newKey (toCode 'e')) then otherConfig player.config else player.config
in { pos = inBoard playerSize pos
, speed = speed
+ , config = newConfig
}
+
+newKeyCode : [KeyCode] -> [KeyCode] -> KeyCode -> Bool
+newKeyCode lastKeyCodes newKeyCodes keyCode =
+ let contains = (\l -> l > 0) . length . filter (\kc -> kc == keyCode)
+ in not (contains lastKeyCodes) && (contains newKeyCodes)