Commit ce677564 authored by Joris Guyonvarch's avatar Joris Guyonvarch

Moving boardSize to Game model

parent 87386e8b
module Model.Board
( boardSize
( initBoardSize
, boardDiagonal
) where
import Model.Vec2 (Vec2)
boardSize : Vec2
boardSize =
initBoardSize : Vec2
initBoardSize =
{ x = 500
, y = 500
}
boardDiagonal : Float
boardDiagonal =
boardDiagonal : Vec2 -> Float
boardDiagonal boardSize =
boardSize.x ^ 2 + boardSize.y ^ 2
|> sqrt
......@@ -11,9 +11,11 @@ import Model.Cloud (..)
import Model.Vec2 (Vec2)
import Model.Config (..)
import Model.Round (Round)
import Model.Board (initBoardSize)
type alias Game =
{ time : Float
, boardSize : Vec2
, keysDown : List KeyCode
, currentScore : Int
, player : Player
......@@ -25,6 +27,7 @@ type alias Game =
initialGame : Seed -> Game
initialGame seed =
{ time = 0
, boardSize = initBoardSize
, keysDown = []
, currentScore = 0
, player = initPlayer
......
......@@ -27,8 +27,8 @@ pointSize = 10
pointSpeed : Float -> Float
pointSpeed dt = dt / 20
pointSpawnDist : Float
pointSpawnDist = boardDiagonal * 3 / 5
pointSpawnDist : Vec2 -> Float
pointSpawnDist boardSize = (boardDiagonal boardSize) * 3 / 5
pointAwayDist : Float
pointAwayDist = boardDiagonal * 3 / 4
pointAwayDist : Vec2 -> Float
pointAwayDist boardSize = (boardDiagonal boardSize) * 3 / 4
......@@ -7,7 +7,7 @@ import Random (..)
import Model.Vec2 (..)
import Model.Player (..)
import Model.Board (boardSize, boardDiagonal)
import Model.Board (boardDiagonal)
import Model.Point (..)
import Model.Cloud (..)
import Model.Config (..)
......@@ -15,16 +15,16 @@ import Model.Config (..)
import Utils.Geometry (..)
import Utils.Physics (getMove, getWaveMove)
cloudUpdate : Float -> Seed -> Player -> Cloud -> (Cloud, Int, Seed)
cloudUpdate time seed player {points, spawn, lastSpawn} =
let pointsToCatch = presentPoints time (points player.config)
cloudUpdate : Float -> Vec2 -> Seed -> Player -> Cloud -> (Cloud, Int, Seed)
cloudUpdate time boardSize seed player {points, spawn, lastSpawn} =
let pointsToCatch = presentPoints time boardSize (points player.config)
presentAndNotCaughtPoints = List.filter (not << (playerPointCollision time player)) pointsToCatch
addScore = (List.length pointsToCatch) - (List.length presentAndNotCaughtPoints)
presentOtherPoints = presentPoints time (points (otherConfig player.config))
presentOtherPoints = presentPoints time boardSize (points (otherConfig player.config))
(newCloud, seed''') =
if time > lastSpawn + spawn then
let (newPoint1, seed') = getNewPoint time seed
(newPoint2, seed'') = getNewPoint time seed'
let (newPoint1, seed') = getNewPoint time boardSize seed
(newPoint2, seed'') = getNewPoint time boardSize seed'
in ( { points config =
if(config == player.config)
then
......@@ -49,16 +49,16 @@ cloudUpdate time seed player {points, spawn, lastSpawn} =
)
in (newCloud, addScore, seed''')
presentPoints : Float -> List Point -> List Point
presentPoints time points =
let isPresent point = (distance (pointMove point time) originVec) < pointAwayDist
presentPoints : Float -> Vec2 -> List Point -> List Point
presentPoints time boardSize points =
let isPresent point = (distance (pointMove point time) originVec) < (pointAwayDist boardSize)
in List.filter isPresent points
getNewPoint : Float -> Seed -> (Point, Seed)
getNewPoint time seed =
let (initPos, seed') = pointInitPos seed
(initDest, seed'') = pointDestination seed'
getNewPoint : Float -> Vec2 -> Seed -> (Point, Seed)
getNewPoint time boardSize seed =
let (initPos, seed') = pointInitPos boardSize seed
(initDest, seed'') = pointDestination boardSize seed'
in ( { initTime = time
, initPos = initPos
, initDest = initDest
......@@ -70,17 +70,17 @@ getNewPoint time seed =
, seed''
)
pointInitPos : Seed -> (Vec2, Seed)
pointInitPos seed =
pointInitPos : Vec2 -> Seed -> (Vec2, Seed)
pointInitPos boardSize seed =
let (rand, seed') = generate floatGen seed
angle = rand * (degrees 360)
dist = boardDiagonal * 3 / 5
dist = (boardDiagonal boardSize) * 3 / 5
in (polarToCartesian angle dist, seed')
pointDestination : Seed -> (Vec2, Seed)
pointDestination seed =
pointDestination : Vec2 -> Seed -> (Vec2, Seed)
pointDestination boardSize seed =
let ([r1, r2, r3, r4], seed') = generateMany 4 floatGen seed
in ( randomBoardPosition (r1, r2) (r3, r4)
in ( randomBoardPosition boardSize (r1, r2) (r3, r4)
, seed'
)
......@@ -97,8 +97,8 @@ generateMany count gen seed =
floatGen : Generator Float
floatGen = float 0 1
randomBoardPosition : (Float, Float) -> (Float, Float) -> Vec2
randomBoardPosition (randomX, randomY) (percentX, percentY) =
randomBoardPosition : Vec2 -> (Float, Float) -> (Float, Float) -> Vec2
randomBoardPosition boardSize (randomX, randomY) (percentX, percentY) =
let width = boardSize.x * percentX
height = boardSize.y * percentY
in { x = width * randomX - width / 2
......
......@@ -8,7 +8,6 @@ import Char (fromCode, toCode)
import Maybe
import Model.Player (..)
import Model.Point (pointSpeed, pointMove, pointAwayDist)
import Model.Vec2 (..)
import Model.Config (otherConfig)
import Model.Cloud (..)
......@@ -35,8 +34,8 @@ update input game =
}
else
let newTime = game.time + input.delta
newPlayer = playerStep input.delta input.dir (newKeyCode game.keysDown input.inputKeysDown) game.player
(newCloud, addScore, newSeed) = cloudUpdate game.time game.seed newPlayer game.cloud
newPlayer = playerStep input.delta game.boardSize input.dir (newKeyCode game.keysDown input.inputKeysDown) game.player
(newCloud, addScore, newSeed) = cloudUpdate game.time game.boardSize game.seed newPlayer game.cloud
in
{ game
| time <- newTime
......@@ -47,11 +46,11 @@ update input game =
, seed <- newSeed
}
playerStep : Float -> Vec2 -> (KeyCode -> Bool) -> Player -> Player
playerStep dt dir newKey player =
playerStep : Float -> Vec2 -> Vec2 -> (KeyCode -> Bool) -> Player -> Player
playerStep dt boardSize dir newKey player =
let (pos, speed) = getNewPosAndSpeed dt dir playerSpeed (player.pos, player.speed)
newConfig = if (newKey 69) then otherConfig player.config else player.config
in { pos = inBoard playerSize pos
in { pos = inBoard boardSize playerSize pos
, speed = speed
, config = newConfig
}
......
......@@ -5,7 +5,6 @@ module Utils.Geometry
) where
import Model.Vec2 (..)
import Model.Board (boardSize)
polarToCartesian : Float -> Float -> Vec2
polarToCartesian angle dist =
......@@ -16,8 +15,8 @@ polarToCartesian angle dist =
distance : Vec2 -> Vec2 -> Float
distance v1 v2 = sqrt((v2.x - v1.x)^2 + (v2.y - v1.y)^2)
inBoard : Float -> Vec2 -> Vec2
inBoard size pos =
inBoard : Vec2 -> Float -> Vec2 -> Vec2
inBoard boardSize size pos =
let leftX = -boardSize.x / 2 + size
rightX = boardSize.x / 2 - size
bottomY = -boardSize.y / 2 + size
......
......@@ -14,7 +14,6 @@ import Model.Vec2 (Vec2)
import Model.Player (..)
import Model.Game (Game)
import Model.Point (..)
import Model.Board (boardSize)
import Model.Config (..)
gameView : Game -> Element
......@@ -22,15 +21,15 @@ gameView game =
let whitePointForms = List.map (pointForm game.time (configColor White)) (game.cloud.points White)
blackPointForms = List.map (pointForm game.time (configColor Black)) (game.cloud.points Black)
forms =
boardForms
boardForms game.boardSize
++ playerForms game.player
++ whitePointForms
++ blackPointForms
++ scoreForms game.currentScore
in collage (truncate boardSize.x) (truncate boardSize.y) forms
++ scoreForms game.boardSize game.currentScore
in collage (truncate game.boardSize.x) (truncate game.boardSize.y) forms
boardForms : List Form
boardForms = [filled boardColor (rect boardSize.x boardSize.y)]
boardForms : Vec2 -> List Form
boardForms boardSize = [filled boardColor (rect boardSize.x boardSize.y)]
boardColor : Color
boardColor = rgb 103 123 244
......@@ -66,8 +65,8 @@ circleForm pos size color =
outlineColor : Color
outlineColor = rgb 34 34 34
scoreForms : Int -> List Form
scoreForms score =
scoreForms : Vec2 -> Int -> List Form
scoreForms boardSize score =
let text = (toString score)
scorePos = { x = 0.0, y = boardSize.y / 2 - 35 }
in [textForm text scorePos centered]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment