aboutsummaryrefslogtreecommitdiff
path: root/src/Model
diff options
context:
space:
mode:
Diffstat (limited to 'src/Model')
-rw-r--r--src/Model/Board.elm17
-rw-r--r--src/Model/Cloud.elm41
-rw-r--r--src/Model/Config.elm14
-rw-r--r--src/Model/Game.elm42
-rw-r--r--src/Model/Player.elm20
-rw-r--r--src/Model/Point.elm34
-rw-r--r--src/Model/Vec2.elm55
7 files changed, 223 insertions, 0 deletions
diff --git a/src/Model/Board.elm b/src/Model/Board.elm
new file mode 100644
index 0000000..1361cfb
--- /dev/null
+++ b/src/Model/Board.elm
@@ -0,0 +1,17 @@
+module Model.Board
+ ( boardSize
+ , boardDiagonal
+ ) where
+
+import Model.Vec2 (Vec2)
+
+boardSize : Vec2
+boardSize =
+ { x = 500
+ , y = 500
+ }
+
+boardDiagonal : Float
+boardDiagonal =
+ boardSize.x ^ 2 + boardSize.y ^ 2
+ |> sqrt
diff --git a/src/Model/Cloud.elm b/src/Model/Cloud.elm
new file mode 100644
index 0000000..11f6311
--- /dev/null
+++ b/src/Model/Cloud.elm
@@ -0,0 +1,41 @@
+module Model.Cloud
+ ( Cloud
+ , initCloud
+ , playerPointsCollision
+ , playerPointCollision
+ ) where
+
+import List
+
+import Model.Point (..)
+import Model.Player (..)
+import Model.Config (..)
+
+import Utils.Geometry (distance)
+
+type alias Cloud =
+ { points : Config -> List Point
+ , spawn : Float
+ , lastSpawn : Float
+ }
+
+initCloud : Cloud
+initCloud =
+ let spawn = 600
+ in { points config =
+ case config of
+ White -> []
+ Black -> []
+ , spawn = spawn
+ , lastSpawn = -spawn
+ }
+
+playerPointsCollision : Float -> Player -> List Point -> Bool
+playerPointsCollision time player points =
+ let collision = playerPointCollision time player
+ in List.length (List.filter collision points) > 0
+
+playerPointCollision : Float -> Player -> Point -> Bool
+playerPointCollision time player point =
+ let pointPos = pointMove point time
+ in (distance pointPos player.pos) < pointSize + playerSize
diff --git a/src/Model/Config.elm b/src/Model/Config.elm
new file mode 100644
index 0000000..2973dc7
--- /dev/null
+++ b/src/Model/Config.elm
@@ -0,0 +1,14 @@
+module Model.Config
+ ( Config(..)
+ , otherConfig
+ ) where
+
+type Config =
+ White
+ | Black
+
+otherConfig : Config -> Config
+otherConfig config =
+ case config of
+ White -> Black
+ Black -> White
diff --git a/src/Model/Game.elm b/src/Model/Game.elm
new file mode 100644
index 0000000..4ef5d89
--- /dev/null
+++ b/src/Model/Game.elm
@@ -0,0 +1,42 @@
+module Model.Game
+ ( Game
+ , initialGame
+ ) where
+
+import Random (..)
+import Keyboard (KeyCode)
+
+import Model.Player (..)
+import Model.Cloud (..)
+import Model.Vec2 (Vec2)
+import Model.Config (..)
+
+type alias Game =
+ { time : Float
+ , keysDown : List KeyCode
+ , score : Int
+ , player : Player
+ , cloud : Cloud
+ , bestScore : Int
+ , seed : Seed
+ }
+
+initialGame : Seed -> Vec2 -> Config -> Int -> Game
+initialGame seed playerPos config bestScore =
+ let initPlayer =
+ { pos = playerPos
+ , speed =
+ { x = 0
+ , y = 0
+ }
+ , config = config
+ }
+ in
+ { time = 0
+ , keysDown = []
+ , score = 0
+ , player = initPlayer
+ , cloud = initCloud
+ , bestScore = bestScore
+ , seed = seed
+ }
diff --git a/src/Model/Player.elm b/src/Model/Player.elm
new file mode 100644
index 0000000..c6aac21
--- /dev/null
+++ b/src/Model/Player.elm
@@ -0,0 +1,20 @@
+module Model.Player
+ ( Player
+ , playerSize
+ , playerSpeed
+ ) where
+
+import Model.Vec2 (..)
+import Model.Config (Config)
+
+type alias Player =
+ { pos : Vec2
+ , speed : Vec2
+ , config : Config
+ }
+
+playerSize : Float
+playerSize = 15
+
+playerSpeed : Float -> Float
+playerSpeed dt = dt / 200
diff --git a/src/Model/Point.elm b/src/Model/Point.elm
new file mode 100644
index 0000000..41967b6
--- /dev/null
+++ b/src/Model/Point.elm
@@ -0,0 +1,34 @@
+module Model.Point
+ ( Point
+ , pointMove
+ , pointSize
+ , pointSpeed
+ , pointSpawnDist
+ , pointAwayDist
+ ) where
+
+import Model.Vec2 (..)
+import Model.Board (boardDiagonal)
+
+type alias Point =
+ { initTime : Float
+ , initPos : Vec2
+ , initDest : Vec2
+ , move : Float -> Vec2 -> Vec2 -> Float -> Vec2
+ }
+
+pointMove : Point -> Float -> Vec2
+pointMove {initTime, initPos, initDest, move} time =
+ move initTime initPos initDest time
+
+pointSize : Float
+pointSize = 10
+
+pointSpeed : Float -> Float
+pointSpeed dt = dt / 20
+
+pointSpawnDist : Float
+pointSpawnDist = boardDiagonal * 3 / 5
+
+pointAwayDist : Float
+pointAwayDist = boardDiagonal * 3 / 4
diff --git a/src/Model/Vec2.elm b/src/Model/Vec2.elm
new file mode 100644
index 0000000..85ff008
--- /dev/null
+++ b/src/Model/Vec2.elm
@@ -0,0 +1,55 @@
+module Model.Vec2
+ ( Vec2
+ , add
+ , sub
+ , mul
+ , div
+ , norm
+ , clockwiseRotate90
+ , isNull
+ , originVec
+ ) where
+
+type alias Vec2 =
+ { x : Float
+ , y : Float
+ }
+
+add : Vec2 -> Vec2 -> Vec2
+add v1 v2 =
+ { x = v1.x + v2.x
+ , y = v1.y + v2.y
+ }
+
+sub : Vec2 -> Vec2 -> Vec2
+sub v1 v2 =
+ { x = v1.x - v2.x
+ , y = v1.y - v2.y
+ }
+
+mul : Float -> Vec2 -> Vec2
+mul m v =
+ { x = m * v.x
+ , y = m * v.y
+ }
+
+div : Vec2 -> Float -> Vec2
+div v d =
+ { x = v.x / d
+ , y = v.y / d
+ }
+
+norm : Vec2 -> Float
+norm v = sqrt(v.x ^ 2 + v.y ^ 2)
+
+clockwiseRotate90 : Vec2 -> Vec2
+clockwiseRotate90 v =
+ { x = -v.y
+ , y = v.x
+ }
+
+isNull : Vec2 -> Bool
+isNull v = (v.x == 0) && (v.y == 0)
+
+originVec : Vec2
+originVec = { x = 0, y = 0 }